From 416dd089be1f6726db445e22f05c19fdc7364fe2 Mon Sep 17 00:00:00 2001 From: Curetix Date: Mon, 1 Apr 2024 17:30:28 +0200 Subject: [PATCH] Some fixes for hostname handling --- src/components/alias-create-modal.tsx | 157 +++++++++++++------------- src/popup.tsx | 4 +- 2 files changed, 84 insertions(+), 77 deletions(-) diff --git a/src/components/alias-create-modal.tsx b/src/components/alias-create-modal.tsx index a59b389..d124a87 100644 --- a/src/components/alias-create-modal.tsx +++ b/src/components/alias-create-modal.tsx @@ -19,7 +19,8 @@ import { useQuery } from "@tanstack/react-query"; import { useAtom } from "jotai"; import { useEffect, useMemo, useState } from "react"; -import { emailRuleNamePrefix } from "~const"; +import type { ComboboxData } from "@mantine/core/lib/components/Combobox/Combobox.types"; +import { emailRuleNamePrefix, isExtension } from "~const"; import { useI18nContext } from "~i18n/i18n-react"; import { handleResponse, useCloudflare } from "~lib/cloudflare/use-cloudflare"; import { useFullscreenModal } from "~utils"; @@ -96,6 +97,7 @@ export default function AliasCreateModal({ opened, onClose }: Props) { }, }); + // Load email rules for currently selected zone, to check if a generated alias already exists const emailRules = useQuery({ queryKey: ["emailRules", aliasCreateForm.values.zoneId], queryFn: async ({ queryKey: [, zoneId] }) => { @@ -122,7 +124,7 @@ export default function AliasCreateModal({ opened, onClose }: Props) { }); } - if (hostname && hostname !== "newtab") { + if (hostname) { aliasCreateForm.setValues({ description: hostname, }); @@ -275,6 +277,72 @@ export default function AliasCreateModal({ opened, onClose }: Props) { ); } + const aliasFormatData = useMemo(() => { + const domainOption = isExtension + ? [ + { + value: "domain", + label: LL.ALIAS_FORMAT_DOMAIN(), + disabled: !hostname, + }, + ] + : []; + + return [ + { + value: "characters", + label: LL.ALIAS_FORMAT_CHARS(), + }, + { + value: "words", + label: LL.ALIAS_FORMAT_WORDS(), + }, + ...domainOption, + { + value: "custom", + label: LL.ALIAS_FORMAT_CUSTOM(), + }, + ]; + }, [LL, hostname]); + + const prefixFormatData = useMemo(() => { + const domainOptions = isExtension + ? [ + { + value: "domainWithoutExtension", + label: LL.PREFIX_DOMAIN_WITHOUT_EXTENSION(), + disabled: !hostname, + }, + { + value: "domainWithExtension", + label: LL.PREFIX_DOMAIN_WITH_EXTENSION(), + disabled: !hostname, + }, + { + value: "fullDomain", + label: LL.PREFIX_FULL_DOMAIN(), + disabled: !hostname, + }, + ] + : []; + + if (isExtension && aliasCreateForm.values.format === "domain") { + return domainOptions; + } + + return [ + { + value: "none", + label: LL.PREFIX_NONE(), + }, + ...domainOptions, + { + value: "custom", + label: LL.PREFIX_CUSTOM(), + }, + ]; + }, [LL, hostname, aliasCreateForm.values.format]); + return ( - )} - - {aliasCreateForm.values.format === "domain" && ( + {aliasCreateForm.values.format !== "custom" && (