Files
web/hooks/useSessionId.ts
Linus Flood 02d3352ea5 Merged in fix/rename-sessionid (pull request #1177)
fix: rename sessionId storage name to avoid conflict with current web

* fix: rename sessionId storage name to avoid conflict with current web
2025-01-13 18:24:41 +00:00

23 lines
546 B
TypeScript

import { nanoid } from "nanoid"
import { useMemo } from "react"
const storageKey = "web_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
}