import React from "react"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { api } from "@/utils/api"; import { z } from "zod"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { toast } from "sonner"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; interface Props { composeId: string; } const AddRedirectSchema = z.object({ command: z.string(), }); type AddCommand = z.infer; export const AddCommandCompose = ({ composeId }: Props) => { const { data } = api.compose.one.useQuery( { composeId, }, { enabled: !!composeId }, ); const { data: defaultCommand, refetch } = api.compose.getDefaultCommand.useQuery( { composeId, }, { enabled: !!composeId }, ); const utils = api.useUtils(); const { mutateAsync, isLoading } = api.compose.update.useMutation(); const form = useForm({ defaultValues: { command: "", }, resolver: zodResolver(AddRedirectSchema), }); useEffect(() => { if (data?.command) { form.reset({ command: data?.command || "", }); } }, [form, form.reset, form.formState.isSubmitSuccessful, data?.command]); const onSubmit = async (data: AddCommand) => { await mutateAsync({ composeId, command: data?.command, }) .then(async () => { toast.success("Command Updated"); refetch(); await utils.compose.one.invalidate({ composeId, }); }) .catch(() => { toast.error("Error to update the command"); }); }; return (
Run Command Append a custom command to the compose file
( Command Default Command ({defaultCommand}) )} />
); };