Files
web/hooks/useSessionId.ts
2024-12-03 14:20:13 +01:00

23 lines
546 B
TypeScript

import { useMemo } from "react"
import { v4 as uuidv4 } from "uuid"
const storageKey = "sessionId"
export function useSessionId(): string | null {
const sessionId = useMemo(() => {
if (typeof window === "undefined") {
// Return null if running on the server
return null
}
let currentSessionId = sessionStorage.getItem(storageKey)
if (!currentSessionId) {
currentSessionId = uuidv4()
sessionStorage.setItem(storageKey, currentSessionId)
}
return currentSessionId
}, [])
return sessionId
}