{"version":3,"file":"useNotifications.mjs","names":[],"sources":["../../../../../src/components/Notifications/hooks/useNotifications.ts"],"sourcesContent":["import { useCallback } from 'react';\nimport { useChatContext } from '../../../context';\nimport { useStateStore } from '../../../store';\nimport type { Notification, NotificationManagerState } from 'stream-chat';\nimport { useNotificationConfigurationContext } from '../NotificationConfigurationContext';\nimport { isNotificationForPanel } from '../notificationTarget';\n\nimport type { NotificationTargetPanel } from '../notificationTarget';\n\nexport type UseNotificationsFilter = (notification: Notification) => boolean;\n\nexport type UseNotificationsOptions = {\n  /** Applies the Chat-level display filter after panel routing and before `filter`. */\n  applyDisplayFilter?: boolean;\n  /** Keeps only the notifications that pass it, e.g. by `origin.emitter` or `metadata`. */\n  filter?: UseNotificationsFilter;\n  /** Keeps only notifications targeted at this panel. */\n  panel?: NotificationTargetPanel;\n  /** Panel assumed for notifications that name none. Defaults to `channel`. */\n  fallbackPanel?: NotificationTargetPanel;\n};\n\n/**\n * Subscribes to `client.notifications.store` and returns the notifications that pass the options.\n *\n * @param options - Panel routing and filters. Returns every notification when omitted.\n */\nexport const useNotifications = (options?: UseNotificationsOptions): Notification[] => {\n  const { client } = useChatContext();\n  const { displayFilter } = useNotificationConfigurationContext();\n  const { applyDisplayFilter, fallbackPanel, filter, panel } = options ?? {};\n  const selector = useCallback(\n    (state: NotificationManagerState) => {\n      const notifications = state.notifications;\n      return {\n        notifications: notifications.filter((notification) => {\n          if (\n            panel &&\n            !isNotificationForPanel(notification, panel, {\n              fallbackPanel,\n            })\n          ) {\n            return false;\n          }\n\n          if (\n            applyDisplayFilter &&\n            !displayFilter({\n              fallbackPanel,\n              filter,\n              notification,\n              panel,\n            })\n          ) {\n            return false;\n          }\n\n          return filter ? filter(notification) : true;\n        }),\n      };\n    },\n    [applyDisplayFilter, displayFilter, fallbackPanel, filter, panel],\n  );\n\n  const { notifications } = useStateStore(client.notifications.store, selector);\n\n  return notifications;\n};\n"],"mappings":";;;;;;;;;;;AA2BA,IAAa,oBAAoB,YAAsD;CACrF,MAAM,EAAE,WAAW,eAAe;CAClC,MAAM,EAAE,kBAAkB,oCAAoC;CAC9D,MAAM,EAAE,oBAAoB,eAAe,QAAQ,UAAU,WAAW,CAAC;CACzE,MAAM,WAAW,aACd,UAAoC;EAEnC,OAAO,EACL,eAFoB,MAAM,cAEG,QAAQ,iBAAiB;GACpD,IACE,SACA,CAAC,uBAAuB,cAAc,OAAO,EAC3C,cACF,CAAC,GAED,OAAO;GAGT,IACE,sBACA,CAAC,cAAc;IACb;IACA;IACA;IACA;GACF,CAAC,GAED,OAAO;GAGT,OAAO,SAAS,OAAO,YAAY,IAAI;EACzC,CAAC,EACH;CACF,GACA;EAAC;EAAoB;EAAe;EAAe;EAAQ;CAAK,CAClE;CAEA,MAAM,EAAE,kBAAkB,cAAc,OAAO,cAAc,OAAO,QAAQ;CAE5E,OAAO;AACT"}