diff --git a/src/hooks/use-resource-actions.ts b/src/hooks/use-resource-actions.ts new file mode 100644 index 0000000..8649ce9 --- /dev/null +++ b/src/hooks/use-resource-actions.ts @@ -0,0 +1,23 @@ +import { useState } from "react"; + +export function useResourceActions() { + const [action, setAction] = useState(null); + const [selected, setSelected] = useState(null); + + const handleOpen = (resource: ResourceName, action: Actions) => { + setSelected(resource); + setAction(action); + }; + + const handleClose = () => { + setSelected(null); + setAction(null); + }; + + return { + action, + selected, + handleOpen, + handleClose, + }; +} diff --git a/src/queries/invoke.ts b/src/hooks/use-resource-list.ts similarity index 91% rename from src/queries/invoke.ts rename to src/hooks/use-resource-list.ts index d2d18e4..3191f3b 100644 --- a/src/queries/invoke.ts +++ b/src/hooks/use-resource-list.ts @@ -11,7 +11,7 @@ type Commands = | "get_replica_sets" | "get_stateful_sets"; -export function useGetResourceList(command: Commands) { +export function useResourceList(command: Commands) { const { namespace } = useCurrentNamespace(); const result = useQuery( [command, namespace], diff --git a/src/workloads/cron-jobs.tsx b/src/workloads/cron-jobs.tsx index aa8cec5..45299cf 100644 --- a/src/workloads/cron-jobs.tsx +++ b/src/workloads/cron-jobs.tsx @@ -2,12 +2,12 @@ import { V1CronJob } from "@kubernetes/client-node"; import { formatDistance } from "date-fns"; import { Table, TableHeader, TableBody, TableCell } from "../components/table"; -import { useGetResourceList } from "../queries/invoke"; +import { useResourceList } from "../hooks/use-resource-list"; export function CronJobs() { const { data: { items }, - } = useGetResourceList("get_cron_jobs"); + } = useResourceList("get_cron_jobs"); return (
diff --git a/src/workloads/deployments.tsx b/src/workloads/deployments.tsx index f1e8ead..904ba22 100644 --- a/src/workloads/deployments.tsx +++ b/src/workloads/deployments.tsx @@ -1,4 +1,4 @@ -import type { V1Deployment } from "@kubernetes/client-node"; +import { V1Deployment } from "@kubernetes/client-node"; import { useMutation } from "@tanstack/react-query"; import { invoke } from "@tauri-apps/api"; import { lazy, Suspense, useState } from "react"; @@ -6,7 +6,8 @@ import { lazy, Suspense, useState } from "react"; import { ActionButton, ActionGroup } from "../components/action-group"; import { ScaleModal } from "../components/scale-modal"; import { Table, TableHeader, TableBody, TableCell } from "../components/table"; -import { useGetResourceList } from "../queries/invoke"; +import { useResourceActions } from "../hooks/use-resource-actions"; +import { useResourceList } from "../hooks/use-resource-list"; const ResourceEditDrawer = lazy(() => import("../components/resource-edit-drawer").then((module) => ({ @@ -17,7 +18,12 @@ const ResourceEditDrawer = lazy(() => export function Deployments() { const { data: { items }, - } = useGetResourceList("get_deployments"); + } = useResourceList("get_deployments"); + + const { selected, handleOpen, handleClose, action } = useResourceActions< + V1Deployment, + "edit" | "scale" + >(); const restartMutation = useMutation({ mutationFn: (deployment: V1Deployment) => { @@ -31,19 +37,6 @@ export function Deployments() { }, }); - const [action, setAction] = useState<"edit" | "scale" | null>(null); - const [selected, setSelected] = useState(null); - - const handleOpen = (deployment: V1Deployment, action: "edit" | "scale") => { - setSelected(deployment); - setAction(action); - }; - - const handleClose = () => { - setSelected(null); - setAction(null); - }; - return (
diff --git a/src/workloads/jobs.tsx b/src/workloads/jobs.tsx index d5a5db8..ebc1971 100644 --- a/src/workloads/jobs.tsx +++ b/src/workloads/jobs.tsx @@ -2,12 +2,12 @@ import type { V1Job } from "@kubernetes/client-node"; import { formatDistance } from "date-fns"; import { Table, TableHeader, TableBody, TableCell } from "../components/table"; -import { useGetResourceList } from "../queries/invoke"; +import { useResourceList } from "../hooks/use-resource-list"; export function Jobs() { const { data: { items }, - } = useGetResourceList("get_jobs"); + } = useResourceList("get_jobs"); return (
diff --git a/src/workloads/pods.tsx b/src/workloads/pods.tsx index f26a27f..994fec4 100644 --- a/src/workloads/pods.tsx +++ b/src/workloads/pods.tsx @@ -3,7 +3,8 @@ import { useState, lazy, Suspense } from "react"; import { ActionButton, ActionGroup } from "../components/action-group"; import { Table, TableHeader, TableBody, TableCell } from "../components/table"; -import { useGetResourceList } from "../queries/invoke"; +import { useResourceActions } from "../hooks/use-resource-actions"; +import { useResourceList } from "../hooks/use-resource-list"; const PodLogs = lazy(() => import("./pod-logs").then((module) => ({ default: module.PodLogs }))); @@ -15,19 +16,12 @@ const ResourceEditDrawer = lazy(() => export function Pods() { const { data: { items }, - } = useGetResourceList("get_pods"); + } = useResourceList("get_pods"); - const [podAction, setPodAction] = useState<"logs" | "edit" | null>(null); - const [selectedPod, setSelectedPod] = useState(null); - - const handleOpen = (pod: V1Pod, action: "logs" | "edit") => { - setSelectedPod(pod); - setPodAction(action); - }; - const handleClose = () => { - setSelectedPod(null); - setPodAction(null); - }; + const { selected, handleOpen, handleClose, action } = useResourceActions< + V1Pod, + "logs" | "edit" + >(); return (
@@ -57,17 +51,13 @@ export function Pods() {
Loading Logs
}> - + Loading Form
}> diff --git a/src/workloads/replica-sets.tsx b/src/workloads/replica-sets.tsx index 003b542..f06803e 100644 --- a/src/workloads/replica-sets.tsx +++ b/src/workloads/replica-sets.tsx @@ -1,12 +1,12 @@ import type { V1ReplicaSet } from "@kubernetes/client-node"; import { Table, TableHeader, TableBody, TableCell } from "../components/table"; -import { useGetResourceList } from "../queries/invoke"; +import { useResourceList } from "../hooks/use-resource-list"; export function ReplicaSets() { const { data: { items }, - } = useGetResourceList("get_replica_sets"); + } = useResourceList("get_replica_sets"); return (
diff --git a/src/workloads/stateful-sets.tsx b/src/workloads/stateful-sets.tsx index 639ee2e..22e7f0e 100644 --- a/src/workloads/stateful-sets.tsx +++ b/src/workloads/stateful-sets.tsx @@ -1,13 +1,11 @@ import type { V1StatefulSet } from "@kubernetes/client-node"; -import { useQuery } from "@tanstack/react-query"; -import { invoke } from "@tauri-apps/api"; import { lazy, Suspense, useState } from "react"; import { ActionButton, ActionGroup } from "../components/action-group"; import { ScaleModal } from "../components/scale-modal"; import { Table, TableHeader, TableBody, TableCell } from "../components/table"; -import { useCurrentNamespace } from "../namespaces/namespaces"; -import { useGetResourceList } from "../queries/invoke"; +import { useResourceActions } from "../hooks/use-resource-actions"; +import { useResourceList } from "../hooks/use-resource-list"; const ResourceEditDrawer = lazy(() => import("../components/resource-edit-drawer").then((module) => ({ @@ -15,35 +13,15 @@ const ResourceEditDrawer = lazy(() => })) ); -type Actions = "edit" | "logs" | "scale"; - export function StatefulSets() { - const { namespace } = useCurrentNamespace(); - - const result = useQuery( - ["deployments", namespace], - () => { - return invoke<{ items: V1StatefulSet[] }>(`get_stateful_sets`, { namespace }); - }, - { refetchInterval: 1000 } - ); - const { data: { items }, - } = useGetResourceList("get_stateful_sets"); - - const [action, setAction] = useState(null); - const [selected, setSelected] = useState(null); - - const handleOpen = (deployment: V1StatefulSet, action: Actions) => { - setSelected(deployment); - setAction(action); - }; + } = useResourceList("get_stateful_sets"); - const handleClose = () => { - setSelected(null); - setAction(null); - }; + const { selected, handleOpen, handleClose, action } = useResourceActions< + V1StatefulSet, + "edit" | "logs" | "scale" + >(); return (