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 link switching

Tim Wilson85a6d3b8b9f5
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/links/[...link]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/links/[...link]/page-client.tsx
index b7ecdd4165..35736777ca 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/links/[...link]/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/links/[...link]/page-client.tsx
@@ -28,7 +28,7 @@ import { useMetatags } from "@/ui/links/link-builder/use-metatags";
import { useKeyboardShortcut, useMediaQuery } from "@dub/ui";
import { cn } from "@dub/utils";
import { notFound, useParams, useRouter } from "next/navigation";
-import { useEffect, useRef } from "react";
+import { useEffect, useRef, useState } from "react";
import { useFormContext, useFormState } from "react-hook-form";
export function LinkPageClient() {
@@ -69,7 +69,7 @@ function LinkBuilder({ link }: { link: ExpandedLinkProps }) {
const { control, handleSubmit, reset, getValues } =
useFormContext<LinkFormData>();
- const { isSubmitSuccessful } = useFormState({ control });
+ const { isSubmitSuccessful, isDirty } = useFormState({ control });
const draftControlsRef = useRef<DraftControlsHandle>(null);
@@ -92,96 +92,108 @@ function LinkBuilder({ link }: { link: ExpandedLinkProps }) {
useMetatags();
+ // Go back to `/links` when ESC is pressed
+ useKeyboardShortcut("Escape", () => router.push(`/${workspace.slug}/links`), {
+ enabled: !isDirty,
+ });
+
+ const [isChangingLink, setIsChangingLink] = useState(false);
+
return (
- <>
- <EscapeShortcut />
- <div className="flex min-h-[calc(100vh-8px)] flex-col rounded-t-[inherit] bg-white">
- <div className="py-2 pl-4 pr-5">
- <LinkBuilderHeader
- className="p-0"
- foldersEnabled={!!workspace.flags?.linkFolders}
+ <div className="flex min-h-[calc(100vh-8px)] flex-col rounded-t-[inherit] bg-white">
+ <div className="py-2 pl-4 pr-5">
+ <LinkBuilderHeader
+ onSelectLink={(selectedLink) => {
+ if (selectedLink.id === link.id) return;
+
+ if (isDirty) {
+ if (
+ !confirm(
+ "You have unsaved changes. Are you sure you want to continue?",
+ )
+ )
+ return;
+ }
+
+ setIsChangingLink(true);
+ router.push(
+ `/${workspace.slug}/links/${selectedLink.domain}/${selectedLink.key}`,
+ );
+ }}
+ className="p-0"
+ foldersEnabled={!!workspace.flags?.linkFolders}
+ >
+ <div
+ className={cn(
+ "flex min-w-0 items-center gap-2 transition-opacity",
+ isChangingLink && "opacity-50",
+ )}
>
- <div className="flex min-w-0 items-center gap-2">
- <DraftControls
- ref={draftControlsRef}
- props={link}
- workspaceId={workspace.id!}
- />
- <div className="shrink-0">
- <LinkAnalyticsBadge link={link} />
- </div>
+ <DraftControls
+ ref={draftControlsRef}
+ props={link}
+ workspaceId={workspace.id!}
+ />
+ <div className="shrink-0">
+ <LinkAnalyticsBadge link={link} />
</div>
- </LinkBuilderHeader>
- </div>
- <form
- className={cn(
- "grid grow grid-cols-1 lg:grid-cols-[minmax(0,1fr)_300px]",
- "divide-neutral-200 border-t border-neutral-200 lg:divide-x lg:divide-y lg:divide-y-0",
- )}
- onSubmit={handleSubmit(onSubmit)}
- >
- <div className="relative flex min-h-full flex-col px-4 md:px-6">
- <div className="relative mx-auto flex w-full max-w-xl flex-col gap-7 pb-4 pt-10 lg:pb-10">
- <LinkBuilderDestinationUrlInput />
-
- <LinkBuilderShortLinkInput />
-
- <TagSelect />
+ </div>
+ </LinkBuilderHeader>
+ </div>
+ <form
+ className={cn(
+ "grid grow grid-cols-1 transition-opacity lg:grid-cols-[minmax(0,1fr)_300px]",
+ "divide-neutral-200 border-t border-neutral-200 lg:divide-x lg:divide-y lg:divide-y-0",
+ isChangingLink && "opacity-50",
+ )}
+ onSubmit={handleSubmit(onSubmit)}
+ >
+ <div className="relative flex min-h-full flex-col px-4 md:px-6">
+ <div className="relative mx-auto flex w-full max-w-xl flex-col gap-7 pb-4 pt-10 lg:pb-10">
+ <LinkBuilderDestinationUrlInput />
- <LinkCommentsInput />
+ <LinkBuilderShortLinkInput />
- <ConversionTrackingToggle />
+ <TagSelect />
- {isDesktop && (
- <LinkFeatureButtons className="mt-1 flex-wrap" variant="page" />
- )}
+ <LinkCommentsInput />
- <OptionsList />
- </div>
+ <ConversionTrackingToggle />
{isDesktop && (
- <>
- <div className="grow" />
- <LinkActionBar />
- </>
+ <LinkFeatureButtons className="mt-1 flex-wrap" variant="page" />
)}
+
+ <OptionsList />
</div>
- <div className="px-4 md:px-6 lg:bg-neutral-50 lg:px-0">
- <div className="mx-auto max-w-xl divide-neutral-200 lg:divide-y">
- <div className="py-4 lg:px-4 lg:py-6">
- <QRCodePreview />
- </div>
- <div className="py-4 lg:px-4 lg:py-6">
- <LinkPreview />
- </div>
+
+ {isDesktop && (
+ <>
+ <div className="grow" />
+ <LinkActionBar />
+ </>
+ )}
+ </div>
+ <div className="px-4 md:px-6 lg:bg-neutral-50 lg:px-0">
+ <div className="mx-auto max-w-xl divide-neutral-200 lg:divide-y">
+ <div className="py-4 lg:px-4 lg:py-6">
+ <QRCodePreview />
+ </div>
+ <div className="py-4 lg:px-4 lg:py-6">
+ <LinkPreview />
</div>
</div>
- {!isDesktop && (
- <LinkActionBar>
- <LinkFeatureButtons variant="page" />
- </LinkActionBar>
- )}
- </form>
- </div>
- </>
+ </div>
+ {!isDesktop && (
+ <LinkActionBar>
+ <LinkFeatureButtons variant="page" />
+ </LinkActionBar>
+ )}
+ </form>
+ </div>
);
}
-function EscapeShortcut() {
- const router = useRouter();
- const { slug } = useWorkspace();
-
- const { control } = useFormContext<LinkFormData>();
- const { isDirty } = useFormState({ control });
-
- // Go back to `/links` when ESC is pressed
- useKeyboardShortcut("Escape", () => router.push(`/${slug}/links`), {
- enabled: !isDirty,
- });
-
- return null;
-}
-
function LoadingSkeleton() {
return (
<div className="flex min-h-[calc(100vh-8px)] flex-col rounded-t-[inherit] bg-white">
@@ -208,14 +220,7 @@ function LoadingSkeleton() {
</div>
</div>
<div className="px-4 md:px-6 lg:bg-neutral-50 lg:px-0">
- <div className="mx-auto max-w-xl divide-neutral-200 lg:divide-y">
- {/* <div className="py-4 lg:px-4 lg:py-6">
- <QRCodePreview />
- </div>
- <div className="py-4 lg:px-4 lg:py-6">
- <LinkPreview />
- </div> */}
- </div>
+ <div className="mx-auto max-w-xl divide-neutral-200 lg:divide-y"></div>
</div>
</div>
</div>
diff --git a/apps/web/ui/links/link-builder/link-builder-header.tsx b/apps/web/ui/links/link-builder/link-builder-header.tsx
index eb626a66a3..803708776e 100644
--- a/apps/web/ui/links/link-builder/link-builder-header.tsx
+++ b/apps/web/ui/links/link-builder/link-builder-header.tsx
@@ -1,24 +1,30 @@
+import useLinks from "@/lib/swr/use-links";
+import { LinkProps } from "@/lib/types";
import { FolderDropdown } from "@/ui/folders/folder-dropdown";
-import { LinkLogo } from "@dub/ui";
+import { Combobox, LinkLogo } from "@dub/ui";
import {
cn,
getApexDomain,
+ getPrettyUrl,
getUrlWithoutUTMParams,
linkConstructor,
+ truncate,
} from "@dub/utils";
import { ChevronRight, X } from "lucide-react";
-import { PropsWithChildren, useMemo } from "react";
+import { PropsWithChildren, useMemo, useState } from "react";
import { useFormContext, useWatch } from "react-hook-form";
import { useDebounce } from "use-debounce";
import { LinkFormData, useLinkBuilderContext } from "./link-builder-provider";
export function LinkBuilderHeader({
onClose,
+ onSelectLink,
children,
className,
foldersEnabled,
}: PropsWithChildren<{
onClose?: () => void;
+ onSelectLink?: (link: LinkProps) => void;
className?: string;
foldersEnabled?: boolean;
}>) {
@@ -50,7 +56,7 @@ export function LinkBuilderHeader({
)}
>
{foldersEnabled && (
- <div className="flex min-w-0 items-center gap-2">
+ <div className="flex min-w-0 items-center gap-1">
<FolderDropdown
hideViewAll={true}
disableAutoRedirect={true}
@@ -64,33 +70,124 @@ export function LinkBuilderHeader({
})}
/>
- <ChevronRight className="hidden size-4 text-neutral-500 md:block" />
+ <ChevronRight className="hidden size-4 shrink-0 text-neutral-500 md:block" />
+
+ {onSelectLink ? (
+ <div className="min-w-0">
+ <LinkSelector selectedLink={props!} onSelect={onSelectLink} />
+ </div>
+ ) : (
+ <div className="flex min-w-0 items-center gap-2">
+ <LinkLogo
+ apexDomain={getApexDomain(debouncedUrl)}
+ className="size-6 shrink-0 sm:size-6 [&>*]:size-3 sm:[&>*]:size-4"
+ />
+ <h3 className="!mt-0 max-w-sm truncate text-sm font-medium">
+ {props ? `Edit ${shortLink}` : "New link"}
+ </h3>
+ </div>
+ )}
</div>
)}
+ <div className="flex items-center gap-4">
+ {children}
+ {onClose && (
+ <button
+ type="button"
+ onClick={onClose}
+ className="group hidden rounded-full p-2 text-neutral-500 transition-all duration-75 hover:bg-neutral-100 focus:outline-none active:bg-neutral-200 md:block"
+ >
+ <X className="h-5 w-5" />
+ </button>
+ )}
+ </div>
+ </div>
+ );
+}
+
+const getLinkOption = (link: LinkProps) => ({
+ value: link.id,
+ label: linkConstructor({ ...link, pretty: true }),
+ icon: (
+ <LinkLogo
+ apexDomain={getApexDomain(link.url)}
+ className="mr-1 size-4 shrink-0 sm:size-4"
+ />
+ ),
+ meta: {
+ url: link.url,
+ },
+});
+
+function LinkSelector({
+ selectedLink: selectedLinkProp,
+ onSelect,
+ disabled,
+}: {
+ selectedLink: LinkProps;
+ onSelect: (link: LinkProps) => void;
+ disabled?: boolean;
+}) {
+ const [search, setSearch] = useState("");
+ const [debouncedSearch] = useDebounce(search, 500);
+
+ const { links } = useLinks(
+ {
+ search: debouncedSearch,
+ },
+ {
+ keepPreviousData: false,
+ },
+ );
+
+ const options = useMemo(
+ () => links?.map((link) => getLinkOption(link)),
+ [links],
+ );
+
+ const [selectedLink, setSelectedLink] = useState(selectedLinkProp);
+ const selectedOption = useMemo(
+ () => getLinkOption(selectedLink),
+ [selectedLink],
+ );
- <div className="flex w-full min-w-0 items-center justify-between gap-2">
- <div className="flex min-w-0 items-center gap-2">
+ return (
+ <Combobox
+ caret
+ matchTriggerWidth
+ side="top" // Since this control is near the bottom of the page, prefer top to avoid jumping
+ options={options}
+ selected={selectedOption}
+ setSelected={(selected) => {
+ const link = links?.find((link) => link.id === selected.value);
+ if (!link) return;
+
+ setSelectedLink(link);
+ onSelect(link);
+ }}
+ shouldFilter={false}
+ onSearchChange={setSearch}
+ buttonProps={{
+ disabled,
+ className: cn(
+ "h-auto py-2 px-2 w-full max-w-full text-neutral-700 border-none items-start text-sm font-medium !ring-0",
+ "hover:bg-neutral-100 active:bg-neutral-200 data-[state=open]:bg-neutral-100",
+ ),
+ }}
+ >
+ {selectedLink ? (
+ <div className="flex items-center gap-2">
<LinkLogo
- apexDomain={getApexDomain(debouncedUrl)}
- className="size-6 shrink-0 sm:size-6 [&>*]:size-3 sm:[&>*]:size-4"
+ apexDomain={getApexDomain(selectedLink.url)}
+ className="size-4 shrink-0 sm:size-4"
/>
- <h3 className="!mt-0 max-w-sm truncate text-sm font-medium">
- {props ? `Edit ${shortLink}` : "New link"}
- </h3>
+ <span className="min-w-0 truncate">
+ {truncate(getPrettyUrl(selectedLink.shortLink), 32)}
+ </span>
</div>
- <div className="flex items-center gap-4">
- {children}
- {onClose && (
- <button
- type="button"
- onClick={onClose}
- className="group hidden rounded-full p-2 text-neutral-500 transition-all duration-75 hover:bg-neutral-100 focus:outline-none active:bg-neutral-200 md:block"
- >
- <X className="h-5 w-5" />
- </button>
- )}
- </div>
- </div>
- </div>
+ ) : (
+ <div className="my-0.5 h-5 w-1/3 animate-pulse rounded bg-neutral-200" />
+ )}
+ </Combobox>
);
}