import { defineStepper } from "@stepperize/react"; import { Bot } from "lucide-react"; import Link from "next/link"; import React, { useState } from "react"; import { toast } from "sonner"; 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 { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Separator } from "@/components/ui/separator"; import { api } from "@/utils/api"; import { StepOne } from "./step-one"; import { StepThree } from "./step-three"; import { StepTwo } from "./step-two"; interface EnvVariable { name: string; value: string; } interface Domain { host: string; port: number; serviceName: string; } interface Details { name: string; id: string; description: string; dockerCompose: string; envVariables: EnvVariable[]; shortDescription: string; domains: Domain[]; configFiles: Mount[]; } interface Mount { filePath: string; content: string; } export interface TemplateInfo { userInput: string; details?: Details | null; suggestions?: Details[]; server?: { serverId: string; name: string; }; aiId: string; } const defaultTemplateInfo: TemplateInfo = { aiId: "", userInput: "", server: undefined, details: null, suggestions: [], }; export const { useStepper, steps, Scoped } = defineStepper( { id: "needs", title: "Describe your needs", }, { id: "variant", title: "Choose a Variant", }, { id: "review", title: "Review and Finalize", }, ); interface Props { environmentId: string; projectName?: string; } export const TemplateGenerator = ({ environmentId }: Props) => { const [open, setOpen] = useState(false); const stepper = useStepper(); const { data: aiSettings } = api.ai.getAll.useQuery(); const { mutateAsync } = api.ai.deploy.useMutation(); const [templateInfo, setTemplateInfo] = useState(defaultTemplateInfo); const utils = api.useUtils(); const haveAtleasOneProviderEnabled = aiSettings?.some( (ai) => ai.isEnabled === true, ); const isDisabled = () => { if (stepper.current.id === "needs") { return !templateInfo.aiId || !templateInfo.userInput; } if (stepper.current.id === "variant") { return !templateInfo?.details?.id; } return false; }; const onSubmit = async () => { await mutateAsync({ environmentId: environmentId, id: templateInfo.details?.id || "", name: templateInfo?.details?.name || "", description: templateInfo?.details?.shortDescription || "", dockerCompose: templateInfo?.details?.dockerCompose || "", envVariables: (templateInfo?.details?.envVariables || []) .map((env: any) => `${env.name}=${env.value}`) .join("\n"), domains: templateInfo?.details?.domains || [], ...(templateInfo.server?.serverId && { serverId: templateInfo.server?.serverId || "", }), configFiles: templateInfo?.details?.configFiles || [], }) .then(async () => { toast.success("Compose Created"); setOpen(false); // Invalidate the project query to refresh the environment data await utils.environment.one.invalidate({ environmentId, }); }) .catch(() => { toast.error("Error creating the compose"); }); }; return ( e.preventDefault()} > AI Assistant AI Assistant Create a custom template based on your needs

Steps

Step {stepper.current.index + 1} of {steps.length}
{stepper.switch({ needs: () => ( <> {!haveAtleasOneProviderEnabled && (
AI features are not enabled To use AI-powered template generation, please{" "} enable AI in your settings .
)} {haveAtleasOneProviderEnabled && aiSettings && aiSettings?.length > 0 && (
{templateInfo.aiId && ( )}
)} ), variant: () => ( ), review: () => ( ), })}
); };