feat(FN-4552): complete Step 3 — guard workspace selector rendering

Fusion-Task-Id: FN-4552
Fusion-Task-Lineage: dad48f2c-25a8-4fca-8f00-16307936621c
This commit is contained in:
Fusion
2026-05-14 20:55:41 -07:00
committed by gsxdsm
parent 002d148c78
commit b58a2c2dd5
2 changed files with 21 additions and 7 deletions

View File

@@ -23,15 +23,16 @@ export function WorkspaceSelector({
onSelect,
}: WorkspaceSelectorProps) {
const [open, setOpen] = useState(false);
const safeWorkspace = typeof currentWorkspace === "string" ? currentWorkspace : "project";
const currentLabel = useMemo(() => {
if (currentWorkspace === "project") {
if (safeWorkspace === "project") {
return projectName || "Project Root";
}
const match = workspaces.find((workspace) => workspace.id === currentWorkspace);
return match?.label ?? currentWorkspace;
}, [currentWorkspace, projectName, workspaces]);
const match = workspaces.find((workspace) => workspace.id === safeWorkspace);
return match?.label ?? safeWorkspace;
}, [safeWorkspace, projectName, workspaces]);
return (
<div className="workspace-selector">
@@ -42,7 +43,7 @@ export function WorkspaceSelector({
aria-haspopup="listbox"
aria-expanded={open}
>
{currentWorkspace === "project" ? <FolderRoot size={14} /> : <FolderGit2 size={14} />}
{safeWorkspace === "project" ? <FolderRoot size={14} /> : <FolderGit2 size={14} />}
<span className="workspace-selector-trigger-label">{currentLabel}</span>
<ChevronDown size={14} className={`workspace-selector-trigger-icon${open ? " open" : ""}`} />
</button>
@@ -51,7 +52,7 @@ export function WorkspaceSelector({
<div className="workspace-selector-menu" role="listbox" aria-label="Select workspace">
<button
type="button"
className={`workspace-selector-option${currentWorkspace === "project" ? " active" : ""}`}
className={`workspace-selector-option${safeWorkspace === "project" ? " active" : ""}`}
onClick={() => {
onSelect("project");
setOpen(false);
@@ -71,7 +72,7 @@ export function WorkspaceSelector({
<button
key={workspace.id}
type="button"
className={`workspace-selector-option${currentWorkspace === workspace.id ? " active" : ""}`}
className={`workspace-selector-option${safeWorkspace === workspace.id ? " active" : ""}`}
onClick={() => {
onSelect(workspace.id);
setOpen(false);

View File

@@ -38,6 +38,19 @@ describe("WorkspaceSelector", () => {
expect(screen.getByRole("button", { name: /kb/i })).toBeInTheDocument();
});
it("falls back to project label when currentWorkspace is not a string", () => {
render(
<WorkspaceSelector
currentWorkspace={{} as unknown as string}
projectName="kb"
workspaces={workspaces}
onSelect={vi.fn()}
/>,
);
expect(screen.getByRole("button", { name: /kb/i })).toBeInTheDocument();
});
it("opens the menu and highlights the current workspace", async () => {
const user = userEvent.setup();
render(