From 9e9f18884c78c21f5785908fb7ccd00e2fa5436b Mon Sep 17 00:00:00 2001 From: soryu Date: Sat, 7 Feb 2026 01:11:26 +0000 Subject: Add new directive initial implementation --- makima/frontend/src/hooks/useDirectives.ts | 108 +++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 makima/frontend/src/hooks/useDirectives.ts (limited to 'makima/frontend/src/hooks/useDirectives.ts') diff --git a/makima/frontend/src/hooks/useDirectives.ts b/makima/frontend/src/hooks/useDirectives.ts new file mode 100644 index 0000000..001cf89 --- /dev/null +++ b/makima/frontend/src/hooks/useDirectives.ts @@ -0,0 +1,108 @@ +import { useState, useCallback, useEffect } from "react"; +import { + listDirectives, + getDirective, + createDirective, + updateDirective, + deleteDirective, + type DirectiveSummary, + type DirectiveWithChains, + type CreateDirectiveRequest, + type UpdateDirectiveRequest, +} from "../lib/api"; + +export function useDirectives() { + const [directives, setDirectives] = useState([]); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + + const fetchDirectives = useCallback(async () => { + setLoading(true); + setError(null); + try { + const response = await listDirectives(); + setDirectives(response.directives); + } catch (e) { + setError(e instanceof Error ? e.message : "Failed to fetch directives"); + } finally { + setLoading(false); + } + }, []); + + const fetchDirective = useCallback( + async (id: string): Promise => { + setError(null); + try { + return await getDirective(id); + } catch (e) { + setError(e instanceof Error ? e.message : "Failed to fetch directive"); + return null; + } + }, + [] + ); + + const saveDirective = useCallback( + async (data: CreateDirectiveRequest): Promise => { + setError(null); + try { + const directive = await createDirective(data); + await fetchDirectives(); + return directive as unknown as DirectiveSummary; + } catch (e) { + setError(e instanceof Error ? e.message : "Failed to create directive"); + return null; + } + }, + [fetchDirectives] + ); + + const editDirective = useCallback( + async ( + id: string, + data: UpdateDirectiveRequest + ): Promise => { + setError(null); + try { + const directive = await updateDirective(id, data); + await fetchDirectives(); + return directive as unknown as DirectiveSummary; + } catch (e) { + setError(e instanceof Error ? e.message : "Failed to update directive"); + return null; + } + }, + [fetchDirectives] + ); + + const removeDirective = useCallback( + async (id: string): Promise => { + setError(null); + try { + await deleteDirective(id); + await fetchDirectives(); + return true; + } catch (e) { + setError(e instanceof Error ? e.message : "Failed to delete directive"); + return false; + } + }, + [fetchDirectives] + ); + + // Initial fetch + useEffect(() => { + fetchDirectives(); + }, [fetchDirectives]); + + return { + directives, + loading, + error, + fetchDirectives, + fetchDirective, + saveDirective, + editDirective, + removeDirective, + }; +} -- cgit v1.2.3