Spaces:
Paused
Paused
File size: 3,779 Bytes
5eb4857 d1e80eb 5eb4857 0e37978 633bcc2 235f8ef ad4fe35 235f8ef 0e37978 235f8ef 633bcc2 235f8ef d1e80eb 235f8ef d1e80eb 235f8ef ad4fe35 0e37978 5eb4857 ad4fe35 0e37978 ad4fe35 5eb4857 ad4fe35 5eb4857 0d8550d ad4fe35 5eb4857 ad4fe35 5eb4857 ad4fe35 5eb4857 d1e80eb 5eb4857 ad4fe35 5eb4857 d1e80eb 5eb4857 ad4fe35 5eb4857 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | import { useState, useCallback, useEffect } from 'react';
import api from '../utils/api';
import { useAuth } from './useAuth';
export interface Workspace {
id: string;
name: string;
slug: string;
branding?: any;
}
// --- SINGULAR: Current Workspace Cache ---
const WS_CACHE_KEY = 'nexus_active_ws_v1';
let globalActiveWorkspace: Workspace | null = (() => {
try {
const cached = sessionStorage.getItem(WS_CACHE_KEY);
return cached ? JSON.parse(cached) : null;
} catch { return null; }
})();
let globalActiveWorkspaceLoading = false;
const activeWorkspaceListeners = new Set<(w: Workspace | null, l: boolean) => void>();
const notifyActiveWorkspace = () => activeWorkspaceListeners.forEach(l => l(globalActiveWorkspace, globalActiveWorkspaceLoading));
export const useWorkspace = () => {
const [workspace, setWorkspace] = useState(globalActiveWorkspace);
const [loading, setLoading] = useState(globalActiveWorkspaceLoading);
useEffect(() => {
const listener = (w: Workspace | null, l: boolean) => {
setWorkspace(w);
setLoading(l);
};
activeWorkspaceListeners.add(listener);
return () => { activeWorkspaceListeners.delete(listener); };
}, []);
const fetchWorkspace = useCallback(async (force: boolean = false) => {
if (!force && globalActiveWorkspace) return;
globalActiveWorkspaceLoading = true;
notifyActiveWorkspace();
try {
const response = await api.get('workspace/');
globalActiveWorkspace = response.data;
sessionStorage.setItem(WS_CACHE_KEY, JSON.stringify(globalActiveWorkspace));
} catch (err) {
console.error("Failed to fetch current workspace", err);
} finally {
globalActiveWorkspaceLoading = false;
notifyActiveWorkspace();
}
}, []);
const { token } = useAuth();
useEffect(() => {
if (token && !globalActiveWorkspace && !globalActiveWorkspaceLoading) {
fetchWorkspace();
}
}, [fetchWorkspace, token]);
return { workspace, loading, refresh: () => fetchWorkspace(true) };
};
// --- PLURAL: Workspace Selection Cache ---
let globalWorkspaces: Workspace[] = [];
let globalWorkspacesLoading = false;
let globalWorkspacesError: string | null = null;
const workspacesListeners = new Set<(w: Workspace[], l: boolean, e: string | null) => void>();
const notifyWorkspaces = () => workspacesListeners.forEach(l => l(globalWorkspaces, globalWorkspacesLoading, globalWorkspacesError));
export const useWorkspaces = () => {
const [localWorkspaces, setLocalWorkspaces] = useState(globalWorkspaces);
const [loading, setLoading] = useState(globalWorkspacesLoading);
const [error, setError] = useState(globalWorkspacesError);
useEffect(() => {
const listener = (w: Workspace[], l: boolean, e: string | null) => {
setLocalWorkspaces(w);
setLoading(l);
setError(e);
};
workspacesListeners.add(listener);
return () => { workspacesListeners.delete(listener); };
}, []);
const fetchWorkspaces = useCallback(async (force: boolean = false) => {
if (!force && globalWorkspaces.length > 0) return;
globalWorkspacesLoading = true;
notifyWorkspaces();
try {
const response = await api.get('workspace/list/');
globalWorkspaces = response.data;
globalWorkspacesError = null;
} catch (err: any) {
globalWorkspacesError = err.response?.data?.detail || 'Failed to fetch workspaces';
} finally {
globalWorkspacesLoading = false;
notifyWorkspaces();
}
}, []);
const { token } = useAuth();
useEffect(() => {
if (token && globalWorkspaces.length === 0) {
fetchWorkspaces();
}
}, [fetchWorkspaces, token]);
return { workspaces: localWorkspaces, loading, error, refresh: () => fetchWorkspaces(true) };
};
|