{"version":3,"file":"index.mjs","names":["Dispatch","SetStateAction","createContext","parseError","Message","MessageOptions","createNotificationsManager","setNotifications","idx","timeoutIds","Map","removeNotification","id","timeoutId","get","clearTimeout","delete","oldNotifications","filter","otherId","addNotification","content","expiration","color","canClose","withLoader","toString","has","Error","newNotification","set","setTimeout","notifyError","err","errorMessage","NotificationsManager","ReturnType","NotificationsContext","ReactNode","useEffect","useRef","useState","createPortal","NotificationsContext","createNotificationsManager","NotificationsManager","Snack","Message","Props","children","NotificationsProvider","notifications","setNotifications","container","HTMLDivElement","currentContainer","current","remove","managerRef","manager","window","document","createElement","id","Math","floor","random","toString","classList","add","body","append","map","props","removeNotification","useContext","NotificationsContext","useContextNotifications","manager","Error","useCallback","CustomFetchOptions","fetchAPI","useContextNotifications","useFetch","$","_c","notificationManager","t0","urlObjOrString","t1","enhancedOptions","undefined","t2","err","notifyError","appFetch"],"sources":["../../../src/components/notification/NotificationsContext.ts","../../../src/components/notification/NotificationsProvider.tsx","../../../src/components/notification/useContextNotifications.tsx","../../../src/components/notification/useFetch.ts"],"sourcesContent":["import { Dispatch, SetStateAction, createContext } from \"react\";\nimport { parseError } from \"../../lib\";\nimport { Message, MessageOptions } from \"../../types\";\n\nexport function createNotificationsManager(setNotifications: Dispatch<SetStateAction<Message[]>>) {\n  let idx = 1;\n  const timeoutIds = new Map<string, any>();\n\n  const removeNotification = (id: string) => {\n    const timeoutId = timeoutIds.get(id);\n    if (!timeoutId) {\n      return;\n    }\n    clearTimeout(timeoutId);\n    timeoutIds.delete(id);\n\n    return setNotifications((oldNotifications) => {\n      return oldNotifications.filter(({ id: otherId }) => id !== otherId);\n    });\n  };\n\n  const addNotification = (\n    content = \"\",\n    {\n      expiration = 5000,\n      color = \"yellow\",\n      canClose = true,\n      withLoader = false,\n    }: MessageOptions = {},\n  ) => {\n    const id = (idx++).toString();\n    if (timeoutIds.has(id)) {\n      throw new Error(\"notification already exists\");\n    }\n\n    setNotifications((oldNotifications) => {\n      const newNotification: Message = {\n        id,\n        content,\n        expiration,\n        color,\n        canClose,\n        withLoader,\n      };\n\n      return [newNotification, ...oldNotifications];\n    });\n\n    timeoutIds.set(\n      id,\n      expiration === -1 ? -1 : setTimeout(() => removeNotification(id), expiration),\n    );\n\n    return id;\n  };\n\n  const notifyError = (err: unknown) => {\n    const errorMessage = parseError(err);\n    if (errorMessage) {\n      addNotification(...errorMessage);\n    } else {\n      throw err;\n    }\n  };\n\n  return {\n    addNotification,\n    removeNotification,\n    notifyError,\n  };\n}\n\nexport type NotificationsManager = ReturnType<typeof createNotificationsManager>;\n\nexport const NotificationsContext = createContext<NotificationsManager>(\n  null as unknown as NotificationsManager,\n);\n\nexport type { Message };\n","import { ReactNode, useEffect, useRef, useState } from \"react\";\nimport { createPortal } from \"react-dom\";\nimport {\n  NotificationsContext,\n  createNotificationsManager,\n  type NotificationsManager,\n} from \"./NotificationsContext\";\nimport { Snack } from \"../snack/Snack\";\nimport { Message } from \"../../types\";\n\ninterface Props {\n  children: ReactNode;\n}\n\nexport function NotificationsProvider({ children }: Props) {\n  const [notifications, setNotifications] = useState<Message[]>([]);\n  const container = useRef<HTMLDivElement>(null!);\n\n  useEffect(() => {\n    const currentContainer = container.current;\n    return () => {\n      currentContainer?.remove();\n    };\n  }, []);\n\n  const managerRef = useRef<NotificationsManager>(null!);\n\n  if (!managerRef.current) {\n    managerRef.current = createNotificationsManager(setNotifications);\n  }\n\n  const manager = managerRef.current;\n\n  if (typeof window === \"undefined\") {\n    return (\n      <NotificationsContext.Provider value={manager}>{children}</NotificationsContext.Provider>\n    );\n  }\n\n  if (!container.current) {\n    container.current = document.createElement(\"div\");\n    container.current.id = Math.floor(Math.random() * 100000).toString();\n    /* .ll-snack-bar */\n    container.current.classList.add(\n      \"fixed\",\n      \"bottom-0\",\n      \"left-0\",\n      \"right-0\",\n      \"z-toast\",\n      \"pointer-events-none\",\n    );\n    document.body.append(container.current);\n  }\n\n  return (\n    <>\n      <NotificationsContext.Provider value={manager}>{children}</NotificationsContext.Provider>\n      {createPortal(\n        /* snack-bar-inner */\n        <div className=\"mx-4 mb-4 flex flex-col items-center gap-4\">\n          {notifications.map((props) => (\n            <Snack\n              {...props}\n              key={props.id}\n              onRemove={() => manager.removeNotification(props.id)}\n            />\n          ))}\n        </div>,\n        container.current,\n      )}\n    </>\n  );\n}\n","import { useContext } from \"react\";\n\nimport { NotificationsContext } from \"./NotificationsContext\";\n\nexport const useContextNotifications = () => {\n  const manager = useContext(NotificationsContext);\n\n  if (!manager) {\n    throw new Error(\"Trying to use uninitialized NotificationsContext\");\n  }\n\n  return manager;\n};\n","import { useCallback } from \"react\";\nimport { CustomFetchOptions, fetchAPI } from \"../../lib\";\nimport { useContextNotifications } from \"./useContextNotifications\";\n\nexport const useFetch = () => {\n  // is it the good useContextNotifications ? from redux or not ??\n  const notificationManager = useContextNotifications();\n\n  const appFetch = useCallback(\n    async (urlObjOrString: string | URL, enhancedOptions: CustomFetchOptions = {}) => {\n      try {\n        return await fetchAPI(urlObjOrString, enhancedOptions);\n      } catch (err) {\n        notificationManager.notifyError(err);\n      }\n    },\n    [notificationManager],\n  );\n\n  return appFetch;\n};\n"],"mappings":";;;;;;;AAIA,SAAgBM,2BAA2BC,kBAAuD;CAChG,IAAIC,MAAM;CACV,MAAMC,6BAAa,IAAIC,IAAiB;CAExC,MAAMC,sBAAsBC,OAAe;EACzC,MAAMC,YAAYJ,WAAWK,IAAIF,EAAE;EACnC,IAAI,CAACC,WACH;EAEFE,aAAaF,SAAS;EACtBJ,WAAWO,OAAOJ,EAAE;EAEpB,OAAOL,kBAAkBU,qBAAqB;GAC5C,OAAOA,iBAAiBC,QAAQ,EAAEN,IAAIO,cAAcP,OAAOO,OAAO;EACpE,CAAC;CACH;CAEA,MAAMC,mBACJC,UAAU,IACV,EACEC,aAAa,KACbC,QAAQ,UACRC,WAAW,MACXC,aAAa,UACK,CAAC,MAClB;EACH,MAAMb,MAAMJ,MAAAA,CAAOkB,SAAS;EAC5B,IAAIjB,WAAWk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