Files
fusion/packages/dashboard/app/hooks/useWorkspaceFileBrowser.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

97 lines
2.3 KiB
TypeScript

import { useState, useEffect, useCallback } from "react";
import { getErrorMessage } from "@fusion/core";
import type { FileNode, FileListResponse } from "../api";
import { fetchWorkspaceFileList } from "../api";
interface UseWorkspaceFileBrowserReturn {
entries: FileNode[];
currentPath: string;
setPath: (path: string) => void;
loading: boolean;
error: string | null;
refresh: () => void;
}
/**
* Hook for browsing files in a selected workspace.
*
* @param workspace - The workspace identifier ("project" or task ID)
* @param enabled - Whether fetching is enabled
* @param projectId - Optional project ID for multi-project scoping
*/
export function useWorkspaceFileBrowser(
workspace: string,
enabled: boolean,
projectId?: string,
): UseWorkspaceFileBrowserReturn {
const [entries, setEntries] = useState<FileNode[]>([]);
const [currentPath, setCurrentPath] = useState<string>(".");
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [refreshKey, setRefreshKey] = useState(0);
const refresh = useCallback(() => {
setRefreshKey((key) => key + 1);
}, []);
const setPath = useCallback((path: string) => {
setCurrentPath(path);
setError(null);
}, []);
useEffect(() => {
setCurrentPath(".");
setError(null);
setEntries([]);
}, [workspace]);
useEffect(() => {
if (!enabled || !workspace) {
return;
}
let cancelled = false;
async function loadFiles() {
setLoading(true);
setError(null);
try {
const response: FileListResponse = await fetchWorkspaceFileList(
workspace,
currentPath === "." ? undefined : currentPath,
projectId,
);
if (!cancelled) {
setEntries(response.entries);
}
} catch (err) {
if (!cancelled) {
setError(getErrorMessage(err) || "Failed to load files");
setEntries([]);
}
} finally {
if (!cancelled) {
setLoading(false);
}
}
}
void loadFiles();
return () => {
cancelled = true;
};
}, [workspace, currentPath, enabled, refreshKey, projectId]);
return {
entries,
currentPath,
setPath,
loading,
error,
refresh,
};
}