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.

Reorganize link builder options

Tim Wilsonbc42f9335490
diff --git a/apps/web/ui/modals/link-builder/ab-testing-modal.tsx b/apps/web/ui/modals/link-builder/ab-testing-modal.tsx
index 53b09030a5..c76456b24a 100644
--- a/apps/web/ui/modals/link-builder/ab-testing-modal.tsx
+++ b/apps/web/ui/modals/link-builder/ab-testing-modal.tsx
@@ -8,16 +8,19 @@ import { X } from "@/ui/shared/icons";
import {
AnimatedSizeContainer,
Button,
+ Flask,
InfoTooltip,
Modal,
SimpleTooltipContent,
Tooltip,
TriangleWarning,
+ useKeyboardShortcut,
} from "@dub/ui";
import {
cn,
formatDateTime,
getDateTimeLocal,
+ isValidUrl,
parseDateTime,
} from "@dub/utils";
import {
@@ -78,7 +81,7 @@ function ABTestingModal({
setValue,
getValues,
reset,
- formState: { isDirty },
+ formState: { isDirty, isValid },
handleSubmit,
} = useForm<
{ tests: z.infer<typeof LinkTestsSchema> } & Pick<
@@ -86,6 +89,7 @@ function ABTestingModal({
"testsCompleteAt"
>
>({
+ mode: "onChange",
values: {
tests: parseTests(getValuesParent("tests")) ?? [
{ url: getValuesParent("url") || "", percentage: 100 },
@@ -303,7 +307,17 @@ function ABTestingModal({
"https://dub.co/help/article/what-is-dub"
}
className="block h-9 grow border-none px-2 text-neutral-900 placeholder-neutral-400 focus:ring-0 sm:text-sm"
- {...register(`tests.${index}.url`)}
+ {...register(`tests.${index}.url`, {
+ validate: (value, { tests }) => {
+ if (!value) return "URL is required";
+
+ if (!isValidUrl(value)) return "Invalid URL";
+
+ return (
+ tests.length > 1 && tests.length <= MAX_TEST_COUNT
+ );
+ },
+ })}
/>
{index > 0 && (
<Button
@@ -457,7 +471,7 @@ function ABTestingModal({
: "Start testing"
}
className="h-9 w-fit"
- disabled={!isDirty}
+ disabled={!isDirty || !isValid}
/>
</div>
</div>
@@ -466,6 +480,48 @@ function ABTestingModal({
);
}
+export function getABTestingLabel({
+ tests,
+ testsCompleteAt,
+}: Pick<LinkFormData, "tests" | "testsCompleteAt">) {
+ const enabled = Boolean(tests && testsCompleteAt);
+
+ if (testsCompleteAt && new Date(testsCompleteAt) < new Date())
+ return "Test Complete";
+
+ return enabled && Array.isArray(tests) ? `${tests?.length} URLs` : "A/B Test";
+}
+
+function ABTestingButton({
+ setShowABTestingModal,
+}: {
+ setShowABTestingModal: Dispatch<SetStateAction<boolean>>;
+}) {
+ const { watch } = useFormContext<LinkFormData>();
+ const [tests, testsCompleteAt] = watch(["tests", "testsCompleteAt"]);
+
+ useKeyboardShortcut("b", () => setShowABTestingModal(true), {
+ modal: true,
+ });
+
+ const enabled = Boolean(tests && testsCompleteAt);
+
+ const label = useMemo(
+ () => getABTestingLabel({ tests, testsCompleteAt }),
+ [tests, testsCompleteAt],
+ );
+
+ return (
+ <Button
+ variant="secondary"
+ text={label}
+ icon={<Flask className={cn("size-4", enabled && "text-blue-500")} />}
+ className="h-9 w-fit px-2.5 font-medium text-neutral-700"
+ onClick={() => setShowABTestingModal(true)}
+ />
+ );
+}
+
export function useABTestingModal() {
const [showABTestingModal, setShowABTestingModal] = useState(false);
@@ -478,12 +534,17 @@ export function useABTestingModal() {
);
}, [showABTestingModal, setShowABTestingModal]);
+ const ABTestingButtonCallback = useCallback(() => {
+ return <ABTestingButton setShowABTestingModal={setShowABTestingModal} />;
+ }, [setShowABTestingModal]);
+
return useMemo(
() => ({
setShowABTestingModal,
ABTestingModal: ABTestingModalCallback,
+ ABTestingButton: ABTestingButtonCallback,
}),
- [setShowABTestingModal, ABTestingModalCallback],
+ [setShowABTestingModal, ABTestingModalCallback, ABTestingButtonCallback],
);
}
diff --git a/apps/web/ui/modals/link-builder/constants.ts b/apps/web/ui/modals/link-builder/constants.ts
index abee5333bf..6f8f65ae3f 100644
--- a/apps/web/ui/modals/link-builder/constants.ts
+++ b/apps/web/ui/modals/link-builder/constants.ts
@@ -11,11 +11,12 @@ import {
import { Settings } from "lucide-react";
import { UseFormSetValue } from "react-hook-form";
import { LinkFormData } from ".";
+import { getABTestingLabel } from "./ab-testing-modal";
import { getExpirationLabel } from "./expiration-modal";
import { getPasswordLabel } from "./password-modal";
import { getTargetingLabel } from "./targeting-modal";
-export const MORE_ITEMS: {
+type MoreItem = {
key: string;
icon: Icon;
label: string;
@@ -23,9 +24,13 @@ export const MORE_ITEMS: {
learnMoreUrl?: string;
shortcutKey: string;
type: string;
+ badgeLabel?: (data: LinkFormData) => string;
enabled?: (data: LinkFormData) => boolean;
+ remove?: (setValue: UseFormSetValue<ExpandedLinkProps>) => void;
add?: boolean;
-}[] = [
+};
+
+export const MORE_ITEMS: MoreItem[] = [
{
key: "rewrite",
icon: Incognito,
@@ -46,39 +51,6 @@ export const MORE_ITEMS: {
shortcutKey: "s",
type: "boolean",
},
- {
- key: "tests",
- icon: Flask,
- label: "A/B Testing",
- shortcutKey: "b",
- type: "modal",
- },
- {
- key: "advanced",
- icon: Settings,
- label: "Advanced Settings",
- shortcutKey: "a",
- type: "modal",
- enabled: (data: LinkFormData) => Boolean(data.externalId || data.tenantId),
- add: false,
- },
-];
-
-export const MOBILE_MORE_ITEMS = [
- {
- key: "password",
- icon: InputPassword,
- label: "Password",
- badgeLabel: getPasswordLabel,
- description:
- "Protect your links with a password so only authorized users can access them.",
- learnMoreUrl: "https://dub.co/help/article/password-protected-links",
- shortcutKey: "l",
- enabled: (data: LinkFormData) => Boolean(data.password),
- remove: (setValue: UseFormSetValue<ExpandedLinkProps>) =>
- setValue("password", null, { shouldDirty: true }),
- type: "modal",
- },
{
key: "expiresAt",
icon: CircleHalfDottedClock,
@@ -88,13 +60,25 @@ export const MOBILE_MORE_ITEMS = [
"Set an expiration date for your links – after which it won't be accessible.",
learnMoreUrl: "https://dub.co/help/article/link-expiration",
shortcutKey: "e",
- enabled: (data: LinkFormData) => Boolean(data.expiresAt),
- remove: (setValue: UseFormSetValue<ExpandedLinkProps>) => {
+ enabled: (data) => Boolean(data.expiresAt),
+ remove: (setValue) => {
setValue("expiresAt", null, { shouldDirty: true });
setValue("expiredUrl", null, { shouldDirty: true });
},
type: "modal",
},
+ {
+ key: "advanced",
+ icon: Settings,
+ label: "Advanced Settings",
+ shortcutKey: "a",
+ type: "modal",
+ enabled: (data) => Boolean(data.externalId || data.tenantId),
+ add: false,
+ },
+];
+
+export const MOBILE_MORE_ITEMS: MoreItem[] = [
{
key: "targeting",
icon: Crosshairs3,
@@ -104,15 +88,31 @@ export const MOBILE_MORE_ITEMS = [
"Target your links to specific audiences based on their location, device, or browser.",
learnMoreUrl: "https://dub.co/help/article/geo-targeting",
shortcutKey: "x",
- enabled: (data: LinkFormData) =>
+ enabled: (data) =>
Boolean(
data.ios || data.android || Object.keys(data.geo || {}).length > 0,
),
- remove: (setValue: UseFormSetValue<ExpandedLinkProps>) => {
- setValue("ios", null, { shouldDirty: true });
- setValue("android", null, { shouldDirty: true });
- setValue("geo", null, { shouldDirty: true });
- },
+ type: "modal",
+ },
+ {
+ key: "tests",
+ icon: Flask,
+ label: "A/B Testing",
+ badgeLabel: getABTestingLabel,
+ shortcutKey: "b",
+ enabled: (data) => Boolean(data.tests && data.testsCompleteAt),
+ type: "modal",
+ },
+ {
+ key: "password",
+ icon: InputPassword,
+ label: "Password",
+ badgeLabel: getPasswordLabel,
+ description:
+ "Protect your links with a password so only authorized users can access them.",
+ learnMoreUrl: "https://dub.co/help/article/password-protected-links",
+ shortcutKey: "l",
+ enabled: (data) => Boolean(data.password),
type: "modal",
},
];
diff --git a/apps/web/ui/modals/link-builder/expiration-modal.tsx b/apps/web/ui/modals/link-builder/expiration-modal.tsx
index ceb69d6102..4732f093ac 100644
--- a/apps/web/ui/modals/link-builder/expiration-modal.tsx
+++ b/apps/web/ui/modals/link-builder/expiration-modal.tsx
@@ -5,16 +5,9 @@ import {
Modal,
SimpleTooltipContent,
Tooltip,
- useKeyboardShortcut,
useMediaQuery,
} from "@dub/ui";
-import { CircleHalfDottedClock } from "@dub/ui/icons";
-import {
- cn,
- formatDateTime,
- getDateTimeLocal,
- parseDateTime,
-} from "@dub/utils";
+import { formatDateTime, getDateTimeLocal, parseDateTime } from "@dub/utils";
import {
Dispatch,
SetStateAction,
@@ -272,34 +265,7 @@ export function getExpirationLabel({
}: Pick<LinkFormData, "expiresAt">) {
return expiresAt
? formatDateTime(expiresAt, { year: undefined })
- : "Expiration";
-}
-
-function ExpirationButton({
- setShowExpirationModal,
-}: {
- setShowExpirationModal: Dispatch<SetStateAction<boolean>>;
-}) {
- const { watch } = useFormContext<LinkFormData>();
- const expiresAt = watch("expiresAt");
-
- useKeyboardShortcut("e", () => setShowExpirationModal(true), {
- modal: true,
- });
-
- return (
- <Button
- variant="secondary"
- text={getExpirationLabel({ expiresAt })}
- icon={
- <CircleHalfDottedClock
- className={cn("size-4", expiresAt && "text-blue-500")}
- />
- }
- className="h-9 w-fit px-2.5 font-medium text-neutral-700"
- onClick={() => setShowExpirationModal(true)}
- />
- );
+ : "Add Link Expiration";
}
export function useExpirationModal() {
@@ -314,16 +280,11 @@ export function useExpirationModal() {
);
}, [showExpirationModal, setShowExpirationModal]);
- const ExpirationButtonCallback = useCallback(() => {
- return <ExpirationButton setShowExpirationModal={setShowExpirationModal} />;
- }, [setShowExpirationModal]);
-
return useMemo(
() => ({
setShowExpirationModal,
ExpirationModal: ExpirationModalCallback,
- ExpirationButton: ExpirationButtonCallback,
}),
- [setShowExpirationModal, ExpirationModalCallback, ExpirationButtonCallback],
+ [setShowExpirationModal, ExpirationModalCallback],
);
}
diff --git a/apps/web/ui/modals/link-builder/index.tsx b/apps/web/ui/modals/link-builder/index.tsx
index f9bc2d8096..c8542c5e46 100644
--- a/apps/web/ui/modals/link-builder/index.tsx
+++ b/apps/web/ui/modals/link-builder/index.tsx
@@ -55,9 +55,9 @@ import TextareaAutosize from "react-textarea-autosize";
import { toast } from "sonner";
import { mutate } from "swr";
import { useDebounce } from "use-debounce";
+import { useABTestingModal } from "./ab-testing-modal";
import { ConversionTrackingToggle } from "./conversion-tracking-toggle";
import { DraftControls, DraftControlsHandle } from "./draft-controls";
-import { useExpirationModal } from "./expiration-modal";
import { LinkPreview } from "./link-preview";
import { MoreDropdown } from "./more-dropdown";
import { OptionsList } from "./options-list";
@@ -212,18 +212,17 @@ function LinkBuilderInner({
const draftControlsRef = useRef<DraftControlsHandle>(null);
const { UTMModal, UTMButton } = useUTMModal();
- const { ExpirationModal, ExpirationButton } = useExpirationModal();
const { TargetingModal, TargetingButton } = useTargetingModal();
+ const { ABTestingModal, ABTestingButton } = useABTestingModal();
const { PasswordModal, PasswordButton } = usePasswordModal();
-
const [, copyToClipboard] = useCopyToClipboard();
return (
<>
- <PasswordModal />
<UTMModal />
<TargetingModal />
- <ExpirationModal />
+ <ABTestingModal />
+ <PasswordModal />
<Modal
showModal={showLinkBuilder}
setShowModal={setShowLinkBuilder}
@@ -521,8 +520,8 @@ function LinkBuilderInner({
<div className="flex min-w-0 items-center gap-2">
<UTMButton />
<div className="flex items-center gap-2 max-sm:hidden">
- <ExpirationButton />
<TargetingButton />
+ <ABTestingButton />
<PasswordButton />
</div>
<WebhookSelect />
diff --git a/apps/web/ui/modals/link-builder/more-dropdown.tsx b/apps/web/ui/modals/link-builder/more-dropdown.tsx
index 3a2dd309a2..5aee89b311 100644
--- a/apps/web/ui/modals/link-builder/more-dropdown.tsx
+++ b/apps/web/ui/modals/link-builder/more-dropdown.tsx
@@ -33,8 +33,8 @@ export function MoreDropdown() {
const { PasswordModal, setShowPasswordModal } = usePasswordModal();
const { TargetingModal, setShowTargetingModal } = useTargetingModal();
const { ExpirationModal, setShowExpirationModal } = useExpirationModal();
- const { AdvancedModal, setShowAdvancedModal } = useAdvancedModal();
const { ABTestingModal, setShowABTestingModal } = useABTestingModal();
+ const { AdvancedModal, setShowAdvancedModal } = useAdvancedModal();
const modalCallbacks = {
password: setShowPasswordModal,
@@ -101,14 +101,19 @@ export function MoreDropdown() {
enabled && "text-blue-500",
)}
/>
- {option.type === "modal"
- ? enabled || ("add" in option && option.add === false)
- ? ""
- : "Add "
- : enabled
- ? "Remove "
- : "Add "}
- {option.label}
+ {option.badgeLabel?.(data) ?? (
+ <>
+ {option.type === "modal"
+ ? enabled ||
+ ("add" in option && option.add === false)
+ ? ""
+ : "Add "
+ : enabled
+ ? "Remove "
+ : "Add "}
+ {option.label}
+ </>
+ )}
{option.description && option.learnMoreUrl && (
<ProBadgeTooltip
content={
diff --git a/apps/web/ui/modals/link-builder/options-list.tsx b/apps/web/ui/modals/link-builder/options-list.tsx
index 8c877cd654..6b0623ac74 100644
--- a/apps/web/ui/modals/link-builder/options-list.tsx
+++ b/apps/web/ui/modals/link-builder/options-list.tsx
@@ -27,8 +27,11 @@ export function OptionsList() {
() => [
...enabledToggles,
...(isMobile
- ? MOBILE_MORE_ITEMS.filter(({ enabled }) => enabled(data)).map(
- (item) => ({ ...item, label: item.badgeLabel(data) }),
+ ? MOBILE_MORE_ITEMS.filter(({ enabled }) => enabled?.(data)).map(
+ (item) => ({
+ ...item,
+ label: item.badgeLabel?.(data) || item.label,
+ }),
)
: []),
],