-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuseExecutable.ts
More file actions
102 lines (91 loc) · 2.59 KB
/
useExecutable.ts
File metadata and controls
102 lines (91 loc) · 2.59 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { useQuery, useQueryClient } from "@tanstack/react-query";
import React from "react";
import { EnrichedExecutable } from "../types/executable.ts";
import { invoke } from "@tauri-apps/api/core";
export function useExecutable(executableRef: string) {
const queryClient = useQueryClient();
const [currentExecutable, setCurrentExecutable] =
React.useState<EnrichedExecutable | null>(null);
const {
data: executable,
isLoading: isExecutableLoading,
error: executableError,
} = useQuery({
queryKey: ["executable", executableRef],
queryFn: async () => {
if (!executableRef) return null;
return await invoke<EnrichedExecutable>("get_executable", {
executableRef: executableRef,
});
},
enabled: !!executableRef,
});
// Update current executable when we have new data
React.useEffect(() => {
if (executable) {
setCurrentExecutable(executable);
}
}, [executable]);
const refreshExecutable = () => {
if (executableRef) {
queryClient.invalidateQueries({
queryKey: ["executable", executableRef],
});
}
};
return {
executable: currentExecutable,
isExecutableLoading,
executableError,
refreshExecutable,
};
}
export function useExecutables(
workspace: string | null,
namespace: string | null,
tags: string[] | null,
verb: string | null,
filter: string | null,
) {
const queryClient = useQueryClient();
const normalizedParams = React.useMemo(() => {
return {
workspace: workspace || undefined,
namespace: namespace || undefined,
tags: tags && tags.length > 0 ? tags : undefined,
verb: verb || undefined,
filter: filter || undefined,
};
}, [workspace, namespace, tags, verb, filter]);
const queryKey = React.useMemo(() => {
return ["executables", normalizedParams];
}, [normalizedParams]);
const {
data: executables,
isLoading: isExecutablesLoading,
error: executablesError,
} = useQuery({
queryKey,
queryFn: async () => {
console.log('Fetching executables with params:', normalizedParams);
return await invoke<EnrichedExecutable[]>("list_executables", {
workspace: normalizedParams.workspace,
namespace: normalizedParams.namespace,
tags: normalizedParams.tags,
verb: normalizedParams.verb,
filter: normalizedParams.filter,
});
},
});
const refreshExecutables = () => {
void queryClient.invalidateQueries({
queryKey,
});
};
return {
executables: executables || [],
isExecutablesLoading,
executablesError,
refreshExecutables,
};
}