Files
fusion/packages/dashboard/app/hooks/useFileBrowser.ts
gsxdsm a299de8b0d feat(KB-029): add file browser for viewing and editing task files
- Add server-side file service with list, read, and write operations
- Add REST API endpoints for file operations with tests
- Add client-side API functions and React hooks for file browsing
- Create FileBrowser, FileEditor, and FileBrowserModal components
- Add CodeMirror 6 for syntax-highlighted file editing
- Integrate Files tab into TaskDetailModal
- Add comprehensive file browser styles and responsive design
2026-03-29 21:18:21 -07:00

85 lines
2.0 KiB
TypeScript

import { useState, useEffect, useCallback } from "react";
import type { FileNode, FileListResponse } from "../api";
import { fetchFileList } from "../api";
interface UseFileBrowserReturn {
entries: FileNode[];
currentPath: string;
setPath: (path: string) => void;
loading: boolean;
error: string | null;
refresh: () => void;
}
/**
* Hook for browsing files in a task directory.
*
* @param taskId - The task ID to browse
* @param enabled - Whether to enable fetching (e.g., when tab is active)
* @returns File browser state and controls
*/
export function useFileBrowser(taskId: string, enabled: boolean): UseFileBrowserReturn {
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((k) => k + 1);
}, []);
const setPath = useCallback((path: string) => {
setCurrentPath(path);
setError(null);
}, []);
useEffect(() => {
if (!enabled || !taskId) {
return;
}
let cancelled = false;
async function loadFiles() {
setLoading(true);
setError(null);
try {
const response: FileListResponse = await fetchFileList(
taskId,
currentPath === "." ? undefined : currentPath
);
if (!cancelled) {
setEntries(response.entries);
}
} catch (err: any) {
if (!cancelled) {
setError(err.message || "Failed to load files");
setEntries([]);
}
} finally {
if (!cancelled) {
setLoading(false);
}
}
}
loadFiles();
return () => {
cancelled = true;
};
}, [taskId, currentPath, enabled, refreshKey]);
return {
entries,
currentPath,
setPath,
loading,
error,
refresh,
};
}