import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { api } from "@/utils/api"; import { zodResolver } from "@hookform/resolvers/zod"; import React, { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; const DockerProviderSchema = z.object({ externalPort: z.preprocess((a) => { if (a !== null) { const parsed = Number.parseInt(z.string().parse(a), 10); return Number.isNaN(parsed) ? null : parsed; } return null; }, z .number() .gte(0, "Range must be 0 - 65535") .lte(65535, "Range must be 0 - 65535") .nullable()), }); type DockerProvider = z.infer; interface Props { postgresId: string; } export const ShowExternalPostgresCredentials = ({ postgresId }: Props) => { const { data, refetch } = api.postgres.one.useQuery({ postgresId }); const { mutateAsync, isLoading } = api.postgres.saveExternalPort.useMutation(); const [connectionUrl, setConnectionUrl] = useState(""); const form = useForm({ defaultValues: {}, resolver: zodResolver(DockerProviderSchema), }); useEffect(() => { if (data?.externalPort) { form.reset({ externalPort: data.externalPort, }); } }, [form.reset, data, form]); const onSubmit = async (values: DockerProvider) => { await mutateAsync({ externalPort: values.externalPort, postgresId, }) .then(async () => { toast.success("External Port updated"); await refetch(); }) .catch(() => { toast.error("Error to save the external port"); }); }; useEffect(() => { const buildConnectionUrl = () => { const hostname = window.location.hostname; const port = form.watch("externalPort") || data?.externalPort; return `postgresql://${data?.databaseUser}:${data?.databasePassword}@${hostname}:${port}/${data?.databaseName}`; }; setConnectionUrl(buildConnectionUrl()); }, [ data?.appName, data?.externalPort, data?.databasePassword, form, data?.databaseName, ]); return ( <>
External Credentials In order to make the database reachable trought internet is required to set a port, make sure the port is not used by another application or database
{ return ( External Port (Internet) ); }} />
{!!data?.externalPort && (
)}
); };