- 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.
26 lines
600 B
TypeScript
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)
|
|
}
|