Download src/lib/store.ts from Zero124/VR-MEDIA: direct link, hf CLI and curl.
- Browser
- Download file 2.06 kB
-
https://huggingface.co/spaces/Zero124/VR-MEDIA/resolve/main/src/lib/store.ts
- Command line
-
hf download hf://spaces/Zero124/VR-MEDIA/src/lib/store.ts
-
curl -L -o store.ts https://huggingface.co/spaces/Zero124/VR-MEDIA/resolve/main/src/lib/store.ts
2.06 kB
| // Zustand store for SPA view routing. | |
| // We use a single source of truth for the "current view" so the sidebar, | |
| // top bar, and main content area all stay in sync. | |
| "use client"; | |
| import { create } from "zustand"; | |
| import type { ViewId } from "./types"; | |
| export interface ViewState { | |
| view: ViewId; | |
| videoId?: string; | |
| searchQuery?: string; | |
| } | |
| interface AppStore { | |
| viewState: ViewState; | |
| sidebarOpen: boolean; | |
| setView: (view: ViewId, opts?: { videoId?: string; searchQuery?: string }) => void; | |
| toggleSidebar: () => void; | |
| setSidebarOpen: (open: boolean) => void; | |
| } | |
| export const useAppStore = create<AppStore>((set) => ({ | |
| viewState: { view: "dashboard" }, | |
| sidebarOpen: true, | |
| setView: (view, opts) => { | |
| set({ | |
| viewState: { | |
| view, | |
| videoId: opts?.videoId, | |
| searchQuery: opts?.searchQuery, | |
| }, | |
| }); | |
| // Update URL hash for shareability | |
| if (typeof window !== "undefined") { | |
| let hash = `#${view}`; | |
| if (opts?.videoId) hash += `/${opts.videoId}`; | |
| if (opts?.searchQuery) hash += `?q=${encodeURIComponent(opts.searchQuery)}`; | |
| window.history.replaceState(null, "", hash); | |
| } | |
| }, | |
| toggleSidebar: () => set((s) => ({ sidebarOpen: !s.sidebarOpen })), | |
| setSidebarOpen: (open) => set({ sidebarOpen: open }), | |
| })); | |
| /** On mount, hydrate the view state from the URL hash. */ | |
| export function hydrateViewFromHash(): void { | |
| if (typeof window === "undefined") return; | |
| const hash = window.location.hash.slice(1); | |
| if (!hash) return; | |
| const [viewPart, queryPart] = hash.split("?"); | |
| const [view, videoId] = viewPart.split("/"); | |
| const validViews: ViewId[] = [ | |
| "dashboard", "library", "inspector", "feed-manager", | |
| "search", "queue", "learning", "settings", "v3-roadmap", | |
| ]; | |
| if (validViews.includes(view as ViewId)) { | |
| const params = new URLSearchParams(queryPart); | |
| useAppStore.getState().setView(view as ViewId, { | |
| videoId: videoId, | |
| searchQuery: params.get("q") ?? undefined, | |
| }); | |
| } | |
| } | |