Merged in feat/sw-3322-move-base-tracking-to-common (pull request #2713)
feat(SW-3322): Move base tracking to common package * Move base tracking to common package * Update lock file Approved-by: Joakim Jäderberg
This commit is contained in:
22
packages/common/hooks/useSessionId.ts
Normal file
22
packages/common/hooks/useSessionId.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { nanoid } from "nanoid"
|
||||
import { useMemo } from "react"
|
||||
|
||||
export const SESSION_ID_KEY_NAME = "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(SESSION_ID_KEY_NAME)
|
||||
if (!currentSessionId) {
|
||||
currentSessionId = nanoid()
|
||||
sessionStorage.setItem(SESSION_ID_KEY_NAME, currentSessionId)
|
||||
}
|
||||
return currentSessionId
|
||||
}, [])
|
||||
|
||||
return sessionId
|
||||
}
|
||||
Reference in New Issue
Block a user