The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add basic toggles
Tim Wilson9c1227ce1df9
diff --git a/apps/web/ui/modals/add-edit-link-modal-new/index.tsx b/apps/web/ui/modals/add-edit-link-modal-new/index.tsxindex 3df1077d0f..f5cc270dbc 100644--- a/apps/web/ui/modals/add-edit-link-modal-new/index.tsx+++ b/apps/web/ui/modals/add-edit-link-modal-new/index.tsx@@ -49,6 +49,7 @@ import { mutate } from "swr";import { useDebounce } from "use-debounce";import { LinkPreview } from "./link-preview";import { QRCodePreview } from "./qr-code-preview";+import { TogglesDropdown, TogglesList } from "./toggles";import { useMetatags } from "./use-metatags";import { UTMBuilder } from "./utm-builder";@@ -56,6 +57,7 @@ export const LinkModalContext = createContext<{workspaceId?: string;workspacePlan?: string;workspaceLogo?: string;+ conversionEnabled?: boolean;generatingMetatags: boolean;}>({ generatingMetatags: false });@@ -92,7 +94,13 @@ function AddEditLinkModalInner({const { slug } = params;const searchParams = useSearchParams();const { queryParams } = useRouterStuff();- const { id: workspaceId, plan, nextPlan, logo } = useWorkspace();+ const {+ id: workspaceId,+ plan,+ nextPlan,+ logo,+ conversionEnabled,+ } = useWorkspace();const {control,@@ -209,6 +217,7 @@ function AddEditLinkModalInner({workspaceId,workspacePlan: plan,workspaceLogo: logo ?? undefined,+ conversionEnabled: conversionEnabled,generatingMetatags,}}>@@ -322,7 +331,7 @@ function AddEditLinkModalInner({)}><div className="scrollbar-hide px-6 md:overflow-auto">- <div className="grid gap-8 py-4">+ <div className="flex min-h-full flex-col gap-8 py-4"><Controllername="url"control={control}@@ -398,6 +407,10 @@ function AddEditLinkModalInner({)}/></div>++ <div className="flex grow flex-col justify-end">+ <TogglesList />+ </div></div></div><div className="scrollbar-hide px-6 md:overflow-auto md:pl-0 md:pr-4">@@ -411,7 +424,9 @@ function AddEditLinkModalInner({</div></div><div className="flex items-center justify-between border-t border-gray-100 bg-gray-50 p-4">- <div></div>+ <div className="flex items-center gap-2">+ <TogglesDropdown />+ </div>{homepageDemo ? (<ButtondisabledTooltip="This is a demo link. You can't edit it."diff --git a/apps/web/ui/modals/add-edit-link-modal-new/toggles.tsx b/apps/web/ui/modals/add-edit-link-modal-new/toggles.tsxnew file mode 100644index 0000000000..c1fd8ae752--- /dev/null+++ b/apps/web/ui/modals/add-edit-link-modal-new/toggles.tsx@@ -0,0 +1,267 @@+import { AlertCircleFill, CheckCircleFill, X } from "@/ui/shared/icons";+import { ProBadgeTooltip } from "@/ui/shared/pro-badge-tooltip";+import {+ Button,+ Popover,+ SimpleTooltipContent,+ Tooltip,+ useKeyboardShortcut,+} from "@dub/ui";+import {+ Dots,+ Incognito,+ LoadingSpinner,+ SquareChart,+ WindowSearch,+} from "@dub/ui/src/icons";+import { fetcher, isValidUrl as isValidUrlFn } from "@dub/utils";+import { AnimatePresence, motion } from "framer-motion";+import { ReactNode, useContext, useMemo, useState } from "react";+import { useFormContext } from "react-hook-form";+import useSWR from "swr";+import { useDebounce } from "use-debounce";+import { LinkFormData, LinkModalContext } from ".";++const TOGGLES = [+ {+ key: "rewrite",+ icon: Incognito,+ label: "Link Cloaking",+ description:+ "Mask your destination URL so your users only see the short link in the browser address bar.",+ learnMoreUrl: "https://dub.co/help/article/link-cloaking",+ shortcutKey: "l",+ badge: LinkCloakingToggleBadge,+ },+ {+ key: "trackConversion",+ icon: SquareChart,+ label: "Conversion Tracking",+ description:+ "Track conversions on your short link to measure the effectiveness of your marketing campaigns.",+ learnMoreUrl: "https://dub.co/help/article/conversion-tracking",+ shortcutKey: "c",+ conversionEnabled: true,+ },+ {+ key: "doIndex",+ icon: WindowSearch,+ label: "Search Engine Indexing",+ description:+ "Allow search engines to index your short link. Disabled by default.",+ learnMoreUrl: "https://dub.co/help/article/how-noindex-works",+ shortcutKey: "s",+ },+];++export function TogglesDropdown() {+ const { conversionEnabled } = useContext(LinkModalContext);++ const { watch, setValue } = useFormContext<LinkFormData>();+ const data = watch();++ const [openPopover, setOpenPopover] = useState(false);++ const toggles = useMemo(+ () =>+ TOGGLES.filter((toggle) =>+ toggle.conversionEnabled ? conversionEnabled || data[toggle.key] : true,+ ),+ [conversionEnabled, data],+ );++ useKeyboardShortcut(+ toggles.map(({ shortcutKey }) => shortcutKey),+ (e) => {+ const toggle = toggles.find(({ shortcutKey }) => shortcutKey === e.key);+ if (!toggle) return;++ setOpenPopover(false);+ setValue(toggle.key as any, !data[toggle.key]);+ },+ { modal: true },+ );++ return (+ <Popover+ align="start"+ content={+ <div className="grid p-1 md:min-w-72">+ {toggles.map((toggle) => {+ const enabled = data[toggle.key];++ return (+ <Button+ type="button"+ variant="outline"+ key={toggle.key}+ onClick={() => {+ setOpenPopover(false);+ setValue(toggle.key as any, !enabled);+ }}+ className="h-9 w-full justify-start px-2 text-sm text-gray-700"+ textWrapperClassName="grow"+ text={+ <div className="flex items-center justify-between gap-2">+ <div className="flex items-center gap-1">+ <toggle.icon className="mr-1 size-4 text-gray-950" />+ {enabled ? "Remove" : "Add"} {toggle.label}+ <ProBadgeTooltip+ content={+ <SimpleTooltipContent+ title={toggle.description}+ cta="Learn more."+ href={toggle.learnMoreUrl}+ />+ }+ />+ </div>+ <kbd className="flex size-6 cursor-default items-center justify-center rounded-md border border-gray-200 font-sans text-xs text-gray-800">+ {toggle.shortcutKey.toUpperCase()}+ </kbd>+ </div>+ }+ />+ );+ })}+ </div>+ }+ openPopover={openPopover}+ setOpenPopover={setOpenPopover}+ >+ <Button+ variant="secondary"+ icon={<Dots className="size-4" />}+ className="h-9 px-2.5"+ />+ </Popover>+ );+}++export function TogglesList() {+ const { watch, setValue } = useFormContext<LinkFormData>();+ const data = watch();++ const enabledToggles = useMemo(+ () => TOGGLES.filter(({ key }) => data[key]),+ [data],+ );++ return enabledToggles.length ? (+ <div className="flex flex-wrap gap-2">+ <AnimatePresence>+ {enabledToggles.map((toggle) => {+ const Component = toggle.badge || ToggleBadge;+ return (+ <motion.div+ key={toggle.key}+ initial={{ opacity: 0 }}+ animate={{ opacity: 1 }}+ transition={{ duration: 0.1 }}+ >+ <Component+ toggle={toggle}+ onRemove={() => setValue(toggle.key as any, false)}+ />+ </motion.div>+ );+ })}+ </AnimatePresence>+ </div>+ ) : null;+}++function ToggleBadge({+ toggle,+ onRemove,+ icon,+}: {+ toggle: (typeof TOGGLES)[number];+ onRemove: () => void;+ icon?: ReactNode;+}) {+ return (+ <span className="group flex cursor-default items-center gap-1.5 rounded-md border border-gray-200 bg-gray-50 pl-1.5 text-xs text-gray-600">+ {icon}+ {toggle.label}+ <button+ type="button"+ onClick={onRemove}+ className="-ml-1 p-1 text-gray-400 hover:text-gray-500"+ >+ <X className="size-3.5" />+ </button>+ </span>+ );+}++function LinkCloakingToggleBadge({+ toggle,+ onRemove,+}: {+ toggle: (typeof TOGGLES)[number];+ onRemove: () => void;+}) {+ const { watch } = useFormContext<LinkFormData>();+ const [url, domain] = watch(["url", "domain"]);+ const [debouncedUrl] = useDebounce(url, 500);+ const isValidUrl = useMemo(+ () => debouncedUrl && isValidUrlFn(debouncedUrl),+ [debouncedUrl],+ );++ const { data, isLoading } = useSWR<{ iframeable: boolean }>(+ domain && isValidUrl+ ? `/api/links/iframeable?url=${debouncedUrl}&domain=${domain}`+ : null,+ fetcher,+ );++ const badge = useMemo(+ () => (+ <ToggleBadge+ toggle={toggle}+ onRemove={onRemove}+ icon={+ isLoading ? (+ <LoadingSpinner className="size-3.5" />+ ) : !data ? null : data?.iframeable ? (+ <CheckCircleFill className="size-3.5 text-green-500" />+ ) : (+ <AlertCircleFill className="size-3.5 text-yellow-500" />+ )+ }+ />+ ),+ [data, isLoading],+ );++ return data && !data?.iframeable ? (+ <Tooltip+ content={+ <div className="block max-w-sm text-pretty px-4 py-2 text-center text-sm text-gray-700">+ We will try to cloak it with{" "}+ <a+ href="https://nextjs.org/docs/pages/api-reference/functions/next-response#rewrite"+ target="_blank"+ className="text-gray-500 underline underline-offset-2 hover:text-gray-700"+ >+ Next.js Rewrites+ </a>+ , but it might not work as expected.{" "}+ <a+ href="https://dub.co/help/article/link-cloaking"+ target="_blank"+ className="text-gray-500 underline underline-offset-2 hover:text-gray-700"+ >+ Learn more.+ </a>+ </div>+ }+ >+ <div>{badge}</div>+ </Tooltip>+ ) : (+ badge+ );+}diff --git a/packages/ui/src/icons/nucleo/incognito.tsx b/packages/ui/src/icons/nucleo/incognito.tsxnew file mode 100644index 0000000000..b9a6ce874a--- /dev/null+++ b/packages/ui/src/icons/nucleo/incognito.tsx@@ -0,0 +1,63 @@+import { SVGProps } from "react";++export function Incognito(props: SVGProps<SVGSVGElement>) {+ return (+ <svg+ height="18"+ width="18"+ viewBox="0 0 18 18"+ xmlns="http://www.w3.org/2000/svg"+ {...props}+ >+ <g fill="currentColor">+ <circle+ cx="5.25"+ cy="13.75"+ fill="none"+ r="2.5"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ <circle+ cx="12.75"+ cy="13.75"+ fill="none"+ r="2.5"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ <path+ d="M11.883,1.75H6.117c-.498,0-.92,.366-.99,.859l-.377,2.641-3,3h14.5l-3-3-.377-2.641c-.07-.493-.492-.859-.99-.859Z"+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ <path+ d="M7.58,12.842c.362-.365,.865-.592,1.42-.592,.555,0,1.058,.226,1.42,.592"+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ <line+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ x1="4.75"+ x2="13.25"+ y1="5.25"+ y2="5.25"+ />+ </g>+ </svg>+ );+}diff --git a/packages/ui/src/icons/nucleo/index.ts b/packages/ui/src/icons/nucleo/index.tsindex 75566a224c..785414832e 100644--- a/packages/ui/src/icons/nucleo/index.ts+++ b/packages/ui/src/icons/nucleo/index.ts@@ -52,6 +52,7 @@ export * from "./headset";export * from "./hexadecagon-star";export * from "./hyperlink";export * from "./icosahedron";+export * from "./incognito";export * from "./input-password";export * from "./input-search";export * from "./invoice-dollar";@@ -78,6 +79,7 @@ export * from "./satellite-dish";export * from "./scribble";export * from "./shield-check";export * from "./sliders";+export * from "./square-chart";export * from "./square-layout-grid6";export * from "./stars2";export * from "./table-icon";@@ -92,5 +94,6 @@ export * from "./users";export * from "./users6";export * from "./watch";export * from "./window";+export * from "./window-search";export * from "./window-settings";export * from "./xmark";diff --git a/packages/ui/src/icons/nucleo/square-chart.tsx b/packages/ui/src/icons/nucleo/square-chart.tsxnew file mode 100644index 0000000000..7565611d47--- /dev/null+++ b/packages/ui/src/icons/nucleo/square-chart.tsx@@ -0,0 +1,62 @@+import { SVGProps } from "react";++export function SquareChart(props: SVGProps<SVGSVGElement>) {+ return (+ <svg+ height="18"+ width="18"+ viewBox="0 0 18 18"+ xmlns="http://www.w3.org/2000/svg"+ {...props}+ >+ <g fill="currentColor">+ <rect+ height="12.5"+ width="12.5"+ fill="none"+ rx="2"+ ry="2"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ x="2.75"+ y="2.75"+ />+ <line+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ x1="5.75"+ x2="5.75"+ y1="8"+ y2="12.25"+ />+ <line+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ x1="12.25"+ x2="12.25"+ y1="10.25"+ y2="12.25"+ />+ <line+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ x1="9"+ x2="9"+ y1="5.75"+ y2="12.25"+ />+ </g>+ </svg>+ );+}diff --git a/packages/ui/src/icons/nucleo/window-search.tsx b/packages/ui/src/icons/nucleo/window-search.tsxnew file mode 100644index 0000000000..792eeea205--- /dev/null+++ b/packages/ui/src/icons/nucleo/window-search.tsx@@ -0,0 +1,58 @@+import { SVGProps } from "react";++export function WindowSearch(props: SVGProps<SVGSVGElement>) {+ return (+ <svg+ height="18"+ width="18"+ viewBox="0 0 18 18"+ xmlns="http://www.w3.org/2000/svg"+ {...props}+ >+ <g fill="currentColor">+ <circle cx="4.25" cy="5.25" fill="currentColor" r=".75" stroke="none" />+ <circle cx="6.75" cy="5.25" fill="currentColor" r=".75" stroke="none" />+ <circle+ cx="14"+ cy="14"+ fill="none"+ r="2.25"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ <line+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ x1="1.75"+ x2="16.25"+ y1="7.75"+ y2="7.75"+ />+ <line+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ x1="15.59"+ x2="17.25"+ y1="15.59"+ y2="17.25"+ />+ <path+ d="M16.25,9.784V4.75c0-1.104-.895-2-2-2H3.75c-1.105,0-2,.896-2,2V13.25c0,1.104,.895,2,2,2h5.653"+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ </g>+ </svg>+ );+}