Files
fusion/packages/dashboard/app/hooks/useWorkspaces.ts
gsxdsm 3fbb7c47cf refactor: eliminate ~400 no-explicit-any warnings across the workspace
Parallel subagent pass: four typescript-pro agents on non-overlapping scopes.

Patterns applied:
- catch (err: any) { ... err.message ... } → catch (err) { ... getErrorMessage(err) ... }
  using the new @fusion/core helper. Bare catch {} where the error was unused.
- SQLite row types: defined typed XxxRow interfaces per table and cast
  .all()/.get() results via `as unknown as XxxRow[]` (the double cast is
  required because better-sqlite3 returns Record<string, SQLOutputValue>).
- rowToX(row: any) converters: typed argument with the matching row interface.
- Dynamic settings key writes: (settings as Record<string, unknown>)[key].
- React event handlers and setState callbacks: inferred types or concrete
  React.{Mouse,Change,Form}Event<...> where needed.
- pi-claude-cli: local PiMessage / PiContext duck types to avoid re-typing
  pi-ai concrete shapes; typed Claude stream event message fields.

72 files changed, ~400 anys eliminated. Typecheck passes across the workspace.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-23 23:02:36 -07:00

89 lines
2.1 KiB
TypeScript

import { useEffect, useState } from "react";
import { getErrorMessage } from "@fusion/core";
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(projectId?: string): 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(projectId);
if (cancelled) {
return;
}
setProjectName(getProjectName(response.project));
setWorkspaces(response.tasks.map(mapTaskWorkspace));
setError(null);
} catch (err) {
if (!cancelled) {
setError(getErrorMessage(err) || "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);
};
}, [projectId]);
return {
projectName,
workspaces,
loading,
error,
};
}