|
| 1 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 2 | +import { Server } from "lucide-react"; |
| 3 | +import Link from "next/link"; |
| 4 | +import { useEffect } from "react"; |
| 5 | +import { useForm } from "react-hook-form"; |
| 6 | +import { toast } from "sonner"; |
| 7 | +import { z } from "zod"; |
| 8 | +import { AlertBlock } from "@/components/shared/alert-block"; |
| 9 | +import { Button } from "@/components/ui/button"; |
| 10 | +import { |
| 11 | + Card, |
| 12 | + CardContent, |
| 13 | + CardDescription, |
| 14 | + CardHeader, |
| 15 | + CardTitle, |
| 16 | +} from "@/components/ui/card"; |
| 17 | +import { |
| 18 | + Form, |
| 19 | + FormControl, |
| 20 | + FormDescription, |
| 21 | + FormField, |
| 22 | + FormItem, |
| 23 | + FormLabel, |
| 24 | + FormMessage, |
| 25 | +} from "@/components/ui/form"; |
| 26 | +import { |
| 27 | + Select, |
| 28 | + SelectContent, |
| 29 | + SelectGroup, |
| 30 | + SelectItem, |
| 31 | + SelectLabel, |
| 32 | + SelectTrigger, |
| 33 | + SelectValue, |
| 34 | +} from "@/components/ui/select"; |
| 35 | +import { api } from "@/utils/api"; |
| 36 | + |
| 37 | +interface Props { |
| 38 | + applicationId: string; |
| 39 | +} |
| 40 | + |
| 41 | +const schema = z.object({ |
| 42 | + buildServerId: z.string().min(1, "Build server is required"), |
| 43 | + buildRegistryId: z.string().min(1, "Build registry is required"), |
| 44 | +}); |
| 45 | + |
| 46 | +type Schema = z.infer<typeof schema>; |
| 47 | + |
| 48 | +export const ShowBuildServer = ({ applicationId }: Props) => { |
| 49 | + const { data, refetch } = api.application.one.useQuery( |
| 50 | + { applicationId }, |
| 51 | + { enabled: !!applicationId }, |
| 52 | + ); |
| 53 | + const { data: buildServers } = api.server.buildServers.useQuery(); |
| 54 | + const { data: registries } = api.registry.all.useQuery(); |
| 55 | + |
| 56 | + const { mutateAsync, isLoading } = api.application.update.useMutation(); |
| 57 | + |
| 58 | + const form = useForm<Schema>({ |
| 59 | + defaultValues: { |
| 60 | + buildServerId: data?.buildServerId || "", |
| 61 | + buildRegistryId: data?.buildRegistryId || "", |
| 62 | + }, |
| 63 | + resolver: zodResolver(schema), |
| 64 | + }); |
| 65 | + |
| 66 | + useEffect(() => { |
| 67 | + if (data) { |
| 68 | + form.reset({ |
| 69 | + buildServerId: data?.buildServerId || "", |
| 70 | + buildRegistryId: data?.buildRegistryId || "", |
| 71 | + }); |
| 72 | + } |
| 73 | + }, [form, form.reset, data]); |
| 74 | + |
| 75 | + const onSubmit = async (formData: Schema) => { |
| 76 | + await mutateAsync({ |
| 77 | + applicationId, |
| 78 | + buildServerId: |
| 79 | + formData?.buildServerId === "none" || !formData?.buildServerId |
| 80 | + ? null |
| 81 | + : formData?.buildServerId, |
| 82 | + buildRegistryId: |
| 83 | + formData?.buildRegistryId === "none" || !formData?.buildRegistryId |
| 84 | + ? null |
| 85 | + : formData?.buildRegistryId, |
| 86 | + }) |
| 87 | + .then(async () => { |
| 88 | + toast.success("Build Server Settings Updated"); |
| 89 | + await refetch(); |
| 90 | + }) |
| 91 | + .catch(() => { |
| 92 | + toast.error("Error updating build server settings"); |
| 93 | + }); |
| 94 | + }; |
| 95 | + |
| 96 | + return ( |
| 97 | + <Card className="bg-background"> |
| 98 | + <CardHeader> |
| 99 | + <div className="flex flex-row items-center gap-2"> |
| 100 | + <Server className="size-6 text-muted-foreground" /> |
| 101 | + <div> |
| 102 | + <CardTitle className="text-xl">Build Server</CardTitle> |
| 103 | + <CardDescription> |
| 104 | + Configure a dedicated server for building your application. |
| 105 | + </CardDescription> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + </CardHeader> |
| 109 | + <CardContent className="flex flex-col gap-4"> |
| 110 | + <AlertBlock type="info"> |
| 111 | + Build servers offload the build process from your deployment servers. |
| 112 | + Select a build server and registry to use for building your |
| 113 | + application. |
| 114 | + </AlertBlock> |
| 115 | + |
| 116 | + <AlertBlock type="info"> |
| 117 | + 📊 <strong>Important:</strong> Once the build finishes, you'll need to |
| 118 | + wait a few seconds for the deployment server to download the image. |
| 119 | + These download logs will <strong>NOT</strong> appear in the build |
| 120 | + deployment logs. Check the <strong>Logs</strong> tab to see when the |
| 121 | + container starts running. |
| 122 | + </AlertBlock> |
| 123 | + |
| 124 | + {!registries || registries.length === 0 ? ( |
| 125 | + <AlertBlock type="warning"> |
| 126 | + You need to add at least one registry to use build servers. Please |
| 127 | + go to{" "} |
| 128 | + <Link |
| 129 | + href="/dashboard/settings/registry" |
| 130 | + className="text-primary underline" |
| 131 | + > |
| 132 | + Settings |
| 133 | + </Link>{" "} |
| 134 | + to add a registry. |
| 135 | + </AlertBlock> |
| 136 | + ) : null} |
| 137 | + |
| 138 | + <Form {...form}> |
| 139 | + <form |
| 140 | + onSubmit={form.handleSubmit(onSubmit)} |
| 141 | + className="grid w-full gap-4" |
| 142 | + > |
| 143 | + <FormField |
| 144 | + control={form.control} |
| 145 | + name="buildServerId" |
| 146 | + render={({ field }) => ( |
| 147 | + <FormItem> |
| 148 | + <FormLabel>Build Server</FormLabel> |
| 149 | + <Select |
| 150 | + onValueChange={field.onChange} |
| 151 | + value={field.value || "none"} |
| 152 | + > |
| 153 | + <FormControl> |
| 154 | + <SelectTrigger> |
| 155 | + <SelectValue placeholder="Select a build server" /> |
| 156 | + </SelectTrigger> |
| 157 | + </FormControl> |
| 158 | + <SelectContent> |
| 159 | + <SelectGroup> |
| 160 | + <SelectItem value="none"> |
| 161 | + <span className="flex items-center gap-2"> |
| 162 | + <span>None</span> |
| 163 | + </span> |
| 164 | + </SelectItem> |
| 165 | + {buildServers?.map((server) => ( |
| 166 | + <SelectItem |
| 167 | + key={server.serverId} |
| 168 | + value={server.serverId} |
| 169 | + > |
| 170 | + <span className="flex items-center gap-2 justify-between w-full"> |
| 171 | + <span>{server.name}</span> |
| 172 | + <span className="text-muted-foreground text-xs"> |
| 173 | + {server.ipAddress} |
| 174 | + </span> |
| 175 | + </span> |
| 176 | + </SelectItem> |
| 177 | + ))} |
| 178 | + <SelectLabel> |
| 179 | + Build Servers ({buildServers?.length || 0}) |
| 180 | + </SelectLabel> |
| 181 | + </SelectGroup> |
| 182 | + </SelectContent> |
| 183 | + </Select> |
| 184 | + <FormDescription> |
| 185 | + Select a build server to handle the build process for this |
| 186 | + application. |
| 187 | + </FormDescription> |
| 188 | + <FormMessage /> |
| 189 | + </FormItem> |
| 190 | + )} |
| 191 | + /> |
| 192 | + |
| 193 | + <FormField |
| 194 | + control={form.control} |
| 195 | + name="buildRegistryId" |
| 196 | + render={({ field }) => ( |
| 197 | + <FormItem> |
| 198 | + <FormLabel>Build Registry</FormLabel> |
| 199 | + <Select |
| 200 | + onValueChange={field.onChange} |
| 201 | + value={field.value || "none"} |
| 202 | + > |
| 203 | + <FormControl> |
| 204 | + <SelectTrigger> |
| 205 | + <SelectValue placeholder="Select a registry" /> |
| 206 | + </SelectTrigger> |
| 207 | + </FormControl> |
| 208 | + <SelectContent> |
| 209 | + <SelectGroup> |
| 210 | + <SelectItem value="none"> |
| 211 | + <span className="flex items-center gap-2"> |
| 212 | + <span>None</span> |
| 213 | + </span> |
| 214 | + </SelectItem> |
| 215 | + {registries?.map((registry) => ( |
| 216 | + <SelectItem |
| 217 | + key={registry.registryId} |
| 218 | + value={registry.registryId} |
| 219 | + > |
| 220 | + {registry.registryName} |
| 221 | + </SelectItem> |
| 222 | + ))} |
| 223 | + <SelectLabel> |
| 224 | + Registries ({registries?.length || 0}) |
| 225 | + </SelectLabel> |
| 226 | + </SelectGroup> |
| 227 | + </SelectContent> |
| 228 | + </Select> |
| 229 | + <FormDescription> |
| 230 | + Select a registry to store the built images from the build |
| 231 | + server. |
| 232 | + </FormDescription> |
| 233 | + <FormMessage /> |
| 234 | + </FormItem> |
| 235 | + )} |
| 236 | + /> |
| 237 | + |
| 238 | + <div className="flex w-full justify-end"> |
| 239 | + <Button isLoading={isLoading} type="submit"> |
| 240 | + Save |
| 241 | + </Button> |
| 242 | + </div> |
| 243 | + </form> |
| 244 | + </Form> |
| 245 | + </CardContent> |
| 246 | + </Card> |
| 247 | + ); |
| 248 | +}; |
0 commit comments