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.

implemented confirmation modal for domain changes in the UI.

Kiran Ke4211aa76e76
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-group-default-link-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-group-default-link-sheet.tsx
index 87e0b5ca2b..ef229a4b42 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-group-default-link-sheet.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-group-default-link-sheet.tsx
@@ -1,6 +1,7 @@
"use client";
import { useApiMutation } from "@/lib/swr/use-api-mutation";
+import useDomains from "@/lib/swr/use-domains";
import useGroup from "@/lib/swr/use-group";
import usePartnerGroupDefaultLinks from "@/lib/swr/use-partner-group-default-links";
import useProgram from "@/lib/swr/use-program";
@@ -8,6 +9,7 @@ import useWorkspace from "@/lib/swr/use-workspace";
import { PartnerGroupDefaultLink } from "@/lib/types";
import { createOrUpdateDefaultLinkSchema } from "@/lib/zod/schemas/groups";
import { DomainSelector } from "@/ui/domains/domain-selector";
+import { useConfirmModal } from "@/ui/modals/confirm-modal";
import { RewardIconSquare } from "@/ui/partners/rewards/reward-icon-square";
import { X } from "@/ui/shared/icons";
import { Button, InfoTooltip, Input, Sheet } from "@dub/ui";
@@ -41,8 +43,8 @@ function DefaultPartnerLinkSheetContent({
const { program } = useProgram();
const { id: workspaceId } = useWorkspace();
const { defaultLinks } = usePartnerGroupDefaultLinks();
- const { makeRequest: createOrUpdateDefaultLink, isSubmitting } =
- useApiMutation();
+ const { allWorkspaceDomains } = useDomains();
+ const { makeRequest, isSubmitting } = useApiMutation();
const { handleSubmit, watch, setValue } = useForm<FormData>({
defaultValues: {
@@ -53,21 +55,53 @@ function DefaultPartnerLinkSheetContent({
const [domain, url] = watch(["domain", "url"]);
- // Save the default link
- const onSubmit = async (data: FormData) => {
- if (!group || !defaultLinks) return;
-
- // Check if the link already exists
- const existingLink = defaultLinks.find(
- (link) => normalizeUrl(link.url) === normalizeUrl(data.url),
- );
+ const { setShowConfirmModal, confirmModal } = useConfirmModal({
+ title: "Switching to a different program domain",
+ description: (
+ <div className="mt-2 space-y-3">
+ <p>
+ You've selected <strong className="text-black">{domain}</strong>,
+ which is different from your program's current domain{" "}
+ <strong className="text-black">{program?.domain}</strong>. Using this
+ domain will:
+ </p>
+ <ul className="list-disc space-y-1.5 pl-5 text-sm">
+ <li>
+ Change the program's primary domain to{" "}
+ <strong className="text-black">{domain}</strong>
+ </li>
+ <li>
+ Update all default links across groups to use{" "}
+ <strong className="text-black">{domain}</strong>
+ </li>
+ <li>
+ Break existing partner links that point to{" "}
+ <strong className="text-black">{program?.domain}</strong>
+ </li>
+ <li>
+ Automatically update all partner links to use{" "}
+ <strong className="text-black">{domain}</strong>
+ </li>
+ </ul>
+ <p className="text-muted-foreground text-sm">
+ You can't use multiple shortlink domains within the same program.
+ </p>
+ <p className="text-sm font-medium">
+ Are you sure you want to continue?
+ </p>
+ </div>
+ ),
+ confirmText: "Continue",
+ onConfirm: async () => {
+ const data = watch();
+ await createOrUpdateDefaultLink(data);
+ },
+ });
- if (existingLink && existingLink.id !== link?.id) {
- toast.error("A default link with this URL already exists.");
- return;
- }
+ const createOrUpdateDefaultLink = async (data: FormData) => {
+ if (!group) return;
- await createOrUpdateDefaultLink(
+ await makeRequest(
link
? `/api/groups/${group.id}/default-links/${link.id}`
: `/api/groups/${group.id}/default-links`,
@@ -90,116 +124,152 @@ function DefaultPartnerLinkSheetContent({
);
};
+ const onSubmit = async (data: FormData) => {
+ if (!group || !defaultLinks) return;
+
+ // Check if the link already exists
+ const existingLink = defaultLinks.find(
+ (link) => normalizeUrl(link.url) === normalizeUrl(data.url),
+ );
+
+ if (existingLink && existingLink.id !== link?.id) {
+ toast.error("A default link with this URL already exists.");
+ return;
+ }
+
+ // Check if domain is different from program domain
+ if (program?.domain && data.domain !== program.domain) {
+ setShowConfirmModal(true);
+ return;
+ }
+
+ await createOrUpdateDefaultLink(data);
+ };
+
const isEditing = !!link;
+ // Check if the selected domain is verified
+ const selectedDomainData = allWorkspaceDomains?.find(
+ (d) => d.slug === domain,
+ );
+ const isDomainUnverified = selectedDomainData && !selectedDomainData.verified;
+
return (
- <form onSubmit={handleSubmit(onSubmit)} className="flex h-full flex-col">
- <div className="flex h-16 items-center justify-between border-b border-neutral-200 px-6 py-4">
- <Sheet.Title className="text-lg font-semibold">
- {isEditing ? "Edit default link" : "Create default link"}
- </Sheet.Title>
- <Sheet.Close asChild>
- <Button
- variant="outline"
- icon={<X className="size-5" />}
- className="h-auto w-fit p-1"
- />
- </Sheet.Close>
- </div>
+ <>
+ {confirmModal}
+ <form onSubmit={handleSubmit(onSubmit)} className="flex h-full flex-col">
+ <div className="flex h-16 items-center justify-between border-b border-neutral-200 px-6 py-4">
+ <Sheet.Title className="text-lg font-semibold">
+ {isEditing ? "Edit default link" : "Create default link"}
+ </Sheet.Title>
+ <Sheet.Close asChild>
+ <Button
+ variant="outline"
+ icon={<X className="size-5" />}
+ className="h-auto w-fit p-1"
+ />
+ </Sheet.Close>
+ </div>
- <div className="flex flex-1 flex-col gap-6 overflow-y-auto p-6">
- <LinkSettingsCard
- title={
- <>
- <RewardIconSquare icon={Hyperlink} />
- <span className="leading-relaxed">Link settings</span>
- </>
- }
- content={
- <div className="space-y-6">
- <div className="space-y-2">
- <label className="text-content-emphasis block text-sm font-medium">
- Domain
- </label>
- <DomainSelector
- selectedDomain={domain || ""}
- setSelectedDomain={(domain) =>
- setValue("domain", domain, { shouldDirty: true })
- }
- />
- <p className="text-xs font-normal text-neutral-500">
- Custom domain that will be used for this group's referral
- links
- </p>
- </div>
- <div className="space-y-2">
- <div className="flex items-center gap-x-2">
+ <div className="flex flex-1 flex-col gap-6 overflow-y-auto p-6">
+ <LinkSettingsCard
+ title={
+ <>
+ <RewardIconSquare icon={Hyperlink} />
+ <span className="leading-relaxed">Link settings</span>
+ </>
+ }
+ content={
+ <div className="space-y-6">
+ <div className="space-y-2">
<label className="text-content-emphasis block text-sm font-medium">
- Destination URL
+ Domain
</label>
- <InfoTooltip
- content={
- "Where people will be redirected after clicking the referral links [Learn more](https://dub.co/help/article/destination-urls)"
+ <DomainSelector
+ selectedDomain={domain || ""}
+ setSelectedDomain={(domain) =>
+ setValue("domain", domain, { shouldDirty: true })
+ }
+ />
+ <p className="text-xs font-normal text-neutral-500">
+ Custom domain that will be used for this group's referral
+ links
+ </p>
+ </div>
+ <div className="space-y-2">
+ <div className="flex items-center gap-x-2">
+ <label className="text-content-emphasis block text-sm font-medium">
+ Destination URL
+ </label>
+ <InfoTooltip
+ content={
+ "Where people will be redirected after clicking the referral links [Learn more](https://dub.co/help/article/destination-urls)"
+ }
+ />
+ </div>
+ <Input
+ value={url || ""}
+ onChange={(e) =>
+ setValue("url", e.target.value, { shouldDirty: true })
}
+ type="url"
+ placeholder="https://acme.dub.sh"
+ className="max-w-full"
/>
+ <p className="text-xs font-normal text-neutral-500">
+ Where people will be redirected after clicking the referral
+ links
+ </p>
</div>
- <Input
- value={url || ""}
- onChange={(e) =>
- setValue("url", e.target.value, { shouldDirty: true })
- }
- type="url"
- placeholder="https://acme.dub.sh"
- className="max-w-full"
- />
- <p className="text-xs font-normal text-neutral-500">
- Where people will be redirected after clicking the referral
- links
- </p>
</div>
- </div>
- }
- />
-
- <LinkSettingsCard
- title={
- <>
- <RewardIconSquare icon={Eye} />
- <span className="leading-relaxed">Link preview</span>
- </>
- }
- content={
- <PartnerLinkPreview
- url={url}
- domain={domain || ""}
- linkStructure={group?.linkStructure || "query"}
- />
- }
- />
- </div>
-
- <div className="flex items-center justify-end border-t border-neutral-200 p-5">
- <div className="flex items-center gap-2">
- <Button
- type="button"
- variant="secondary"
- onClick={() => setIsOpen(false)}
- text="Cancel"
- className="w-fit"
- disabled={isSubmitting}
+ }
/>
- <Button
- type="submit"
- variant="primary"
- text={isEditing ? "Update link" : "Create link"}
- className="w-fit"
- loading={isSubmitting}
- disabled={!url}
+ <LinkSettingsCard
+ title={
+ <>
+ <RewardIconSquare icon={Eye} />
+ <span className="leading-relaxed">Link preview</span>
+ </>
+ }
+ content={
+ <PartnerLinkPreview
+ url={url}
+ domain={domain || ""}
+ linkStructure={group?.linkStructure || "query"}
+ />
+ }
/>
</div>
- </div>
- </form>
+
+ <div className="flex items-center justify-end border-t border-neutral-200 p-5">
+ <div className="flex items-center gap-2">
+ <Button
+ type="button"
+ variant="secondary"
+ onClick={() => setIsOpen(false)}
+ text="Cancel"
+ className="w-fit"
+ disabled={isSubmitting}
+ />
+
+ <Button
+ type="submit"
+ variant="primary"
+ text={isEditing ? "Update link" : "Create link"}
+ className="w-fit"
+ loading={isSubmitting}
+ disabled={!url || isDomainUnverified}
+ disabledTooltip={
+ isDomainUnverified
+ ? "Please verify the domain before using it for partner links."
+ : undefined
+ }
+ />
+ </div>
+ </div>
+ </form>
+ </>
);
}