Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | import React, { useMemo } from "react";
import { QueryClient, QueryClientProvider, useQuery } from "@tanstack/react-query";
import { Link, useParams } from "react-router-dom";
import MyApp from "@/components/app/MyApp";
import ChatDock from "@/components/chat/ChatDock/ChatDock";
import NavTitle from "@/components/nav/NavTitle/NavTitle";
import { Building2, FolderKanban } from "lucide-react";
import { decodeResourceId } from "@/components/detail/useResourceId";
import { DynamicNavbarWrapper } from "@/components/nav/NavBar/DynamicNavbarWrapper";
import {
WorkspaceInfo,
WorkspaceProvider,
} from "@/context/WorkspaceContext";
import {
ORGANISATION_WORKSPACE_APP,
PROJECT_WORKSPACE_APP,
} from "@/routes/apps/Workspace";
import { APP_PREFIXES, APP_ROUTES } from "@/routes/const";
import { WorkspaceRef, workspaceRef } from "@/routes/workspace";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { errorDetail } from "@/util/apiError";
type Kind = "organisation" | "project";
/**
* One query cache per workspace. Query keys carry no workspace, and every
* request does (`useApiHandler`), so the same key in two workspaces is two
* different answers: a cache shared between them would show one
* workspace's agents in another's list for as long as it took to refetch.
*/
const clients = new Map<string, QueryClient>();
function clientFor(key: string): QueryClient {
let client = clients.get(key);
if (!client) {
client = new QueryClient();
clients.set(key, client);
}
return client;
}
const CONFIG: Record<Kind, typeof ORGANISATION_WORKSPACE_APP> = {
organisation: ORGANISATION_WORKSPACE_APP,
project: PROJECT_WORKSPACE_APP,
};
const Refused: React.FC<{ message: string }> = ({ message }) => (
<div className="p-10 max-w-xl space-y-3">
<h1 className="text-lg font-semibold text-gray-800 dark:text-gray-200">
You can't open this workspace
</h1>
<p className="text-sm text-gray-600 dark:text-gray-300">{message}</p>
<Link to={APP_ROUTES.USER.overview} className="text-sm underline">
Back to your personal workspace
</Link>
</div>
);
const Resolved: React.FC<{ kind: Kind; workspace: WorkspaceRef }> = ({
kind,
workspace,
}) => {
const { getApiHandler } = useApiHandler();
const query = useQuery<WorkspaceInfo>({
queryKey: [QUERY_KEYS.GET.WORKSPACE, workspace.key],
queryFn: () => getApiHandler().workspace_router.resolve(workspace.key),
retry: false,
});
// Typed here: TypeScript 5.0 reads useQuery's data as `any`.
const info: WorkspaceInfo | undefined = query.data;
if (query.isError) {
return (
<Refused
message={errorDetail(
query.error,
"It doesn't exist, or you aren't a member of it.",
)}
/>
);
}
const config = CONFIG[kind];
const homeName = kind === "organisation" ? "ORGANISATION" : "PROJECT";
return (
<WorkspaceProvider workspace={workspace} info={info}>
<MyApp
homeName={homeName}
config={config}
prefix={APP_PREFIXES.USER}
navbar={(desktopOpen, onDesktopToggle) => (
<DynamicNavbarWrapper
homeName={homeName}
app={config}
titleMap={
info?.name
? {
[APP_ROUTES.USER.overview]: (
<NavTitle
icon={kind === "organisation" ? Building2 : FolderKanban}
label={info.name}
/>
),
}
: undefined
}
desktopOpen={desktopOpen}
onDesktopToggle={onDesktopToggle}
/>
)}
dock={<ChatDock />}
/>
</WorkspaceProvider>
);
};
/**
* An organisation or a project (`routes/workspace.ts`): the same pages as
* Personal, mounted under `/organisation/<id>` or `/project/<id>`, each
* request naming the workspace and each link staying inside it.
*/
const WorkspaceApp: React.FC<{ kind: Kind }> = ({ kind }) => {
const params = useParams();
const id = decodeResourceId(
kind === "organisation" ? params.organisationId : params.projectId,
);
const workspace = useMemo(
() => (id ? workspaceRef(kind, id) : null),
[kind, id],
);
if (!workspace) {
return <Refused message="This link doesn't name a workspace." />;
}
return (
<WorkspaceProvider workspace={workspace}>
<QueryClientProvider client={clientFor(workspace.key)}>
<Resolved kind={kind} workspace={workspace} />
</QueryClientProvider>
</WorkspaceProvider>
);
};
export default WorkspaceApp;
|