mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
feat: edit volume
This commit is contained in:
@@ -1,6 +1,14 @@
|
|||||||
// This file is auto-generated by @hey-api/openapi-ts
|
// This file is auto-generated by @hey-api/openapi-ts
|
||||||
|
|
||||||
import { type Options, listVolumes, createVolume, testConnection, deleteVolume } from "../sdk.gen";
|
import {
|
||||||
|
type Options,
|
||||||
|
listVolumes,
|
||||||
|
createVolume,
|
||||||
|
testConnection,
|
||||||
|
deleteVolume,
|
||||||
|
getVolume,
|
||||||
|
updateVolume,
|
||||||
|
} from "../sdk.gen";
|
||||||
import { queryOptions, type UseMutationOptions, type DefaultError } from "@tanstack/react-query";
|
import { queryOptions, type UseMutationOptions, type DefaultError } from "@tanstack/react-query";
|
||||||
import type {
|
import type {
|
||||||
ListVolumesData,
|
ListVolumesData,
|
||||||
@@ -10,6 +18,9 @@ import type {
|
|||||||
TestConnectionResponse,
|
TestConnectionResponse,
|
||||||
DeleteVolumeData,
|
DeleteVolumeData,
|
||||||
DeleteVolumeResponse,
|
DeleteVolumeResponse,
|
||||||
|
GetVolumeData,
|
||||||
|
UpdateVolumeData,
|
||||||
|
UpdateVolumeResponse,
|
||||||
} from "../types.gen";
|
} from "../types.gen";
|
||||||
import { client as _heyApiClient } from "../client.gen";
|
import { client as _heyApiClient } from "../client.gen";
|
||||||
|
|
||||||
@@ -169,3 +180,42 @@ export const deleteVolumeMutation = (
|
|||||||
};
|
};
|
||||||
return mutationOptions;
|
return mutationOptions;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getVolumeQueryKey = (options: Options<GetVolumeData>) => createQueryKey("getVolume", options);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a volume by name
|
||||||
|
*/
|
||||||
|
export const getVolumeOptions = (options: Options<GetVolumeData>) => {
|
||||||
|
return queryOptions({
|
||||||
|
queryFn: async ({ queryKey, signal }) => {
|
||||||
|
const { data } = await getVolume({
|
||||||
|
...options,
|
||||||
|
...queryKey[0],
|
||||||
|
signal,
|
||||||
|
throwOnError: true,
|
||||||
|
});
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
queryKey: getVolumeQueryKey(options),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a volume's configuration
|
||||||
|
*/
|
||||||
|
export const updateVolumeMutation = (
|
||||||
|
options?: Partial<Options<UpdateVolumeData>>,
|
||||||
|
): UseMutationOptions<UpdateVolumeResponse, DefaultError, Options<UpdateVolumeData>> => {
|
||||||
|
const mutationOptions: UseMutationOptions<UpdateVolumeResponse, DefaultError, Options<UpdateVolumeData>> = {
|
||||||
|
mutationFn: async (localOptions) => {
|
||||||
|
const { data } = await updateVolume({
|
||||||
|
...options,
|
||||||
|
...localOptions,
|
||||||
|
throwOnError: true,
|
||||||
|
});
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
return mutationOptions;
|
||||||
|
};
|
||||||
|
|||||||
@@ -10,6 +10,12 @@ import type {
|
|||||||
TestConnectionResponses,
|
TestConnectionResponses,
|
||||||
DeleteVolumeData,
|
DeleteVolumeData,
|
||||||
DeleteVolumeResponses,
|
DeleteVolumeResponses,
|
||||||
|
GetVolumeData,
|
||||||
|
GetVolumeResponses,
|
||||||
|
GetVolumeErrors,
|
||||||
|
UpdateVolumeData,
|
||||||
|
UpdateVolumeResponses,
|
||||||
|
UpdateVolumeErrors,
|
||||||
} from "./types.gen";
|
} from "./types.gen";
|
||||||
import { client as _heyApiClient } from "./client.gen";
|
import { client as _heyApiClient } from "./client.gen";
|
||||||
|
|
||||||
@@ -83,3 +89,29 @@ export const deleteVolume = <ThrowOnError extends boolean = false>(
|
|||||||
...options,
|
...options,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a volume by name
|
||||||
|
*/
|
||||||
|
export const getVolume = <ThrowOnError extends boolean = false>(options: Options<GetVolumeData, ThrowOnError>) => {
|
||||||
|
return (options.client ?? _heyApiClient).get<GetVolumeResponses, GetVolumeErrors, ThrowOnError>({
|
||||||
|
url: "/api/v1/volumes/{name}",
|
||||||
|
...options,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a volume's configuration
|
||||||
|
*/
|
||||||
|
export const updateVolume = <ThrowOnError extends boolean = false>(
|
||||||
|
options: Options<UpdateVolumeData, ThrowOnError>,
|
||||||
|
) => {
|
||||||
|
return (options.client ?? _heyApiClient).put<UpdateVolumeResponses, UpdateVolumeErrors, ThrowOnError>({
|
||||||
|
url: "/api/v1/volumes/{name}",
|
||||||
|
...options,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
...options.headers,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
@@ -31,9 +31,9 @@ export type CreateVolumeData = {
|
|||||||
| {
|
| {
|
||||||
backend: "nfs";
|
backend: "nfs";
|
||||||
exportPath: string;
|
exportPath: string;
|
||||||
port: number;
|
|
||||||
server: string;
|
server: string;
|
||||||
version: "3" | "4" | "4.1";
|
version: "3" | "4" | "4.1";
|
||||||
|
port?: number | string;
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
backend: "smb";
|
backend: "smb";
|
||||||
@@ -70,9 +70,9 @@ export type TestConnectionData = {
|
|||||||
| {
|
| {
|
||||||
backend: "nfs";
|
backend: "nfs";
|
||||||
exportPath: string;
|
exportPath: string;
|
||||||
port: number;
|
|
||||||
server: string;
|
server: string;
|
||||||
version: "3" | "4" | "4.1";
|
version: "3" | "4" | "4.1";
|
||||||
|
port?: number | string;
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
backend: "smb";
|
backend: "smb";
|
||||||
@@ -115,6 +115,114 @@ export type DeleteVolumeResponses = {
|
|||||||
|
|
||||||
export type DeleteVolumeResponse = DeleteVolumeResponses[keyof DeleteVolumeResponses];
|
export type DeleteVolumeResponse = DeleteVolumeResponses[keyof DeleteVolumeResponses];
|
||||||
|
|
||||||
|
export type GetVolumeData = {
|
||||||
|
body?: never;
|
||||||
|
path: {
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
query?: never;
|
||||||
|
url: "/api/v1/volumes/{name}";
|
||||||
|
};
|
||||||
|
|
||||||
|
export type GetVolumeErrors = {
|
||||||
|
/**
|
||||||
|
* Volume not found
|
||||||
|
*/
|
||||||
|
404: unknown;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type GetVolumeResponses = {
|
||||||
|
/**
|
||||||
|
* Volume details
|
||||||
|
*/
|
||||||
|
200: {
|
||||||
|
config:
|
||||||
|
| {
|
||||||
|
backend: "directory";
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
backend: "nfs";
|
||||||
|
exportPath: string;
|
||||||
|
server: string;
|
||||||
|
version: "3" | "4" | "4.1";
|
||||||
|
port?: number | string;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
backend: "smb";
|
||||||
|
};
|
||||||
|
createdAt: number;
|
||||||
|
name: string;
|
||||||
|
path: string;
|
||||||
|
type: string;
|
||||||
|
updatedAt: number;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export type GetVolumeResponse = GetVolumeResponses[keyof GetVolumeResponses];
|
||||||
|
|
||||||
|
export type UpdateVolumeData = {
|
||||||
|
body?: {
|
||||||
|
config:
|
||||||
|
| {
|
||||||
|
backend: "directory";
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
backend: "nfs";
|
||||||
|
exportPath: string;
|
||||||
|
server: string;
|
||||||
|
version: "3" | "4" | "4.1";
|
||||||
|
port?: number | string;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
backend: "smb";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
path: {
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
query?: never;
|
||||||
|
url: "/api/v1/volumes/{name}";
|
||||||
|
};
|
||||||
|
|
||||||
|
export type UpdateVolumeErrors = {
|
||||||
|
/**
|
||||||
|
* Volume not found
|
||||||
|
*/
|
||||||
|
404: unknown;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type UpdateVolumeResponses = {
|
||||||
|
/**
|
||||||
|
* Volume updated successfully
|
||||||
|
*/
|
||||||
|
200: {
|
||||||
|
message: string;
|
||||||
|
volume: {
|
||||||
|
config:
|
||||||
|
| {
|
||||||
|
backend: "directory";
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
backend: "nfs";
|
||||||
|
exportPath: string;
|
||||||
|
server: string;
|
||||||
|
version: "3" | "4" | "4.1";
|
||||||
|
port?: number | string;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
backend: "smb";
|
||||||
|
};
|
||||||
|
createdAt: number;
|
||||||
|
name: string;
|
||||||
|
path: string;
|
||||||
|
type: string;
|
||||||
|
updatedAt: number;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export type UpdateVolumeResponse = UpdateVolumeResponses[keyof UpdateVolumeResponses];
|
||||||
|
|
||||||
export type ClientOptions = {
|
export type ClientOptions = {
|
||||||
baseUrl: "http://localhost:3000" | (string & {});
|
baseUrl: "http://localhost:3000" | (string & {});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import { arktypeResolver } from "@hookform/resolvers/arktype";
|
import { useMutation } from "@tanstack/react-query";
|
||||||
import { volumeConfigSchema } from "@ironmount/schemas";
|
import { Plus } from "lucide-react";
|
||||||
import { type } from "arktype";
|
import { useId } from "react";
|
||||||
import { CheckCircle, Loader2, Plus, XCircle } from "lucide-react";
|
import { toast } from "sonner";
|
||||||
import { useState } from "react";
|
import { createVolumeMutation } from "~/api-client/@tanstack/react-query.gen";
|
||||||
import { useForm } from "react-hook-form";
|
import { parseError } from "~/lib/errors";
|
||||||
import { testConnection } from "~/api-client";
|
import { CreateVolumeForm } from "./create-volume-form";
|
||||||
import { slugify } from "~/lib/utils";
|
|
||||||
import { Button } from "./ui/button";
|
import { Button } from "./ui/button";
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
@@ -16,66 +15,28 @@ import {
|
|||||||
DialogTitle,
|
DialogTitle,
|
||||||
DialogTrigger,
|
DialogTrigger,
|
||||||
} from "./ui/dialog";
|
} from "./ui/dialog";
|
||||||
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "./ui/form";
|
|
||||||
import { Input } from "./ui/input";
|
|
||||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./ui/select";
|
|
||||||
import { useMutation } from "@tanstack/react-query";
|
|
||||||
import { testConnectionMutation } from "~/api-client/@tanstack/react-query.gen";
|
|
||||||
import { toast } from "sonner";
|
|
||||||
|
|
||||||
export const formSchema = type({
|
|
||||||
name: "2<=string<=32",
|
|
||||||
}).and(volumeConfigSchema);
|
|
||||||
|
|
||||||
type FormValues = typeof formSchema.infer;
|
|
||||||
type Props = {
|
type Props = {
|
||||||
open: boolean;
|
open: boolean;
|
||||||
setOpen: (open: boolean) => void;
|
setOpen: (open: boolean) => void;
|
||||||
onSubmit: (values: FormValues) => void;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const CreateVolumeDialog = ({ open, setOpen, onSubmit }: Props) => {
|
export const CreateVolumeDialog = ({ open, setOpen }: Props) => {
|
||||||
const [testStatus, setTestStatus] = useState<"idle" | "loading" | "success" | "error">("idle");
|
const formId = useId();
|
||||||
const [testMessage, setTestMessage] = useState<string>("");
|
|
||||||
|
|
||||||
const form = useForm<typeof formSchema.infer>({
|
const create = useMutation({
|
||||||
resolver: arktypeResolver(formSchema),
|
...createVolumeMutation(),
|
||||||
defaultValues: {
|
onSuccess: () => {
|
||||||
name: "",
|
toast.success("Volume created successfully");
|
||||||
backend: "directory",
|
setOpen(false);
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
toast.error("Failed to create volume", {
|
||||||
|
description: parseError(error)?.message,
|
||||||
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const testBackendConnection = useMutation({
|
|
||||||
...testConnectionMutation(),
|
|
||||||
onMutate: () => {
|
|
||||||
setTestStatus("loading");
|
|
||||||
},
|
|
||||||
onError: () => {
|
|
||||||
setTestStatus("error");
|
|
||||||
setTestMessage("Failed to test connection. Please try again.");
|
|
||||||
},
|
|
||||||
onSuccess: (data) => {
|
|
||||||
if (data?.success) {
|
|
||||||
setTestStatus("success");
|
|
||||||
setTestMessage(data.message);
|
|
||||||
} else {
|
|
||||||
setTestStatus("error");
|
|
||||||
setTestMessage(data?.message || "Connection test failed");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const watchedBackend = form.watch("backend");
|
|
||||||
|
|
||||||
const handleTestConnection = async () => {
|
|
||||||
const formValues = form.getValues();
|
|
||||||
|
|
||||||
testBackendConnection.mutate({
|
|
||||||
body: { config: formValues },
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={open} onOpenChange={setOpen}>
|
<Dialog open={open} onOpenChange={setOpen}>
|
||||||
<DialogTrigger asChild>
|
<DialogTrigger asChild>
|
||||||
@@ -87,167 +48,23 @@ export const CreateVolumeDialog = ({ open, setOpen, onSubmit }: Props) => {
|
|||||||
<DialogContent>
|
<DialogContent>
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle>Create volume</DialogTitle>
|
<DialogTitle>Create volume</DialogTitle>
|
||||||
<DialogDescription>Enter a name for the new volume.</DialogDescription>
|
<DialogDescription>Enter a name for the new volume</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<Form {...form}>
|
<CreateVolumeForm
|
||||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
mode="create"
|
||||||
<FormField
|
formId={formId}
|
||||||
name="name"
|
onSubmit={(values) => {
|
||||||
render={({ field }) => (
|
create.mutate({ body: { config: values, name: values.name } });
|
||||||
<FormItem>
|
}}
|
||||||
<FormLabel>Name</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input
|
|
||||||
placeholder="Volume name"
|
|
||||||
value={field.value ?? ""}
|
|
||||||
onChange={(e) => field.onChange(slugify(e.target.value))}
|
|
||||||
max={32}
|
|
||||||
min={1}
|
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
|
||||||
<FormDescription>Unique identifier for the volume.</FormDescription>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<FormField
|
|
||||||
name="backend"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>Backend</FormLabel>
|
|
||||||
<Select onValueChange={field.onChange} defaultValue={field.value}>
|
|
||||||
<FormControl>
|
|
||||||
<SelectTrigger>
|
|
||||||
<SelectValue placeholder="Select a backend" />
|
|
||||||
</SelectTrigger>
|
|
||||||
</FormControl>
|
|
||||||
<SelectContent>
|
|
||||||
<SelectItem value="directory">Directory</SelectItem>
|
|
||||||
<SelectItem value="nfs">NFS</SelectItem>
|
|
||||||
</SelectContent>
|
|
||||||
</Select>
|
|
||||||
<FormDescription>Choose the storage backend for this volume.</FormDescription>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
{watchedBackend === "nfs" && (
|
|
||||||
<>
|
|
||||||
<FormField
|
|
||||||
name="server"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>Server</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input placeholder="192.168.1.100" value={field.value ?? ""} onChange={field.onChange} />
|
|
||||||
</FormControl>
|
|
||||||
<FormDescription>NFS server IP address or hostname.</FormDescription>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<FormField
|
|
||||||
name="exportPath"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>Export Path</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input placeholder="/export/data" value={field.value ?? ""} onChange={field.onChange} />
|
|
||||||
</FormControl>
|
|
||||||
<FormDescription>Path to the NFS export on the server.</FormDescription>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<FormField
|
|
||||||
name="port"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>Port</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input
|
|
||||||
type="number"
|
|
||||||
placeholder="2049"
|
|
||||||
value={field.value ?? ""}
|
|
||||||
onChange={(e) => field.onChange(parseInt(e.target.value, 10) || undefined)}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormDescription>NFS server port (default: 2049).</FormDescription>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<FormField
|
|
||||||
name="version"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>Version</FormLabel>
|
|
||||||
<Select onValueChange={field.onChange} defaultValue={field.value}>
|
|
||||||
<FormControl>
|
|
||||||
<SelectTrigger>
|
|
||||||
<SelectValue placeholder="Select NFS version" />
|
|
||||||
</SelectTrigger>
|
|
||||||
</FormControl>
|
|
||||||
<SelectContent>
|
|
||||||
<SelectItem value="3">NFS v3</SelectItem>
|
|
||||||
<SelectItem value="4">NFS v4</SelectItem>
|
|
||||||
<SelectItem value="4.1">NFS v4.1</SelectItem>
|
|
||||||
</SelectContent>
|
|
||||||
</Select>
|
|
||||||
<FormDescription>NFS protocol version to use.</FormDescription>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
{watchedBackend === "nfs" && (
|
|
||||||
<div className="space-y-3">
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<Button
|
|
||||||
type="button"
|
|
||||||
variant="outline"
|
|
||||||
onClick={handleTestConnection}
|
|
||||||
disabled={testStatus === "loading" || !form.watch("server") || !form.watch("exportPath")}
|
|
||||||
className="flex-1"
|
|
||||||
>
|
|
||||||
{testStatus === "loading" && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
|
||||||
{testStatus === "success" && <CheckCircle className="mr-2 h-4 w-4 text-green-500" />}
|
|
||||||
{testStatus === "error" && <XCircle className="mr-2 h-4 w-4 text-red-500" />}
|
|
||||||
{testStatus === "idle" && "Test Connection"}
|
|
||||||
{testStatus === "loading" && "Testing..."}
|
|
||||||
{testStatus === "success" && "Connection Successful"}
|
|
||||||
{testStatus === "error" && "Test Failed"}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
{testMessage && (
|
|
||||||
<div
|
|
||||||
className={`text-sm p-2 rounded-md ${
|
|
||||||
testStatus === "success"
|
|
||||||
? "bg-green-50 text-green-700 border border-green-200"
|
|
||||||
: testStatus === "error"
|
|
||||||
? "bg-red-50 text-red-700 border border-red-200"
|
|
||||||
: "bg-gray-50 text-gray-700 border border-gray-200"
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
{testMessage}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<DialogFooter>
|
<DialogFooter>
|
||||||
<Button variant="secondary" onClick={() => setOpen(false)}>
|
<Button type="button" variant="secondary" onClick={() => setOpen(false)}>
|
||||||
Cancel
|
Cancel
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button type="submit" form={formId} disabled={create.isPending}>
|
||||||
type="submit"
|
|
||||||
// disabled={createVolume.status === "pending" || !nameValue}
|
|
||||||
>
|
|
||||||
Create
|
Create
|
||||||
</Button>
|
</Button>
|
||||||
</DialogFooter>
|
</DialogFooter>
|
||||||
</form>
|
|
||||||
</Form>
|
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
);
|
);
|
||||||
|
|||||||
236
apps/client/app/components/create-volume-form.tsx
Normal file
236
apps/client/app/components/create-volume-form.tsx
Normal file
@@ -0,0 +1,236 @@
|
|||||||
|
import { arktypeResolver } from "@hookform/resolvers/arktype";
|
||||||
|
import { volumeConfigSchema } from "@ironmount/schemas";
|
||||||
|
import { useMutation } from "@tanstack/react-query";
|
||||||
|
import { type } from "arktype";
|
||||||
|
import { CheckCircle, Loader2, XCircle } from "lucide-react";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { useForm } from "react-hook-form";
|
||||||
|
import { testConnectionMutation } from "~/api-client/@tanstack/react-query.gen";
|
||||||
|
import { slugify } from "~/lib/utils";
|
||||||
|
import { Button } from "./ui/button";
|
||||||
|
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "./ui/form";
|
||||||
|
import { Input } from "./ui/input";
|
||||||
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./ui/select";
|
||||||
|
|
||||||
|
export const formSchema = type({
|
||||||
|
name: "2<=string<=32",
|
||||||
|
}).and(volumeConfigSchema);
|
||||||
|
|
||||||
|
type FormValues = typeof formSchema.inferIn;
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
onSubmit: (values: FormValues) => void;
|
||||||
|
mode?: "create" | "update";
|
||||||
|
initialValues?: Partial<FormValues>;
|
||||||
|
formId?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const CreateVolumeForm = ({ onSubmit, mode = "create", initialValues, formId }: Props) => {
|
||||||
|
const form = useForm<FormValues>({
|
||||||
|
resolver: arktypeResolver(formSchema),
|
||||||
|
defaultValues: {
|
||||||
|
name: "",
|
||||||
|
backend: "directory",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const { setValue, formState, watch, getValues } = form;
|
||||||
|
const { isDirty } = formState;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (initialValues && !isDirty) {
|
||||||
|
for (const [key, value] of Object.entries(initialValues)) {
|
||||||
|
setValue(key as keyof FormValues, value as string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [initialValues, isDirty, setValue]);
|
||||||
|
|
||||||
|
const watchedBackend = watch("backend");
|
||||||
|
|
||||||
|
const [testStatus, setTestStatus] = useState<"idle" | "loading" | "success" | "error">("idle");
|
||||||
|
const [testMessage, setTestMessage] = useState<string>("");
|
||||||
|
|
||||||
|
const testBackendConnection = useMutation({
|
||||||
|
...testConnectionMutation(),
|
||||||
|
onMutate: () => {
|
||||||
|
setTestStatus("loading");
|
||||||
|
},
|
||||||
|
onError: () => {
|
||||||
|
setTestStatus("error");
|
||||||
|
setTestMessage("Failed to test connection. Please try again.");
|
||||||
|
},
|
||||||
|
onSuccess: (data) => {
|
||||||
|
if (data?.success) {
|
||||||
|
setTestStatus("success");
|
||||||
|
setTestMessage(data.message);
|
||||||
|
} else {
|
||||||
|
setTestStatus("error");
|
||||||
|
setTestMessage(data?.message || "Connection test failed");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const handleTestConnection = async () => {
|
||||||
|
const formValues = getValues();
|
||||||
|
|
||||||
|
if (formValues.backend === "nfs") {
|
||||||
|
testBackendConnection.mutate({
|
||||||
|
body: { config: formValues },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Form {...form}>
|
||||||
|
<form id={formId} onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
||||||
|
<FormField
|
||||||
|
name="name"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Name</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
placeholder="Volume name"
|
||||||
|
value={field.value ?? ""}
|
||||||
|
onChange={(e) => field.onChange(slugify(e.target.value))}
|
||||||
|
max={32}
|
||||||
|
min={1}
|
||||||
|
disabled={mode === "update"}
|
||||||
|
className={mode === "update" ? "bg-gray-50" : ""}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>Unique identifier for the volume.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
name="backend"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Backend</FormLabel>
|
||||||
|
<Select onValueChange={field.onChange} defaultValue={field.value}>
|
||||||
|
<FormControl>
|
||||||
|
<SelectTrigger>
|
||||||
|
<SelectValue placeholder="Select a backend" />
|
||||||
|
</SelectTrigger>
|
||||||
|
</FormControl>
|
||||||
|
<SelectContent>
|
||||||
|
<SelectItem value="directory">Directory</SelectItem>
|
||||||
|
<SelectItem value="nfs">NFS</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
<FormDescription>Choose the storage backend for this volume.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{watchedBackend === "nfs" && (
|
||||||
|
<>
|
||||||
|
<FormField
|
||||||
|
name="server"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Server</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input placeholder="192.168.1.100" value={field.value ?? ""} onChange={field.onChange} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>NFS server IP address or hostname.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
name="exportPath"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Export Path</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input placeholder="/export/data" value={field.value ?? ""} onChange={field.onChange} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>Path to the NFS export on the server.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
name="port"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Port</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
type="number"
|
||||||
|
placeholder="2049"
|
||||||
|
value={field.value ?? ""}
|
||||||
|
onChange={(e) => field.onChange(parseInt(e.target.value, 10) || undefined)}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>NFS server port (default: 2049).</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
name="version"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Version</FormLabel>
|
||||||
|
<Select onValueChange={field.onChange} defaultValue={field.value}>
|
||||||
|
<FormControl>
|
||||||
|
<SelectTrigger>
|
||||||
|
<SelectValue placeholder="Select NFS version" />
|
||||||
|
</SelectTrigger>
|
||||||
|
</FormControl>
|
||||||
|
<SelectContent>
|
||||||
|
<SelectItem value="3">NFS v3</SelectItem>
|
||||||
|
<SelectItem value="4">NFS v4</SelectItem>
|
||||||
|
<SelectItem value="4.1">NFS v4.1</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
<FormDescription>NFS protocol version to use.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{watchedBackend === "nfs" && (
|
||||||
|
<div className="space-y-3">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
onClick={handleTestConnection}
|
||||||
|
disabled={testStatus === "loading" || !form.watch("server") || !form.watch("exportPath")}
|
||||||
|
className="flex-1"
|
||||||
|
>
|
||||||
|
{testStatus === "loading" && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||||
|
{testStatus === "success" && <CheckCircle className="mr-2 h-4 w-4 text-green-500" />}
|
||||||
|
{testStatus === "error" && <XCircle className="mr-2 h-4 w-4 text-red-500" />}
|
||||||
|
{testStatus === "idle" && "Test Connection"}
|
||||||
|
{testStatus === "loading" && "Testing..."}
|
||||||
|
{testStatus === "success" && "Connection Successful"}
|
||||||
|
{testStatus === "error" && "Test Failed"}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
{testMessage && (
|
||||||
|
<div
|
||||||
|
className={`text-sm p-2 rounded-md ${
|
||||||
|
testStatus === "success"
|
||||||
|
? "bg-green-50 text-green-700 border border-green-200"
|
||||||
|
: testStatus === "error"
|
||||||
|
? "bg-red-50 text-red-700 border border-red-200"
|
||||||
|
: "bg-gray-50 text-gray-700 border border-gray-200"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{testMessage}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</form>
|
||||||
|
</Form>
|
||||||
|
);
|
||||||
|
};
|
||||||
59
apps/client/app/components/edit-volume-dialog.tsx
Normal file
59
apps/client/app/components/edit-volume-dialog.tsx
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import { useMutation } from "@tanstack/react-query";
|
||||||
|
import { useId } from "react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import type { GetVolumeResponse } from "~/api-client";
|
||||||
|
import { updateVolumeMutation } from "~/api-client/@tanstack/react-query.gen";
|
||||||
|
import { parseError } from "~/lib/errors";
|
||||||
|
import { CreateVolumeForm } from "./create-volume-form";
|
||||||
|
import { Button } from "./ui/button";
|
||||||
|
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "./ui/dialog";
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
open: boolean;
|
||||||
|
setOpen: (open: boolean) => void;
|
||||||
|
initialValues?: Partial<GetVolumeResponse>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const EditVolumeDialog = ({ open, setOpen, initialValues }: Props) => {
|
||||||
|
const formId = useId();
|
||||||
|
|
||||||
|
const update = useMutation({
|
||||||
|
...updateVolumeMutation(),
|
||||||
|
onSuccess: () => {
|
||||||
|
toast.success("Volume updated successfully");
|
||||||
|
setOpen(false);
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
toast.error("Failed to update volume", {
|
||||||
|
description: parseError(error)?.message,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={open} onOpenChange={setOpen}>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>Create volume</DialogTitle>
|
||||||
|
<DialogDescription>Enter a name for the new volume</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
<CreateVolumeForm
|
||||||
|
mode="update"
|
||||||
|
formId={formId}
|
||||||
|
initialValues={{ ...initialValues, ...initialValues?.config }}
|
||||||
|
onSubmit={(values) => {
|
||||||
|
update.mutate({ body: { config: values }, path: { name: values.name } });
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<DialogFooter>
|
||||||
|
<Button type="button" variant="secondary" onClick={() => setOpen(false)}>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button type="submit" form={formId} disabled={update.isPending}>
|
||||||
|
Update
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
import { MutationCache, QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||||
|
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
|
||||||
import { isRouteErrorResponse, Links, Meta, Outlet, Scripts, ScrollRestoration } from "react-router";
|
import { isRouteErrorResponse, Links, Meta, Outlet, Scripts, ScrollRestoration } from "react-router";
|
||||||
import { Toaster } from "~/components/ui/sonner";
|
import { Toaster } from "~/components/ui/sonner";
|
||||||
|
|
||||||
@@ -23,7 +24,13 @@ export const links: Route.LinksFunction = () => [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const queryClient = new QueryClient();
|
const queryClient = new QueryClient({
|
||||||
|
mutationCache: new MutationCache({
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries();
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
export function Layout({ children }: { children: React.ReactNode }) {
|
export function Layout({ children }: { children: React.ReactNode }) {
|
||||||
return (
|
return (
|
||||||
@@ -41,6 +48,7 @@ export function Layout({ children }: { children: React.ReactNode }) {
|
|||||||
<ScrollRestoration />
|
<ScrollRestoration />
|
||||||
<Scripts />
|
<Scripts />
|
||||||
</body>
|
</body>
|
||||||
|
<ReactQueryDevtools initialIsOpen={false} />
|
||||||
</QueryClientProvider>
|
</QueryClientProvider>
|
||||||
</html>
|
</html>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import { type } from "arktype";
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||||
import { Copy, Folder } from "lucide-react";
|
import { Copy, Folder } from "lucide-react";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { useFetcher } from "react-router";
|
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { createVolume, deleteVolume, listVolumes } from "~/api-client";
|
import { type ListVolumesResponse, listVolumes } from "~/api-client";
|
||||||
import { CreateVolumeDialog, formSchema } from "~/components/create-volume-dialog";
|
import { deleteVolumeMutation, listVolumesOptions } from "~/api-client/@tanstack/react-query.gen";
|
||||||
|
import { CreateVolumeDialog } from "~/components/create-volume-dialog";
|
||||||
|
import { EditVolumeDialog } from "~/components/edit-volume-dialog";
|
||||||
import { Button } from "~/components/ui/button";
|
import { Button } from "~/components/ui/button";
|
||||||
import { Input } from "~/components/ui/input";
|
import { Input } from "~/components/ui/input";
|
||||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "~/components/ui/select";
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "~/components/ui/select";
|
||||||
@@ -23,71 +24,39 @@ export function meta(_: Route.MetaArgs) {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function clientAction({ request }: Route.ClientActionArgs) {
|
export const clientLoader = async () => {
|
||||||
const formData = await request.formData();
|
const volumes = await listVolumes();
|
||||||
const { _action, ...rest } = Object.fromEntries(formData.entries());
|
if (volumes.data) return { volumes: volumes.data.volumes };
|
||||||
|
return { volumes: [] };
|
||||||
|
};
|
||||||
|
|
||||||
if (_action === "delete") {
|
export default function Home({ loaderData }: Route.ComponentProps) {
|
||||||
const name = rest.name as string;
|
const [volumeToEdit, setVolumeToEdit] = useState<ListVolumesResponse["volumes"][number]>();
|
||||||
const { error } = await deleteVolume({ path: { name: name } });
|
const [createVolumeOpen, setCreateVolumeOpen] = useState(false);
|
||||||
|
|
||||||
if (error) {
|
const deleteVol = useMutation({
|
||||||
|
...deleteVolumeMutation(),
|
||||||
|
onSuccess: () => {
|
||||||
|
toast.success("Volume deleted successfully");
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
toast.error("Failed to delete volume", {
|
toast.error("Failed to delete volume", {
|
||||||
description: parseError(error)?.message,
|
description: parseError(error)?.message,
|
||||||
});
|
});
|
||||||
} else {
|
},
|
||||||
toast.success("Volume deleted successfully");
|
|
||||||
}
|
|
||||||
|
|
||||||
return { error: parseError(error), _action: "delete" as const };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_action === "create") {
|
|
||||||
const validationResult = formSchema(rest);
|
|
||||||
|
|
||||||
if (validationResult instanceof type.errors) {
|
|
||||||
toast.error("Invalid form data", {
|
|
||||||
description: "Please check your input and try again.",
|
|
||||||
});
|
});
|
||||||
return { error: validationResult, _action: "create" as const };
|
|
||||||
}
|
|
||||||
|
|
||||||
const validatedData = validationResult as typeof formSchema.infer;
|
|
||||||
const { error } = await createVolume({ body: { name: validatedData.name, config: validatedData } });
|
|
||||||
if (error) {
|
|
||||||
toast.error("Failed to create volume", {
|
|
||||||
description: parseError(error)?.message,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
toast.success("Volume created successfully");
|
|
||||||
}
|
|
||||||
return { error: parseError(error), _action: "create" as const };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function clientLoader(_: Route.ClientLoaderArgs) {
|
|
||||||
const volumes = await listVolumes();
|
|
||||||
return volumes.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function Home({ loaderData, actionData }: Route.ComponentProps) {
|
|
||||||
const [open, setOpen] = useState(false);
|
|
||||||
|
|
||||||
const createFetcher = useFetcher<Extract<typeof actionData, { _action: "create" }>>();
|
|
||||||
const deleteFetcher = useFetcher<Extract<typeof actionData, { _action: "delete" }>>();
|
|
||||||
|
|
||||||
createFetcher.data;
|
|
||||||
deleteFetcher.data;
|
|
||||||
|
|
||||||
const isDeleting = deleteFetcher.state === "submitting";
|
|
||||||
const isCreating = createFetcher.state === "submitting";
|
|
||||||
|
|
||||||
console.log(createFetcher);
|
|
||||||
|
|
||||||
const handleDeleteConfirm = (name: string) => {
|
const handleDeleteConfirm = (name: string) => {
|
||||||
deleteFetcher.submit({ _action: "delete", name }, { method: "DELETE" });
|
if (confirm(`Are you sure you want to delete the volume "${name}"? This action cannot be undone.`)) {
|
||||||
|
deleteVol.mutate({ path: { name } });
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const { data } = useQuery({
|
||||||
|
...listVolumesOptions(),
|
||||||
|
initialData: loaderData,
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
@@ -127,11 +96,7 @@ export default function Home({ loaderData, actionData }: Route.ComponentProps) {
|
|||||||
</SelectContent>
|
</SelectContent>
|
||||||
</Select>
|
</Select>
|
||||||
</span>
|
</span>
|
||||||
<CreateVolumeDialog
|
<CreateVolumeDialog open={createVolumeOpen} setOpen={setCreateVolumeOpen} />
|
||||||
open={open}
|
|
||||||
setOpen={setOpen}
|
|
||||||
onSubmit={(values) => createFetcher.submit({ _action: "create", ...values }, { method: "POST" })}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
<Table className="mt-4 border bg-white dark:bg-secondary">
|
<Table className="mt-4 border bg-white dark:bg-secondary">
|
||||||
<TableCaption>A list of your managed volumes.</TableCaption>
|
<TableCaption>A list of your managed volumes.</TableCaption>
|
||||||
@@ -145,7 +110,7 @@ export default function Home({ loaderData, actionData }: Route.ComponentProps) {
|
|||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHeader>
|
</TableHeader>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
{loaderData?.volumes.map((volume) => (
|
{data?.volumes.map((volume) => (
|
||||||
<TableRow key={volume.name}>
|
<TableRow key={volume.name}>
|
||||||
<TableCell className="font-medium">{volume.name}</TableCell>
|
<TableCell className="font-medium">{volume.name}</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
@@ -169,14 +134,29 @@ export default function Home({ loaderData, actionData }: Route.ComponentProps) {
|
|||||||
</span>
|
</span>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell className="text-right">
|
<TableCell className="text-right">
|
||||||
<Button variant="destructive" onClick={() => handleDeleteConfirm(volume.name)}>
|
<div className="flex justify-end gap-2">
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
onClick={() => {
|
||||||
|
setVolumeToEdit(volume);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Edit
|
||||||
|
</Button>
|
||||||
|
<Button type="button" variant="destructive" onClick={() => handleDeleteConfirm(volume.name)}>
|
||||||
Delete
|
Delete
|
||||||
</Button>
|
</Button>
|
||||||
|
</div>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
))}
|
))}
|
||||||
</TableBody>
|
</TableBody>
|
||||||
</Table>
|
</Table>
|
||||||
|
<EditVolumeDialog
|
||||||
|
open={Boolean(volumeToEdit)}
|
||||||
|
setOpen={() => setVolumeToEdit(undefined)}
|
||||||
|
initialValues={volumeToEdit}
|
||||||
|
/>
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
"@react-router/node": "^7.7.1",
|
"@react-router/node": "^7.7.1",
|
||||||
"@react-router/serve": "^7.7.1",
|
"@react-router/serve": "^7.7.1",
|
||||||
"@tanstack/react-query": "^5.84.2",
|
"@tanstack/react-query": "^5.84.2",
|
||||||
|
"@tanstack/react-query-devtools": "^5.85.9",
|
||||||
"@tanstack/react-table": "^8.21.3",
|
"@tanstack/react-table": "^8.21.3",
|
||||||
"arktype": "^2.1.20",
|
"arktype": "^2.1.20",
|
||||||
"class-variance-authority": "^0.7.1",
|
"class-variance-authority": "^0.7.1",
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ export const createVolumeBackend = (volume: Volume): VolumeBackend => {
|
|||||||
return makeNfsBackend(config, path);
|
return makeNfsBackend(config, path);
|
||||||
}
|
}
|
||||||
case "directory": {
|
case "directory": {
|
||||||
return makeDirectoryBackend();
|
return makeDirectoryBackend(config, path);
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
throw new Error(`Backend ${config.backend} not implemented`);
|
throw new Error(`Backend ${config.backend} not implemented`);
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
|
import * as fs from "node:fs/promises";
|
||||||
|
import type { BackendConfig } from "@ironmount/schemas";
|
||||||
import type { VolumeBackend } from "../backend";
|
import type { VolumeBackend } from "../backend";
|
||||||
|
|
||||||
const mount = async () => {
|
const mount = async (_config: BackendConfig, path: string) => {
|
||||||
console.log("Mounting directory volume...");
|
console.log("Mounting directory volume...");
|
||||||
|
await fs.mkdir(path, { recursive: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
const unmount = async () => {
|
const unmount = async () => {
|
||||||
console.log("Cannot unmount directory volume.");
|
console.log("Cannot unmount directory volume.");
|
||||||
};
|
};
|
||||||
|
|
||||||
export const makeDirectoryBackend = (): VolumeBackend => ({
|
export const makeDirectoryBackend = (config: BackendConfig, path: string): VolumeBackend => ({
|
||||||
mount,
|
mount: () => mount(config, path),
|
||||||
unmount,
|
unmount,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { exec } from "node:child_process";
|
import { exec } from "node:child_process";
|
||||||
|
import * as fs from "node:fs/promises";
|
||||||
import * as os from "node:os";
|
import * as os from "node:os";
|
||||||
import type { BackendConfig } from "@ironmount/schemas";
|
import type { BackendConfig } from "@ironmount/schemas";
|
||||||
import type { VolumeBackend } from "../backend";
|
import type { VolumeBackend } from "../backend";
|
||||||
@@ -13,6 +14,8 @@ const mount = async (config: BackendConfig, path: string) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await fs.mkdir(path, { recursive: true });
|
||||||
|
|
||||||
const source = `${config.server}:${config.exportPath}`;
|
const source = `${config.server}:${config.exportPath}`;
|
||||||
const options = [`vers=${config.version}`, `port=${config.port}`];
|
const options = [`vers=${config.version}`, `port=${config.port}`];
|
||||||
const cmd = `mount -t nfs -o ${options.join(",")} ${source} ${path}`;
|
const cmd = `mount -t nfs -o ${options.join(",")} ${source} ${path}`;
|
||||||
@@ -21,7 +24,7 @@ const mount = async (config: BackendConfig, path: string) => {
|
|||||||
exec(cmd, (error, stdout, stderr) => {
|
exec(cmd, (error, stdout, stderr) => {
|
||||||
console.log("Mount command executed:", { cmd, error, stdout, stderr });
|
console.log("Mount command executed:", { cmd, error, stdout, stderr });
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error(`Error mounting NFS volume: ${stderr}`);
|
// console.error(`Error mounting NFS volume: ${stderr}`);
|
||||||
return reject(new Error(`Failed to mount NFS volume: ${stderr}`));
|
return reject(new Error(`Failed to mount NFS volume: ${stderr}`));
|
||||||
}
|
}
|
||||||
console.log(`NFS volume mounted successfully: ${stdout}`);
|
console.log(`NFS volume mounted successfully: ${stdout}`);
|
||||||
@@ -30,11 +33,28 @@ const mount = async (config: BackendConfig, path: string) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const unmount = async () => {
|
const unmount = async (path: string) => {
|
||||||
console.log("Unmounting nfs volume...");
|
if (os.platform() !== "linux") {
|
||||||
|
console.error("NFS unmounting is only supported on Linux hosts.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const cmd = `umount -f ${path}`;
|
||||||
|
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
|
exec(cmd, (error, stdout, stderr) => {
|
||||||
|
console.log("Unmount command executed:", { cmd, error, stdout, stderr });
|
||||||
|
if (error) {
|
||||||
|
console.error(`Error unmounting NFS volume: ${stderr}`);
|
||||||
|
return reject(new Error(`Failed to unmount NFS volume: ${stderr}`));
|
||||||
|
}
|
||||||
|
console.log(`NFS volume unmounted successfully: ${stdout}`);
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const makeNfsBackend = (config: BackendConfig, path: string): VolumeBackend => ({
|
export const makeNfsBackend = (config: BackendConfig, path: string): VolumeBackend => ({
|
||||||
mount: () => mount(config, path),
|
mount: () => mount(config, path),
|
||||||
unmount,
|
unmount: () => unmount(path),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,10 +5,13 @@ import {
|
|||||||
createVolumeBody,
|
createVolumeBody,
|
||||||
createVolumeDto,
|
createVolumeDto,
|
||||||
deleteVolumeDto,
|
deleteVolumeDto,
|
||||||
|
getVolumeDto,
|
||||||
type ListVolumesResponseDto,
|
type ListVolumesResponseDto,
|
||||||
listVolumesDto,
|
listVolumesDto,
|
||||||
testConnectionBody,
|
testConnectionBody,
|
||||||
testConnectionDto,
|
testConnectionDto,
|
||||||
|
updateVolumeBody,
|
||||||
|
updateVolumeDto,
|
||||||
} from "./volume.dto";
|
} from "./volume.dto";
|
||||||
import { volumeService } from "./volume.service";
|
import { volumeService } from "./volume.service";
|
||||||
|
|
||||||
@@ -18,8 +21,8 @@ export const volumeController = new Hono()
|
|||||||
|
|
||||||
const response = {
|
const response = {
|
||||||
volumes: volumes.map((volume) => ({
|
volumes: volumes.map((volume) => ({
|
||||||
name: volume.name,
|
...volume,
|
||||||
path: volume.path,
|
updatedAt: volume.updatedAt.getTime(),
|
||||||
createdAt: volume.createdAt.getTime(),
|
createdAt: volume.createdAt.getTime(),
|
||||||
})),
|
})),
|
||||||
} satisfies ListVolumesResponseDto;
|
} satisfies ListVolumesResponseDto;
|
||||||
@@ -54,12 +57,47 @@ export const volumeController = new Hono()
|
|||||||
|
|
||||||
return c.json({ message: "Volume deleted" });
|
return c.json({ message: "Volume deleted" });
|
||||||
})
|
})
|
||||||
.get("/:name", (c) => {
|
.get("/:name", getVolumeDto, async (c) => {
|
||||||
return c.json({ message: `Details of volume ${c.req.param("name")}` });
|
const { name } = c.req.param();
|
||||||
|
const res = await volumeService.getVolume(name);
|
||||||
|
|
||||||
|
if (res.error) {
|
||||||
|
const { message, status } = handleServiceError(res.error);
|
||||||
|
return c.json(message, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = {
|
||||||
|
name: res.volume.name,
|
||||||
|
path: res.volume.path,
|
||||||
|
type: res.volume.type,
|
||||||
|
createdAt: res.volume.createdAt.getTime(),
|
||||||
|
updatedAt: res.volume.updatedAt.getTime(),
|
||||||
|
config: res.volume.config,
|
||||||
|
};
|
||||||
|
|
||||||
|
return c.json(response, 200);
|
||||||
})
|
})
|
||||||
.put("/:name", (c) => {
|
.put("/:name", updateVolumeDto, validator("json", updateVolumeBody), async (c) => {
|
||||||
return c.json({ message: `Update volume ${c.req.param("name")}` });
|
const { name } = c.req.param();
|
||||||
})
|
const body = c.req.valid("json");
|
||||||
.delete("/:name", (c) => {
|
const res = await volumeService.updateVolume(name, body.config);
|
||||||
return c.json({ message: `Delete volume ${c.req.param("name")}` });
|
|
||||||
|
if (res.error) {
|
||||||
|
const { message, status } = handleServiceError(res.error);
|
||||||
|
return c.json(message, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = {
|
||||||
|
message: "Volume updated",
|
||||||
|
volume: {
|
||||||
|
name: res.volume.name,
|
||||||
|
path: res.volume.path,
|
||||||
|
type: res.volume.type,
|
||||||
|
createdAt: res.volume.createdAt.getTime(),
|
||||||
|
updatedAt: res.volume.updatedAt.getTime(),
|
||||||
|
config: res.volume.config,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
return c.json(response, 200);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,15 +3,20 @@ import { type } from "arktype";
|
|||||||
import { describeRoute } from "hono-openapi";
|
import { describeRoute } from "hono-openapi";
|
||||||
import { resolver } from "hono-openapi/arktype";
|
import { resolver } from "hono-openapi/arktype";
|
||||||
|
|
||||||
|
const volumeSchema = type({
|
||||||
|
name: "string",
|
||||||
|
path: "string",
|
||||||
|
type: "string",
|
||||||
|
createdAt: "number",
|
||||||
|
updatedAt: "number",
|
||||||
|
config: volumeConfigSchema,
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List all volumes
|
* List all volumes
|
||||||
*/
|
*/
|
||||||
export const listVolumesResponse = type({
|
export const listVolumesResponse = type({
|
||||||
volumes: type({
|
volumes: volumeSchema.array(),
|
||||||
name: "string",
|
|
||||||
path: "string",
|
|
||||||
createdAt: "number",
|
|
||||||
}).array(),
|
|
||||||
});
|
});
|
||||||
export type ListVolumesResponseDto = typeof listVolumesResponse.infer;
|
export type ListVolumesResponseDto = typeof listVolumesResponse.infer;
|
||||||
|
|
||||||
@@ -90,6 +95,68 @@ export const deleteVolumeDto = describeRoute({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a volume
|
||||||
|
*/
|
||||||
|
export const getVolumeDto = describeRoute({
|
||||||
|
description: "Get a volume by name",
|
||||||
|
operationId: "getVolume",
|
||||||
|
validateResponse: true,
|
||||||
|
tags: ["Volumes"],
|
||||||
|
responses: {
|
||||||
|
200: {
|
||||||
|
description: "Volume details",
|
||||||
|
content: {
|
||||||
|
"application/json": {
|
||||||
|
schema: resolver(volumeSchema),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
404: {
|
||||||
|
description: "Volume not found",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a volume
|
||||||
|
*/
|
||||||
|
export const updateVolumeBody = type({
|
||||||
|
config: volumeConfigSchema,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const updateVolumeResponse = type({
|
||||||
|
message: "string",
|
||||||
|
volume: type({
|
||||||
|
name: "string",
|
||||||
|
path: "string",
|
||||||
|
type: "string",
|
||||||
|
createdAt: "number",
|
||||||
|
updatedAt: "number",
|
||||||
|
config: volumeConfigSchema,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const updateVolumeDto = describeRoute({
|
||||||
|
description: "Update a volume's configuration",
|
||||||
|
operationId: "updateVolume",
|
||||||
|
validateResponse: true,
|
||||||
|
tags: ["Volumes"],
|
||||||
|
responses: {
|
||||||
|
200: {
|
||||||
|
description: "Volume updated successfully",
|
||||||
|
content: {
|
||||||
|
"application/json": {
|
||||||
|
schema: resolver(updateVolumeResponse),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
404: {
|
||||||
|
description: "Volume not found",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test connection
|
* Test connection
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -86,6 +86,55 @@ const mountVolume = async (name: string) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getVolume = async (name: string) => {
|
||||||
|
const volume = await db.query.volumesTable.findFirst({
|
||||||
|
where: eq(volumesTable.name, name),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!volume) {
|
||||||
|
return { error: new NotFoundError("Volume not found") };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { volume };
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateVolume = async (name: string, backendConfig: BackendConfig) => {
|
||||||
|
try {
|
||||||
|
const existing = await db.query.volumesTable.findFirst({
|
||||||
|
where: eq(volumesTable.name, name),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!existing) {
|
||||||
|
return { error: new NotFoundError("Volume not found") };
|
||||||
|
}
|
||||||
|
|
||||||
|
const oldBackend = createVolumeBackend(existing);
|
||||||
|
await oldBackend.unmount();
|
||||||
|
|
||||||
|
const updated = await db
|
||||||
|
.update(volumesTable)
|
||||||
|
.set({
|
||||||
|
config: backendConfig,
|
||||||
|
type: backendConfig.backend,
|
||||||
|
updatedAt: new Date(),
|
||||||
|
})
|
||||||
|
.where(eq(volumesTable.name, name))
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
// Mount with new configuration
|
||||||
|
const newBackend = createVolumeBackend(updated[0]);
|
||||||
|
await newBackend.mount();
|
||||||
|
|
||||||
|
return { volume: updated[0] };
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
error: new InternalServerError("Failed to update volume", {
|
||||||
|
cause: error,
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const testConnection = async (backendConfig: BackendConfig) => {
|
const testConnection = async (backendConfig: BackendConfig) => {
|
||||||
let tempDir: string | null = null;
|
let tempDir: string | null = null;
|
||||||
|
|
||||||
@@ -134,5 +183,7 @@ export const volumeService = {
|
|||||||
createVolume,
|
createVolume,
|
||||||
mountVolume,
|
mountVolume,
|
||||||
deleteVolume,
|
deleteVolume,
|
||||||
|
getVolume,
|
||||||
|
updateVolume,
|
||||||
testConnection,
|
testConnection,
|
||||||
};
|
};
|
||||||
|
|||||||
5
bun.lock
5
bun.lock
@@ -21,6 +21,7 @@
|
|||||||
"@react-router/node": "^7.7.1",
|
"@react-router/node": "^7.7.1",
|
||||||
"@react-router/serve": "^7.7.1",
|
"@react-router/serve": "^7.7.1",
|
||||||
"@tanstack/react-query": "^5.84.2",
|
"@tanstack/react-query": "^5.84.2",
|
||||||
|
"@tanstack/react-query-devtools": "^5.85.9",
|
||||||
"@tanstack/react-table": "^8.21.3",
|
"@tanstack/react-table": "^8.21.3",
|
||||||
"arktype": "^2.1.20",
|
"arktype": "^2.1.20",
|
||||||
"class-variance-authority": "^0.7.1",
|
"class-variance-authority": "^0.7.1",
|
||||||
@@ -396,8 +397,12 @@
|
|||||||
|
|
||||||
"@tanstack/query-core": ["@tanstack/query-core@5.85.5", "", {}, "sha512-KO0WTob4JEApv69iYp1eGvfMSUkgw//IpMnq+//cORBzXf0smyRwPLrUvEe5qtAEGjwZTXrjxg+oJNP/C00t6w=="],
|
"@tanstack/query-core": ["@tanstack/query-core@5.85.5", "", {}, "sha512-KO0WTob4JEApv69iYp1eGvfMSUkgw//IpMnq+//cORBzXf0smyRwPLrUvEe5qtAEGjwZTXrjxg+oJNP/C00t6w=="],
|
||||||
|
|
||||||
|
"@tanstack/query-devtools": ["@tanstack/query-devtools@5.84.0", "", {}, "sha512-fbF3n+z1rqhvd9EoGp5knHkv3p5B2Zml1yNRjh7sNXklngYI5RVIWUrUjZ1RIcEoscarUb0+bOvIs5x9dwzOXQ=="],
|
||||||
|
|
||||||
"@tanstack/react-query": ["@tanstack/react-query@5.85.5", "", { "dependencies": { "@tanstack/query-core": "5.85.5" }, "peerDependencies": { "react": "^18 || ^19" } }, "sha512-/X4EFNcnPiSs8wM2v+b6DqS5mmGeuJQvxBglmDxl6ZQb5V26ouD2SJYAcC3VjbNwqhY2zjxVD15rDA5nGbMn3A=="],
|
"@tanstack/react-query": ["@tanstack/react-query@5.85.5", "", { "dependencies": { "@tanstack/query-core": "5.85.5" }, "peerDependencies": { "react": "^18 || ^19" } }, "sha512-/X4EFNcnPiSs8wM2v+b6DqS5mmGeuJQvxBglmDxl6ZQb5V26ouD2SJYAcC3VjbNwqhY2zjxVD15rDA5nGbMn3A=="],
|
||||||
|
|
||||||
|
"@tanstack/react-query-devtools": ["@tanstack/react-query-devtools@5.85.9", "", { "dependencies": { "@tanstack/query-devtools": "5.84.0" }, "peerDependencies": { "@tanstack/react-query": "^5.85.9", "react": "^18 || ^19" } }, "sha512-BAdhgwpzxkC1vdyCfiPbbC7FU/t/x6q2d9ZyhON/WykVUdznD69nlppuWpSIlIGipdRG7sF6tRZ6x3GtSq0EUQ=="],
|
||||||
|
|
||||||
"@tanstack/react-table": ["@tanstack/react-table@8.21.3", "", { "dependencies": { "@tanstack/table-core": "8.21.3" }, "peerDependencies": { "react": ">=16.8", "react-dom": ">=16.8" } }, "sha512-5nNMTSETP4ykGegmVkhjcS8tTLW6Vl4axfEGQN3v0zdHYbK4UfoqfPChclTrJ4EoK9QynqAu9oUf8VEmrpZ5Ww=="],
|
"@tanstack/react-table": ["@tanstack/react-table@8.21.3", "", { "dependencies": { "@tanstack/table-core": "8.21.3" }, "peerDependencies": { "react": ">=16.8", "react-dom": ">=16.8" } }, "sha512-5nNMTSETP4ykGegmVkhjcS8tTLW6Vl4axfEGQN3v0zdHYbK4UfoqfPChclTrJ4EoK9QynqAu9oUf8VEmrpZ5Ww=="],
|
||||||
|
|
||||||
"@tanstack/table-core": ["@tanstack/table-core@8.21.3", "", {}, "sha512-ldZXEhOBb8Is7xLs01fR3YEc3DERiz5silj8tnGkFZytt1abEvl/GhUmCE0PMLaMPTa3Jk4HbKmRlHmu+gCftg=="],
|
"@tanstack/table-core": ["@tanstack/table-core@8.21.3", "", {}, "sha512-ldZXEhOBb8Is7xLs01fR3YEc3DERiz5silj8tnGkFZytt1abEvl/GhUmCE0PMLaMPTa3Jk4HbKmRlHmu+gCftg=="],
|
||||||
|
|||||||
@@ -6,9 +6,5 @@ export default defineConfig({
|
|||||||
path: "./apps/client/app/api-client",
|
path: "./apps/client/app/api-client",
|
||||||
format: "biome",
|
format: "biome",
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [...defaultPlugins, "@tanstack/react-query", "@hey-api/client-fetch"],
|
||||||
...defaultPlugins,
|
|
||||||
"@tanstack/react-query",
|
|
||||||
"@hey-api/client-fetch",
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user