Files
fusion/packages/dashboard/app/hooks/useFileEditor.ts
gsxdsm 4b83c13ff7 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

121 lines
3.0 KiB
TypeScript

import { useState, useEffect, useCallback } from "react";
import type { FileContentResponse, SaveFileResponse } from "../api";
import { fetchFileContent, saveFileContent } from "../api";
interface UseFileEditorReturn {
content: string;
setContent: (content: string) => void;
originalContent: string;
loading: boolean;
saving: boolean;
error: string | null;
save: () => Promise<void>;
hasChanges: boolean;
mtime: string | null;
}
/**
* Hook for editing a file in a task directory.
*
* @param taskId - The task ID
* @param filePath - The file path to edit (null if no file selected)
* @param enabled - Whether to enable loading (e.g., when editor is visible)
* @returns File editor state and controls
*/
export function useFileEditor(
taskId: string,
filePath: string | null,
enabled: boolean
): UseFileEditorReturn {
const [content, setContentState] = useState<string>("");
const [originalContent, setOriginalContent] = useState<string>("");
const [mtime, setMtime] = useState<string | null>(null);
const [loading, setLoading] = useState(false);
const [saving, setSaving] = useState(false);
const [error, setError] = useState<string | null>(null);
const setContent = useCallback((newContent: string) => {
setContentState(newContent);
setError(null);
}, []);
// Load file content when filePath changes
useEffect(() => {
if (!enabled || !taskId || !filePath) {
setContentState("");
setOriginalContent("");
setMtime(null);
setError(null);
return;
}
let cancelled = false;
async function loadFile() {
setLoading(true);
setError(null);
try {
const response: FileContentResponse = await fetchFileContent(taskId, filePath);
if (!cancelled) {
setContentState(response.content);
setOriginalContent(response.content);
setMtime(response.mtime);
}
} catch (err: any) {
if (!cancelled) {
setError(err.message || "Failed to load file");
setContentState("");
setOriginalContent("");
setMtime(null);
}
} finally {
if (!cancelled) {
setLoading(false);
}
}
}
loadFile();
return () => {
cancelled = true;
};
}, [taskId, filePath, enabled]);
const hasChanges = content !== originalContent;
const save = useCallback(async () => {
if (!taskId || !filePath || !hasChanges) {
return;
}
setSaving(true);
setError(null);
try {
const response: SaveFileResponse = await saveFileContent(taskId, filePath, content);
setOriginalContent(content);
setMtime(response.mtime);
} catch (err: any) {
setError(err.message || "Failed to save file");
throw err;
} finally {
setSaving(false);
}
}, [taskId, filePath, content, hasChanges]);
return {
content,
setContent,
originalContent,
loading,
saving,
error,
save,
hasChanges,
mtime,
};
}