useNotificationCenter.d.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { ToastItem, Id } from 'react-toastify';
  2. type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
  3. export interface NotificationCenterItem<Data = {}> extends Optional<ToastItem<Data>, 'content' | 'data'> {
  4. read: boolean;
  5. createdAt: number;
  6. }
  7. export type SortFn<Data> = (l: NotificationCenterItem<Data>, r: NotificationCenterItem<Data>) => number;
  8. export type FilterFn<Data = {}> = (item: NotificationCenterItem<Data>) => boolean;
  9. export interface UseNotificationCenterParams<Data = {}> {
  10. /**
  11. * initial data to rehydrate the notification center
  12. */
  13. data?: NotificationCenterItem<Data>[];
  14. /**
  15. * By default, the notifications are sorted from the newest to the oldest using
  16. * the `createdAt` field. Use this to provide your own sort function
  17. *
  18. * Usage:
  19. * ```
  20. * // old notifications first
  21. * useNotificationCenter({
  22. * sort: ((l, r) => l.createdAt - r.createdAt)
  23. * })
  24. * ```
  25. */
  26. sort?: SortFn<Data>;
  27. /**
  28. * Keep the toast that meets the condition specified in the callback function.
  29. *
  30. * Usage:
  31. * ```
  32. * // keep only the toasts when hidden is set to false
  33. * useNotificationCenter({
  34. * filter: item => item.data.hidden === false
  35. * })
  36. * ```
  37. */
  38. filter?: FilterFn<Data>;
  39. }
  40. export interface UseNotificationCenter<Data> {
  41. /**
  42. * Contains all the notifications
  43. */
  44. notifications: NotificationCenterItem<Data>[];
  45. /**
  46. * Clear all notifications
  47. */
  48. clear(): void;
  49. /**
  50. * Mark all notification as read
  51. */
  52. markAllAsRead(): void;
  53. /**
  54. * Mark all notification as read or not.
  55. *
  56. * Usage:
  57. * ```
  58. * markAllAsRead(false) // mark all notification as not read
  59. *
  60. * markAllAsRead(true) // same as calling markAllAsRead()
  61. * ```
  62. */
  63. markAllAsRead(read?: boolean): void;
  64. /**
  65. * Mark one or more notifications as read.
  66. *
  67. * Usage:
  68. * ```
  69. * markAsRead("anId")
  70. * markAsRead(["a","list", "of", "id"])
  71. * ```
  72. */
  73. markAsRead(id: Id | Id[]): void;
  74. /**
  75. * Mark one or more notifications as read.The second parameter let you mark the notificaiton as read or not.
  76. *
  77. * Usage:
  78. * ```
  79. * markAsRead("anId", false)
  80. * markAsRead(["a","list", "of", "id"], false)
  81. *
  82. * markAsRead("anId", true) // same as markAsRead("anId")
  83. * ```
  84. */
  85. markAsRead(id: Id | Id[], read?: boolean): void;
  86. /**
  87. * Remove one or more notifications
  88. *
  89. * Usage:
  90. * ```
  91. * remove("anId")
  92. * remove(["a","list", "of", "id"])
  93. * ```
  94. */
  95. remove(id: Id | Id[]): void;
  96. /**
  97. * Push a notification to the notification center.
  98. * Returns null when an item with the given id already exists
  99. *
  100. * Usage:
  101. * ```
  102. * const id = add({id: "id", content: "test", data: { foo: "hello" } })
  103. *
  104. * // Return the id of the notificaiton, generate one if none provided
  105. * const id = add({ data: {title: "a title", text: "some text"} })
  106. * ```
  107. */
  108. add(item: Partial<NotificationCenterItem<Data>>): Id | null;
  109. /**
  110. * Update the notification that match the id
  111. * Returns null when no matching notification found
  112. *
  113. * Usage:
  114. * ```
  115. * const id = update("anId", {content: "test", data: { foo: "hello" } })
  116. *
  117. * // It's also possible to update the id
  118. * const id = update("anId"m { id:"anotherOne", data: {title: "a title", text: "some text"} })
  119. * ```
  120. */
  121. update(id: Id, item: Partial<NotificationCenterItem<Data>>): Id | null;
  122. /**
  123. * Retrive one or more notifications
  124. *
  125. * Usage:
  126. * ```
  127. * find("anId")
  128. * find(["a","list", "of", "id"])
  129. * ```
  130. */
  131. find(id: Id): NotificationCenterItem<Data> | undefined;
  132. /**
  133. * Retrive one or more notifications
  134. *
  135. * Usage:
  136. * ```
  137. * find("anId")
  138. * find(["a","list", "of", "id"])
  139. * ```
  140. */
  141. find(id: Id[]): NotificationCenterItem<Data>[] | undefined;
  142. /**
  143. * Retrieve the count for unread notifications
  144. */
  145. unreadCount: number;
  146. /**
  147. * Sort notifications using the newly provided function
  148. *
  149. * Usage:
  150. * ```
  151. * // old notifications first
  152. * sort((l, r) => l.createdAt - r.createdAt)
  153. * ```
  154. */
  155. sort(sort: SortFn<Data>): void;
  156. }
  157. export declare function useNotificationCenter<Data = {}>(params?: UseNotificationCenterParams<Data>): UseNotificationCenter<Data>;
  158. export {};