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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
import { useState, useCallback, useEffect } from "react";
import {
listChains,
getChain,
createChain,
updateChain,
archiveChain,
getChainGraph,
type ChainSummary,
type ChainWithContracts,
type ChainGraphResponse,
type ChainStatus,
type CreateChainRequest,
type UpdateChainRequest,
} from "../lib/api";
interface UseChainsResult {
chains: ChainSummary[];
loading: boolean;
error: string | null;
refresh: () => Promise<void>;
createNewChain: (req: CreateChainRequest) => Promise<ChainWithContracts | null>;
updateExistingChain: (
chainId: string,
req: UpdateChainRequest
) => Promise<ChainWithContracts | null>;
archiveExistingChain: (chainId: string) => Promise<boolean>;
getChainById: (chainId: string) => Promise<ChainWithContracts | null>;
getGraph: (chainId: string) => Promise<ChainGraphResponse | null>;
}
export function useChains(statusFilter?: ChainStatus): UseChainsResult {
const [chains, setChains] = useState<ChainSummary[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const fetchChains = useCallback(async () => {
setLoading(true);
setError(null);
try {
const response = await listChains(statusFilter);
setChains(response.chains);
} catch (err) {
console.error("Failed to fetch chains:", err);
setError(err instanceof Error ? err.message : "Failed to fetch chains");
} finally {
setLoading(false);
}
}, [statusFilter]);
useEffect(() => {
fetchChains();
}, [fetchChains]);
const createNewChain = useCallback(
async (req: CreateChainRequest): Promise<ChainWithContracts | null> => {
try {
const chain = await createChain(req);
// Refresh the list
await fetchChains();
// Return the full chain with contracts
return await getChain(chain.id);
} catch (err) {
console.error("Failed to create chain:", err);
setError(err instanceof Error ? err.message : "Failed to create chain");
return null;
}
},
[fetchChains]
);
const updateExistingChain = useCallback(
async (
chainId: string,
req: UpdateChainRequest
): Promise<ChainWithContracts | null> => {
try {
await updateChain(chainId, req);
// Refresh the list
await fetchChains();
// Return the updated chain
return await getChain(chainId);
} catch (err) {
console.error("Failed to update chain:", err);
setError(err instanceof Error ? err.message : "Failed to update chain");
return null;
}
},
[fetchChains]
);
const archiveExistingChain = useCallback(
async (chainId: string): Promise<boolean> => {
try {
await archiveChain(chainId);
// Refresh the list
await fetchChains();
return true;
} catch (err) {
console.error("Failed to archive chain:", err);
setError(err instanceof Error ? err.message : "Failed to archive chain");
return false;
}
},
[fetchChains]
);
const getChainById = useCallback(
async (chainId: string): Promise<ChainWithContracts | null> => {
try {
return await getChain(chainId);
} catch (err) {
console.error("Failed to get chain:", err);
setError(err instanceof Error ? err.message : "Failed to get chain");
return null;
}
},
[]
);
const getGraph = useCallback(
async (chainId: string): Promise<ChainGraphResponse | null> => {
try {
return await getChainGraph(chainId);
} catch (err) {
console.error("Failed to get chain graph:", err);
setError(err instanceof Error ? err.message : "Failed to get chain graph");
return null;
}
},
[]
);
return {
chains,
loading,
error,
refresh: fetchChains,
createNewChain,
updateExistingChain,
archiveExistingChain,
getChainById,
getGraph,
};
}
|