Files
tesseract/web/src/workspaces/api.ts

163 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-11-12 00:31:10 +00:00
import { fetchApi } from "@/api";
import useSWR, { useSWRConfig } from "swr";
2024-11-17 18:10:35 +00:00
import { WorkspaceStatus, type Workspace } from "./types";
2024-11-12 00:31:10 +00:00
import { useCallback, useState } from "react";
2024-11-17 18:10:35 +00:00
import type { QueryStatus } from "@/lib/query";
2024-11-12 00:31:10 +00:00
function useWorkspaces() {
return useSWR(
"/workspaces",
(): Promise<Workspace[]> =>
fetchApi("/workspaces").then((res) => res.json()),
);
}
function useCreateWorkspace() {
const [status, setStatus] = useState<QueryStatus>({ type: "idle" });
const { mutate } = useSWRConfig();
const createWorkspace = useCallback(
async ({
workspaceName,
imageId,
2024-11-17 18:10:35 +00:00
}: {
workspaceName: string;
imageId: string;
}): Promise<Workspace | null> => {
2024-11-12 00:31:10 +00:00
setStatus({ type: "loading" });
try {
2024-11-17 18:10:35 +00:00
const workspace = await mutate(
"/workspaces",
fetchApi(`/workspaces/${workspaceName}`, {
method: "POST",
body: JSON.stringify({ imageId }),
headers: {
"Content-Type": "application/json",
},
}).then((res): Promise<Workspace> => res.json()),
{
populateCache: (createdWorkspace, workspaces) => [
...workspaces,
createdWorkspace,
],
throwOnError: true,
2024-11-12 00:31:10 +00:00
},
2024-11-17 18:10:35 +00:00
);
return workspace ?? null;
} catch (error: unknown) {
setStatus({ type: "error", error });
return null;
}
},
[mutate],
);
return { createWorkspace, status };
}
function useChangeWorkspaceStatus() {
const [status, setStatus] = useState<QueryStatus>({ type: "idle" });
const { mutate } = useSWRConfig();
2024-11-12 00:31:10 +00:00
2024-11-17 18:10:35 +00:00
const startWorkspace = useCallback(
async (workspaceName: string) => {
setStatus({ type: "loading" });
try {
await mutate(
"/workspaces",
fetchApi(`/workspaces/${workspaceName}`, {
method: "POST",
body: JSON.stringify({ status: WorkspaceStatus.Running }),
headers: {
"Content-Type": "application/json",
},
}).then((res): Promise<Workspace> => res.json()),
{
populateCache: (updatedWorkspace, workspaces) =>
workspaces.map((workspace: Workspace) =>
workspace.containerId === updatedWorkspace.containerId
? updatedWorkspace
: workspace,
),
throwOnError: true,
},
);
2024-11-12 00:31:10 +00:00
setStatus({ type: "ok" });
2024-11-17 18:10:35 +00:00
} catch (error: unknown) {
setStatus({ type: "error", error });
}
},
[mutate],
);
2024-11-12 00:31:10 +00:00
2024-11-17 18:10:35 +00:00
const stopWorkspace = useCallback(
async (workspaceName: string) => {
setStatus({ type: "loading" });
try {
await mutate(
"/workspaces",
fetchApi(`/workspaces/${workspaceName}`, {
method: "POST",
body: JSON.stringify({ status: WorkspaceStatus.Stopped }),
headers: {
"Content-Type": "application/json",
},
}).then((res): Promise<Workspace> => res.json()),
{
populateCache: (updatedWorkspace, workspaces) =>
workspaces.map((workspace: Workspace) =>
workspace.containerId === updatedWorkspace.containerId
? updatedWorkspace
: workspace,
),
throwOnError: true,
},
);
setStatus({ type: "ok" });
2024-11-12 00:31:10 +00:00
} catch (error: unknown) {
setStatus({ type: "error", error });
}
},
2024-11-17 18:10:35 +00:00
[mutate],
2024-11-12 00:31:10 +00:00
);
2024-11-17 18:10:35 +00:00
return { startWorkspace, stopWorkspace, status };
}
function useDeleteWorkspace() {
const [status, setStatus] = useState<QueryStatus>({ type: "idle" });
const { mutate } = useSWRConfig();
const deleteWorkspace = useCallback(
async (workspaceName: string) => {
setStatus({ type: "loading" });
try {
await mutate(
"/workspaces",
fetchApi(`/workspaces/${workspaceName}`, { method: "DELETE" }),
{
populateCache: (_, workspaces) =>
workspaces.filter(
(workspace: Workspace) => workspace.name === workspaceName,
),
throwOnError: true,
},
);
setStatus({ type: "ok" });
} catch (error: unknown) {
setStatus({ type: "error", error });
}
},
[mutate],
);
return { deleteWorkspace, status };
2024-11-12 00:31:10 +00:00
}
2024-11-17 18:10:35 +00:00
export {
useWorkspaces,
useCreateWorkspace,
useChangeWorkspaceStatus,
useDeleteWorkspace,
};