Skip to content

Commit

Permalink
chore: create use resource actions to remove duplicate code (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhu2000 authored Jan 21, 2023
1 parent 011f865 commit d5ef5dc
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 72 deletions.
23 changes: 23 additions & 0 deletions src/hooks/use-resource-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useState } from "react";

export function useResourceActions<ResourceName, Actions>() {
const [action, setAction] = useState<Actions | null>(null);
const [selected, setSelected] = useState<ResourceName | null>(null);

const handleOpen = (resource: ResourceName, action: Actions) => {
setSelected(resource);
setAction(action);
};

const handleClose = () => {
setSelected(null);
setAction(null);
};

return {
action,
selected,
handleOpen,
handleClose,
};
}
2 changes: 1 addition & 1 deletion src/queries/invoke.ts → src/hooks/use-resource-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Commands =
| "get_replica_sets"
| "get_stateful_sets";

export function useGetResourceList<T>(command: Commands) {
export function useResourceList<T>(command: Commands) {
const { namespace } = useCurrentNamespace();
const result = useQuery(
[command, namespace],
Expand Down
4 changes: 2 additions & 2 deletions src/workloads/cron-jobs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<V1CronJob>("get_cron_jobs");
} = useResourceList<V1CronJob>("get_cron_jobs");

return (
<div>
Expand Down
25 changes: 9 additions & 16 deletions src/workloads/deployments.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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";

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) => ({
Expand All @@ -17,7 +18,12 @@ const ResourceEditDrawer = lazy(() =>
export function Deployments() {
const {
data: { items },
} = useGetResourceList<V1Deployment>("get_deployments");
} = useResourceList<V1Deployment>("get_deployments");

const { selected, handleOpen, handleClose, action } = useResourceActions<
V1Deployment,
"edit" | "scale"
>();

const restartMutation = useMutation({
mutationFn: (deployment: V1Deployment) => {
Expand All @@ -31,19 +37,6 @@ export function Deployments() {
},
});

const [action, setAction] = useState<"edit" | "scale" | null>(null);
const [selected, setSelected] = useState<V1Deployment | null>(null);

const handleOpen = (deployment: V1Deployment, action: "edit" | "scale") => {
setSelected(deployment);
setAction(action);
};

const handleClose = () => {
setSelected(null);
setAction(null);
};

return (
<div>
<Table>
Expand Down
4 changes: 2 additions & 2 deletions src/workloads/jobs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<V1Job>("get_jobs");
} = useResourceList<V1Job>("get_jobs");

return (
<div>
Expand Down
30 changes: 10 additions & 20 deletions src/workloads/pods.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 })));

Expand All @@ -15,19 +16,12 @@ const ResourceEditDrawer = lazy(() =>
export function Pods() {
const {
data: { items },
} = useGetResourceList<V1Pod>("get_pods");
} = useResourceList<V1Pod>("get_pods");

const [podAction, setPodAction] = useState<"logs" | "edit" | null>(null);
const [selectedPod, setSelectedPod] = useState<V1Pod | null>(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 (
<div>
Expand Down Expand Up @@ -57,17 +51,13 @@ export function Pods() {
</TableBody>
</Table>
<Suspense fallback={<div>Loading Logs</div>}>
<PodLogs
isOpen={podAction === "logs"}
handleClose={handleClose}
selectedPod={selectedPod}
/>
<PodLogs isOpen={action === "logs"} handleClose={handleClose} selectedPod={selected} />
</Suspense>
<Suspense fallback={<div>Loading Form</div>}>
<ResourceEditDrawer
isOpen={podAction === "edit"}
isOpen={action === "edit"}
handleClose={handleClose}
selectedResource={selectedPod}
selectedResource={selected}
/>
</Suspense>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/workloads/replica-sets.tsx
Original file line number Diff line number Diff line change
@@ -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<V1ReplicaSet>("get_replica_sets");
} = useResourceList<V1ReplicaSet>("get_replica_sets");

return (
<div>
Expand Down
36 changes: 7 additions & 29 deletions src/workloads/stateful-sets.tsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,27 @@
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) => ({
default: module.ResourceEditDrawer,
}))
);

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<V1StatefulSet>("get_stateful_sets");

const [action, setAction] = useState<Actions | null>(null);
const [selected, setSelected] = useState<V1StatefulSet | null>(null);

const handleOpen = (deployment: V1StatefulSet, action: Actions) => {
setSelected(deployment);
setAction(action);
};
} = useResourceList<V1StatefulSet>("get_stateful_sets");

const handleClose = () => {
setSelected(null);
setAction(null);
};
const { selected, handleOpen, handleClose, action } = useResourceActions<
V1StatefulSet,
"edit" | "logs" | "scale"
>();

return (
<div>
Expand Down

0 comments on commit d5ef5dc

Please sign in to comment.