Spaces:
Running
Running
Download frontend/src/features/workspace/hooks/useWorkspaceStore.tsx from Shadowfilebot/Bot: direct link, hf CLI and curl.
- Browser
- Download file 6 kB
-
https://huggingface.co/spaces/Shadowfilebot/Bot/resolve/main/frontend/src/features/workspace/hooks/useWorkspaceStore.tsx
- Command line
-
hf download hf://spaces/Shadowfilebot/Bot/frontend/src/features/workspace/hooks/useWorkspaceStore.tsx
-
curl -L -o useWorkspaceStore.tsx https://huggingface.co/spaces/Shadowfilebot/Bot/resolve/main/frontend/src/features/workspace/hooks/useWorkspaceStore.tsx
6 kB
| 'use client'; | |
| import React, { createContext, useCallback, useContext, useEffect, useState } from 'react'; | |
| import { useRouter } from 'next/navigation'; | |
| import { ROUTES } from '@/shared/constants/routes'; | |
| import { useAuthSession } from '@/features/auth'; | |
| import { workspaceService } from '../services/workspaceService'; | |
| import { EditorTabItem, Zone } from '../types/workspaceTypes'; | |
| const ACTIVE_ZONE_STORAGE_KEY = 'panelthtuanit_active_zone'; | |
| interface WorkspaceContextValue { | |
| zones: Zone[]; | |
| zonesLoading: boolean; | |
| activeZone: string | null; | |
| selectZone: (zoneName: string) => void; | |
| createZone: (name: string, description: string, ownerId?: string) => Promise<void>; | |
| deleteZone: (name: string) => Promise<void>; | |
| refreshZones: () => Promise<void>; | |
| sidebarOpen: boolean; | |
| setSidebarOpen: (open: boolean) => void; | |
| editorTabs: EditorTabItem[]; | |
| activeEditorPath: string | null; | |
| setActiveEditorPath: (path: string | null) => void; | |
| openFileInEditor: (path: string) => void; | |
| closeEditorTab: (path: string) => void; | |
| paletteOpen: boolean; | |
| openPalette: () => void; | |
| closePalette: () => void; | |
| } | |
| const WorkspaceContext = createContext<WorkspaceContextValue | null>(null); | |
| export function WorkspaceProvider({ children }: { children: React.ReactNode }) { | |
| const router = useRouter(); | |
| const { user } = useAuthSession(); | |
| const [zones, setZones] = useState<Zone[]>([]); | |
| const [zonesLoading, setZonesLoading] = useState(false); | |
| const [activeZone, setActiveZone] = useState<string | null>(null); | |
| const [sidebarOpen, setSidebarOpen] = useState(false); | |
| const [editorTabsByZone, setEditorTabsByZone] = useState<Record<string, EditorTabItem[]>>({}); | |
| const [activeEditorByZone, setActiveEditorByZone] = useState<Record<string, string | null>>({}); | |
| const [paletteOpen, setPaletteOpen] = useState(false); | |
| const refreshZones = useCallback(async () => { | |
| if (!user) { | |
| setZones([]); | |
| setActiveZone(null); | |
| return; | |
| } | |
| setZonesLoading(true); | |
| try { | |
| const list = await workspaceService.listZones(); | |
| setZones(list || []); | |
| setActiveZone((prev) => { | |
| if (prev && list.some((z) => z.name === prev)) return prev; | |
| try { | |
| const saved = window.localStorage.getItem(ACTIVE_ZONE_STORAGE_KEY); | |
| if (saved && list.some((z) => z.name === saved)) return saved; | |
| } catch {} | |
| return list.length > 0 ? list[0].name : null; | |
| }); | |
| } catch { | |
| setZones([]); | |
| } finally { | |
| setZonesLoading(false); | |
| } | |
| }, [user]); | |
| useEffect(() => { | |
| void refreshZones(); | |
| }, [refreshZones]); | |
| useEffect(() => { | |
| const onKeyDown = (e: KeyboardEvent) => { | |
| if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'k') { | |
| e.preventDefault(); | |
| setPaletteOpen((prev) => !prev); | |
| } | |
| }; | |
| window.addEventListener('keydown', onKeyDown); | |
| return () => window.removeEventListener('keydown', onKeyDown); | |
| }, []); | |
| const selectZone = useCallback((zoneName: string) => { | |
| setActiveZone(zoneName); | |
| setSidebarOpen(false); | |
| try { | |
| window.localStorage.setItem(ACTIVE_ZONE_STORAGE_KEY, zoneName); | |
| } catch {} | |
| }, []); | |
| const createZone = useCallback( | |
| async (name: string, description: string, ownerId?: string) => { | |
| await workspaceService.createZone(name, description, ownerId); | |
| await refreshZones(); | |
| selectZone(name); | |
| }, | |
| [refreshZones, selectZone], | |
| ); | |
| const deleteZone = useCallback( | |
| async (name: string) => { | |
| await workspaceService.deleteZone(name); | |
| setEditorTabsByZone((prev) => { | |
| const next = { ...prev }; | |
| delete next[name]; | |
| return next; | |
| }); | |
| await refreshZones(); | |
| }, | |
| [refreshZones], | |
| ); | |
| const editorTabs = activeZone ? editorTabsByZone[activeZone] || [] : []; | |
| const activeEditorPath = activeZone ? activeEditorByZone[activeZone] || null : null; | |
| const setActiveEditorPath = useCallback( | |
| (path: string | null) => { | |
| if (!activeZone) return; | |
| setActiveEditorByZone((prev) => ({ ...prev, [activeZone]: path })); | |
| }, | |
| [activeZone], | |
| ); | |
| const openFileInEditor = useCallback( | |
| (path: string) => { | |
| if (!activeZone) return; | |
| const name = path.split('/').pop() || path; | |
| setEditorTabsByZone((prev) => { | |
| const current = prev[activeZone] || []; | |
| if (current.some((t) => t.path === path)) return prev; | |
| return { ...prev, [activeZone]: [...current, { path, name }] }; | |
| }); | |
| setActiveEditorByZone((prev) => ({ ...prev, [activeZone]: path })); | |
| router.push(ROUTES.PANEL_EDITOR); | |
| }, | |
| [activeZone, router], | |
| ); | |
| const closeEditorTab = useCallback( | |
| (path: string) => { | |
| if (!activeZone) return; | |
| setEditorTabsByZone((prev) => { | |
| const current = prev[activeZone] || []; | |
| const filtered = current.filter((t) => t.path !== path); | |
| setActiveEditorByZone((activePrev) => { | |
| if (activePrev[activeZone] !== path) return activePrev; | |
| const fallback = filtered.length > 0 ? filtered[filtered.length - 1].path : null; | |
| return { ...activePrev, [activeZone]: fallback }; | |
| }); | |
| return { ...prev, [activeZone]: filtered }; | |
| }); | |
| }, | |
| [activeZone], | |
| ); | |
| return ( | |
| <WorkspaceContext.Provider | |
| value={{ | |
| zones, | |
| zonesLoading, | |
| activeZone, | |
| selectZone, | |
| createZone, | |
| deleteZone, | |
| refreshZones, | |
| sidebarOpen, | |
| setSidebarOpen, | |
| editorTabs, | |
| activeEditorPath, | |
| setActiveEditorPath, | |
| openFileInEditor, | |
| closeEditorTab, | |
| paletteOpen, | |
| openPalette: () => setPaletteOpen(true), | |
| closePalette: () => setPaletteOpen(false), | |
| }} | |
| > | |
| {children} | |
| </WorkspaceContext.Provider> | |
| ); | |
| } | |
| export function useWorkspaceStore() { | |
| const ctx = useContext(WorkspaceContext); | |
| if (!ctx) throw new Error('useWorkspaceStore must be used inside WorkspaceProvider'); | |
| return ctx; | |
| } | |