From a32dc56d2e5447ef8988cb98b8686476cc94e70c Mon Sep 17 00:00:00 2001 From: soryu Date: Tue, 23 Dec 2025 02:14:58 +0000 Subject: Add Postgres for persistence and File cabinet Migrations are local only currently, and must be run manually by setting POSTGRES_CONNECTION_URI --- makima/frontend/src/hooks/useFiles.ts | 105 ++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 makima/frontend/src/hooks/useFiles.ts (limited to 'makima/frontend/src/hooks/useFiles.ts') diff --git a/makima/frontend/src/hooks/useFiles.ts b/makima/frontend/src/hooks/useFiles.ts new file mode 100644 index 0000000..aacbb6a --- /dev/null +++ b/makima/frontend/src/hooks/useFiles.ts @@ -0,0 +1,105 @@ +import { useState, useCallback, useEffect } from "react"; +import { + listFiles, + getFile, + createFile, + updateFile, + deleteFile, + type FileSummary, + type FileDetail, + type CreateFileRequest, + type UpdateFileRequest, +} from "../lib/api"; + +export function useFiles() { + const [files, setFiles] = useState([]); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + + const fetchFiles = useCallback(async () => { + setLoading(true); + setError(null); + try { + const response = await listFiles(); + setFiles(response.files); + } catch (e) { + setError(e instanceof Error ? e.message : "Failed to fetch files"); + } finally { + setLoading(false); + } + }, []); + + const fetchFile = useCallback( + async (id: string): Promise => { + setError(null); + try { + return await getFile(id); + } catch (e) { + setError(e instanceof Error ? e.message : "Failed to fetch file"); + return null; + } + }, + [] + ); + + const saveFile = useCallback( + async (data: CreateFileRequest): Promise => { + setError(null); + try { + const file = await createFile(data); + await fetchFiles(); // Refresh list + return file; + } catch (e) { + setError(e instanceof Error ? e.message : "Failed to save file"); + return null; + } + }, + [fetchFiles] + ); + + const editFile = useCallback( + async (id: string, data: UpdateFileRequest): Promise => { + setError(null); + try { + const file = await updateFile(id, data); + await fetchFiles(); // Refresh list + return file; + } catch (e) { + setError(e instanceof Error ? e.message : "Failed to update file"); + return null; + } + }, + [fetchFiles] + ); + + const removeFile = useCallback( + async (id: string): Promise => { + setError(null); + try { + await deleteFile(id); + await fetchFiles(); // Refresh list + return true; + } catch (e) { + setError(e instanceof Error ? e.message : "Failed to delete file"); + return false; + } + }, + [fetchFiles] + ); + + // Initial fetch + useEffect(() => { + fetchFiles(); + }, [fetchFiles]); + + return { + files, + loading, + error, + fetchFiles, + fetchFile, + saveFile, + editFile, + removeFile, + }; +} -- cgit v1.2.3