{"version":3,"file":"useNotificationApi.mjs","names":[],"sources":["../../../../../src/components/Notifications/hooks/useNotificationApi.ts"],"sourcesContent":["import { useCallback } from 'react';\n\nimport type { Notification, NotificationAction, NotificationSeverity } from 'stream-chat';\n\nimport type { AriaLivePriority } from '../../Accessibility';\nimport { useChatContext, useModalDialogManager } from '../../../context';\nimport { useStateStore } from '../../../store';\nimport {\n  addNotificationTargetTag,\n  getNotificationTargetTag,\n  type NotificationTargetPanel,\n} from '../notificationTarget';\nimport { useNotificationTarget } from './useNotificationTarget';\n\nimport type { DialogManagerState } from '../../Dialog/service/DialogManager';\n\nconst modalDialogManagerStateSelector = ({\n  dialogsById,\n  openedDialogIds,\n}: DialogManagerState) => ({\n  hasOpenDialog: openedDialogIds\n    ? openedDialogIds.length > 0\n    : Object.values(dialogsById).some((dialog) => dialog.isOpen),\n});\n\n/** Tag of full-width system banners (e.g. connection status), which `NotificationList` excludes. */\nexport const SYSTEM_NOTIFICATION_TAG = 'system' as const;\n\nexport const hasSystemNotificationTag = (notification: Notification) =>\n  notification.tags?.includes(SYSTEM_NOTIFICATION_TAG) ?? false;\n\nexport type NotificationIncidentDescriptor = {\n  /** Where the incident happened (e.g. api, browser, validation, permission). */\n  domain: string;\n  /** Entity being operated on (e.g. message, poll, location, attachment). */\n  entity: string;\n  /** Attempted operation (e.g. send, end, share, upload). */\n  operation: string;\n  /** Status of the operation (e.g. failed, success, blocked). */\n  status?: string;\n};\n\nexport type AddNotificationParams = {\n  /** Interactive actions rendered by the notification component. */\n  actions?: NotificationAction[];\n  /**\n   * Overrides the aria-live priority the notification is announced with. Defaults to `assertive`\n   * for `error` severity and `polite` otherwise. Use `assertive` for a confirmation that should be\n   * heard promptly instead of queued behind a competing announcement (e.g. a focus change).\n   */\n  ariaLive?: AriaLivePriority;\n  /** Arbitrary context metadata stored in `origin.context`. */\n  context?: Record<string, unknown>;\n  /** Duration in milliseconds after which the notification auto-dismisses. */\n  duration?: number;\n  /** Logical source emitting the notification (e.g. component or feature name). */\n  emitter: string;\n  /** Underlying error object attached as `options.originalError`. */\n  error?: Error;\n  /** Human-readable notification message. */\n  message: string;\n  /** Notification severity visualized by the UI. */\n  severity?: NotificationSeverity;\n  /** Additional tags appended to target panel tags. */\n  tags?: string[];\n  /** Explicit target panels; when provided, inferred panel is ignored. */\n  targetPanels?: NotificationTargetPanel[];\n  /** Structured descriptor of the incident this notification reports on. */\n  incident?: NotificationIncidentDescriptor;\n  /**\n   * Machine-readable type identifier (`domain:entity:operation:status`), used by consumers to route\n   * behavior, including translation lookup. Generated from `incident` when omitted and `incident`\n   * is provided.\n   */\n  type?: string;\n};\n\n/**\n * {@link AddNotificationParams} without `targetPanels`: system banners are global and get the\n * `system` tag instead of `target:*` panel tags.\n */\nexport type AddSystemNotificationParams = Omit<AddNotificationParams, 'targetPanels'>;\n\n/**\n * Returns the notification ID, which `removeNotification`, `startNotificationTimeout` and\n * `ensureNotificationTimeout` take.\n */\nexport type AddNotification = (params: AddNotificationParams) => string;\n/** Returns the notification ID, for removal and timeouts. */\nexport type AddSystemNotification = (params: AddSystemNotificationParams) => string;\nexport type RemoveNotification = (id: string) => void;\nexport type StartNotificationTimeout = (id: string) => void;\n/** Starts a notification's countdown unless one is already running. */\nexport type EnsureNotificationTimeout = (id: string) => void;\n\nexport type NotificationApi = {\n  addNotification: AddNotification;\n  addSystemNotification: AddSystemNotification;\n  ensureNotificationTimeout: EnsureNotificationTimeout;\n  removeNotification: RemoveNotification;\n  startNotificationTimeout: StartNotificationTimeout;\n};\n\nconst getTargetTags = (\n  targetPanels: NotificationTargetPanel[] | undefined,\n  inferredPanel: NotificationTargetPanel | undefined,\n  tags: string[] | undefined,\n  modalManagerHasOpenDialog: boolean,\n) => {\n  if (targetPanels) {\n    const effectiveTargetPanels =\n      modalManagerHasOpenDialog && targetPanels.length > 0\n        ? [...targetPanels, 'modal' as const]\n        : targetPanels;\n\n    return Array.from(\n      new Set([...effectiveTargetPanels.map(getNotificationTargetTag), ...(tags ?? [])]),\n    );\n  }\n\n  if (modalManagerHasOpenDialog) {\n    return Array.from(\n      new Set([\n        ...(inferredPanel ? [getNotificationTargetTag(inferredPanel)] : []),\n        getNotificationTargetTag('modal'),\n        ...(tags ?? []),\n      ]),\n    );\n  }\n\n  return addNotificationTargetTag(inferredPanel, tags);\n};\n\nconst getTypeFromIncident = ({\n  incident,\n  severity,\n  type,\n}: Pick<AddNotificationParams, 'incident' | 'severity' | 'type'>) => {\n  if (type) return type;\n  if (!incident) return undefined;\n\n  const status =\n    incident.status ??\n    (severity === 'error' ? 'failed' : severity === 'success' ? 'success' : severity);\n\n  return [incident.domain, incident.entity, incident.operation, status]\n    .filter((segment): segment is string => !!segment)\n    .join(':');\n};\n\nexport const useNotificationApi = (): NotificationApi => {\n  const { client } = useChatContext();\n  const inferredPanel = useNotificationTarget();\n  const modalDialogManager = useModalDialogManager();\n  const modalManagerHasOpenDialog =\n    useStateStore(modalDialogManager?.state, modalDialogManagerStateSelector)\n      ?.hasOpenDialog ?? false;\n\n  const addNotification: AddNotification = useCallback(\n    ({\n      actions,\n      ariaLive,\n      context,\n      duration,\n      emitter,\n      error,\n      incident,\n      message,\n      severity,\n      tags,\n      targetPanels,\n      type,\n    }: AddNotificationParams) => {\n      const notificationTags = getTargetTags(\n        targetPanels,\n        inferredPanel,\n        tags,\n        modalManagerHasOpenDialog,\n      );\n      const resolvedType = getTypeFromIncident({ incident, severity, type });\n      const origin = context ? { context, emitter } : { emitter };\n\n      const options = {\n        ...(actions ? { actions } : {}),\n        ...(typeof duration === 'number' ? { duration } : {}),\n        ...(error ? { originalError: error } : {}),\n        ...(ariaLive ? { metadata: { ariaLive } } : {}),\n        ...(notificationTags.length > 0 ? { tags: notificationTags } : {}),\n        ...(severity ? { severity } : {}),\n        ...(resolvedType ? { type: resolvedType } : {}),\n      };\n\n      return client.notifications.add({\n        message,\n        options,\n        origin,\n      });\n    },\n    [client, inferredPanel, modalManagerHasOpenDialog],\n  );\n\n  const addSystemNotification: AddSystemNotification = useCallback(\n    ({\n      actions,\n      ariaLive,\n      context,\n      duration,\n      emitter,\n      error,\n      incident,\n      message,\n      severity,\n      tags,\n      type,\n    }: AddSystemNotificationParams) => {\n      const notificationTags = Array.from(\n        new Set([SYSTEM_NOTIFICATION_TAG, ...(tags ?? [])]),\n      );\n      const resolvedType = getTypeFromIncident({ incident, severity, type });\n      const origin = context ? { context, emitter } : { emitter };\n\n      const options = {\n        ...(actions ? { actions } : {}),\n        ...(typeof duration === 'number' ? { duration } : {}),\n        ...(error ? { originalError: error } : {}),\n        ...(ariaLive ? { metadata: { ariaLive } } : {}),\n        ...(notificationTags.length > 0 ? { tags: notificationTags } : {}),\n        ...(severity ? { severity } : {}),\n        ...(resolvedType ? { type: resolvedType } : {}),\n      };\n\n      return client.notifications.add({\n        message,\n        options,\n        origin,\n      });\n    },\n    [client],\n  );\n\n  const removeNotification: RemoveNotification = useCallback(\n    (id) => {\n      client.notifications.remove(id);\n    },\n    [client],\n  );\n\n  const startNotificationTimeout: StartNotificationTimeout = useCallback(\n    (id) => {\n      client.notifications.startTimeout(id);\n    },\n    [client],\n  );\n\n  // Idempotency lives in the manager: a caller that cannot know whether the countdown is already\n  // running (e.g. a remounted component) would otherwise reset the notification's deadline.\n  const ensureNotificationTimeout: EnsureNotificationTimeout = useCallback(\n    (id) => {\n      client.notifications.ensureTimeout(id);\n    },\n    [client],\n  );\n\n  return {\n    addNotification,\n    addSystemNotification,\n    ensureNotificationTimeout,\n    removeNotification,\n    startNotificationTimeout,\n  };\n};\n"],"mappings":";;;;;;;AAgBA,IAAM,mCAAmC,EACvC,aACA,uBACyB,EACzB,eAAe,kBACX,gBAAgB,SAAS,IACzB,OAAO,OAAO,WAAW,CAAC,CAAC,MAAM,WAAW,OAAO,MAAM,EAC/D;;AAGA,IAAa,0BAA0B;AAEvC,IAAa,4BAA4B,iBACvC,aAAa,MAAM,SAAA,QAAgC,KAAK;AA0E1D,IAAM,iBACJ,cACA,eACA,MACA,8BACG;CACH,IAAI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