Files
web/hooks/useSessionId.ts
2024-12-05 10:57:45 +01:00

23 lines
542 B
TypeScript

import { nanoid } from "nanoid"
import { useMemo } from "react"
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 = nanoid()
sessionStorage.setItem(storageKey, currentSessionId)
}
return currentSessionId
}, [])
return sessionId
}