2024-11-17 18:10:35 +00:00
|
|
|
import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
import { DialogFooter, DialogHeader } from "@/components/ui/dialog";
|
|
|
|
import {
|
|
|
|
Form,
|
|
|
|
FormField,
|
|
|
|
FormItem,
|
|
|
|
FormLabel,
|
|
|
|
FormControl,
|
|
|
|
FormDescription,
|
|
|
|
FormMessage,
|
|
|
|
} from "@/components/ui/form";
|
|
|
|
import {
|
|
|
|
Select,
|
|
|
|
SelectTrigger,
|
|
|
|
SelectValue,
|
|
|
|
SelectContent,
|
|
|
|
SelectItem,
|
|
|
|
} from "@/components/ui/select";
|
|
|
|
import { ToastAction } from "@/components/ui/toast";
|
|
|
|
import { DialogContent, DialogTitle } from "@/components/ui/dialog";
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
import { LoadingSpinner } from "@/components/ui/loading-spinner";
|
|
|
|
import { useToast } from "@/hooks/use-toast";
|
|
|
|
import { useTemplateImages } from "@/templates/api";
|
|
|
|
import { superstructResolver } from "@hookform/resolvers/superstruct";
|
2024-11-29 23:52:53 +00:00
|
|
|
import { useRef, useEffect } from "react";
|
2024-11-17 18:10:35 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
import { nonempty, object, pattern, string, type Infer } from "superstruct";
|
2024-12-02 09:38:01 +00:00
|
|
|
import { useCreateWorkspace, useWorkspaceRuntimes } from "./api";
|
|
|
|
import type { TemplateImage } from "@/templates/types";
|
|
|
|
import type { WorkspaceRuntime } from "./types";
|
2024-12-02 19:12:26 +00:00
|
|
|
import { API_ERROR_WORKSPACE_EXISTS, isApiErrorResponse } from "@/api";
|
2024-11-17 18:10:35 +00:00
|
|
|
|
2024-11-28 19:32:33 +00:00
|
|
|
interface NewWorkspaceDialogProps {
|
|
|
|
onCreateSuccess: () => void;
|
|
|
|
}
|
|
|
|
|
2024-12-02 09:38:01 +00:00
|
|
|
const NewWorkspaceFormSchema = object({
|
2024-11-17 18:10:35 +00:00
|
|
|
workspaceName: pattern(string(), /^[\w-]+$/),
|
2024-12-03 00:07:41 +00:00
|
|
|
image: nonempty(string()),
|
2024-12-02 09:38:01 +00:00
|
|
|
runtime: nonempty(string()),
|
2024-11-17 18:10:35 +00:00
|
|
|
});
|
|
|
|
|
2024-11-28 19:32:33 +00:00
|
|
|
function NewWorkspaceDialog({ onCreateSuccess }: NewWorkspaceDialogProps) {
|
2024-12-02 09:38:01 +00:00
|
|
|
const {
|
|
|
|
data: templateImages,
|
|
|
|
isLoading: isLoadingImages,
|
|
|
|
error,
|
|
|
|
} = useTemplateImages();
|
|
|
|
const { data: runtimes, isLoading: isLoadingRuntimes } =
|
|
|
|
useWorkspaceRuntimes();
|
|
|
|
const isLoading = isLoadingImages || isLoadingRuntimes;
|
2024-11-17 18:10:35 +00:00
|
|
|
|
|
|
|
function content() {
|
|
|
|
if (error) {
|
|
|
|
console.log(error);
|
|
|
|
return (
|
|
|
|
<p className="opacity-80">
|
|
|
|
An error occurred when fetching available options.
|
|
|
|
</p>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (isLoading) {
|
|
|
|
return (
|
|
|
|
<div className="w-full flex items-center justify-center">
|
|
|
|
<LoadingSpinner />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2024-12-02 09:38:01 +00:00
|
|
|
if (!templateImages || !runtimes) {
|
2024-11-17 18:10:35 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (templateImages.length === 0) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<p className="opacity-80">
|
|
|
|
No images found. Create and build a template, and the resulting
|
|
|
|
image will show up here.
|
|
|
|
</p>
|
|
|
|
<Alert>
|
|
|
|
<AlertTitle>What are images?</AlertTitle>
|
|
|
|
<AlertDescription>
|
|
|
|
An image is used to bootstrap a workspace, including the operating
|
|
|
|
system, the environment, and packages, as specified by a template.
|
|
|
|
</AlertDescription>
|
|
|
|
</Alert>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2024-12-02 09:38:01 +00:00
|
|
|
<NewWorkspaceForm
|
|
|
|
templateImages={templateImages}
|
|
|
|
runtimes={runtimes}
|
|
|
|
onCreateSuccess={onCreateSuccess}
|
|
|
|
/>
|
2024-11-17 18:10:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<DialogContent>
|
|
|
|
<DialogHeader>
|
|
|
|
<DialogTitle>New workspace</DialogTitle>
|
|
|
|
</DialogHeader>
|
|
|
|
{content()}
|
|
|
|
</DialogContent>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-12-02 09:38:01 +00:00
|
|
|
function NewWorkspaceForm({
|
|
|
|
templateImages,
|
|
|
|
runtimes,
|
|
|
|
onCreateSuccess,
|
|
|
|
}: {
|
|
|
|
templateImages: TemplateImage[];
|
|
|
|
runtimes: WorkspaceRuntime[];
|
|
|
|
onCreateSuccess: () => void;
|
|
|
|
}) {
|
|
|
|
const form = useForm({
|
|
|
|
resolver: superstructResolver(NewWorkspaceFormSchema),
|
|
|
|
defaultValues: {
|
|
|
|
workspaceName: "",
|
2024-12-03 00:07:41 +00:00
|
|
|
// image is in the form "imageTag imageId" (space as separator)
|
|
|
|
// this is to prevent two image tags pointing to the same image id
|
|
|
|
image: "",
|
2024-12-02 09:38:01 +00:00
|
|
|
runtime: "",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const { createWorkspace, status } = useCreateWorkspace();
|
|
|
|
const { toast } = useToast();
|
|
|
|
const formRef = useRef<HTMLFormElement | null>(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
switch (status.type) {
|
|
|
|
case "error":
|
2024-12-02 19:12:26 +00:00
|
|
|
if (isApiErrorResponse(status.error)) {
|
|
|
|
let toastTitle = "";
|
|
|
|
switch (status.error.code) {
|
|
|
|
case API_ERROR_WORKSPACE_EXISTS:
|
|
|
|
toastTitle = "Workspace already exists.";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
toastTitle = "Failed to create the workspace.";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
toast({
|
|
|
|
variant: "destructive",
|
|
|
|
title: toastTitle,
|
|
|
|
description: status.error.error,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
toast({
|
|
|
|
variant: "destructive",
|
|
|
|
title: "Failed to create the workspace.",
|
|
|
|
description: "Unknown error",
|
|
|
|
action: (
|
|
|
|
<ToastAction
|
|
|
|
onClick={() => {
|
|
|
|
formRef.current?.requestSubmit();
|
|
|
|
}}
|
|
|
|
altText="Try again"
|
|
|
|
>
|
|
|
|
Try again
|
|
|
|
</ToastAction>
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
2024-12-02 09:38:01 +00:00
|
|
|
break;
|
2024-12-02 19:12:26 +00:00
|
|
|
|
2024-12-02 09:38:01 +00:00
|
|
|
case "ok":
|
|
|
|
onCreateSuccess();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2024-12-02 19:12:26 +00:00
|
|
|
}, [status, toast, onCreateSuccess]);
|
2024-12-02 09:38:01 +00:00
|
|
|
|
|
|
|
async function onSubmit(values: Infer<typeof NewWorkspaceFormSchema>) {
|
|
|
|
await createWorkspace({
|
|
|
|
workspaceName: values.workspaceName,
|
2024-12-03 00:07:41 +00:00
|
|
|
imageId: values.image.split(" ")[1],
|
2024-12-02 09:38:01 +00:00
|
|
|
runtime: values.runtime,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form {...form}>
|
|
|
|
<form
|
|
|
|
ref={formRef}
|
|
|
|
onSubmit={form.handleSubmit(onSubmit)}
|
|
|
|
className="space-y-4"
|
|
|
|
>
|
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="workspaceName"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem>
|
|
|
|
<FormLabel>Workspace name</FormLabel>
|
|
|
|
<FormControl>
|
|
|
|
<Input placeholder="my-workspace" {...field} />
|
|
|
|
</FormControl>
|
|
|
|
<FormDescription>
|
|
|
|
Must only contain alphanumeric characters and "-".
|
|
|
|
</FormDescription>
|
|
|
|
<FormMessage />
|
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<FormField
|
|
|
|
control={form.control}
|
2024-12-03 00:07:41 +00:00
|
|
|
name="image"
|
2024-12-02 09:38:01 +00:00
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem>
|
|
|
|
<FormLabel>Image for this workspace</FormLabel>
|
2024-12-02 11:10:59 +00:00
|
|
|
<Select onValueChange={field.onChange} value={field.value}>
|
2024-12-02 09:38:01 +00:00
|
|
|
<FormControl>
|
|
|
|
<SelectTrigger>
|
|
|
|
<SelectValue placeholder="Select an image" />
|
|
|
|
</SelectTrigger>
|
|
|
|
</FormControl>
|
|
|
|
<SelectContent>
|
|
|
|
{templateImages.map((image) => (
|
2024-12-03 00:07:41 +00:00
|
|
|
<SelectItem
|
|
|
|
key={image.imageTag}
|
|
|
|
value={`${image.imageTag} ${image.imageId}`}
|
|
|
|
>
|
2024-12-02 09:38:01 +00:00
|
|
|
{image.imageTag}
|
|
|
|
</SelectItem>
|
|
|
|
))}
|
|
|
|
</SelectContent>
|
|
|
|
</Select>
|
|
|
|
<FormMessage />
|
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="runtime"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem>
|
|
|
|
<FormLabel>Docker runtime</FormLabel>
|
2024-12-02 11:10:59 +00:00
|
|
|
<Select onValueChange={field.onChange} value={field.value}>
|
2024-12-02 09:38:01 +00:00
|
|
|
<FormControl>
|
|
|
|
<SelectTrigger>
|
|
|
|
<SelectValue placeholder="Select a Docker runtime" />
|
|
|
|
</SelectTrigger>
|
|
|
|
</FormControl>
|
|
|
|
<SelectContent>
|
|
|
|
{runtimes.map((runtime) => (
|
|
|
|
<SelectItem key={runtime.name} value={runtime.name}>
|
|
|
|
{runtime.name}
|
|
|
|
</SelectItem>
|
|
|
|
))}
|
|
|
|
</SelectContent>
|
|
|
|
</Select>
|
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<DialogFooter>
|
|
|
|
<Button type="submit">Create</Button>
|
|
|
|
</DialogFooter>
|
|
|
|
</form>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-17 18:10:35 +00:00
|
|
|
export { NewWorkspaceDialog };
|