Files
fusion/packages/dashboard/app/hooks/useWorkspaces.ts
gsxdsm 9e03c47b91 feat(KB-175): move file browser to header with workspace support
- Add workspace file APIs and hooks for session-scoped file browsing
- Create WorkspaceSelector component for switching between workspaces
- Update FileBrowserModal with workspace mode and improved UX
- Add files button to header with workspace-aware file browsing
- Add session files indicator to task cards for quick file access
- Include comprehensive tests for all new components and hooks
2026-03-31 01:26:57 -07:00

88 lines
2.1 KiB
TypeScript

import { useEffect, useState } from "react";
import { fetchWorkspaces, type WorkspaceTaskInfo } from "../api";
export interface WorkspaceInfo {
id: string;
label: string;
title?: string;
worktree?: string;
kind: "project" | "task";
}
interface UseWorkspacesReturn {
projectName: string;
workspaces: WorkspaceInfo[];
loading: boolean;
error: string | null;
}
const POLL_INTERVAL_MS = 10000;
function getProjectName(projectPath: string): string {
const normalized = projectPath.replace(/[\\/]+$/, "");
const segments = normalized.split(/[\\/]/).filter(Boolean);
return segments[segments.length - 1] || projectPath || "Project Root";
}
function mapTaskWorkspace(task: WorkspaceTaskInfo): WorkspaceInfo {
return {
id: task.id,
label: task.id,
title: task.title,
worktree: task.worktree,
kind: "task",
};
}
/**
* Fetch and poll the list of available file browser workspaces.
*/
export function useWorkspaces(): UseWorkspacesReturn {
const [projectName, setProjectName] = useState("Project Root");
const [workspaces, setWorkspaces] = useState<WorkspaceInfo[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
let cancelled = false;
async function loadWorkspaces() {
try {
const response = await fetchWorkspaces();
if (cancelled) {
return;
}
setProjectName(getProjectName(response.project));
setWorkspaces(response.tasks.map(mapTaskWorkspace));
setError(null);
} catch (err: any) {
if (!cancelled) {
setError(err.message || "Failed to load workspaces");
}
} finally {
if (!cancelled) {
setLoading(false);
}
}
}
void loadWorkspaces();
const intervalId = window.setInterval(() => {
void loadWorkspaces();
}, POLL_INTERVAL_MS);
return () => {
cancelled = true;
window.clearInterval(intervalId);
};
}, []);
return {
projectName,
workspaces,
loading,
error,
};
}