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.

Folder updates

Tim Wilson9fc7d0d0d3af
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 4afcf74f38..1c3834fa2f 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
@@ -5,6 +5,7 @@ import useWorkspace from "@/lib/swr/use-workspace";
import { ExpandedLinkProps } from "@/lib/types";
import { LinkAnalyticsBadge } from "@/ui/links/link-analytics-badge";
import { LinkBuilderDestinationUrlInput } from "@/ui/links/link-builder/controls/link-builder-destination-url-input";
+import { LinkBuilderFolderSelector } from "@/ui/links/link-builder/controls/link-builder-folder-selector";
import { LinkBuilderShortLinkInput } from "@/ui/links/link-builder/controls/link-builder-short-link-input";
import { LinkCommentsInput } from "@/ui/links/link-builder/controls/link-comments-input";
import { ConversionTrackingToggle } from "@/ui/links/link-builder/conversion-tracking-toggle";
@@ -149,6 +150,7 @@ function LinkBuilder({ link }: { link: ExpandedLinkProps }) {
}}
className="p-0"
foldersEnabled={!!workspace.flags?.linkFolders}
+ linkToFolder={!!workspace.flags?.linkFolders}
>
<div
className={cn(
@@ -234,6 +236,9 @@ function LinkBuilder({ link }: { link: ExpandedLinkProps }) {
</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">
+ <LinkBuilderFolderSelector />
+ </div>
<div className="py-4 lg:px-4 lg:py-6">
<QRCodePreview />
</div>
@@ -265,7 +270,7 @@ const Controls = memo(({ link }: { link: ExpandedLinkProps }) => {
openPopover={openPopover}
setOpenPopover={setOpenPopover}
shortcutsEnabled={openPopover}
- options={["id", "move", "archive", "transfer", "delete"]}
+ options={["id", "archive", "transfer", "delete"]}
onMoveSuccess={(folderId) => {
setValue("folderId", folderId);
reset(getValues(), { keepValues: true, keepDirty: false });
@@ -282,7 +287,7 @@ const Controls = memo(({ link }: { link: ExpandedLinkProps }) => {
function LoadingSkeleton() {
return (
<div className="flex min-h-[calc(100vh-8px)] flex-col rounded-t-[inherit] bg-white">
- <div className="flex items-center justify-between gap-4 py-3 pl-4 pr-5">
+ <div className="flex items-center justify-between gap-4 py-2.5 pl-4 pr-5">
<div className="h-8 w-64 max-w-full animate-pulse rounded-md bg-neutral-100" />
<div className="h-7 w-32 max-w-full animate-pulse rounded-md bg-neutral-100" />
</div>
diff --git a/apps/web/ui/folders/folder-dropdown.tsx b/apps/web/ui/folders/folder-dropdown.tsx
index a9a5a70dbb..2717e94fe6 100644
--- a/apps/web/ui/folders/folder-dropdown.tsx
+++ b/apps/web/ui/folders/folder-dropdown.tsx
@@ -10,7 +10,7 @@ import { cn } from "@dub/utils";
import { ChevronsUpDown } from "lucide-react";
import Link from "next/link";
import { useRouter, useSearchParams } from "next/navigation";
-import { useEffect, useMemo, useState } from "react";
+import { ReactNode, useEffect, useMemo, useState } from "react";
import { useDebounce } from "use-debounce";
import { useAddFolderModal } from "../modals/add-folder-modal";
import { FolderIcon } from "./folder-icon";
@@ -22,8 +22,10 @@ interface FolderDropdownProps {
hideFolderIcon?: boolean;
buttonClassName?: string;
buttonTextClassName?: string;
+ iconClassName?: string;
disableAutoRedirect?: boolean; // decide if we should auto redirect to the folder after it's created
selectedFolderId?: string;
+ loadingPlaceholder?: ReactNode;
}
export const FolderDropdown = ({
@@ -33,8 +35,10 @@ export const FolderDropdown = ({
hideFolderIcon = false,
buttonClassName,
buttonTextClassName,
+ iconClassName,
disableAutoRedirect = false,
selectedFolderId,
+ loadingPlaceholder,
}: FolderDropdownProps) => {
const router = useRouter();
const { slug, plan, defaultFolderId } = useWorkspace();
@@ -154,7 +158,8 @@ export const FolderDropdown = ({
}, [selectedFolder]);
if (folderId && folderId !== "unsorted" && !selectedFolderData) {
- return <FolderSwitcherPlaceholder />;
+ // if (true) {
+ return loadingPlaceholder ?? <FolderSwitcherPlaceholder />;
}
return (
@@ -195,6 +200,7 @@ export const FolderDropdown = ({
folder={selectedFolder}
shape="square"
className="hidden md:block"
+ iconClassName={iconClassName}
/>
) : undefined
}
@@ -212,7 +218,7 @@ export const FolderDropdown = ({
className: cn(
"group flex items-center gap-2 rounded-lg px-2 py-1 w-fit",
variant === "inline" && "border-none !ring-0",
- "transition-colors hover:bg-neutral-100 active:bg-neutral-200 data-[state=open]:bg-neutral-100",
+ "transition-all hover:bg-neutral-100 active:bg-neutral-200 data-[state=open]:bg-neutral-100",
buttonClassName,
),
textWrapperClassName: cn(
diff --git a/apps/web/ui/links/link-builder/controls/link-builder-folder-selector.tsx b/apps/web/ui/links/link-builder/controls/link-builder-folder-selector.tsx
new file mode 100644
index 0000000000..df62f68bae
--- /dev/null
+++ b/apps/web/ui/links/link-builder/controls/link-builder-folder-selector.tsx
@@ -0,0 +1,33 @@
+import { FolderDropdown } from "@/ui/folders/folder-dropdown";
+import { useFormContext, useWatch } from "react-hook-form";
+import { LinkFormData } from "../link-builder-provider";
+
+export function LinkBuilderFolderSelector() {
+ const { setValue } = useFormContext<LinkFormData>();
+ const folderId = useWatch({ name: "folderId" });
+
+ return (
+ <div>
+ <div className="mb-1 flex items-center gap-2">
+ <h2 className="text-sm font-medium text-neutral-700">Folder</h2>
+ </div>
+ <FolderDropdown
+ variant="input"
+ hideViewAll={true}
+ disableAutoRedirect={true}
+ onFolderSelect={(folder) => {
+ setValue("folderId", folder.id === "unsorted" ? null : folder.id, {
+ shouldDirty: true,
+ });
+ }}
+ buttonClassName="w-full min-w-0 bg-transparent h-8 pl-1 rounded-md"
+ buttonTextClassName="text-sm md:text-sm font-medium"
+ iconClassName="size-3"
+ selectedFolderId={folderId ?? undefined}
+ loadingPlaceholder={
+ <div className="my-px h-8 w-full animate-pulse rounded-lg bg-neutral-200" />
+ }
+ />
+ </div>
+ );
+}
diff --git a/apps/web/ui/links/link-builder/link-action-bar.tsx b/apps/web/ui/links/link-builder/link-action-bar.tsx
index e4984d0d5b..073f5ff372 100644
--- a/apps/web/ui/links/link-builder/link-action-bar.tsx
+++ b/apps/web/ui/links/link-builder/link-action-bar.tsx
@@ -6,10 +6,9 @@ import { LinkFormData } from "./link-builder-provider";
export function LinkActionBar({ children }: PropsWithChildren) {
const { control, reset } = useFormContext<LinkFormData>();
- const { isDirty, isSubmitting, isSubmitSuccessful, dirtyFields } =
- useFormState({
- control,
- });
+ const { isDirty, isSubmitting, isSubmitSuccessful } = useFormState({
+ control,
+ });
const showActionBar = isDirty || isSubmitting;
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 b17661de65..17d6fd5223 100644
--- a/apps/web/ui/links/link-builder/link-builder-header.tsx
+++ b/apps/web/ui/links/link-builder/link-builder-header.tsx
@@ -1,6 +1,9 @@
+import { unsortedLinks } from "@/lib/folder/constants";
+import { getPlanCapabilities } from "@/lib/plan-capabilities";
+import useFolder from "@/lib/swr/use-folder";
import useLinks from "@/lib/swr/use-links";
import { LinkProps } from "@/lib/types";
-import { FolderDropdown } from "@/ui/folders/folder-dropdown";
+import { FolderIcon } from "@/ui/folders/folder-icon";
import { Combobox, LinkLogo } from "@dub/ui";
import {
cn,
@@ -11,10 +14,11 @@ import {
truncate,
} from "@dub/utils";
import { ChevronRight, X } from "lucide-react";
+import Link from "next/link";
import { PropsWithChildren, useMemo, useState } from "react";
-import { useFormContext, useWatch } from "react-hook-form";
+import { useFormState, useWatch } from "react-hook-form";
import { useDebounce } from "use-debounce";
-import { LinkFormData, useLinkBuilderContext } from "./link-builder-provider";
+import { useLinkBuilderContext } from "./link-builder-provider";
export function LinkBuilderHeader({
onClose,
@@ -22,17 +26,18 @@ export function LinkBuilderHeader({
children,
className,
foldersEnabled,
+ linkToFolder,
}: PropsWithChildren<{
onClose?: () => void;
onSelectLink?: (link: LinkProps) => void;
className?: string;
foldersEnabled?: boolean;
+ linkToFolder?: boolean;
}>) {
- const { control, setValue } = useFormContext<LinkFormData>();
- const { props } = useLinkBuilderContext();
+ const { props, workspace } = useLinkBuilderContext();
+ const { isDirty } = useFormState();
const [url, key, domain, folderId] = useWatch({
- control,
name: ["url", "key", "domain", "folderId"],
});
@@ -48,39 +53,90 @@ export function LinkBuilderHeader({
[key, domain],
);
+ const { canAddFolder } = getPlanCapabilities(workspace.plan);
+ const { folder, loading: isFolderLoading } = useFolder({
+ folderId,
+ enabled: !!folderId,
+ });
+
+ const folderContent = useMemo(() => {
+ if (!foldersEnabled) {
+ return (
+ <span className="text-sm font-semibold text-neutral-800">Links</span>
+ );
+ }
+
+ if (folderId && folderId !== "unsorted" && folder?.id !== folderId)
+ return (
+ <div className="h-7 w-24 animate-pulse rounded-md bg-neutral-200" />
+ );
+
+ let selectedFolder =
+ !folderId || folderId === "unsorted"
+ ? unsortedLinks
+ : folder ?? unsortedLinks;
+
+ return (
+ <>
+ {canAddFolder && <FolderIcon folder={selectedFolder} shape="square" />}
+ <span className="text-sm font-semibold text-neutral-800">
+ {selectedFolder.name}
+ </span>
+ </>
+ );
+ }, [foldersEnabled, folderId, folder, isFolderLoading]);
+
return (
<div
className={cn(
- "flex flex-col items-start gap-2 px-6 py-4 md:flex-row md:items-center md:justify-between",
+ "flex flex-col items-start gap-2 px-6 py-3 md:flex-row md:items-center md:justify-between",
className,
)}
>
<div className="flex min-w-0 max-w-full items-center gap-1">
{foldersEnabled && (
<>
- <FolderDropdown
- hideViewAll={true}
- disableAutoRedirect={true}
- onFolderSelect={(folder) => {
- setValue("folderId", folder.id, { shouldDirty: true });
- }}
- buttonClassName="max-w-60 md:max-w-[24rem] min-w-0"
- buttonTextClassName="text-sm md:text-sm font-medium"
- selectedFolderId={folderId ?? undefined}
- />
+ {linkToFolder && canAddFolder ? (
+ <Link
+ href={`/${workspace.slug}/links?folderId=${folderId ?? "unsorted"}`}
+ className="flex items-center gap-2 rounded-md py-1 pl-1 pr-2 hover:bg-neutral-100"
+ onClick={
+ isDirty
+ ? (e) => {
+ if (
+ !window.confirm(
+ "Are you sure you want to discard your changes and continue?",
+ )
+ )
+ e.preventDefault();
+ }
+ : undefined
+ }
+ >
+ {folderContent}
+ </Link>
+ ) : (
+ <div className="flex items-center gap-2 px-1.5">
+ {folderContent}
+ </div>
+ )}
<ChevronRight className="hidden size-4 shrink-0 text-neutral-500 md:block" />
</>
)}
{onSelectLink ? (
<div className="min-w-0">
- <LinkSelector selectedLink={props!} onSelect={onSelectLink} />
+ <LinkSelector
+ selectedLink={props!}
+ onSelect={onSelectLink}
+ folderId={folderId}
+ />
</div>
) : (
- <div className="flex min-w-0 items-center gap-2">
+ <div className="flex min-w-0 items-center gap-2 px-1">
<LinkLogo
apexDomain={getApexDomain(debouncedUrl)}
- className="size-6 shrink-0 sm:size-6 [&>*]:size-3 sm:[&>*]:size-4"
+ className="size-5 shrink-0 sm:size-5 [&>*]:size-3 sm:[&>*]:size-4"
/>
<h3 className="!mt-0 max-w-sm truncate text-sm font-medium">
{props ? `Edit ${shortLink}` : "New link"}
@@ -122,10 +178,12 @@ function LinkSelector({
selectedLink: selectedLinkProp,
onSelect,
disabled,
+ folderId,
}: {
selectedLink: LinkProps;
onSelect: (link: LinkProps) => void;
disabled?: boolean;
+ folderId?: string;
}) {
const [search, setSearch] = useState("");
const [debouncedSearch] = useDebounce(search, 500);
@@ -133,6 +191,7 @@ function LinkSelector({
const { links } = useLinks(
{
search: debouncedSearch,
+ ...(folderId && { folderId }),
},
{
keepPreviousData: false,
diff --git a/apps/web/ui/modals/link-builder/index.tsx b/apps/web/ui/modals/link-builder/index.tsx
index d9ac6e450c..40d53eb5d8 100644
--- a/apps/web/ui/modals/link-builder/index.tsx
+++ b/apps/web/ui/modals/link-builder/index.tsx
@@ -3,6 +3,7 @@
import useWorkspace from "@/lib/swr/use-workspace";
import { ExpandedLinkProps } from "@/lib/types";
import { LinkBuilderDestinationUrlInput } from "@/ui/links/link-builder/controls/link-builder-destination-url-input";
+import { LinkBuilderFolderSelector } from "@/ui/links/link-builder/controls/link-builder-folder-selector";
import { LinkBuilderShortLinkInput } from "@/ui/links/link-builder/controls/link-builder-short-link-input";
import { LinkCommentsInput } from "@/ui/links/link-builder/controls/link-comments-input";
import { ConversionTrackingToggle } from "@/ui/links/link-builder/conversion-tracking-toggle";
@@ -188,8 +189,8 @@ function LinkBuilderInner({
<div
className={cn(
"grid w-full gap-y-6 max-md:overflow-auto md:grid-cols-[2fr_1fr]",
- "max-md:max-h-[calc(100dvh-200px)] max-md:min-h-[min(510px,_calc(100dvh-200px))]",
- "md:[&>div]:max-h-[calc(100dvh-200px)] md:[&>div]:min-h-[min(510px,_calc(100dvh-200px))]",
+ "max-md:max-h-[calc(100dvh-200px)] max-md:min-h-[min(566px,_calc(100dvh-200px))]",
+ "md:[&>div]:max-h-[calc(100dvh-200px)] md:[&>div]:min-h-[min(566px,_calc(100dvh-200px))]",
)}
>
<div className="scrollbar-hide px-6 md:overflow-auto">
@@ -212,7 +213,8 @@ function LinkBuilderInner({
<div className="scrollbar-hide px-6 md:overflow-auto md:pl-0 md:pr-4">
<div className="relative">
<div className="absolute inset-0 rounded-xl border border-neutral-200 bg-neutral-50 [mask-image:linear-gradient(to_bottom,black,transparent)]"></div>
- <div className="relative flex flex-col gap-6 p-4">
+ <div className="relative flex flex-col gap-6 px-4 py-3">
+ <LinkBuilderFolderSelector />
<QRCodePreview />
<LinkPreview />
</div>