summaryrefslogtreecommitdiff
path: root/makima/frontend/src/hooks/useDirectives.ts
blob: af1c8c63f800387cfd11ae9a08bddaa73a5762c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { useState, useCallback, useEffect } from "react";
import {
  listDirectives,
  getDirective,
  createDirective,
  updateDirective,
  deleteDirective,
  startDirective as startDirectiveApi,
  type DirectiveSummary,
  type DirectiveWithChains,
  type CreateDirectiveRequest,
  type UpdateDirectiveRequest,
} from "../lib/api";

export function useDirectives() {
  const [directives, setDirectives] = useState<DirectiveSummary[]>([]);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(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<DirectiveWithChains | null> => {
      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<DirectiveSummary | null> => {
      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<DirectiveSummary | null> => {
      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<boolean> => {
      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]
  );

  const startDirective = useCallback(
    async (id: string): Promise<boolean> => {
      setError(null);
      try {
        await startDirectiveApi(id);
        await fetchDirectives();
        return true;
      } catch (e) {
        setError(
          e instanceof Error ? e.message : "Failed to start directive"
        );
        return false;
      }
    },
    [fetchDirectives]
  );

  // Initial fetch
  useEffect(() => {
    fetchDirectives();
  }, [fetchDirectives]);

  return {
    directives,
    loading,
    error,
    fetchDirectives,
    fetchDirective,
    saveDirective,
    editDirective,
    removeDirective,
    startDirective,
  };
}