From a63100b426e2bfc2a0f26c29ad4b92d07976014e Mon Sep 17 00:00:00 2001 From: Gauthier Fiorentino Date: Wed, 30 Oct 2024 14:26:15 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20D=C3=A9grade=20gracieusement=20lorsque?= =?UTF-8?q?=20pas=20de=20randomUUID=20(#3411)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/hooks/useSessionId.ts | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) 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;