ohctechv3/.svn/pristine/ba/ba72301e748ca7cdd8e694d20ca633be706e0814.svn-base

1 line
14 KiB
Plaintext
Raw Normal View History

2024-10-28 15:03:36 +05:30
{"version":3,"file":"index.js","sources":["../../../src/addons/use-notification-center/useNotificationCenter.ts"],"sourcesContent":["import { useState, useEffect, useRef } from 'react';\nimport { toast, ToastItem, Id } from 'react-toastify';\n\ntype Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;\n\nexport interface NotificationCenterItem<Data = {}>\n extends Optional<ToastItem<Data>, 'content' | 'data' | 'status'> {\n read: boolean;\n createdAt: number;\n}\n\nexport type SortFn<Data> = (\n l: NotificationCenterItem<Data>,\n r: NotificationCenterItem<Data>\n) => number;\n\nexport type FilterFn<Data = {}> = (\n item: NotificationCenterItem<Data>\n) => boolean;\n\nexport interface UseNotificationCenterParams<Data = {}> {\n /**\n * initial data to rehydrate the notification center\n */\n data?: NotificationCenterItem<Data>[];\n\n /**\n * By default, the notifications are sorted from the newest to the oldest using\n * the `createdAt` field. Use this to provide your own sort function\n *\n * Usage:\n * ```\n * // old notifications first\n * useNotificationCenter({\n * sort: ((l, r) => l.createdAt - r.createdAt)\n * })\n * ```\n */\n sort?: SortFn<Data>;\n\n /**\n * Keep the toast that meets the condition specified in the callback function.\n *\n * Usage:\n * ```\n * // keep only the toasts when hidden is set to false\n * useNotificationCenter({\n * filter: item => item.data.hidden === false\n * })\n * ```\n */\n filter?: FilterFn<Data>;\n}\n\nexport interface UseNotificationCenter<Data> {\n /**\n * Contains all the notifications\n */\n notifications: NotificationCenterItem<Data>[];\n\n /**\n * Clear all notifications\n */\n clear(): void;\n\n /**\n * Mark all notification as read\n */\n markAllAsRead(): void;\n\n /**\n * Mark all notification as read or not.\n *\n * Usage:\n * ```\n * markAllAsRead(false) // mark all notification as not read\n *\n * markAllAsRead(true) // same as calling markAllAsRead()\n * ```\n */\n markAllAsRead(read?: boolean): void;\n\n /**\n * Mark one or more notifications as read.\n *\n * Usage:\n * ```\n * markAsRead(\"anId\")\n * markAsRead([\"a\",\"list\", \"of\", \"id\"])\n * ```\n */\n markAsRead(id: Id | Id[]): void;\n\n /**\n * Mark one or more notifications as read.The second parameter let you mark the notification as read or not.\n *\n * Usage:\n * ```\n * markAsRead(\"anId\", false)\n * markAsRead([\"a\",\"list\", \"of\", \"id\"], false)\n *\n * markAsRead(\"anId\", true) // same as markAsRead(\"anId\")\n * ```\n */\n markAsRead(id: Id | Id[], read?: boolean): void;\n\n /**\n * Remove one or more notifications\n *\n * Usage:\n * ```\n * remove(\"anId\")\n * remove([\"a\",\"list\", \"of\", \"id\"])\n * ```\n */\n remove(id: Id | Id[]): void;\n\n /**\n * Push a notification to the notification center.\n * Returns null when an item with the given id already exists\n *\n * Usage:\n * ```\n * const id = add({id: \"id\", content: \"test\", data: { foo: \"hello\" } })\n *\n * // Return the id of the notification, generate one if none provided\n * const id = add({ data: {title: \"a title\", text: \"some text\"} })\n * ```\n */\n add(item: Partial<NotificationCenterItem<Data>>): Id | null;\n\n /**\n * Update the notification that match the id\n * Returns null when no matching notification found\n *\n * Usage:\n * ```\n * const id = update(\"anId\", {content: \"test\", data: { foo: \"hello\" } })\n *\n * // It's also possible to update the id\n * const id = update(\"anId\"m { id:\"anotherOne\", data: {title: \"a title\", text: \"some text\"} })\n * ```\n */\n update(id: Id, item: Partial<NotificationCenterItem<Data>>): Id | null;\n\n /**\n * Retrieve one or more notifications\n *\n * Usage:\n * ```\n * find(\"anId\")\n * find([\"a\",\"list\", \"of\", \"id\"])\n * ```\n */\n find(id: Id): NotificationCenterItem<Data> | undefined;\n\n /**\n * Retrieve one or m