This commit is contained in:
1
node_modules/react-toastify/addons/use-notification-center/index.d.ts
generated
vendored
Normal file
1
node_modules/react-toastify/addons/use-notification-center/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export * from './useNotificationCenter';
|
||||
153
node_modules/react-toastify/addons/use-notification-center/index.esm.mjs
generated
vendored
Normal file
153
node_modules/react-toastify/addons/use-notification-center/index.esm.mjs
generated
vendored
Normal file
@@ -0,0 +1,153 @@
|
||||
import { useRef, useState, useEffect } from 'react';
|
||||
import { toast } from 'react-toastify';
|
||||
|
||||
function useNotificationCenter(params) {
|
||||
if (params === void 0) {
|
||||
params = {};
|
||||
}
|
||||
|
||||
const sortFn = useRef(params.sort || defaultSort);
|
||||
const filterFn = useRef(params.filter || null);
|
||||
const [notifications, setNotifications] = useState(() => {
|
||||
if (params.data) {
|
||||
return filterFn.current ? params.data.filter(filterFn.current).sort(sortFn.current) : [...params.data].sort(sortFn.current);
|
||||
}
|
||||
|
||||
return [];
|
||||
}); // used to method to be used inside effect without having stale `notifications`
|
||||
|
||||
const notificationsRef = useRef(notifications);
|
||||
useEffect(() => {
|
||||
notificationsRef.current = notifications;
|
||||
}, [notifications]);
|
||||
useEffect(() => {
|
||||
return toast.onChange(toast => {
|
||||
if (toast.status === 'added' || toast.status === 'updated') {
|
||||
const newItem = decorate(toast);
|
||||
if (filterFn.current && !filterFn.current(newItem)) return;
|
||||
setNotifications(prev => {
|
||||
let nextState = [];
|
||||
const updateIdx = prev.findIndex(v => v.id === newItem.id);
|
||||
|
||||
if (updateIdx !== -1) {
|
||||
nextState = prev.slice();
|
||||
Object.assign(nextState[updateIdx], newItem, {
|
||||
createdAt: Date.now()
|
||||
});
|
||||
} else if (prev.length === 0) {
|
||||
nextState = [newItem];
|
||||
} else {
|
||||
nextState = [newItem, ...prev];
|
||||
}
|
||||
|
||||
return nextState.sort(sortFn.current);
|
||||
});
|
||||
}
|
||||
});
|
||||
}, []);
|
||||
|
||||
const remove = id => {
|
||||
setNotifications(prev => prev.filter(Array.isArray(id) ? v => !id.includes(v.id) : v => v.id !== id));
|
||||
};
|
||||
|
||||
const clear = () => {
|
||||
setNotifications([]);
|
||||
};
|
||||
|
||||
const markAllAsRead = function (read) {
|
||||
if (read === void 0) {
|
||||
read = true;
|
||||
}
|
||||
|
||||
setNotifications(prev => prev.map(v => {
|
||||
v.read = read;
|
||||
return v;
|
||||
}));
|
||||
};
|
||||
|
||||
const markAsRead = function (id, read) {
|
||||
if (read === void 0) {
|
||||
read = true;
|
||||
}
|
||||
|
||||
let map = v => {
|
||||
if (v.id === id) v.read = read;
|
||||
return v;
|
||||
};
|
||||
|
||||
if (Array.isArray(id)) {
|
||||
map = v => {
|
||||
if (id.includes(v.id)) v.read = read;
|
||||
return v;
|
||||
};
|
||||
}
|
||||
|
||||
setNotifications(prev => prev.map(map));
|
||||
};
|
||||
|
||||
const find = id => {
|
||||
return Array.isArray(id) ? notificationsRef.current.filter(v => id.includes(v.id)) : notificationsRef.current.find(v => v.id === id);
|
||||
};
|
||||
|
||||
const add = item => {
|
||||
if (notificationsRef.current.find(v => v.id === item.id)) return null;
|
||||
const newItem = decorate(item);
|
||||
setNotifications(prev => [...prev, newItem].sort(sortFn.current));
|
||||
return newItem.id;
|
||||
};
|
||||
|
||||
const update = (id, item) => {
|
||||
const index = notificationsRef.current.findIndex(v => v.id === id);
|
||||
|
||||
if (index !== -1) {
|
||||
setNotifications(prev => {
|
||||
const nextState = [...prev];
|
||||
Object.assign(nextState[index], item, {
|
||||
createdAt: item.createdAt || Date.now()
|
||||
});
|
||||
return nextState.sort(sortFn.current);
|
||||
});
|
||||
return item.id;
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
const sort = compareFn => {
|
||||
sortFn.current = compareFn;
|
||||
setNotifications(prev => prev.slice().sort(compareFn));
|
||||
};
|
||||
|
||||
return {
|
||||
notifications,
|
||||
clear,
|
||||
markAllAsRead,
|
||||
markAsRead,
|
||||
add,
|
||||
update,
|
||||
remove,
|
||||
// @ts-ignore fixme: overloading issue
|
||||
find,
|
||||
sort,
|
||||
|
||||
get unreadCount() {
|
||||
return notifications.reduce((prev, cur) => !cur.read ? prev + 1 : prev, 0);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
function decorate(item) {
|
||||
if (item.id == null) item.id = Date.now().toString(36).substring(2, 9);
|
||||
if (!item.createdAt) item.createdAt = Date.now();
|
||||
if (item.read == null) item.read = false;
|
||||
return item;
|
||||
} // newest to oldest
|
||||
|
||||
|
||||
function defaultSort(l, r) {
|
||||
return r.createdAt - l.createdAt;
|
||||
}
|
||||
|
||||
export { useNotificationCenter };
|
||||
//# sourceMappingURL=index.esm.mjs.map
|
||||
1
node_modules/react-toastify/addons/use-notification-center/index.esm.mjs.map
generated
vendored
Normal file
1
node_modules/react-toastify/addons/use-notification-center/index.esm.mjs.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
153
node_modules/react-toastify/addons/use-notification-center/index.js
generated
vendored
Normal file
153
node_modules/react-toastify/addons/use-notification-center/index.js
generated
vendored
Normal file
@@ -0,0 +1,153 @@
|
||||
var react = require('react');
|
||||
var reactToastify = require('react-toastify');
|
||||
|
||||
function useNotificationCenter(params) {
|
||||
if (params === void 0) {
|
||||
params = {};
|
||||
}
|
||||
|
||||
const sortFn = react.useRef(params.sort || defaultSort);
|
||||
const filterFn = react.useRef(params.filter || null);
|
||||
const [notifications, setNotifications] = react.useState(() => {
|
||||
if (params.data) {
|
||||
return filterFn.current ? params.data.filter(filterFn.current).sort(sortFn.current) : [...params.data].sort(sortFn.current);
|
||||
}
|
||||
|
||||
return [];
|
||||
}); // used to method to be used inside effect without having stale `notifications`
|
||||
|
||||
const notificationsRef = react.useRef(notifications);
|
||||
react.useEffect(() => {
|
||||
notificationsRef.current = notifications;
|
||||
}, [notifications]);
|
||||
react.useEffect(() => {
|
||||
return reactToastify.toast.onChange(toast => {
|
||||
if (toast.status === 'added' || toast.status === 'updated') {
|
||||
const newItem = decorate(toast);
|
||||
if (filterFn.current && !filterFn.current(newItem)) return;
|
||||
setNotifications(prev => {
|
||||
let nextState = [];
|
||||
const updateIdx = prev.findIndex(v => v.id === newItem.id);
|
||||
|
||||
if (updateIdx !== -1) {
|
||||
nextState = prev.slice();
|
||||
Object.assign(nextState[updateIdx], newItem, {
|
||||
createdAt: Date.now()
|
||||
});
|
||||
} else if (prev.length === 0) {
|
||||
nextState = [newItem];
|
||||
} else {
|
||||
nextState = [newItem, ...prev];
|
||||
}
|
||||
|
||||
return nextState.sort(sortFn.current);
|
||||
});
|
||||
}
|
||||
});
|
||||
}, []);
|
||||
|
||||
const remove = id => {
|
||||
setNotifications(prev => prev.filter(Array.isArray(id) ? v => !id.includes(v.id) : v => v.id !== id));
|
||||
};
|
||||
|
||||
const clear = () => {
|
||||
setNotifications([]);
|
||||
};
|
||||
|
||||
const markAllAsRead = function (read) {
|
||||
if (read === void 0) {
|
||||
read = true;
|
||||
}
|
||||
|
||||
setNotifications(prev => prev.map(v => {
|
||||
v.read = read;
|
||||
return v;
|
||||
}));
|
||||
};
|
||||
|
||||
const markAsRead = function (id, read) {
|
||||
if (read === void 0) {
|
||||
read = true;
|
||||
}
|
||||
|
||||
let map = v => {
|
||||
if (v.id === id) v.read = read;
|
||||
return v;
|
||||
};
|
||||
|
||||
if (Array.isArray(id)) {
|
||||
map = v => {
|
||||
if (id.includes(v.id)) v.read = read;
|
||||
return v;
|
||||
};
|
||||
}
|
||||
|
||||
setNotifications(prev => prev.map(map));
|
||||
};
|
||||
|
||||
const find = id => {
|
||||
return Array.isArray(id) ? notificationsRef.current.filter(v => id.includes(v.id)) : notificationsRef.current.find(v => v.id === id);
|
||||
};
|
||||
|
||||
const add = item => {
|
||||
if (notificationsRef.current.find(v => v.id === item.id)) return null;
|
||||
const newItem = decorate(item);
|
||||
setNotifications(prev => [...prev, newItem].sort(sortFn.current));
|
||||
return newItem.id;
|
||||
};
|
||||
|
||||
const update = (id, item) => {
|
||||
const index = notificationsRef.current.findIndex(v => v.id === id);
|
||||
|
||||
if (index !== -1) {
|
||||
setNotifications(prev => {
|
||||
const nextState = [...prev];
|
||||
Object.assign(nextState[index], item, {
|
||||
createdAt: item.createdAt || Date.now()
|
||||
});
|
||||
return nextState.sort(sortFn.current);
|
||||
});
|
||||
return item.id;
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
const sort = compareFn => {
|
||||
sortFn.current = compareFn;
|
||||
setNotifications(prev => prev.slice().sort(compareFn));
|
||||
};
|
||||
|
||||
return {
|
||||
notifications,
|
||||
clear,
|
||||
markAllAsRead,
|
||||
markAsRead,
|
||||
add,
|
||||
update,
|
||||
remove,
|
||||
// @ts-ignore fixme: overloading issue
|
||||
find,
|
||||
sort,
|
||||
|
||||
get unreadCount() {
|
||||
return notifications.reduce((prev, cur) => !cur.read ? prev + 1 : prev, 0);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
function decorate(item) {
|
||||
if (item.id == null) item.id = Date.now().toString(36).substring(2, 9);
|
||||
if (!item.createdAt) item.createdAt = Date.now();
|
||||
if (item.read == null) item.read = false;
|
||||
return item;
|
||||
} // newest to oldest
|
||||
|
||||
|
||||
function defaultSort(l, r) {
|
||||
return r.createdAt - l.createdAt;
|
||||
}
|
||||
|
||||
exports.useNotificationCenter = useNotificationCenter;
|
||||
//# sourceMappingURL=index.js.map
|
||||
1
node_modules/react-toastify/addons/use-notification-center/index.js.map
generated
vendored
Normal file
1
node_modules/react-toastify/addons/use-notification-center/index.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
158
node_modules/react-toastify/addons/use-notification-center/useNotificationCenter.d.ts
generated
vendored
Normal file
158
node_modules/react-toastify/addons/use-notification-center/useNotificationCenter.d.ts
generated
vendored
Normal file
@@ -0,0 +1,158 @@
|
||||
import { ToastItem, Id } from 'react-toastify';
|
||||
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
|
||||
export interface NotificationCenterItem<Data = {}> extends Optional<ToastItem<Data>, 'content' | 'data'> {
|
||||
read: boolean;
|
||||
createdAt: number;
|
||||
}
|
||||
export type SortFn<Data> = (l: NotificationCenterItem<Data>, r: NotificationCenterItem<Data>) => number;
|
||||
export type FilterFn<Data = {}> = (item: NotificationCenterItem<Data>) => boolean;
|
||||
export interface UseNotificationCenterParams<Data = {}> {
|
||||
/**
|
||||
* initial data to rehydrate the notification center
|
||||
*/
|
||||
data?: NotificationCenterItem<Data>[];
|
||||
/**
|
||||
* By default, the notifications are sorted from the newest to the oldest using
|
||||
* the `createdAt` field. Use this to provide your own sort function
|
||||
*
|
||||
* Usage:
|
||||
* ```
|
||||
* // old notifications first
|
||||
* useNotificationCenter({
|
||||
* sort: ((l, r) => l.createdAt - r.createdAt)
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
sort?: SortFn<Data>;
|
||||
/**
|
||||
* Keep the toast that meets the condition specified in the callback function.
|
||||
*
|
||||
* Usage:
|
||||
* ```
|
||||
* // keep only the toasts when hidden is set to false
|
||||
* useNotificationCenter({
|
||||
* filter: item => item.data.hidden === false
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
filter?: FilterFn<Data>;
|
||||
}
|
||||
export interface UseNotificationCenter<Data> {
|
||||
/**
|
||||
* Contains all the notifications
|
||||
*/
|
||||
notifications: NotificationCenterItem<Data>[];
|
||||
/**
|
||||
* Clear all notifications
|
||||
*/
|
||||
clear(): void;
|
||||
/**
|
||||
* Mark all notification as read
|
||||
*/
|
||||
markAllAsRead(): void;
|
||||
/**
|
||||
* Mark all notification as read or not.
|
||||
*
|
||||
* Usage:
|
||||
* ```
|
||||
* markAllAsRead(false) // mark all notification as not read
|
||||
*
|
||||
* markAllAsRead(true) // same as calling markAllAsRead()
|
||||
* ```
|
||||
*/
|
||||
markAllAsRead(read?: boolean): void;
|
||||
/**
|
||||
* Mark one or more notifications as read.
|
||||
*
|
||||
* Usage:
|
||||
* ```
|
||||
* markAsRead("anId")
|
||||
* markAsRead(["a","list", "of", "id"])
|
||||
* ```
|
||||
*/
|
||||
markAsRead(id: Id | Id[]): void;
|
||||
/**
|
||||
* Mark one or more notifications as read.The second parameter let you mark the notificaiton as read or not.
|
||||
*
|
||||
* Usage:
|
||||
* ```
|
||||
* markAsRead("anId", false)
|
||||
* markAsRead(["a","list", "of", "id"], false)
|
||||
*
|
||||
* markAsRead("anId", true) // same as markAsRead("anId")
|
||||
* ```
|
||||
*/
|
||||
markAsRead(id: Id | Id[], read?: boolean): void;
|
||||
/**
|
||||
* Remove one or more notifications
|
||||
*
|
||||
* Usage:
|
||||
* ```
|
||||
* remove("anId")
|
||||
* remove(["a","list", "of", "id"])
|
||||
* ```
|
||||
*/
|
||||
remove(id: Id | Id[]): void;
|
||||
/**
|
||||
* Push a notification to the notification center.
|
||||
* Returns null when an item with the given id already exists
|
||||
*
|
||||
* Usage:
|
||||
* ```
|
||||
* const id = add({id: "id", content: "test", data: { foo: "hello" } })
|
||||
*
|
||||
* // Return the id of the notificaiton, generate one if none provided
|
||||
* const id = add({ data: {title: "a title", text: "some text"} })
|
||||
* ```
|
||||
*/
|
||||
add(item: Partial<NotificationCenterItem<Data>>): Id | null;
|
||||
/**
|
||||
* Update the notification that match the id
|
||||
* Returns null when no matching notification found
|
||||
*
|
||||
* Usage:
|
||||
* ```
|
||||
* const id = update("anId", {content: "test", data: { foo: "hello" } })
|
||||
*
|
||||
* // It's also possible to update the id
|
||||
* const id = update("anId"m { id:"anotherOne", data: {title: "a title", text: "some text"} })
|
||||
* ```
|
||||
*/
|
||||
update(id: Id, item: Partial<NotificationCenterItem<Data>>): Id | null;
|
||||
/**
|
||||
* Retrive one or more notifications
|
||||
*
|
||||
* Usage:
|
||||
* ```
|
||||
* find("anId")
|
||||
* find(["a","list", "of", "id"])
|
||||
* ```
|
||||
*/
|
||||
find(id: Id): NotificationCenterItem<Data> | undefined;
|
||||
/**
|
||||
* Retrive one or more notifications
|
||||
*
|
||||
* Usage:
|
||||
* ```
|
||||
* find("anId")
|
||||
* find(["a","list", "of", "id"])
|
||||
* ```
|
||||
*/
|
||||
find(id: Id[]): NotificationCenterItem<Data>[] | undefined;
|
||||
/**
|
||||
* Retrieve the count for unread notifications
|
||||
*/
|
||||
unreadCount: number;
|
||||
/**
|
||||
* Sort notifications using the newly provided function
|
||||
*
|
||||
* Usage:
|
||||
* ```
|
||||
* // old notifications first
|
||||
* sort((l, r) => l.createdAt - r.createdAt)
|
||||
* ```
|
||||
*/
|
||||
sort(sort: SortFn<Data>): void;
|
||||
}
|
||||
export declare function useNotificationCenter<Data = {}>(params?: UseNotificationCenterParams<Data>): UseNotificationCenter<Data>;
|
||||
export {};
|
||||
Reference in New Issue
Block a user