multica/apps/web/app/hooks/use-device-id.ts
Naiyuan Qing 5f367fb6b7 fix(web): replace Zustand device store with local useDeviceId hook
Move device ID logic from @multica/store (Zustand persist) into a
simple useDeviceId hook in the web app. SSR returns empty string,
client reads/writes localStorage directly — no hydration mismatch,
no suppressHydrationWarning needed.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-30 22:16:54 +08:00

19 lines
412 B
TypeScript

import { useState, useEffect } from "react"
import { v7 as uuidv7 } from "uuid"
const STORAGE_KEY = "multica-device-id"
export function useDeviceId(): string {
const [deviceId, setDeviceId] = useState("")
useEffect(() => {
let id = localStorage.getItem(STORAGE_KEY)
if (!id) {
id = uuidv7()
localStorage.setItem(STORAGE_KEY, id)
}
setDeviceId(id)
}, [])
return deviceId
}