"use client"; import { create } from "zustand"; import type { RoomState, ChatMsg } from "./types"; type Store = { room: RoomState | null; chat: ChatMsg[]; myId: string | null; sessionToken: string | null; setRoom: (r: RoomState | null) => void; pushChat: (m: ChatMsg) => void; clearChat: () => void; setMe: (id: string | null, token: string | null) => void; }; export const useGame = create((set) => ({ room: null, chat: [], myId: null, sessionToken: null, setRoom: (r) => set({ room: r }), pushChat: (m) => set((s) => ({ chat: [...s.chat, m].slice(-150) })), clearChat: () => set({ chat: [] }), setMe: (id, token) => set({ myId: id, sessionToken: token }), })); export function readSession(code?: string) { if (typeof window === "undefined") return null; try { const key = code ? `dt_session_${code}` : "dt_session_last"; const raw = localStorage.getItem(key); return raw ? JSON.parse(raw) : null; } catch { return null; } } export function writeSession(code: string, data: { token: string; nickname: string; avatar: string; playerId: string }) { try { localStorage.setItem(`dt_session_${code}`, JSON.stringify(data)); localStorage.setItem("dt_session_last", JSON.stringify({ ...data, code })); } catch {} }