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 143 144 145 146 147 148 149 150 151 152 153 | 1x 1x 7x 7x 7x 7x 7x 6x 7x 7x 7x 7x 7x 7x 7x 2x 2x 2x 2x | import React from "react";
import { useQuery } from "@tanstack/react-query";
// The unwrapped router: switching goes *between* workspaces, which the app's
// own `useNavigate` (routes/router.tsx) would keep inside this one.
import { useLocation, useNavigate } from "react-router";
import {
Building2,
Check,
ChevronsUpDown,
FolderKanban,
FolderPlus,
Plus,
Settings2,
User,
} from "lucide-react";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { WorkspaceInfo, useWorkspace } from "@/context/WorkspaceContext";
import { APP_ROUTES } from "@/routes/const";
import {
PERSONAL_WORKSPACE,
WorkspaceKind,
switchTarget,
workspaceFromKey,
} from "@/routes/workspace";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
const ICON: Record<WorkspaceKind, React.ElementType> = {
personal: User,
organisation: Building2,
project: FolderKanban,
};
/**
* Which workspace the app is showing — Personal, an organisation or a
* project — and the way to another, beside the account button in the top
* right. Projects sit under their parent. Switching keeps the section
* (Inference, Knowledge…) where the target has it.
*/
export const WorkspaceMenu: React.FC = () => {
const ws = useWorkspace();
const navigate = useNavigate();
const { pathname } = useLocation();
const { getApiHandler } = useApiHandler();
const query = useQuery<WorkspaceInfo[]>({
queryKey: [QUERY_KEYS.GET.WORKSPACES],
queryFn: () => getApiHandler().workspace_router.list(),
staleTime: 60_000,
retry: false,
});
// Typed here: TypeScript 5.0 reads useQuery's data as `any`.
const entries: WorkspaceInfo[] = Array.isArray(query.data) ? query.data : [];
const mine = entries.find((e) => e.kind === "personal")?.key;
const currentKey = ws.kind === "personal" ? mine : ws.key;
const name =
ws.kind === "personal"
? "Personal"
: (ws.info?.name ?? entries.find((e) => e.key === ws.key)?.name ?? "…");
const Icon = ICON[ws.kind];
const open = (entry: WorkspaceInfo) => {
if (entry.key === currentKey) return;
const target =
entry.kind === "personal"
? PERSONAL_WORKSPACE.prefix
: workspaceFromKey(entry.key).prefix;
navigate(switchTarget(pathname, ws.prefix, target));
};
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<button
type="button"
aria-label={`Workspace: ${name}`}
title={name}
className="text-white rounded-lg border border-white bg-gray-900 px-2.5 py-2 flex flex-row items-center gap-2 max-w-[10rem] sm:max-w-[16rem] cursor-pointer hover:bg-gray-800 transition-colors"
>
<Icon className="size-4 shrink-0" />
<span className="truncate text-sm">{name}</span>
<ChevronsUpDown className="size-3.5 shrink-0 opacity-70" />
</button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-64 max-h-[70vh] overflow-y-auto">
<DropdownMenuLabel className="text-xs font-normal text-muted-foreground">
Workspace
</DropdownMenuLabel>
{entries.map((entry) => {
const ItemIcon = ICON[entry.kind];
const nested = entry.kind === "project";
const owner =
nested && entry.parent?.kind === "personal" && entry.parent.key !== mine
? entry.parent.name
: null;
return (
<DropdownMenuItem
key={entry.key}
className={`cursor-pointer ${nested ? "pl-7" : ""}`}
onSelect={() => open(entry)}
>
<ItemIcon className="size-4" />
<span className="flex-1 min-w-0">
<span className="block truncate">
{entry.kind === "personal" ? "Personal" : entry.name}
</span>
{owner && (
<span className="block truncate text-[11px] text-muted-foreground">
{owner}'s
</span>
)}
</span>
{entry.key === currentKey && <Check className="size-4" />}
</DropdownMenuItem>
);
})}
<DropdownMenuSeparator />
{ws.kind !== "project" && (
<DropdownMenuItem
className="cursor-pointer"
onSelect={() => navigate(ws.path(APP_ROUTES.USER.PROJECTS.new))}
>
<FolderPlus className="size-4" />
New project{ws.kind === "organisation" ? ` in ${name}` : ""}
</DropdownMenuItem>
)}
<DropdownMenuItem
className="cursor-pointer"
onSelect={() => navigate(APP_ROUTES.USER.ORGANISATIONS.new)}
>
<Plus className="size-4" />
New organisation
</DropdownMenuItem>
<DropdownMenuItem
className="cursor-pointer"
onSelect={() => navigate(APP_ROUTES.USER.organisations)}
>
<Settings2 className="size-4" />
Your organisations
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
};
export default WorkspaceMenu;
|