-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathuseCustomImageName.ts
More file actions
55 lines (47 loc) · 1.67 KB
/
Copy pathuseCustomImageName.ts
File metadata and controls
55 lines (47 loc) · 1.67 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
import { useQuery } from "@tanstack/react-query";
import { useAuthStore } from "@/features/auth";
import { getSandboxCustomImages, getSandboxEnvironments } from "../api";
export const sandboxKeys = {
customImages: () => ["sandbox-custom-images"] as const,
environments: () => ["sandbox-environments"] as const,
};
interface UseCustomImageNameArgs {
customImageId: string | null;
sandboxEnvironmentId: string | null;
enabled: boolean;
}
// Returns null while loading, when the fetch fails (custom images disabled), or
// when the image can't be resolved — in every case the badge renders nothing.
export function useCustomImageName({
customImageId,
sandboxEnvironmentId,
enabled,
}: UseCustomImageNameArgs): string | null {
const { projectId, oauthAccessToken } = useAuthStore();
const canQuery = enabled && !!projectId && !!oauthAccessToken;
const hasImageRef = !!customImageId || !!sandboxEnvironmentId;
const imagesQuery = useQuery({
queryKey: sandboxKeys.customImages(),
queryFn: getSandboxCustomImages,
enabled: canQuery && hasImageRef,
staleTime: 60_000,
retry: 0,
});
const environmentsQuery = useQuery({
queryKey: sandboxKeys.environments(),
queryFn: getSandboxEnvironments,
enabled: canQuery && !!sandboxEnvironmentId,
staleTime: 60_000,
retry: 0,
});
const environment = sandboxEnvironmentId
? environmentsQuery.data?.find((env) => env.id === sandboxEnvironmentId)
: undefined;
const imageId = customImageId ?? environment?.custom_image_id ?? null;
if (!imageId) return null;
return (
imagesQuery.data?.find((image) => image.id === imageId)?.name ??
environment?.custom_image_name ??
null
);
}