feat(KB-104): move file browser from task modal to toolbar
- Add backend API routes and file service for project-scoped file operations - Create useProjectFileBrowser and useProjectFileEditor hooks with comprehensive tests - Update FileBrowserModal to support both task and project modes via dualMode prop - Add Files button to dashboard header toolbar - Remove Files tab from TaskDetailModal - Integrate project file browser into main App layout - Include changeset for @dustinbyrne/kb package
This commit is contained in:
120
packages/dashboard/app/hooks/useProjectFileEditor.ts
Normal file
120
packages/dashboard/app/hooks/useProjectFileEditor.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
import type { FileContentResponse, SaveFileResponse } from "../api";
|
||||
import { fetchProjectFileContent, saveProjectFileContent } from "../api";
|
||||
|
||||
interface UseProjectFileEditorReturn {
|
||||
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 the project directory.
|
||||
*
|
||||
* @param rootPath - The project root directory path (from config/store)
|
||||
* @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 useProjectFileEditor(
|
||||
rootPath: string,
|
||||
filePath: string | null,
|
||||
enabled: boolean
|
||||
): UseProjectFileEditorReturn {
|
||||
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 || !filePath) {
|
||||
setContentState("");
|
||||
setOriginalContent("");
|
||||
setMtime(null);
|
||||
setError(null);
|
||||
return;
|
||||
}
|
||||
|
||||
let cancelled = false;
|
||||
|
||||
async function loadFile() {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const response: FileContentResponse = await fetchProjectFileContent(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;
|
||||
};
|
||||
}, [filePath, enabled]);
|
||||
|
||||
const hasChanges = content !== originalContent;
|
||||
|
||||
const save = useCallback(async () => {
|
||||
if (!filePath || !hasChanges) {
|
||||
return;
|
||||
}
|
||||
|
||||
setSaving(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const response: SaveFileResponse = await saveProjectFileContent(filePath, content);
|
||||
setOriginalContent(content);
|
||||
setMtime(response.mtime);
|
||||
} catch (err: any) {
|
||||
setError(err.message || "Failed to save file");
|
||||
throw err;
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}, [filePath, content, hasChanges]);
|
||||
|
||||
return {
|
||||
content,
|
||||
setContent,
|
||||
originalContent,
|
||||
loading,
|
||||
saving,
|
||||
error,
|
||||
save,
|
||||
hasChanges,
|
||||
mtime,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user