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>
124 lines
3.2 KiB
TypeScript
124 lines
3.2 KiB
TypeScript
import { useState, useEffect, useCallback } from "react";
|
|
import { getErrorMessage } from "@fusion/core";
|
|
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)
|
|
* @param projectId - Optional project ID for scoped store resolution
|
|
* @returns File editor state and controls
|
|
*/
|
|
export function useFileEditor(
|
|
taskId: string,
|
|
filePath: string | null,
|
|
enabled: boolean,
|
|
projectId?: string
|
|
): 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!, projectId);
|
|
|
|
if (!cancelled) {
|
|
setContentState(response.content);
|
|
setOriginalContent(response.content);
|
|
setMtime(response.mtime);
|
|
}
|
|
} catch (err) {
|
|
if (!cancelled) {
|
|
setError(getErrorMessage(err) || "Failed to load file");
|
|
setContentState("");
|
|
setOriginalContent("");
|
|
setMtime(null);
|
|
}
|
|
} finally {
|
|
if (!cancelled) {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
loadFile();
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [taskId, filePath, enabled, projectId]);
|
|
|
|
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, projectId);
|
|
setOriginalContent(content);
|
|
setMtime(response.mtime);
|
|
} catch (err) {
|
|
setError(getErrorMessage(err) || "Failed to save file");
|
|
throw err;
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
}, [taskId, filePath, content, hasChanges, projectId]);
|
|
|
|
return {
|
|
content,
|
|
setContent,
|
|
originalContent,
|
|
loading,
|
|
saving,
|
|
error,
|
|
save,
|
|
hasChanges,
|
|
mtime,
|
|
};
|
|
}
|