119 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-11-08 18:24:08 +07:00
import Icons from "@/components/ui/icons";
2024-11-09 10:33:07 +00:00
import Modal from "@/components/ui/modal";
import { SelectField } from "@/components/ui/select";
2024-11-09 14:37:09 +00:00
import { useZForm } from "@/hooks/useZForm";
2024-11-09 10:33:07 +00:00
import { createDisclosure } from "@/lib/utils";
2024-11-08 18:24:08 +07:00
import React from "react";
2024-11-09 14:37:09 +00:00
import { ScrollView, XStack } from "tamagui";
import { FormSchema, formSchema, typeOptions } from "../schema/form";
2024-11-09 10:33:07 +00:00
import { InputField } from "@/components/ui/input";
import FormField from "@/components/ui/form";
2024-11-18 01:28:31 +07:00
import { useSaveHost, useTags } from "../hooks/query";
2024-11-09 14:37:09 +00:00
import { ErrorAlert } from "@/components/ui/alert";
import Button from "@/components/ui/button";
import { PVEFormFields } from "./pve";
import { IncusFormFields } from "./incus";
import { SSHFormFields } from "./ssh";
2024-11-18 01:28:31 +07:00
import {
SelectMultipleField,
useSelectCreatableItems,
} from "@/components/ui/select-multiple";
2024-11-08 18:24:08 +07:00
2024-11-09 10:33:07 +00:00
export const hostFormModal = createDisclosure<FormSchema>();
2024-11-08 18:24:08 +07:00
2024-11-09 10:33:07 +00:00
const HostForm = () => {
const { data } = hostFormModal.use();
const form = useZForm(formSchema, data);
const isEditing = data?.id != null;
const type = form.watch("type");
2024-11-18 01:28:31 +07:00
const hostTags = useTags();
const [tags, addTag] = useSelectCreatableItems(hostTags.data);
2024-11-08 18:24:08 +07:00
2024-11-09 10:33:07 +00:00
const saveMutation = useSaveHost();
const onSubmit = form.handleSubmit((values) => {
saveMutation.mutate(values, {
onSuccess: () => {
hostFormModal.onClose();
form.reset();
},
});
2024-11-08 18:24:08 +07:00
});
return (
2024-11-09 10:33:07 +00:00
<Modal
disclosure={hostFormModal}
title="Host"
description={`${isEditing ? "Edit" : "Add new"} host.`}
>
2024-11-09 14:37:09 +00:00
<ErrorAlert mx="$4" mb="$4" error={saveMutation.error} />
2024-11-09 10:33:07 +00:00
<ScrollView contentContainerStyle={{ padding: "$4", pt: 0, gap: "$4" }}>
<FormField label="Label">
<InputField f={1} form={form} name="label" placeholder="Label..." />
</FormField>
<FormField label="Type">
<SelectField form={form} name="type" items={typeOptions} />
</FormField>
2024-11-08 18:24:08 +07:00
2024-11-16 23:50:02 +07:00
{type !== "group" && (
<>
2024-11-18 01:28:31 +07:00
<FormField label="Tags">
<SelectMultipleField
form={form}
name="tags"
placeholder="Select Tags"
items={tags}
isCreatable
onCreate={addTag}
/>
</FormField>
2024-11-16 23:50:02 +07:00
<FormField label="Hostname">
<InputField
form={form}
name="host"
placeholder="IP or hostname..."
/>
</FormField>
<FormField label="Port">
<InputField
form={form}
name="port"
keyboardType="number-pad"
placeholder="Port"
/>
</FormField>
</>
)}
2024-11-08 18:24:08 +07:00
2024-11-09 14:37:09 +00:00
{type === "ssh" ? (
<SSHFormFields form={form} />
) : type === "pve" ? (
<PVEFormFields form={form} />
) : type === "incus" ? (
<IncusFormFields form={form} />
) : null}
2024-11-09 10:33:07 +00:00
</ScrollView>
<XStack p="$4" gap="$4">
<Button flex={1} onPress={hostFormModal.onClose} bg="$colorTransparent">
Cancel
</Button>
<Button
flex={1}
icon={<Icons name="content-save" size={18} />}
onPress={onSubmit}
2024-11-09 14:37:09 +00:00
isLoading={saveMutation.isPending}
2024-11-09 10:33:07 +00:00
>
Save
</Button>
</XStack>
</Modal>
);
};
2024-11-18 01:28:31 +07:00
export default React.memo(HostForm);