Skip to content

Commit

Permalink
fix: Dégrade gracieusement lorsque pas de randomUUID (#3411)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mintoo200 authored Oct 30, 2024
1 parent a08f0d3 commit a63100b
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/client/hooks/useSessionId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ import { isStorageAvailable } from '~/client/utils/isStorageAvailable';

const SESSION_ID = 'session_Id';
function useSessionId (): string | undefined {
const [value, setValue] = useState<string>();
const [value, setValue] = useState<string | undefined>();

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;
Expand Down

0 comments on commit a63100b

Please sign in to comment.