2
0

useDeploymentTarget.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { useContext } from "react";
  2. import { useQuery } from "@tanstack/react-query";
  3. import { z } from "zod";
  4. import api from "shared/api";
  5. import { Context } from "shared/Context";
  6. export const deploymentTargetValidator = z.object({
  7. id: z.string(),
  8. project_id: z.number(),
  9. cluster_id: z.number(),
  10. namespace: z.string(),
  11. name: z.string(),
  12. is_preview: z.boolean(),
  13. is_default: z.boolean(),
  14. created_at: z.string(),
  15. updated_at: z.string(),
  16. });
  17. export type DeploymentTarget = z.infer<typeof deploymentTargetValidator>;
  18. const emptyDeploymentTarget: DeploymentTarget = {
  19. cluster_id: 0,
  20. created_at: "",
  21. id: "",
  22. is_default: false,
  23. is_preview: false,
  24. name: "",
  25. namespace: "",
  26. project_id: 0,
  27. updated_at: "",
  28. };
  29. export function useDefaultDeploymentTarget(): {
  30. defaultDeploymentTarget: DeploymentTarget;
  31. isDefaultDeploymentTargetLoading: boolean;
  32. } {
  33. const { currentProject, currentCluster } = useContext(Context);
  34. const { data = emptyDeploymentTarget, isLoading } = useQuery(
  35. ["getDefaultDeploymentTarget", currentProject?.id, currentCluster?.id],
  36. async () => {
  37. // see Context.tsx L98 for why the last check is necessary
  38. if (
  39. !currentProject?.id ||
  40. !currentCluster?.id ||
  41. currentCluster.id === -1
  42. ) {
  43. return;
  44. }
  45. const res = await api.getDefaultDeploymentTarget(
  46. "<token>",
  47. {},
  48. {
  49. project_id: currentProject?.id,
  50. cluster_id: currentCluster?.id,
  51. }
  52. );
  53. const object = await z
  54. .object({
  55. deployment_target: deploymentTargetValidator,
  56. })
  57. .parseAsync(res.data);
  58. return object.deployment_target;
  59. },
  60. {
  61. enabled:
  62. !!currentProject &&
  63. !!currentCluster &&
  64. currentProject.validate_apply_v2,
  65. }
  66. );
  67. return {
  68. defaultDeploymentTarget: data,
  69. isDefaultDeploymentTargetLoading: isLoading,
  70. };
  71. }
  72. export function useDeploymentTargetList(input: { preview: boolean }): {
  73. deploymentTargetList: DeploymentTarget[];
  74. isDeploymentTargetListLoading: boolean;
  75. } {
  76. const { currentProject, currentCluster } = useContext(Context);
  77. const { data = [], isLoading } = useQuery(
  78. [
  79. "listDeploymentTargets",
  80. currentProject?.id,
  81. currentCluster?.id,
  82. input.preview,
  83. ],
  84. async () => {
  85. if (!currentProject || !currentCluster) {
  86. return;
  87. }
  88. const res = await api.listDeploymentTargets(
  89. "<token>",
  90. {
  91. preview: input.preview,
  92. },
  93. {
  94. project_id: currentProject?.id,
  95. cluster_id: currentCluster?.id,
  96. }
  97. );
  98. const deploymentTargets = await z
  99. .object({
  100. deployment_targets: z.array(deploymentTargetValidator),
  101. })
  102. .parseAsync(res.data);
  103. return deploymentTargets.deployment_targets;
  104. },
  105. {
  106. enabled: !!currentProject && !!currentCluster,
  107. }
  108. );
  109. return {
  110. deploymentTargetList: data,
  111. isDeploymentTargetListLoading: isLoading,
  112. };
  113. }