Spaces:
Running
Running
Delete console/src/workspaces/WorkspaceProvider.tsx
Browse files
console/src/workspaces/WorkspaceProvider.tsx
DELETED
|
@@ -1,134 +0,0 @@
|
|
| 1 |
-
import {
|
| 2 |
-
createContext,
|
| 3 |
-
useCallback,
|
| 4 |
-
useContext,
|
| 5 |
-
useEffect,
|
| 6 |
-
useMemo,
|
| 7 |
-
useState,
|
| 8 |
-
} from "react";
|
| 9 |
-
import { useLocation, useNavigate, useParams } from "react-router-dom";
|
| 10 |
-
import {
|
| 11 |
-
createWorkspace as createWorkspaceRequest,
|
| 12 |
-
fetchWorkspaces,
|
| 13 |
-
getSession,
|
| 14 |
-
updateWorkspace as updateWorkspaceRequest,
|
| 15 |
-
workspaceApiFetch,
|
| 16 |
-
type CreateWorkspaceInput,
|
| 17 |
-
type UpdateWorkspaceInput,
|
| 18 |
-
type Workspace,
|
| 19 |
-
} from "../lib/api";
|
| 20 |
-
import { resolveRoutedWorkspace, switchWorkspacePath, workspacePath } from "./workspaceRoutes";
|
| 21 |
-
|
| 22 |
-
interface WorkspaceContextValue {
|
| 23 |
-
workspaces: Workspace[];
|
| 24 |
-
activeWorkspace: Workspace | null;
|
| 25 |
-
requestedSlug?: string;
|
| 26 |
-
loading: boolean;
|
| 27 |
-
error: string | null;
|
| 28 |
-
notFound: boolean;
|
| 29 |
-
canManageWorkspaces: boolean;
|
| 30 |
-
refreshWorkspaces: () => Promise<Workspace[]>;
|
| 31 |
-
switchWorkspace: (slug: string) => void;
|
| 32 |
-
createWorkspace: (input: CreateWorkspaceInput) => Promise<Workspace>;
|
| 33 |
-
updateWorkspace: (input: UpdateWorkspaceInput) => Promise<Workspace>;
|
| 34 |
-
apiFetch: <T>(path: string, options?: RequestInit) => Promise<T>;
|
| 35 |
-
}
|
| 36 |
-
|
| 37 |
-
const WorkspaceContext = createContext<WorkspaceContextValue | null>(null);
|
| 38 |
-
|
| 39 |
-
export function WorkspaceProvider({ children }: { children: React.ReactNode }) {
|
| 40 |
-
const { workspaceSlug } = useParams();
|
| 41 |
-
const location = useLocation();
|
| 42 |
-
const navigate = useNavigate();
|
| 43 |
-
const session = getSession();
|
| 44 |
-
const [workspaces, setWorkspaces] = useState<Workspace[]>([]);
|
| 45 |
-
const [loading, setLoading] = useState(true);
|
| 46 |
-
const [error, setError] = useState<string | null>(null);
|
| 47 |
-
|
| 48 |
-
const refreshWorkspaces = useCallback(async () => {
|
| 49 |
-
setLoading(true);
|
| 50 |
-
setError(null);
|
| 51 |
-
try {
|
| 52 |
-
const items = await fetchWorkspaces();
|
| 53 |
-
setWorkspaces(items);
|
| 54 |
-
return items;
|
| 55 |
-
} catch (reason) {
|
| 56 |
-
setError(reason instanceof Error ? reason.message : "Unable to load workspaces");
|
| 57 |
-
return [];
|
| 58 |
-
} finally {
|
| 59 |
-
setLoading(false);
|
| 60 |
-
}
|
| 61 |
-
}, []);
|
| 62 |
-
|
| 63 |
-
useEffect(() => {
|
| 64 |
-
void refreshWorkspaces();
|
| 65 |
-
}, [refreshWorkspaces]);
|
| 66 |
-
|
| 67 |
-
const activeWorkspace = useMemo(
|
| 68 |
-
() => resolveRoutedWorkspace(workspaces, workspaceSlug),
|
| 69 |
-
[workspaceSlug, workspaces],
|
| 70 |
-
);
|
| 71 |
-
const notFound = !loading && Boolean(workspaceSlug) && !activeWorkspace;
|
| 72 |
-
|
| 73 |
-
const switchWorkspace = useCallback(
|
| 74 |
-
(slug: string) => navigate(switchWorkspacePath(location.pathname, slug)),
|
| 75 |
-
[location.pathname, navigate],
|
| 76 |
-
);
|
| 77 |
-
|
| 78 |
-
const createWorkspace = useCallback(
|
| 79 |
-
async (input: CreateWorkspaceInput) => {
|
| 80 |
-
const created = await createWorkspaceRequest(input);
|
| 81 |
-
await refreshWorkspaces();
|
| 82 |
-
navigate(workspacePath(created.slug));
|
| 83 |
-
return created;
|
| 84 |
-
},
|
| 85 |
-
[navigate, refreshWorkspaces],
|
| 86 |
-
);
|
| 87 |
-
|
| 88 |
-
const updateWorkspace = useCallback(
|
| 89 |
-
async (input: UpdateWorkspaceInput) => {
|
| 90 |
-
if (!activeWorkspace) throw new Error("No active workspace");
|
| 91 |
-
const updated = await updateWorkspaceRequest(activeWorkspace.slug, input);
|
| 92 |
-
await refreshWorkspaces();
|
| 93 |
-
return updated;
|
| 94 |
-
},
|
| 95 |
-
[activeWorkspace, refreshWorkspaces],
|
| 96 |
-
);
|
| 97 |
-
|
| 98 |
-
const scopedFetch = useCallback(
|
| 99 |
-
<T,>(path: string, options: RequestInit = {}) => {
|
| 100 |
-
if (!activeWorkspace || activeWorkspace.archived) {
|
| 101 |
-
return Promise.reject(new Error("No active workspace"));
|
| 102 |
-
}
|
| 103 |
-
return workspaceApiFetch<T>(activeWorkspace, path, options);
|
| 104 |
-
},
|
| 105 |
-
[activeWorkspace],
|
| 106 |
-
);
|
| 107 |
-
|
| 108 |
-
return (
|
| 109 |
-
<WorkspaceContext.Provider
|
| 110 |
-
value={{
|
| 111 |
-
workspaces,
|
| 112 |
-
activeWorkspace,
|
| 113 |
-
requestedSlug: workspaceSlug,
|
| 114 |
-
loading,
|
| 115 |
-
error,
|
| 116 |
-
notFound,
|
| 117 |
-
canManageWorkspaces: Boolean(session?.isSystemPat),
|
| 118 |
-
refreshWorkspaces,
|
| 119 |
-
switchWorkspace,
|
| 120 |
-
createWorkspace,
|
| 121 |
-
updateWorkspace,
|
| 122 |
-
apiFetch: scopedFetch,
|
| 123 |
-
}}
|
| 124 |
-
>
|
| 125 |
-
{children}
|
| 126 |
-
</WorkspaceContext.Provider>
|
| 127 |
-
);
|
| 128 |
-
}
|
| 129 |
-
|
| 130 |
-
export function useWorkspace(): WorkspaceContextValue {
|
| 131 |
-
const value = useContext(WorkspaceContext);
|
| 132 |
-
if (!value) throw new Error("useWorkspace must be used inside WorkspaceProvider");
|
| 133 |
-
return value;
|
| 134 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|