{"version":3,"file":"useAddNotificationOnError.mjs","names":[],"sources":["../../../../../src/components/Notifications/hooks/useAddNotificationOnError.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\n\nimport { useStableCallback } from '../../../utils/useStableCallback';\nimport type { AddNotificationParams } from './useNotificationApi';\nimport { useNotificationApi } from './useNotificationApi';\n\n/** Notification params of {@link useAddNotificationOnError}; the error and the `error` severity are its own. */\nexport type AddNotificationOnErrorParams = Omit<\n  AddNotificationParams,\n  'error' | 'severity'\n>;\n\n/**\n * Publishes an error notification each time `error` changes to a new error object while the\n * component is mounted. The error present on mount is not published: the hook reports only errors\n * that occur while it is mounted.\n *\n * @param error - The error to report; `undefined` while there is none.\n * @param params - The notification to publish for it, as for `addNotification`.\n */\nexport const useAddNotificationOnError = (\n  error: Error | undefined,\n  params: AddNotificationOnErrorParams,\n) => {\n  const { addNotification } = useNotificationApi();\n  const errorOnMount = useRef(error);\n\n  // The notification API and the params change with unrelated state (an open dialog, the\n  // language), which must not publish the same error again.\n  const addErrorNotification = useStableCallback((nextError: Error) =>\n    addNotification({ ...params, error: nextError, severity: 'error' }),\n  );\n\n  useEffect(() => {\n    if (error && error !== errorOnMount.current) addErrorNotification(error);\n  }, [error, addErrorNotification]);\n};\n"],"mappings":";;;;;;;;;;;;AAoBA,IAAa,6BACX,OACA,WACG;CACH,MAAM,EAAE,oBAAoB,mBAAmB;CAC/C,MAAM,eAAe,OAAO,KAAK;CAIjC,MAAM,uBAAuB,mBAAmB,cAC9C,gBAAgB;EAAE,GAAG;EAAQ,OAAO;EAAW,UAAU;CAAQ,CAAC,CACpE;CAEA,gBAAgB;EACd,IAAI,SAAS,UAAU,aAAa,SAAS,qBAAqB,KAAK;CACzE,GAAG,CAAC,OAAO,oBAAoB,CAAC;AAClC"}