Repository navigation
Expand file tree
/
Copy pathuseRegistryRefresh.ts
More file actions
80 lines (74 loc) · 2.38 KB
/
Copy pathuseRegistryRefresh.ts
File metadata and controls
80 lines (74 loc) · 2.38 KB
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
import { useCallback, useContext, useState } from "react";
import { useQueryClient } from "react-query";
import {
ProjectListContext,
ProjectsListSchema,
} from "../contexts/ProjectListContext";
import { useDataMode } from "../contexts/DataModeContext";
interface Toast {
id: string;
title: string;
color: "success" | "danger";
iconType: string;
}
const useRegistryRefresh = () => {
const [refreshing, setRefreshing] = useState(false);
const [toasts, setToasts] = useState<Toast[]>([]);
const queryClient = useQueryClient();
const projectListCtx = useContext(ProjectListContext);
const basename = projectListCtx?.basename || "";
const { fetchOptions } = useDataMode();
const removeToast = useCallback((removedToast: { id: string }) => {
setToasts((prev) => prev.filter((t) => t.id !== removedToast.id));
}, []);
const handleRefresh = useCallback(async () => {
setRefreshing(true);
try {
const refreshRes = await fetch(`${basename}/api/v1/registry/refresh`, {
method: "POST",
headers: { ...fetchOptions?.headers },
credentials: fetchOptions?.credentials,
});
if (!refreshRes.ok) {
throw new Error(`Registry refresh failed (${refreshRes.status})`);
}
const res = await fetch(`${basename}/projects-list.json`, {
headers: {
"Content-Type": "application/json",
...fetchOptions?.headers,
},
credentials: fetchOptions?.credentials,
});
if (!res.ok) {
throw new Error(`Failed to fetch project list (${res.status})`);
}
const json = await res.json();
const parsed = ProjectsListSchema.parse(json);
queryClient.setQueryData("feast-projects-list", parsed);
await queryClient.invalidateQueries("registry-rest-bulk");
setToasts((prev) => [
...prev,
{
id: String(Date.now()),
title: "Refresh successful",
color: "success" as const,
iconType: "check",
},
]);
} catch {
setToasts((prev) => [
...prev,
{
id: String(Date.now()),
title: "Refresh failed",
color: "danger" as const,
iconType: "alert",
},
]);
} finally {
setRefreshing(false);
}
}, [basename, queryClient, fetchOptions]);
return { refreshing, toasts, handleRefresh, removeToast };
};
export default useRegistryRefresh;