diff --git a/src/client/hooks/useSessionId.ts b/src/client/hooks/useSessionId.ts index 2ab5605822..af3978f172 100644 --- a/src/client/hooks/useSessionId.ts +++ b/src/client/hooks/useSessionId.ts @@ -4,21 +4,25 @@ import { isStorageAvailable } from '~/client/utils/isStorageAvailable'; const SESSION_ID = 'session_Id'; function useSessionId (): string | undefined { - const [value, setValue] = useState(); + const [value, setValue] = useState(); useEffect(() => { - if (isStorageAvailable('sessionStorage')) { - let sessionId = sessionStorage ? sessionStorage.getItem(SESSION_ID) : undefined; - if (!sessionId) { - // FIXME (GAFI 28-10-2024): Idéalement à injecter - sessionId = window.crypto.randomUUID(); - sessionStorage.setItem(SESSION_ID, sessionId); - } + if (!isStorageAvailable('sessionStorage')) { + return; + } + // FIXME (GAFI 29-10-2024): À injecter + const sessionId = sessionStorage?.getItem(SESSION_ID); + if (sessionId) { setValue(sessionId); - } else { - setValue(undefined); - }; - + return; + } + const randomUUIDAvailable = typeof window.crypto?.randomUUID === 'function'; + if (randomUUIDAvailable) { + // FIXME (GAFI 28-10-2024): Idéalement à injecter + const newSessionId = window.crypto.randomUUID(); + sessionStorage?.setItem(SESSION_ID, newSessionId); + setValue(newSessionId); + } }, []); return value;