import { zodResolver } from "@hookform/resolvers/zod"; import { Folder, HelpCircle } from "lucide-react"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; import { AlertBlock } from "@/components/shared/alert-block"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { DropdownMenuItem } from "@/components/ui/dropdown-menu"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Textarea } from "@/components/ui/textarea"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip"; import { slugify } from "@/lib/slug"; import { api } from "@/utils/api"; const AddTemplateSchema = z.object({ name: z.string().min(1, { message: "Name is required", }), appName: z .string() .min(1, { message: "App name is required", }) .regex(/^[a-z](?!.*--)([a-z0-9-]*[a-z])?$/, { message: "App name supports lowercase letters, numbers, '-' and can only start and end letters, and does not support continuous '-'", }), description: z.string().optional(), serverId: z.string().optional(), }); type AddTemplate = z.infer; interface Props { projectId: string; projectName?: string; } export const AddApplication = ({ projectId, projectName }: Props) => { const utils = api.useUtils(); const { data: isCloud } = api.settings.isCloud.useQuery(); const [visible, setVisible] = useState(false); const slug = slugify(projectName); const { data: servers } = api.server.withSSHKey.useQuery(); const hasServers = servers && servers.length > 0; const { mutateAsync, isLoading, error, isError } = api.application.create.useMutation(); const form = useForm({ defaultValues: { name: "", appName: `${slug}-`, description: "", }, resolver: zodResolver(AddTemplateSchema), }); const onSubmit = async (data: AddTemplate) => { await mutateAsync({ name: data.name, appName: data.appName, description: data.description, projectId, serverId: data.serverId, }) .then(async () => { toast.success("Service Created"); form.reset(); setVisible(false); await utils.project.one.invalidate({ projectId, }); }) .catch(() => { toast.error("Error creating the service"); }); }; return ( e.preventDefault()} > Application Create Assign a name and description to your application {isError && {error?.message}}
( Name { const val = e.target.value?.trim() || ""; const serviceName = slugify(val); form.setValue("appName", `${slug}-${serviceName}`); field.onChange(val); }} /> )} /> {hasServers && ( ( Select a Server {!isCloud ? "(Optional)" : ""} If no server is selected, the application will be deployed on the server where the user is logged in. )} /> )} ( App Name

This will be the name of the Docker Swarm service

)} /> ( Description