multica/apps/web/app/hooks/use-device-id.ts
Naiyuan Qing 716bbceaf4 refactor(chat, hooks): simplify handleSend and update useDeviceId to use useSyncExternalStore
- Simplified handleSend function in Chat component by removing unnecessary useCallback.
- Refactored useDeviceId hook to utilize useSyncExternalStore for better state management and removed useState and useEffect for device ID retrieval.
- Updated useGateway hook to ensure onMessageRef is set correctly on options change.
- Enhanced useScrollFade hook to properly cancel animation frames on cleanup.
2026-01-30 23:38:10 +08:00

26 lines
600 B
TypeScript

import { useSyncExternalStore } from "react"
import { v7 as uuidv7 } from "uuid"
const STORAGE_KEY = "multica-device-id"
function getSnapshot(): string {
let id = localStorage.getItem(STORAGE_KEY)
if (!id) {
id = uuidv7()
localStorage.setItem(STORAGE_KEY, id)
}
return id
}
function subscribe(cb: () => void) {
window.addEventListener("storage", cb)
return () => window.removeEventListener("storage", cb)
}
function getServerSnapshot(): string {
return ""
}
export function useDeviceId(): string {
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot)
}