fosrl.pangolin/src/app/[orgId]/settings/resources/create/page.tsx

938 lines
54 KiB
TypeScript
Raw Normal View History

2024-11-11 00:00:16 -05:00
"use client";
2025-04-23 20:58:53 -04:00
import {
SettingsContainer,
SettingsSection,
SettingsSectionBody,
SettingsSectionDescription,
SettingsSectionForm,
SettingsSectionHeader,
SettingsSectionTitle
} from "@app/components/Settings";
2024-11-11 00:00:16 -05:00
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage
2024-11-11 00:00:16 -05:00
} from "@app/components/ui/form";
2025-04-23 20:58:53 -04:00
import HeaderTitle from "@app/components/SettingsSectionTitle";
2024-11-11 00:00:16 -05:00
import { z } from "zod";
2025-04-23 20:58:53 -04:00
import { useEffect, useState } from "react";
import { Controller, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { Input } from "@app/components/ui/input";
import { Button } from "@app/components/ui/button";
2024-11-11 00:00:16 -05:00
import { useParams, useRouter } from "next/navigation";
import { ListSitesResponse } from "@server/routers/site";
2025-01-01 21:41:31 -05:00
import { formatAxiosError } from "@app/lib/api";
import { createApiClient } from "@app/lib/api";
import { useEnvContext } from "@app/hooks/useEnvContext";
2025-04-23 20:58:53 -04:00
import { toast } from "@app/hooks/useToast";
import { AxiosResponse } from "axios";
2025-06-04 12:02:07 -04:00
import { Resource } from "@server/db";
2025-04-23 20:58:53 -04:00
import { StrategySelect } from "@app/components/StrategySelect";
Squashed commit of the following: commit c276d2193da5dbe7af5197bdf7e2bcce6f87b0cf Author: Owen Schwartz <owen@txv.io> Date: Tue Jan 28 22:06:04 2025 -0500 Okay actually now commit 9afdc0aadc3f4fb4e811930bacff70a9e17eab9f Author: Owen Schwartz <owen@txv.io> Date: Tue Jan 28 21:58:44 2025 -0500 Migrations working finally commit a7336b3b2466fe74d650b9c253ecadbe1eff749d Merge: e7c7203 fdb1ab4 Author: Owen Schwartz <owen@txv.io> Date: Mon Jan 27 22:19:15 2025 -0500 Merge branch 'dev' into tcp-udp-traffic commit e7c7203330b1b08e570048b10ef314b55068e466 Author: Owen Schwartz <owen@txv.io> Date: Mon Jan 27 22:18:09 2025 -0500 Working on migration commit a4704dfd44b10647257c7c7054c0dae806d315bb Author: Owen Schwartz <owen@txv.io> Date: Mon Jan 27 21:40:52 2025 -0500 Add flag to allow raw resources commit d74f7a57ed11e2a6bf1a7e0c28c29fb07eb573a0 Merge: 6817788 d791b9b Author: Owen Schwartz <owen@txv.io> Date: Mon Jan 27 21:28:50 2025 -0500 Merge branch 'tcp-udp-traffic' of https://github.com/fosrl/pangolin into tcp-udp-traffic commit 68177882781b54ef30b62cca7dee8bbed7c5a2fa Author: Owen Schwartz <owen@txv.io> Date: Mon Jan 27 21:28:32 2025 -0500 Get everything working commit d791b9b47f9f6ca050d6edfd1d674438f8562d99 Author: Milo Schwartz <mschwartz10612@gmail.com> Date: Mon Jan 27 17:46:19 2025 -0500 fix orgId check in verifyAdmin commit 6ac30afd7a449a126190d311bd98d7f1048f73a4 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 23:19:33 2025 -0500 Trying to figure out traefik... commit 9886b42272882f8bb6baff2efdbe26cee7cac2b6 Merge: 786e67e 85e9129 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 21:53:32 2025 -0500 Merge branch 'tcp-udp-traffic' of https://github.com/fosrl/pangolin into tcp-udp-traffic commit 786e67eadd6df1ee8df24e77aed20c1f1fc9ca67 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 21:51:37 2025 -0500 Bug fixing commit 85e9129ae313b2e4a460a8bc53a0af9f9fbbafb2 Author: Milo Schwartz <mschwartz10612@gmail.com> Date: Sun Jan 26 18:35:24 2025 -0500 rethrow errors in migration and remove permanent redirect commit bd82699505fc7510c27f72cd80ea0ce815d8c5ef Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 17:49:12 2025 -0500 Fix merge issue commit 933dbf3a02b1f19fd1f627410b2407fdf05cd9bf Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 17:46:13 2025 -0500 Add sql to update resources and targets commit f19437bad847c8dbf57fddd2c48cd17bab20ddb0 Merge: 58980eb 9f1f291 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 17:19:51 2025 -0500 Merge branch 'dev' into tcp-udp-traffic commit 58980ebb64d1040b4d224c76beb38c2254f3c5d9 Merge: 1de682a d284d36 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 17:10:09 2025 -0500 Merge branch 'dev' into tcp-udp-traffic commit 1de682a9f6039f40e05c8901c7381a94b0d018ed Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 17:08:29 2025 -0500 Working on migrations commit dc853d2bc02b11997be5c3c7ea789402716fb4c2 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 16:56:49 2025 -0500 Finish config of resource pages commit 37c681c08d7ab73d2cad41e7ef1dbe3a8852e1f2 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 16:07:25 2025 -0500 Finish up table commit 461c6650bbea0d7439cc042971ec13fdb52a7431 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 15:54:46 2025 -0500 Working toward having dual resource types commit f0894663627375e16ce6994370cb30b298efc2dc Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 22:31:25 2025 -0500 Add qutoes commit edc535b79b94c2e65b290cd90a69fe17d27245e9 Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 22:28:45 2025 -0500 Add readTimeout to allow long file uploads commit 194892fa14b505bd7c2b31873dc13d4b8996c0e1 Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 20:37:34 2025 -0500 Rework traefik config generation commit ad3f896b5333e4706d610c3198f29dcd67610365 Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 13:01:47 2025 -0500 Add proxy port to api commit ca6013b2ffda0924a696ec3141825a54a4e5297d Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 12:58:01 2025 -0500 Add migration commit 2258d76cb3a49d3db7f05f76d8b8a9f1c248b5e4 Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 12:55:02 2025 -0500 Add new proxy port
2025-01-28 22:26:45 -05:00
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue
} from "@app/components/ui/select";
import { subdomainSchema } from "@server/lib/schemas";
2025-02-18 22:56:46 -05:00
import { ListDomainsResponse } from "@server/routers/domain";
2025-03-01 17:45:38 -05:00
import LoaderPlaceholder from "@app/components/PlaceHolderLoader";
2025-04-23 20:58:53 -04:00
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList
} from "@app/components/ui/command";
import {
Popover,
PopoverContent,
PopoverTrigger
} from "@app/components/ui/popover";
import { CaretSortIcon, CheckIcon } from "@radix-ui/react-icons";
import { cn } from "@app/lib/cn";
2025-04-28 21:45:43 -04:00
import { SquareArrowOutUpRight } from "lucide-react";
import CopyTextBox from "@app/components/CopyTextBox";
import Link from "next/link";
2025-05-25 19:01:20 +00:00
import { useTranslations } from "next-intl";
2024-11-11 00:00:16 -05:00
2025-04-23 20:58:53 -04:00
const baseResourceFormSchema = z.object({
name: z.string().min(1).max(255),
siteId: z.number(),
http: z.boolean()
});
const httpResourceFormSchema = z.discriminatedUnion("isBaseDomain", [
z.object({
isBaseDomain: z.literal(true),
domainId: z.string().min(1)
}),
z.object({
isBaseDomain: z.literal(false),
domainId: z.string().min(1),
subdomain: z.string().pipe(subdomainSchema)
Squashed commit of the following: commit c276d2193da5dbe7af5197bdf7e2bcce6f87b0cf Author: Owen Schwartz <owen@txv.io> Date: Tue Jan 28 22:06:04 2025 -0500 Okay actually now commit 9afdc0aadc3f4fb4e811930bacff70a9e17eab9f Author: Owen Schwartz <owen@txv.io> Date: Tue Jan 28 21:58:44 2025 -0500 Migrations working finally commit a7336b3b2466fe74d650b9c253ecadbe1eff749d Merge: e7c7203 fdb1ab4 Author: Owen Schwartz <owen@txv.io> Date: Mon Jan 27 22:19:15 2025 -0500 Merge branch 'dev' into tcp-udp-traffic commit e7c7203330b1b08e570048b10ef314b55068e466 Author: Owen Schwartz <owen@txv.io> Date: Mon Jan 27 22:18:09 2025 -0500 Working on migration commit a4704dfd44b10647257c7c7054c0dae806d315bb Author: Owen Schwartz <owen@txv.io> Date: Mon Jan 27 21:40:52 2025 -0500 Add flag to allow raw resources commit d74f7a57ed11e2a6bf1a7e0c28c29fb07eb573a0 Merge: 6817788 d791b9b Author: Owen Schwartz <owen@txv.io> Date: Mon Jan 27 21:28:50 2025 -0500 Merge branch 'tcp-udp-traffic' of https://github.com/fosrl/pangolin into tcp-udp-traffic commit 68177882781b54ef30b62cca7dee8bbed7c5a2fa Author: Owen Schwartz <owen@txv.io> Date: Mon Jan 27 21:28:32 2025 -0500 Get everything working commit d791b9b47f9f6ca050d6edfd1d674438f8562d99 Author: Milo Schwartz <mschwartz10612@gmail.com> Date: Mon Jan 27 17:46:19 2025 -0500 fix orgId check in verifyAdmin commit 6ac30afd7a449a126190d311bd98d7f1048f73a4 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 23:19:33 2025 -0500 Trying to figure out traefik... commit 9886b42272882f8bb6baff2efdbe26cee7cac2b6 Merge: 786e67e 85e9129 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 21:53:32 2025 -0500 Merge branch 'tcp-udp-traffic' of https://github.com/fosrl/pangolin into tcp-udp-traffic commit 786e67eadd6df1ee8df24e77aed20c1f1fc9ca67 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 21:51:37 2025 -0500 Bug fixing commit 85e9129ae313b2e4a460a8bc53a0af9f9fbbafb2 Author: Milo Schwartz <mschwartz10612@gmail.com> Date: Sun Jan 26 18:35:24 2025 -0500 rethrow errors in migration and remove permanent redirect commit bd82699505fc7510c27f72cd80ea0ce815d8c5ef Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 17:49:12 2025 -0500 Fix merge issue commit 933dbf3a02b1f19fd1f627410b2407fdf05cd9bf Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 17:46:13 2025 -0500 Add sql to update resources and targets commit f19437bad847c8dbf57fddd2c48cd17bab20ddb0 Merge: 58980eb 9f1f291 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 17:19:51 2025 -0500 Merge branch 'dev' into tcp-udp-traffic commit 58980ebb64d1040b4d224c76beb38c2254f3c5d9 Merge: 1de682a d284d36 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 17:10:09 2025 -0500 Merge branch 'dev' into tcp-udp-traffic commit 1de682a9f6039f40e05c8901c7381a94b0d018ed Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 17:08:29 2025 -0500 Working on migrations commit dc853d2bc02b11997be5c3c7ea789402716fb4c2 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 16:56:49 2025 -0500 Finish config of resource pages commit 37c681c08d7ab73d2cad41e7ef1dbe3a8852e1f2 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 16:07:25 2025 -0500 Finish up table commit 461c6650bbea0d7439cc042971ec13fdb52a7431 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 15:54:46 2025 -0500 Working toward having dual resource types commit f0894663627375e16ce6994370cb30b298efc2dc Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 22:31:25 2025 -0500 Add qutoes commit edc535b79b94c2e65b290cd90a69fe17d27245e9 Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 22:28:45 2025 -0500 Add readTimeout to allow long file uploads commit 194892fa14b505bd7c2b31873dc13d4b8996c0e1 Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 20:37:34 2025 -0500 Rework traefik config generation commit ad3f896b5333e4706d610c3198f29dcd67610365 Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 13:01:47 2025 -0500 Add proxy port to api commit ca6013b2ffda0924a696ec3141825a54a4e5297d Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 12:58:01 2025 -0500 Add migration commit 2258d76cb3a49d3db7f05f76d8b8a9f1c248b5e4 Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 12:55:02 2025 -0500 Add new proxy port
2025-01-28 22:26:45 -05:00
})
2025-04-23 20:58:53 -04:00
]);
2024-11-11 00:00:16 -05:00
2025-04-23 20:58:53 -04:00
const tcpUdpResourceFormSchema = z.object({
protocol: z.string(),
proxyPort: z.number().int().min(1).max(65535)
});
2024-11-11 00:00:16 -05:00
2025-04-23 20:58:53 -04:00
type BaseResourceFormValues = z.infer<typeof baseResourceFormSchema>;
type HttpResourceFormValues = z.infer<typeof httpResourceFormSchema>;
type TcpUdpResourceFormValues = z.infer<typeof tcpUdpResourceFormSchema>;
2025-04-23 20:58:53 -04:00
type ResourceType = "http" | "raw";
2024-11-11 00:00:16 -05:00
2025-04-23 20:58:53 -04:00
interface ResourceTypeOption {
id: ResourceType;
title: string;
description: string;
disabled?: boolean;
}
2024-11-11 00:00:16 -05:00
2025-04-23 20:58:53 -04:00
export default function Page() {
Squashed commit of the following: commit c276d2193da5dbe7af5197bdf7e2bcce6f87b0cf Author: Owen Schwartz <owen@txv.io> Date: Tue Jan 28 22:06:04 2025 -0500 Okay actually now commit 9afdc0aadc3f4fb4e811930bacff70a9e17eab9f Author: Owen Schwartz <owen@txv.io> Date: Tue Jan 28 21:58:44 2025 -0500 Migrations working finally commit a7336b3b2466fe74d650b9c253ecadbe1eff749d Merge: e7c7203 fdb1ab4 Author: Owen Schwartz <owen@txv.io> Date: Mon Jan 27 22:19:15 2025 -0500 Merge branch 'dev' into tcp-udp-traffic commit e7c7203330b1b08e570048b10ef314b55068e466 Author: Owen Schwartz <owen@txv.io> Date: Mon Jan 27 22:18:09 2025 -0500 Working on migration commit a4704dfd44b10647257c7c7054c0dae806d315bb Author: Owen Schwartz <owen@txv.io> Date: Mon Jan 27 21:40:52 2025 -0500 Add flag to allow raw resources commit d74f7a57ed11e2a6bf1a7e0c28c29fb07eb573a0 Merge: 6817788 d791b9b Author: Owen Schwartz <owen@txv.io> Date: Mon Jan 27 21:28:50 2025 -0500 Merge branch 'tcp-udp-traffic' of https://github.com/fosrl/pangolin into tcp-udp-traffic commit 68177882781b54ef30b62cca7dee8bbed7c5a2fa Author: Owen Schwartz <owen@txv.io> Date: Mon Jan 27 21:28:32 2025 -0500 Get everything working commit d791b9b47f9f6ca050d6edfd1d674438f8562d99 Author: Milo Schwartz <mschwartz10612@gmail.com> Date: Mon Jan 27 17:46:19 2025 -0500 fix orgId check in verifyAdmin commit 6ac30afd7a449a126190d311bd98d7f1048f73a4 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 23:19:33 2025 -0500 Trying to figure out traefik... commit 9886b42272882f8bb6baff2efdbe26cee7cac2b6 Merge: 786e67e 85e9129 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 21:53:32 2025 -0500 Merge branch 'tcp-udp-traffic' of https://github.com/fosrl/pangolin into tcp-udp-traffic commit 786e67eadd6df1ee8df24e77aed20c1f1fc9ca67 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 21:51:37 2025 -0500 Bug fixing commit 85e9129ae313b2e4a460a8bc53a0af9f9fbbafb2 Author: Milo Schwartz <mschwartz10612@gmail.com> Date: Sun Jan 26 18:35:24 2025 -0500 rethrow errors in migration and remove permanent redirect commit bd82699505fc7510c27f72cd80ea0ce815d8c5ef Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 17:49:12 2025 -0500 Fix merge issue commit 933dbf3a02b1f19fd1f627410b2407fdf05cd9bf Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 17:46:13 2025 -0500 Add sql to update resources and targets commit f19437bad847c8dbf57fddd2c48cd17bab20ddb0 Merge: 58980eb 9f1f291 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 17:19:51 2025 -0500 Merge branch 'dev' into tcp-udp-traffic commit 58980ebb64d1040b4d224c76beb38c2254f3c5d9 Merge: 1de682a d284d36 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 17:10:09 2025 -0500 Merge branch 'dev' into tcp-udp-traffic commit 1de682a9f6039f40e05c8901c7381a94b0d018ed Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 17:08:29 2025 -0500 Working on migrations commit dc853d2bc02b11997be5c3c7ea789402716fb4c2 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 16:56:49 2025 -0500 Finish config of resource pages commit 37c681c08d7ab73d2cad41e7ef1dbe3a8852e1f2 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 16:07:25 2025 -0500 Finish up table commit 461c6650bbea0d7439cc042971ec13fdb52a7431 Author: Owen Schwartz <owen@txv.io> Date: Sun Jan 26 15:54:46 2025 -0500 Working toward having dual resource types commit f0894663627375e16ce6994370cb30b298efc2dc Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 22:31:25 2025 -0500 Add qutoes commit edc535b79b94c2e65b290cd90a69fe17d27245e9 Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 22:28:45 2025 -0500 Add readTimeout to allow long file uploads commit 194892fa14b505bd7c2b31873dc13d4b8996c0e1 Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 20:37:34 2025 -0500 Rework traefik config generation commit ad3f896b5333e4706d610c3198f29dcd67610365 Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 13:01:47 2025 -0500 Add proxy port to api commit ca6013b2ffda0924a696ec3141825a54a4e5297d Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 12:58:01 2025 -0500 Add migration commit 2258d76cb3a49d3db7f05f76d8b8a9f1c248b5e4 Author: Owen Schwartz <owen@txv.io> Date: Sat Jan 25 12:55:02 2025 -0500 Add new proxy port
2025-01-28 22:26:45 -05:00
const { env } = useEnvContext();
2025-04-23 20:58:53 -04:00
const api = createApiClient({ env });
const { orgId } = useParams();
const router = useRouter();
2025-05-04 15:11:42 +00:00
const t = useTranslations();
2025-04-23 20:58:53 -04:00
const [loadingPage, setLoadingPage] = useState(true);
2024-11-11 00:00:16 -05:00
const [sites, setSites] = useState<ListSitesResponse["sites"]>([]);
2025-02-18 22:56:46 -05:00
const [baseDomains, setBaseDomains] = useState<
{ domainId: string; baseDomain: string }[]
>([]);
2025-04-23 20:58:53 -04:00
const [createLoading, setCreateLoading] = useState(false);
2025-04-28 21:45:43 -04:00
const [showSnippets, setShowSnippets] = useState(false);
const [resourceId, setResourceId] = useState<number | null>(null);
2025-01-30 22:31:29 -05:00
2025-04-23 20:58:53 -04:00
const resourceTypes: ReadonlyArray<ResourceTypeOption> = [
{
id: "http",
2025-06-17 16:53:57 -04:00
title: t("resourceHTTP"),
description: t("resourceHTTPDescription")
2025-04-23 20:58:53 -04:00
},
2025-06-17 17:02:55 -04:00
...(!env.flags.allowRawResources
2025-06-17 16:53:57 -04:00
? []
: [
{
id: "raw" as ResourceType,
title: t("resourceRaw"),
description: t("resourceRawDescription")
}
])
2025-04-23 20:58:53 -04:00
];
const baseForm = useForm<BaseResourceFormValues>({
resolver: zodResolver(baseResourceFormSchema),
defaultValues: {
name: "",
2025-04-23 20:58:53 -04:00
http: true
}
2024-11-11 00:00:16 -05:00
});
2025-04-23 20:58:53 -04:00
const httpForm = useForm<HttpResourceFormValues>({
resolver: zodResolver(httpResourceFormSchema),
defaultValues: {
subdomain: "",
domainId: "",
isBaseDomain: false
2024-11-11 00:00:16 -05:00
}
2025-04-23 20:58:53 -04:00
});
2024-11-11 00:00:16 -05:00
2025-04-23 20:58:53 -04:00
const tcpUdpForm = useForm<TcpUdpResourceFormValues>({
resolver: zodResolver(tcpUdpResourceFormSchema),
defaultValues: {
protocol: "tcp",
proxyPort: undefined
}
});
2025-04-23 20:58:53 -04:00
async function onSubmit() {
setCreateLoading(true);
const baseData = baseForm.getValues();
const isHttp = baseData.http;
try {
const payload = {
name: baseData.name,
siteId: baseData.siteId,
http: baseData.http
};
if (isHttp) {
const httpData = httpForm.getValues();
if (httpData.isBaseDomain) {
Object.assign(payload, {
domainId: httpData.domainId,
2025-05-03 12:21:43 -04:00
isBaseDomain: true,
protocol: "tcp"
2025-04-23 20:58:53 -04:00
});
} else {
Object.assign(payload, {
subdomain: httpData.subdomain,
domainId: httpData.domainId,
2025-05-03 12:21:43 -04:00
isBaseDomain: false,
protocol: "tcp"
2025-02-18 22:56:46 -05:00
});
}
2025-04-23 20:58:53 -04:00
} else {
const tcpUdpData = tcpUdpForm.getValues();
Object.assign(payload, {
protocol: tcpUdpData.protocol,
proxyPort: tcpUdpData.proxyPort
});
2025-02-18 22:56:46 -05:00
}
const res = await api
2025-04-23 20:58:53 -04:00
.put<
AxiosResponse<Resource>
>(`/org/${orgId}/site/${baseData.siteId}/resource/`, payload)
2025-02-18 22:56:46 -05:00
.catch((e) => {
toast({
variant: "destructive",
2025-06-17 16:53:57 -04:00
title: t("resourceErrorCreate"),
2025-02-18 22:56:46 -05:00
description: formatAxiosError(
e,
2025-06-17 16:53:57 -04:00
t("resourceErrorCreateDescription")
2025-02-18 22:56:46 -05:00
)
});
});
2025-04-23 20:58:53 -04:00
if (res && res.status === 201) {
const id = res.data.data.resourceId;
2025-04-28 21:45:43 -04:00
setResourceId(id);
if (isHttp) {
router.push(`/${orgId}/settings/resources/${id}`);
} else {
setShowSnippets(true);
router.refresh();
}
}
2025-04-23 20:58:53 -04:00
} catch (e) {
2025-06-17 16:53:57 -04:00
console.error(t("resourceErrorCreateMessage"), e);
2025-04-23 20:58:53 -04:00
toast({
variant: "destructive",
2025-06-17 16:53:57 -04:00
title: t("resourceErrorCreate"),
description: t("resourceErrorCreateMessageDescription")
2025-04-23 20:58:53 -04:00
});
}
2024-11-11 00:00:16 -05:00
2025-04-23 20:58:53 -04:00
setCreateLoading(false);
}
useEffect(() => {
2025-03-01 17:45:38 -05:00
const load = async () => {
setLoadingPage(true);
2025-04-23 20:58:53 -04:00
const fetchSites = async () => {
const res = await api
.get<
AxiosResponse<ListSitesResponse>
>(`/org/${orgId}/sites/`)
.catch((e) => {
toast({
variant: "destructive",
2025-06-17 16:53:57 -04:00
title: t("sitesErrorFetch"),
2025-04-23 20:58:53 -04:00
description: formatAxiosError(
e,
2025-06-17 16:53:57 -04:00
t("sitesErrorFetchDescription")
2025-04-23 20:58:53 -04:00
)
});
});
2025-03-01 17:45:38 -05:00
2025-04-23 20:58:53 -04:00
if (res?.status === 200) {
setSites(res.data.data.sites);
2025-03-01 17:45:38 -05:00
2025-04-23 20:58:53 -04:00
if (res.data.data.sites.length > 0) {
baseForm.setValue(
"siteId",
res.data.data.sites[0].siteId
);
}
}
2025-04-23 20:58:53 -04:00
};
const fetchDomains = async () => {
const res = await api
.get<
AxiosResponse<ListDomainsResponse>
>(`/org/${orgId}/domains/`)
.catch((e) => {
toast({
variant: "destructive",
2025-06-17 16:53:57 -04:00
title: t("domainsErrorFetch"),
2025-04-23 20:58:53 -04:00
description: formatAxiosError(
e,
2025-06-17 16:53:57 -04:00
t("domainsErrorFetchDescription")
2025-04-23 20:58:53 -04:00
)
});
});
2024-11-11 00:00:16 -05:00
2025-04-23 20:58:53 -04:00
if (res?.status === 200) {
const domains = res.data.data.domains;
setBaseDomains(domains);
if (domains.length) {
httpForm.setValue("domainId", domains[0].domainId);
}
}
};
2025-01-30 22:31:29 -05:00
2025-04-23 20:58:53 -04:00
await fetchSites();
await fetchDomains();
2024-11-11 00:00:16 -05:00
2025-04-23 20:58:53 -04:00
setLoadingPage(false);
};
2025-01-30 22:31:29 -05:00
2025-04-23 20:58:53 -04:00
load();
}, []);
2024-11-11 00:00:16 -05:00
return (
<>
2025-04-23 20:58:53 -04:00
<div className="flex justify-between">
<HeaderTitle
2025-06-17 16:53:57 -04:00
title={t("resourceCreate")}
description={t("resourceCreateDescription")}
2025-04-23 20:58:53 -04:00
/>
<Button
variant="outline"
onClick={() => {
router.push(`/${orgId}/settings/resources`);
}}
>
2025-06-17 16:53:57 -04:00
{t("resourceSeeAll")}
2025-04-23 20:58:53 -04:00
</Button>
</div>
{!loadingPage && (
<div>
2025-04-28 21:45:43 -04:00
{!showSnippets ? (
<SettingsContainer>
<SettingsSection>
<SettingsSectionHeader>
<SettingsSectionTitle>
2025-06-17 16:53:57 -04:00
{t("resourceInfo")}
2025-04-28 21:45:43 -04:00
</SettingsSectionTitle>
</SettingsSectionHeader>
<SettingsSectionBody>
<SettingsSectionForm>
<Form {...baseForm}>
<form
className="space-y-4"
id="base-resource-form"
>
<FormField
control={baseForm.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>
2025-06-17 16:53:57 -04:00
{t("name")}
2025-04-28 21:45:43 -04:00
</FormLabel>
<FormControl>
<Input
{...field}
/>
</FormControl>
<FormMessage />
<FormDescription>
2025-06-17 16:53:57 -04:00
{t(
"resourceNameDescription"
)}
2025-04-28 21:45:43 -04:00
</FormDescription>
</FormItem>
)}
/>
2025-03-01 17:45:38 -05:00
2025-04-28 21:45:43 -04:00
<FormField
control={baseForm.control}
name="siteId"
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel>
2025-06-17 16:53:57 -04:00
{t("site")}
2025-04-28 21:45:43 -04:00
</FormLabel>
<Popover>
<PopoverTrigger
asChild
>
<FormControl>
<Button
variant="outline"
role="combobox"
className={cn(
"justify-between",
!field.value &&
"text-muted-foreground"
)}
>
{field.value
? sites.find(
(
site
) =>
site.siteId ===
field.value
)
?.name
2025-06-17 16:53:57 -04:00
: t(
"siteSelect"
)}
2025-04-28 21:45:43 -04:00
<CaretSortIcon className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="p-0">
<Command>
2025-06-17 16:53:57 -04:00
<CommandInput
placeholder={t(
"siteSearch"
)}
/>
2025-04-28 21:45:43 -04:00
<CommandList>
<CommandEmpty>
2025-06-17 16:53:57 -04:00
{t(
"siteNotFound"
)}
2025-04-28 21:45:43 -04:00
</CommandEmpty>
<CommandGroup>
{sites.map(
(
site
) => (
<CommandItem
value={`${site.siteId}:${site.name}:${site.niceId}`}
key={
site.siteId
}
onSelect={() => {
baseForm.setValue(
"siteId",
site.siteId
);
}}
>
<CheckIcon
className={cn(
"mr-2 h-4 w-4",
site.siteId ===
field.value
? "opacity-100"
: "opacity-0"
)}
/>
{
site.name
}
</CommandItem>
)
)}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
<FormMessage />
<FormDescription>
2025-06-17 16:53:57 -04:00
{t(
"siteSelectionDescription"
)}
2025-04-28 21:45:43 -04:00
</FormDescription>
</FormItem>
)}
/>
</form>
</Form>
</SettingsSectionForm>
</SettingsSectionBody>
</SettingsSection>
2025-06-30 09:33:48 -07:00
{resourceTypes.length > 1 && (
<SettingsSection>
<SettingsSectionHeader>
<SettingsSectionTitle>
{t("resourceType")}
</SettingsSectionTitle>
<SettingsSectionDescription>
{t("resourceTypeDescription")}
</SettingsSectionDescription>
</SettingsSectionHeader>
<SettingsSectionBody>
<StrategySelect
options={resourceTypes}
defaultValue="http"
onChange={(value) => {
baseForm.setValue(
"http",
value === "http"
);
}}
cols={2}
/>
</SettingsSectionBody>
</SettingsSection>
)}
2025-04-28 21:45:43 -04:00
{baseForm.watch("http") ? (
<SettingsSection>
<SettingsSectionHeader>
<SettingsSectionTitle>
2025-06-17 16:53:57 -04:00
{t("resourceHTTPSSettings")}
2025-04-28 21:45:43 -04:00
</SettingsSectionTitle>
<SettingsSectionDescription>
2025-06-17 16:53:57 -04:00
{t(
"resourceHTTPSSettingsDescription"
)}
2025-04-28 21:45:43 -04:00
</SettingsSectionDescription>
</SettingsSectionHeader>
<SettingsSectionBody>
<SettingsSectionForm>
<Form {...httpForm}>
<form
className="space-y-4"
id="http-settings-form"
>
{env.flags
.allowBaseDomainResources && (
<FormField
control={
httpForm.control
}
name="isBaseDomain"
render={({
field
}) => (
<FormItem>
<FormLabel>
2025-06-17 16:53:57 -04:00
{t(
"domainType"
)}
2025-04-28 21:45:43 -04:00
</FormLabel>
<Select
value={
field.value
? "basedomain"
: "subdomain"
}
onValueChange={(
value
) => {
field.onChange(
value ===
"basedomain"
);
}}
>
<FormControl>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="subdomain">
2025-06-17 16:53:57 -04:00
{t(
"subdomain"
)}
2025-04-28 21:45:43 -04:00
</SelectItem>
<SelectItem value="basedomain">
2025-06-17 16:53:57 -04:00
{t(
"baseDomain"
)}
2025-04-28 21:45:43 -04:00
</SelectItem>
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
)}
{!httpForm.watch(
"isBaseDomain"
) && (
<FormItem>
<FormLabel>
2025-06-17 16:53:57 -04:00
{t("subdomain")}
2025-04-28 21:45:43 -04:00
</FormLabel>
<div className="flex space-x-0">
<div className="w-1/2">
<FormField
control={
httpForm.control
}
name="subdomain"
render={({
field
}) => (
<FormItem>
<FormControl>
<Input
{...field}
className="border-r-0 rounded-r-none"
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<div className="w-1/2">
<FormField
control={
httpForm.control
}
name="domainId"
render={({
field
}) => (
<FormItem>
<Select
onValueChange={
field.onChange
}
value={
field.value
}
defaultValue={
field.value
}
>
<FormControl>
<SelectTrigger className="rounded-l-none">
<SelectValue />
</SelectTrigger>
</FormControl>
<SelectContent>
{baseDomains.map(
(
option
) => (
<SelectItem
key={
option.domainId
}
value={
option.domainId
}
>
.
{
option.baseDomain
}
</SelectItem>
)
)}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
2025-04-28 21:45:43 -04:00
/>
</div>
</div>
<FormDescription>
2025-06-17 16:53:57 -04:00
{t(
"subdomnainDescription"
)}
2025-04-28 21:45:43 -04:00
</FormDescription>
</FormItem>
)}
{httpForm.watch(
"isBaseDomain"
) && (
<FormField
control={
httpForm.control
}
name="domainId"
render={({
field
}) => (
<FormItem>
<FormLabel>
2025-06-17 16:53:57 -04:00
{t(
"baseDomain"
)}
2025-04-28 21:45:43 -04:00
</FormLabel>
<Select
onValueChange={
field.onChange
}
defaultValue={
field.value
}
{...field}
>
2025-04-28 21:45:43 -04:00
<FormControl>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
</FormControl>
<SelectContent>
{baseDomains.map(
(
2025-04-28 21:45:43 -04:00
option
) => (
2025-04-28 21:45:43 -04:00
<SelectItem
key={
2025-04-28 21:45:43 -04:00
option.domainId
}
value={
option.domainId
}
>
{
2025-04-28 21:45:43 -04:00
option.baseDomain
}
2025-04-28 21:45:43 -04:00
</SelectItem>
)
)}
2025-04-28 21:45:43 -04:00
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
)}
</form>
</Form>
</SettingsSectionForm>
</SettingsSectionBody>
</SettingsSection>
2025-06-17 16:53:57 -04:00
) : (
2025-04-28 21:45:43 -04:00
<SettingsSection>
<SettingsSectionHeader>
<SettingsSectionTitle>
2025-06-17 16:53:57 -04:00
{t("resourceRawSettings")}
2025-04-28 21:45:43 -04:00
</SettingsSectionTitle>
<SettingsSectionDescription>
2025-06-17 16:53:57 -04:00
{t(
"resourceRawSettingsDescription"
)}
2025-04-28 21:45:43 -04:00
</SettingsSectionDescription>
</SettingsSectionHeader>
<SettingsSectionBody>
<SettingsSectionForm>
<Form {...tcpUdpForm}>
<form
className="space-y-4"
id="tcp-udp-settings-form"
>
<Controller
2025-04-23 20:58:53 -04:00
control={
2025-04-28 21:45:43 -04:00
tcpUdpForm.control
2025-04-23 20:58:53 -04:00
}
2025-04-28 21:45:43 -04:00
name="protocol"
2025-03-01 17:45:38 -05:00
render={({ field }) => (
<FormItem>
<FormLabel>
2025-06-17 16:53:57 -04:00
{t(
"protocol"
)}
2025-03-01 17:45:38 -05:00
</FormLabel>
<Select
2025-04-28 21:45:43 -04:00
onValueChange={
field.onChange
2025-03-01 17:45:38 -05:00
}
2025-04-28 21:45:43 -04:00
{...field}
2025-03-01 17:45:38 -05:00
>
<FormControl>
<SelectTrigger>
2025-06-17 16:53:57 -04:00
<SelectValue
placeholder={t(
"protocolSelect"
)}
/>
2025-03-01 17:45:38 -05:00
</SelectTrigger>
</FormControl>
<SelectContent>
2025-04-28 21:45:43 -04:00
<SelectItem value="tcp">
TCP
2025-03-01 17:45:38 -05:00
</SelectItem>
2025-04-28 21:45:43 -04:00
<SelectItem value="udp">
UDP
2025-03-01 17:45:38 -05:00
</SelectItem>
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
2025-04-28 21:45:43 -04:00
2025-03-01 17:45:38 -05:00
<FormField
2025-04-23 20:58:53 -04:00
control={
2025-04-28 21:45:43 -04:00
tcpUdpForm.control
2025-04-23 20:58:53 -04:00
}
2025-04-28 21:45:43 -04:00
name="proxyPort"
2025-03-01 17:45:38 -05:00
render={({ field }) => (
<FormItem>
<FormLabel>
2025-06-17 16:53:57 -04:00
{t(
"resourcePortNumber"
)}
2025-03-01 17:45:38 -05:00
</FormLabel>
2025-04-28 21:45:43 -04:00
<FormControl>
<Input
type="number"
value={
field.value ??
""
}
onChange={(
e
) =>
field.onChange(
e
.target
.value
? parseInt(
e
.target
.value
)
: undefined
2025-04-23 20:58:53 -04:00
)
2025-04-28 21:45:43 -04:00
}
/>
</FormControl>
2025-03-01 17:45:38 -05:00
<FormMessage />
2025-04-28 21:45:43 -04:00
<FormDescription>
2025-06-17 16:53:57 -04:00
{t(
"resourcePortNumberDescription"
)}
2025-04-28 21:45:43 -04:00
</FormDescription>
2025-03-01 17:45:38 -05:00
</FormItem>
)}
/>
2025-04-28 21:45:43 -04:00
</form>
</Form>
</SettingsSectionForm>
</SettingsSectionBody>
</SettingsSection>
2025-06-17 16:53:57 -04:00
)}
2025-04-28 21:45:43 -04:00
<div className="flex justify-end space-x-2 mt-8">
<Button
type="button"
variant="outline"
onClick={() =>
2025-06-17 16:53:57 -04:00
router.push(
`/${orgId}/settings/resources`
)
2025-04-28 21:45:43 -04:00
}
>
2025-06-17 16:53:57 -04:00
{t("cancel")}
2025-04-28 21:45:43 -04:00
</Button>
<Button
type="button"
onClick={async () => {
const isHttp = baseForm.watch("http");
2025-06-17 16:53:57 -04:00
const baseValid =
await baseForm.trigger();
2025-04-28 21:45:43 -04:00
const settingsValid = isHttp
? await httpForm.trigger()
: await tcpUdpForm.trigger();
if (baseValid && settingsValid) {
onSubmit();
}
}}
loading={createLoading}
>
2025-06-17 16:53:57 -04:00
{t("resourceCreate")}
2025-04-28 21:45:43 -04:00
</Button>
</div>
</SettingsContainer>
) : (
<SettingsContainer>
2025-04-23 20:58:53 -04:00
<SettingsSection>
<SettingsSectionHeader>
<SettingsSectionTitle>
2025-06-17 16:53:57 -04:00
{t("resourceConfig")}
2025-04-23 20:58:53 -04:00
</SettingsSectionTitle>
<SettingsSectionDescription>
2025-06-17 16:53:57 -04:00
{t("resourceConfigDescription")}
2025-04-23 20:58:53 -04:00
</SettingsSectionDescription>
</SettingsSectionHeader>
<SettingsSectionBody>
2025-04-28 21:45:43 -04:00
<div className="space-y-6">
<div className="space-y-4">
<h3 className="text-lg font-semibold">
2025-06-17 16:53:57 -04:00
{t("resourceAddEntrypoints")}
2025-04-28 21:45:43 -04:00
</h3>
<CopyTextBox
text={`entryPoints:
${tcpUdpForm.getValues("protocol")}-${tcpUdpForm.getValues("proxyPort")}:
address: ":${tcpUdpForm.getValues("proxyPort")}/${tcpUdpForm.getValues("protocol")}"`}
wrapText={false}
/>
</div>
<div className="space-y-4">
<h3 className="text-lg font-semibold">
2025-06-17 16:53:57 -04:00
{t("resourceExposePorts")}
2025-04-28 21:45:43 -04:00
</h3>
<CopyTextBox
text={`ports:
- ${tcpUdpForm.getValues("proxyPort")}:${tcpUdpForm.getValues("proxyPort")}${tcpUdpForm.getValues("protocol") === "tcp" ? "" : "/" + tcpUdpForm.getValues("protocol")}`}
wrapText={false}
/>
</div>
2025-04-28 21:45:43 -04:00
<Link
className="text-sm text-primary flex items-center gap-1"
href="https://docs.fossorial.io/Pangolin/tcp-udp"
target="_blank"
rel="noopener noreferrer"
>
2025-06-17 16:53:57 -04:00
<span>{t("resourceLearnRaw")}</span>
2025-04-28 21:45:43 -04:00
<SquareArrowOutUpRight size={14} />
</Link>
</div>
2025-04-23 20:58:53 -04:00
</SettingsSectionBody>
</SettingsSection>
2025-04-28 21:45:43 -04:00
<div className="flex justify-end space-x-2 mt-8">
<Button
type="button"
variant="outline"
onClick={() =>
2025-06-17 16:53:57 -04:00
router.push(
`/${orgId}/settings/resources`
)
2025-04-28 21:45:43 -04:00
}
>
2025-06-17 16:53:57 -04:00
{t("resourceBack")}
2025-04-28 21:45:43 -04:00
</Button>
<Button
type="button"
onClick={() =>
router.push(
`/${orgId}/settings/resources/${resourceId}`
)
}
>
2025-06-17 16:53:57 -04:00
{t("resourceGoTo")}
2025-04-28 21:45:43 -04:00
</Button>
</div>
</SettingsContainer>
)}
2025-04-23 20:58:53 -04:00
</div>
)}
2024-11-11 00:00:16 -05:00
</>
);
}