Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

create primary domain modal

Steven Tey4aeaeea36358
diff --git a/apps/web/ui/domains/domain-card.tsx b/apps/web/ui/domains/domain-card.tsx
index 096111d81c..a529505c3c 100644
--- a/apps/web/ui/domains/domain-card.tsx
+++ b/apps/web/ui/domains/domain-card.tsx
@@ -29,6 +29,7 @@ import { useRef, useState } from "react";
import { useDeleteDomainModal } from "../modals/delete-domain-modal";
import { useArchiveDomainModal } from "../modals/archive-domain-modal";
import { toast } from "sonner";
+import { usePrimaryDomainModal } from "../modals/primary-domain-modal";
export default function DomainCard({ props }: { props: DomainProps }) {
const { id: workspaceId, slug } = useWorkspace();
@@ -82,6 +83,11 @@ export default function DomainCard({ props }: { props: DomainProps }) {
props,
});
+ const { setShowPrimaryDomainModal, PrimaryDomainModal } =
+ usePrimaryDomainModal({
+ props,
+ });
+
const { setShowArchiveDomainModal, ArchiveDomainModal } =
useArchiveDomainModal({
props,
@@ -91,25 +97,11 @@ export default function DomainCard({ props }: { props: DomainProps }) {
props,
});
- const setPrimary = async () => {
- const response = await fetch(
- `/api/domains/${domain}/primary?workspaceId=${workspaceId}`,
- {
- method: "POST",
- },
- );
- if (response.ok) {
- await mutate(`/api/domains?workspaceId=${workspaceId}`);
- } else {
- const { error } = await response.json();
- throw new Error(error.message);
- }
- };
-
return (
<>
<AddEditDomainModal />
<LinkQRModal />
+ <PrimaryDomainModal />
<ArchiveDomainModal />
<DeleteDomainModal />
<div
@@ -189,20 +181,8 @@ export default function DomainCard({ props }: { props: DomainProps }) {
text="Set as Primary"
variant="outline"
onClick={() => {
- if (
- window.confirm(
- "Setting this domain as primary will make it the default domain in the link creation modal, as well as via the API. Are you sure you want to proceed?",
- )
- ) {
- setOpenPopover(false);
- toast.promise(setPrimary(), {
- loading: `Setting ${domain} as the primary domain...`,
- success: `Successfully set ${domain} as the primary domain!`,
- error: (error) => {
- return error.message;
- },
- });
- }
+ setOpenPopover(false);
+ setShowPrimaryDomainModal(true);
}}
icon={<FileCog className="h-4 w-4" />}
className="h-9 justify-start px-2 font-medium"
diff --git a/apps/web/ui/modals/add-workspace-modal.tsx b/apps/web/ui/modals/add-workspace-modal.tsx
index 3fb9846e9c..a993844024 100644
--- a/apps/web/ui/modals/add-workspace-modal.tsx
+++ b/apps/web/ui/modals/add-workspace-modal.tsx
@@ -114,7 +114,6 @@ function AddWorkspaceModalHelper({
<a
href={`${HOME_DOMAIN}/help/article/what-is-a-workspace`}
target="_blank"
- rel="noopener noreferrer"
className="-translate-y-2 text-center text-xs text-gray-500 underline underline-offset-4 hover:text-gray-800"
>
What is a workspace?
diff --git a/apps/web/ui/modals/archive-domain-modal.tsx b/apps/web/ui/modals/archive-domain-modal.tsx
index 8bd90bbb72..ef659e4a0a 100644
--- a/apps/web/ui/modals/archive-domain-modal.tsx
+++ b/apps/web/ui/modals/archive-domain-modal.tsx
@@ -1,7 +1,6 @@
import useWorkspace from "@/lib/swr/use-workspace";
import { DomainProps } from "@/lib/types";
import { Button, Modal, useToastWithUndo } from "@dub/ui";
-import { getApexDomain } from "@dub/utils";
import {
Dispatch,
MouseEvent,
@@ -46,7 +45,6 @@ function ArchiveDomainModal({
const { id: workspaceId } = useWorkspace();
const [archiving, setArchiving] = useState(false);
const domain = props.slug;
- const apexDomain = getApexDomain(domain);
const handleArchiveRequest = async (event: MouseEvent<HTMLButtonElement>) => {
event.preventDefault();
@@ -99,7 +97,7 @@ function ArchiveDomainModal({
setShowModal={setShowArchiveDomainModal}
>
<div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 px-4 py-4 pt-8 text-center sm:px-16">
- <LinkLogo apexDomain={apexDomain} />
+ <LinkLogo apexDomain={domain} />
<h3 className="text-lg font-medium">
{props.archived ? "Unarchive" : "Archive"} {domain}
</h3>
diff --git a/apps/web/ui/modals/delete-domain-modal.tsx b/apps/web/ui/modals/delete-domain-modal.tsx
index abcc87381d..500fae35d5 100644
--- a/apps/web/ui/modals/delete-domain-modal.tsx
+++ b/apps/web/ui/modals/delete-domain-modal.tsx
@@ -1,7 +1,6 @@
import useWorkspace from "@/lib/swr/use-workspace";
import { DomainProps } from "@/lib/types";
import { Button, Modal, useMediaQuery } from "@dub/ui";
-import { getApexDomain } from "@dub/utils";
import {
Dispatch,
SetStateAction,
@@ -25,7 +24,6 @@ function DeleteDomainModal({
const { id } = useWorkspace();
const [deleting, setDeleting] = useState(false);
const domain = props.slug;
- const apexDomain = getApexDomain(domain);
const { isMobile } = useMediaQuery();
@@ -35,7 +33,7 @@ function DeleteDomainModal({
setShowModal={setShowDeleteDomainModal}
>
<div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 px-4 py-4 pt-8 text-center sm:px-16">
- <LinkLogo apexDomain={apexDomain} />
+ <LinkLogo apexDomain={domain} />
<h3 className="text-lg font-medium">Delete {domain}</h3>
<p className="text-sm text-gray-500">
Warning: Deleting this domain will delete all associated links as well
diff --git a/apps/web/ui/modals/primary-domain-modal.tsx b/apps/web/ui/modals/primary-domain-modal.tsx
new file mode 100644
index 0000000000..5f8df29cbc
--- /dev/null
+++ b/apps/web/ui/modals/primary-domain-modal.tsx
@@ -0,0 +1,108 @@
+import useWorkspace from "@/lib/swr/use-workspace";
+import { DomainProps } from "@/lib/types";
+import { Button, Modal } from "@dub/ui";
+import { HOME_DOMAIN } from "@dub/utils";
+import {
+ Dispatch,
+ SetStateAction,
+ useCallback,
+ useMemo,
+ useState,
+} from "react";
+import { toast } from "sonner";
+import { mutate } from "swr";
+import LinkLogo from "../links/link-logo";
+
+function PrimaryDomainModal({
+ showPrimaryDomainModal,
+ setShowPrimaryDomainModal,
+ props,
+}: {
+ showPrimaryDomainModal: boolean;
+ setShowPrimaryDomainModal: Dispatch<SetStateAction<boolean>>;
+ props: DomainProps;
+}) {
+ const { id: workspaceId } = useWorkspace();
+ const [loading, setLoading] = useState(false);
+ const domain = props.slug;
+
+ const setPrimary = async () => {
+ setLoading(true);
+ const response = await fetch(
+ `/api/domains/${domain}/primary?workspaceId=${workspaceId}`,
+ {
+ method: "POST",
+ },
+ );
+ if (response.ok) {
+ await mutate(`/api/domains?workspaceId=${workspaceId}`);
+ setLoading(false);
+ setShowPrimaryDomainModal(false);
+ } else {
+ const { error } = await response.json();
+ throw new Error(error.message);
+ }
+ };
+
+ return (
+ <Modal
+ showModal={showPrimaryDomainModal}
+ setShowModal={setShowPrimaryDomainModal}
+ >
+ <div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 px-4 py-4 pt-8 text-center sm:px-16">
+ <LinkLogo apexDomain={domain} />
+ <h3 className="text-lg font-medium">Set {domain} as primary domain</h3>
+ <p className="text-sm text-gray-500">
+ Setting this domain as primary will make it the default domain in the
+ link creation modal, as well as in the API. Proceed with caution.{" "}
+ <a
+ href={`${HOME_DOMAIN}/help/article/how-to-set-primary-domain`}
+ target="_blank"
+ className="text-gray-500 underline underline-offset-4 hover:text-gray-800"
+ >
+ Learn more
+ </a>
+ </p>
+ </div>
+
+ <div className="flex flex-col space-y-6 bg-gray-50 px-4 py-8 text-left sm:px-16">
+ <Button
+ onClick={() =>
+ toast.promise(setPrimary, {
+ loading: `Setting ${domain} as the primary domain...`,
+ success: `Successfully set ${domain} as the primary domain!`,
+ error: (error) => {
+ return error.message;
+ },
+ })
+ }
+ autoFocus
+ loading={loading}
+ text="Set as primary domain"
+ />
+ </div>
+ </Modal>
+ );
+}
+
+export function usePrimaryDomainModal({ props }: { props: DomainProps }) {
+ const [showPrimaryDomainModal, setShowPrimaryDomainModal] = useState(false);
+
+ const PrimaryDomainModalCallback = useCallback(() => {
+ return props ? (
+ <PrimaryDomainModal
+ showPrimaryDomainModal={showPrimaryDomainModal}
+ setShowPrimaryDomainModal={setShowPrimaryDomainModal}
+ props={props}
+ />
+ ) : null;
+ }, [showPrimaryDomainModal, setShowPrimaryDomainModal]);
+
+ return useMemo(
+ () => ({
+ setShowPrimaryDomainModal,
+ PrimaryDomainModal: PrimaryDomainModalCallback,
+ }),
+ [setShowPrimaryDomainModal, PrimaryDomainModalCallback],
+ );
+}