| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import { useContext } from "react";
- import { useQuery } from "@tanstack/react-query";
- import { z } from "zod";
- import api from "shared/api";
- import { Context } from "shared/Context";
- export const clusterValidator = z.object({
- id: z.number(),
- name: z.string(),
- vanity_name: z.string(),
- cloud_provider: z.enum(["AWS", "GCP", "AZURE"]),
- });
- export type Cluster = z.infer<typeof clusterValidator>;
- export const isAWSCluster = (
- cluster: Cluster
- ): cluster is Cluster & { cloud_provider: "AWS" } => {
- return cluster.cloud_provider === "AWS";
- };
- type TUseClusterList = {
- clusters: Array<z.infer<typeof clusterValidator>>;
- isLoading: boolean;
- };
- export const useClusterList = (): TUseClusterList => {
- const { currentProject } = useContext(Context);
- const clusterReq = useQuery(
- ["getClusters", currentProject?.id],
- async () => {
- if (!currentProject?.id || currentProject.id === -1) {
- return;
- }
- const res = await api.getClusters(
- "<token>",
- {},
- { id: currentProject.id }
- );
- const parsed = await z.array(clusterValidator).parseAsync(res.data);
- return parsed;
- },
- {
- enabled: !!currentProject && currentProject.id !== -1,
- }
- );
- return {
- clusters: clusterReq.data ?? [],
- isLoading: clusterReq.isLoading,
- };
- };
|