Files
fusion/packages/dashboard/app/hooks/useFileEditor.ts
Fusion 123ec5949f feat(FN-1862): add completion state tracking for onboarding
- Add markOnboardingCompleted() to set completedAt timestamp when user finishes onboarding
- Add isOnboardingCompleted() to check if onboarding was completed (vs dismissed)
- Update ModelOnboardingModal to call markOnboardingCompleted on successful completion
- Skip onboarding auto-open when already completed locally
- Add completion state tracking tests in ModelOnboardingModal.test.tsx
- Add model-onboarding-state unit tests
- Update useAuthOnboarding to check local completion state before auto-opening
- Document localStorage completion state tracking pattern in .fusion/memory.md
2026-04-14 23:44:40 -07:00

123 lines
3.1 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)
* @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: 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, 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: any) {
setError(err.message || "Failed to save file");
throw err;
} finally {
setSaving(false);
}
}, [taskId, filePath, content, hasChanges, projectId]);
return {
content,
setContent,
originalContent,
loading,
saving,
error,
save,
hasChanges,
mtime,
};
}