Track sessionId
This commit is contained in:
17
hooks/useSessionId.ts
Normal file
17
hooks/useSessionId.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { useMemo } from "react"
|
||||
import { v4 as uuidv4 } from "uuid"
|
||||
|
||||
const storageKey = "sessionId"
|
||||
|
||||
export function useSessionId(): string {
|
||||
const sessionId = useMemo(() => {
|
||||
let currentSessionId = sessionStorage.getItem(storageKey)
|
||||
if (!currentSessionId) {
|
||||
currentSessionId = uuidv4()
|
||||
sessionStorage.setItem(storageKey, currentSessionId)
|
||||
}
|
||||
return currentSessionId
|
||||
}, [])
|
||||
|
||||
return sessionId
|
||||
}
|
||||
Reference in New Issue
Block a user