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.

add linkStructure to the link settings

Kiran Ka50dd44e8eab
diff --git a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
index 9be7807a8b..74bf04d1ab 100644
--- a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
+++ b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
@@ -62,6 +62,7 @@ export const PATCH = withWorkspace(
maxPartnerLinks,
additionalLinks,
utmTemplateId,
+ linkStructure
} = updateGroupSchema.parse(await parseRequestBody(req));
// Only check slug uniqueness if slug is being updated
@@ -124,6 +125,7 @@ export const PATCH = withWorkspace(
...(additionalLinksInput && { additionalLinks: additionalLinksInput }),
maxPartnerLinks,
utmTemplateId,
+ linkStructure
},
include: {
clickReward: true,
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-default-links.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-default-links.tsx
index c76574c49b..01613c0f83 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-default-links.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-default-links.tsx
@@ -27,7 +27,7 @@ export function GroupDefaultLinks() {
<div className="flex items-center justify-between">
<div>
<h3 className="text-content-emphasis text-lg font-semibold leading-7">
- Default partner links
+ Default links
</h3>
<p className="text-content-subtle text-sm font-normal leading-5">
Links that are automatically created for each partner in this group
@@ -69,7 +69,7 @@ function CreateDefaultLinkButton({
return (
<>
<Button
- text="Create default link"
+ text="Create link"
variant="primary"
className="h-8 w-fit rounded-lg px-3"
onClick={() => setIsOpen(true)}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-link-settings.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-link-settings.tsx
new file mode 100644
index 0000000000..0847a2647a
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-link-settings.tsx
@@ -0,0 +1,286 @@
+"use client";
+
+import { getLinkStructureOptions } from "@/lib/partners/get-link-structure-options";
+import { mutatePrefix } from "@/lib/swr/mutate";
+import { useApiMutation } from "@/lib/swr/use-api-mutation";
+import useGroup from "@/lib/swr/use-group";
+import useProgram from "@/lib/swr/use-program";
+import useWorkspace from "@/lib/swr/use-workspace";
+import { GroupProps } from "@/lib/types";
+import { UTMTemplateSchema } from "@/lib/zod/schemas/utm";
+import { useConfirmModal } from "@/ui/modals/confirm-modal";
+import { Badge, Button, UTMBuilder } from "@dub/ui";
+import { CircleCheckFill } from "@dub/ui/icons";
+import { cn } from "@dub/utils";
+import { PropsWithChildren, useEffect, useRef, useState } from "react";
+import { useForm } from "react-hook-form";
+import { toast } from "sonner";
+import { z } from "zod";
+
+type FormData = {
+ utmTemplateId?: string | null;
+ linkStructure?: string;
+} & z.infer<typeof UTMTemplateSchema>;
+
+export function GroupLinkSettings() {
+ const { group, loading: isLoadingGroup } = useGroup();
+
+ return (
+ <div className="flex flex-col divide-y divide-neutral-200 rounded-lg border border-neutral-200">
+ <div className="px-6 py-6">
+ <h3 className="text-content-emphasis text-lg font-semibold leading-7">
+ Links settings
+ </h3>
+ <p className="text-content-subtle text-sm font-normal leading-5">
+ Configure link structure and UTM parameters
+ </p>
+ </div>
+
+ {group ? (
+ <GroupLinkSettingsForm group={group} />
+ ) : isLoadingGroup ? (
+ <div className="flex h-[4.5rem] animate-pulse rounded-b-lg border-t border-neutral-200 bg-neutral-100" />
+ ) : (
+ <div className="text-content-subtle h-20 text-center">
+ Failed to load link settings
+ </div>
+ )}
+ </div>
+ );
+}
+
+function GroupLinkSettingsForm({ group }: { group: GroupProps }) {
+ const { id: workspaceId } = useWorkspace();
+ const { program } = useProgram();
+ const formRef = useRef<HTMLFormElement>(null);
+ const [isUpdatingTemplate, setIsUpdatingTemplate] = useState(false);
+
+ const { makeRequest: updateGroup, isSubmitting: isUpdatingGroup } =
+ useApiMutation();
+
+ const { confirmModal, setShowConfirmModal } = useConfirmModal({
+ title: "Save updated settings",
+ description:
+ "All existing and new partner links will automatically be updated with these settings.",
+ confirmText: "Save",
+ onConfirm: async () => {
+ formRef.current?.requestSubmit();
+ },
+ });
+
+ const { handleSubmit, watch, setValue, reset, register } = useForm<FormData>({
+ mode: "onBlur",
+ defaultValues: {
+ utmTemplateId: group?.utmTemplate?.id,
+ utm_source: group?.utmTemplate?.utm_source,
+ utm_medium: group?.utmTemplate?.utm_medium,
+ utm_campaign: group?.utmTemplate?.utm_campaign,
+ utm_term: group?.utmTemplate?.utm_term,
+ utm_content: group?.utmTemplate?.utm_content,
+ ref: group?.utmTemplate?.ref,
+ linkStructure: group?.linkStructure,
+ },
+ });
+
+ // Reset form values when group data becomes available
+ useEffect(() => {
+ if (group) {
+ reset({
+ utmTemplateId: group.utmTemplate?.id,
+ utm_source: group.utmTemplate?.utm_source,
+ utm_medium: group.utmTemplate?.utm_medium,
+ utm_campaign: group.utmTemplate?.utm_campaign,
+ utm_term: group.utmTemplate?.utm_term,
+ utm_content: group.utmTemplate?.utm_content,
+ ref: group.utmTemplate?.ref,
+ linkStructure: group.linkStructure,
+ });
+ }
+ }, [group, reset]);
+
+ const onSubmit = async (data: FormData) => {
+ if (!group || !workspaceId) return;
+
+ const {
+ utmTemplateId,
+ utm_source,
+ utm_medium,
+ utm_campaign,
+ utm_term,
+ utm_content,
+ ref,
+ linkStructure,
+ } = data;
+
+ const templateData = {
+ name: `${group.name} UTM Template`,
+ utm_source,
+ utm_medium,
+ utm_campaign,
+ utm_term,
+ utm_content,
+ ref,
+ };
+
+ setIsUpdatingTemplate(true);
+
+ const endpoint = utmTemplateId
+ ? `/api/utm/${utmTemplateId}?workspaceId=${workspaceId}`
+ : `/api/utm?workspaceId=${workspaceId}`;
+
+ // Create or update the UTM template
+ const response = await fetch(endpoint, {
+ method: utmTemplateId ? "PATCH" : "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify(templateData),
+ });
+
+ if (!response.ok) {
+ const { error } = await response.json();
+ toast.error(error.message);
+ setIsUpdatingTemplate(false);
+ return;
+ }
+
+ // Update the group with UTM template and link structure
+ const updateData: any = {};
+
+ if (!utmTemplateId) {
+ const { id: newUtmTemplateId } = await response.json();
+ updateData.utmTemplateId = newUtmTemplateId;
+ }
+
+ if (linkStructure !== group.linkStructure) {
+ updateData.linkStructure = linkStructure;
+ }
+
+ if (Object.keys(updateData).length > 0) {
+ await updateGroup(`/api/groups/${group.id}`, {
+ method: "PATCH",
+ body: updateData,
+ onSuccess: async () => {
+ await mutatePrefix(["/api/groups", "/api/utm"]);
+ toast.success("Settings saved successfully!");
+ setIsUpdatingTemplate(false);
+ },
+ });
+ } else {
+ setIsUpdatingTemplate(false);
+ }
+ };
+
+ const currentValues = watch();
+
+ return (
+ <form
+ onSubmit={handleSubmit(onSubmit)}
+ className="flex flex-col divide-y divide-neutral-200"
+ >
+ <SettingsRow
+ heading="Link structure"
+ description="How your partner links are displayed"
+ >
+ <div className="grid grid-cols-1 gap-3">
+ {program &&
+ getLinkStructureOptions({
+ domain: program.domain,
+ url: program.url,
+ }).map((type) => {
+ const isSelected = type.id === currentValues.linkStructure;
+
+ return (
+ <label
+ key={type.id}
+ className={cn(
+ "relative flex w-full cursor-pointer items-start gap-0.5 rounded-md border border-neutral-200 bg-white p-3 text-neutral-600",
+ type.comingSoon
+ ? "cursor-default opacity-80"
+ : "hover:bg-neutral-50",
+ "transition-all duration-150",
+ isSelected &&
+ "border-black bg-neutral-50 text-neutral-900 ring-1 ring-black",
+ )}
+ >
+ <input
+ type="radio"
+ value={type.id}
+ className="hidden"
+ disabled={type.comingSoon === true}
+ {...register("linkStructure")}
+ />
+
+ <div className="flex grow flex-col text-sm">
+ <span className="font-medium">{type.label}</span>
+ <span className="text-neutral-600">{type.example}</span>
+ </div>
+
+ {type.comingSoon ? (
+ <Badge variant="blueGradient">Coming soon</Badge>
+ ) : (
+ <CircleCheckFill
+ className={cn(
+ "-mr-px -mt-px flex size-4 scale-75 items-center justify-center rounded-full opacity-0 transition-[transform,opacity] duration-150",
+ isSelected && "scale-100 opacity-100",
+ )}
+ />
+ )}
+ </label>
+ );
+ })}
+ </div>
+ </SettingsRow>
+
+ <SettingsRow
+ heading="UTM Parameters"
+ description="Configure UTM tracking parameters for all links in this group"
+ >
+ <UTMBuilder
+ values={{
+ utm_source: currentValues.utm_source || "",
+ utm_medium: currentValues.utm_medium || "",
+ utm_campaign: currentValues.utm_campaign || "",
+ utm_term: currentValues.utm_term || "",
+ utm_content: currentValues.utm_content || "",
+ ref: currentValues.ref || "",
+ }}
+ onChange={(key, value) => {
+ setValue(key, value, { shouldDirty: true });
+ }}
+ />
+ </SettingsRow>
+
+ <div className="flex items-center justify-end rounded-b-lg border-t border-neutral-200 bg-neutral-50 px-6 py-5">
+ <Button
+ text="Save changes"
+ className="h-8 w-fit"
+ loading={isUpdatingGroup || isUpdatingTemplate}
+ onClick={() => setShowConfirmModal(true)}
+ />
+ </div>
+ </form>
+ );
+}
+
+function SettingsRow({
+ heading,
+ description,
+ children,
+}: PropsWithChildren<{
+ heading: string;
+ description: string;
+}>) {
+ return (
+ <div className="grid grid-cols-1 gap-10 px-6 py-8 sm:grid-cols-2">
+ <div className="flex flex-col gap-1">
+ <h3 className="text-content-emphasis text-base font-semibold leading-none">
+ {heading}
+ </h3>
+ <p className="text-content-subtle text-sm">{description}</p>
+ </div>
+
+ <div>{children}</div>
+ </div>
+ );
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-utm.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-utm.tsx
deleted file mode 100644
index bc9319bc27..0000000000
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-utm.tsx
+++ /dev/null
@@ -1,199 +0,0 @@
-"use client";
-
-import { mutatePrefix } from "@/lib/swr/mutate";
-import { useApiMutation } from "@/lib/swr/use-api-mutation";
-import useGroup from "@/lib/swr/use-group";
-import useWorkspace from "@/lib/swr/use-workspace";
-import { UTMTemplateSchema } from "@/lib/zod/schemas/utm";
-import { useConfirmModal } from "@/ui/modals/confirm-modal";
-import { Button, UTMBuilder } from "@dub/ui";
-import { PropsWithChildren, useEffect, useRef, useState } from "react";
-import { useForm } from "react-hook-form";
-import { toast } from "sonner";
-import { z } from "zod";
-
-type FormData = {
- utmTemplateId?: string | null;
-} & z.infer<typeof UTMTemplateSchema>;
-
-export function GroupUTM() {
- const { id: workspaceId } = useWorkspace();
- const { group, loading: isLoadingGroup } = useGroup();
-
- const formRef = useRef<HTMLFormElement>(null);
-
- const { makeRequest: updateGroup, isSubmitting: isUpdatingGroup } =
- useApiMutation();
- const [isUpdatingTemplate, setIsUpdatingTemplate] = useState(false);
-
- const { confirmModal, setShowConfirmModal } = useConfirmModal({
- title: "Save updated UTM parameters",
- description:
- "All existing and new partner links will automatically be updated with these UTM parameters.",
- confirmText: "Save",
- onConfirm: async () => {
- formRef.current?.requestSubmit();
- },
- });
-
- const { handleSubmit, watch, setValue, reset } = useForm<FormData>({
- mode: "onBlur",
- defaultValues: {
- utmTemplateId: group?.utmTemplate?.id || null,
- utm_source: group?.utmTemplate?.utm_source || "",
- utm_medium: group?.utmTemplate?.utm_medium || "",
- utm_campaign: group?.utmTemplate?.utm_campaign || "",
- utm_term: group?.utmTemplate?.utm_term || "",
- utm_content: group?.utmTemplate?.utm_content || "",
- ref: group?.utmTemplate?.ref || "",
- },
- });
-
- // Reset form values when group data becomes available
- useEffect(() => {
- if (group) {
- reset({
- utmTemplateId: group.utmTemplate?.id || null,
- utm_source: group.utmTemplate?.utm_source || "",
- utm_medium: group.utmTemplate?.utm_medium || "",
- utm_campaign: group.utmTemplate?.utm_campaign || "",
- utm_term: group.utmTemplate?.utm_term || "",
- utm_content: group.utmTemplate?.utm_content || "",
- ref: group.utmTemplate?.ref || "",
- });
- }
- }, [group, reset]);
-
- const onSubmit = async (data: FormData) => {
- if (!group || !workspaceId) return;
-
- const {
- utmTemplateId,
- utm_source,
- utm_medium,
- utm_campaign,
- utm_term,
- utm_content,
- ref,
- } = data;
-
- const hasUtmParams =
- utm_source ||
- utm_medium ||
- utm_campaign ||
- utm_term ||
- utm_content ||
- ref;
-
- const templateData = {
- name: `${group.name} UTM Template`,
- utm_source,
- utm_medium,
- utm_campaign,
- utm_term,
- utm_content,
- ref,
- };
-
- setIsUpdatingTemplate(true);
-
- const endpoint = utmTemplateId
- ? `/api/utm/${utmTemplateId}?workspaceId=${workspaceId}`
- : `/api/utm?workspaceId=${workspaceId}`;
-
- // Create or update the UTM template
- const response = await fetch(endpoint, {
- method: utmTemplateId ? "PATCH" : "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(templateData),
- });
-
- if (!response.ok) {
- const { error } = await response.json();
- toast.error(error.message);
- setIsUpdatingTemplate(false);
- return;
- }
-
- // Update the group to use the new utmTemplateId
- if (!utmTemplateId) {
- const { id: utmTemplateId } = await response.json();
-
- await updateGroup(`/api/groups/${group.id}`, {
- method: "PATCH",
- body: {
- utmTemplateId,
- },
- onSuccess: async () => {
- await mutatePrefix(["/api/groups", "/api/utm"]);
- toast.success("UTM settings saved successfully!");
- setIsUpdatingTemplate(false);
- },
- });
- }
-
- setIsUpdatingTemplate(false);
- };
-
- const currentValues = watch();
-
- return (
- <form ref={formRef} onSubmit={handleSubmit(onSubmit)}>
- {confirmModal}
- <div className="flex min-h-80 flex-col divide-y divide-neutral-200 rounded-lg border border-neutral-200">
- <SettingsRow
- heading="UTM Parameters"
- description="Configure UTM tracking parameters for all links in this group"
- >
- <UTMBuilder
- values={{
- utm_source: currentValues.utm_source || "",
- utm_medium: currentValues.utm_medium || "",
- utm_campaign: currentValues.utm_campaign || "",
- utm_term: currentValues.utm_term || "",
- utm_content: currentValues.utm_content || "",
- ref: currentValues.ref || "",
- }}
- onChange={(key, value) => {
- setValue(key, value, { shouldDirty: true });
- }}
- />
- </SettingsRow>
-
- <div className="flex items-center justify-end rounded-b-lg border-t border-neutral-200 bg-neutral-50 px-6 py-5">
- <Button
- text="Save changes"
- className="h-8 w-fit"
- loading={isUpdatingGroup || isUpdatingTemplate}
- disabled={isLoadingGroup}
- onClick={() => setShowConfirmModal(true)}
- />
- </div>
- </div>
- </form>
- );
-}
-
-function SettingsRow({
- heading,
- description,
- children,
-}: PropsWithChildren<{
- heading: string;
- description: string;
-}>) {
- return (
- <div className="grid grid-cols-1 gap-10 px-6 py-8 sm:grid-cols-2">
- <div className="flex flex-col gap-1">
- <h3 className="text-content-emphasis text-base font-semibold leading-none">
- {heading}
- </h3>
- <p className="text-content-subtle text-sm">{description}</p>
- </div>
-
- <div>{children}</div>
- </div>
- );
-}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/page.tsx
index 3717b7c6cc..fc82db89b4 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/page.tsx
@@ -1,13 +1,13 @@
import { GroupAdditionalLinks } from "./group-additional-links";
import { GroupDefaultLinks } from "./group-default-links";
-import { GroupUTM } from "./group-utm";
+import { GroupLinkSettings } from "./group-link-settings";
export default function GroupDefaultLinksPage() {
return (
<div className="flex flex-col gap-6">
<GroupDefaultLinks />
<GroupAdditionalLinks />
- <GroupUTM />
+ <GroupLinkSettings />
</div>
);
}
diff --git a/apps/web/lib/api/groups/get-groups.ts b/apps/web/lib/api/groups/get-groups.ts
index 01aeb40763..9000b6975d 100644
--- a/apps/web/lib/api/groups/get-groups.ts
+++ b/apps/web/lib/api/groups/get-groups.ts
@@ -43,6 +43,7 @@ export async function getGroups(filters: GroupFilters) {
pg.defaultLinks,
pg.additionalLinks,
pg.maxPartnerLinks,
+ pg.linkStructure,
${
includeExpandedFields
? Prisma.sql`
diff --git a/apps/web/lib/zod/schemas/groups.ts b/apps/web/lib/zod/schemas/groups.ts
index ec409e2a86..02b81550d4 100644
--- a/apps/web/lib/zod/schemas/groups.ts
+++ b/apps/web/lib/zod/schemas/groups.ts
@@ -96,6 +96,7 @@ export const updateGroupSchema = createGroupSchema.partial().extend({
.optional(),
maxPartnerLinks: z.number().optional(),
utmTemplateId: z.string().optional(),
+ linkStructure: z.nativeEnum(PartnerLinkStructure).optional(),
});
export const changeGroupSchema = z.object({