The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add controls to allow for selecting and creating folders when creating and editing links.
Kiran K4a400dd2a5d9
diff --git a/apps/web/lib/folder/types.ts b/apps/web/lib/folder/types.tsindex 0dd608e36c..06a9cb3198 100644--- a/apps/web/lib/folder/types.ts+++ b/apps/web/lib/folder/types.ts@@ -17,3 +17,15 @@ export type FolderWithPermissions = {id: string;permissions: FolderPermission[];};++export type FolderSummary = Pick<+ Folder,+ "id" | "name" | "accessLevel" | "linkCount"+>;++export const allLinksOverview: FolderSummary = {+ id: "unsorted",+ name: "Links",+ accessLevel: "edit",+ linkCount: -1,+};diff --git a/apps/web/ui/folders/folder-access-icon.tsx b/apps/web/ui/folders/folder-access-icon.tsxindex 9055e55803..ed6eec218b 100644--- a/apps/web/ui/folders/folder-access-icon.tsx+++ b/apps/web/ui/folders/folder-access-icon.tsx@@ -3,10 +3,16 @@ import {Folder,FolderBookmark,FolderLock,+ FolderPlus,FolderShield,} from "@dub/ui/icons";+import { cn } from "@dub/utils";-const iconMap = {+const FolderOpen = ({ className }: { className: string }) => {+ return <Folder className={cn("text-blue-800", className)} />;+};++const folderIconsMap = {unsorted: {borderColor: "border-green-200",bgColor: "bg-green-100",@@ -19,90 +25,106 @@ const iconMap = {},restricted: {borderColor: "border-indigo-200",- bgColor: "bg-indigo-200",+ bgColor: "bg-indigo-100",IconComponent: FolderShield,},open: {borderColor: "border-blue-200",bgColor: "bg-blue-100",- IconComponent: Folder,+ IconComponent: FolderOpen,+ },+ new: {+ borderColor: "border-gray-200",+ bgColor: "bg-gray-100",+ IconComponent: FolderPlus,},} as const;+const accessLevelMap = {+ new: "new",+ unsorted: "unsorted",+ private: "private",+ view: "restricted",+ edit: "open",+} as const;+export const FolderAccessIcon = ({folder,circlePadding = "p-2.5",withBorder = true,+ className,}: {folder: Pick<FolderProps, "accessLevel" | "id">;circlePadding?: string;withBorder?: boolean;+ className?: string;}) => {- if (folder.id === "unsorted") {- return (- <FolderIcon- type="unsorted"- circlePadding={circlePadding}- withBorder={withBorder}- />- );- }+ const type =+ accessLevelMap[folder.id] ||+ accessLevelMap[folder.accessLevel || "private"];- if (folder.accessLevel === null) {- return (- <FolderIcon- type="private"- circlePadding={circlePadding}- withBorder={withBorder}- />- );- }-- if (folder.accessLevel === "view") {- return (- <FolderIcon- type="restricted"- circlePadding={circlePadding}- withBorder={withBorder}- />- );- }-- if (folder.accessLevel === "edit") {- return (- <FolderIcon- type="open"- circlePadding={circlePadding}- withBorder={withBorder}- />- );+ if (!type) {+ return null;}- return null;+ return (+ <FolderIcon+ type={type}+ circlePadding={circlePadding}+ withBorder={withBorder}+ className={className}+ />+ );};const FolderIcon = ({type = "open",circlePadding = "p-2.5",withBorder = true,+ className = "rounded-full",}: {type: "unsorted" | "private" | "restricted" | "open";circlePadding?: string;withBorder?: boolean;+ className?: string;}) => {- const { borderColor, bgColor, IconComponent } = iconMap[type];+ const { borderColor, bgColor, IconComponent } = folderIconsMap[type];if (!withBorder) {return (- <div className={`rounded-full p-1.5 ${bgColor}`}>+ <div className={`${className} p-1.5 ${bgColor}`}><IconComponent className="size-4" /></div>);}return (- <div className={`rounded-full border ${borderColor} ${bgColor}`}>- <div className={`rounded-full border-2 border-white ${circlePadding}`}>+ <div className={cn("border", className, borderColor, bgColor)}>+ <div className={cn(circlePadding)}>+ <IconComponent className="size-4" />+ </div>+ </div>+ );+};++export const FolderSquareIcon = ({+ folder,+}: {+ folder: Pick<FolderProps, "id" | "accessLevel">;+}) => {+ const type =+ accessLevelMap[folder.id] ||+ accessLevelMap[folder.accessLevel || "private"];++ if (!type) {+ return null;+ }++ const { borderColor, bgColor, IconComponent } = folderIconsMap[type];++ return (+ <div className={cn("rounded-md border", borderColor, bgColor)}>+ <div className="p-1.5"><IconComponent className="size-4" /></div></div>diff --git a/apps/web/ui/folders/folder-dropdown.tsx b/apps/web/ui/folders/folder-dropdown.tsxnew file mode 100644index 0000000000..e73f1e35aa--- /dev/null+++ b/apps/web/ui/folders/folder-dropdown.tsx@@ -0,0 +1,140 @@+import { allLinksOverview, FolderSummary } from "@/lib/folder/types";+import useFolders from "@/lib/swr/use-folders";+import { Popover, Tick } from "@dub/ui";+import { cn } from "@dub/utils";+import Link from "next/link";+import { useSearchParams } from "next/navigation";+import { useEffect, useState } from "react";+import { useAddFolderModal } from "../modals/add-folder-modal";+import { FolderSquareIcon } from "./folder-access-icon";++interface FolderDropdownProps {+ onFolderSelect: (folder: FolderSummary) => void;+}++export const FolderDropdown = ({ onFolderSelect }: FolderDropdownProps) => {+ const searchParams = useSearchParams();+ const { folders, isLoading } = useFolders();+ const [openPopover, setOpenPopover] = useState(false);+ const { AddFolderModal, setShowAddFolderModal } = useAddFolderModal();+ const [selectedFolder, setSelectedFolder] = useState<FolderSummary | null>(+ allLinksOverview,+ );++ const folderId = searchParams.get("folderId");++ useEffect(() => {+ if (!folderId || !folders || selectedFolder) {+ return;+ }++ const folder = folders.find((f) => f.id === folderId);++ setSelectedFolder(folder || allLinksOverview);+ }, [folderId, folders, onFolderSelect]);++ useEffect(() => {+ onFolderSelect(selectedFolder || allLinksOverview);+ }, [selectedFolder, onFolderSelect]);++ if (isLoading || !folders) {+ return <FolderSwitcherPlaceholder />;+ }++ const foldersWithAllLinks = [allLinksOverview, ...folders];++ return (+ <>+ <AddFolderModal />+ <Popover+ content={+ <div className="relative mt-1 max-h-72 w-full space-y-0.5 overflow-auto rounded-md bg-white p-2 text-base sm:w-60 sm:text-sm sm:shadow-lg">+ <div className="flex items-center justify-between px-2 pb-1">+ <p className="text-xs font-medium text-gray-500">Folders</p>+ {folders.length > 0 && (+ <Link+ href="/settings/library/folders"+ onClick={() => setOpenPopover(false)}+ className="rounded-md border border-gray-200 px-2 py-1 text-xs transition-colors hover:bg-gray-100"+ >+ View All+ </Link>+ )}+ </div>++ {foldersWithAllLinks.map((folder) => {+ return (+ <button+ key={folder.id}+ className={cn(+ "relative flex w-full items-center gap-x-2 rounded-md px-2 py-1.5 transition-all duration-75 hover:bg-gray-100 active:bg-gray-200",+ {+ "font-medium": selectedFolder?.id === folder.id,+ },+ )}+ onClick={() => {+ setOpenPopover(false);+ setSelectedFolder(folder);+ }}+ >+ <FolderSquareIcon folder={folder} />+ <span+ className={`flex items-center justify-start gap-1.5 text-sm sm:max-w-[140px] ${+ selectedFolder?.id === folder.id+ ? "font-medium"+ : "font-normal"+ }`}+ >+ <span className="truncate">{folder.name}</span>++ {folder.id === "unsorted" && (+ <div className="rounded bg-gray-100 p-1">+ <div className="text-xs font-normal text-black">+ Unsorted+ </div>+ </div>+ )}+ </span>++ {selectedFolder?.id === folder.id && (+ <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-black">+ <Tick className="size-5" aria-hidden="true" />+ </span>+ )}+ </button>+ );+ })}++ <button+ key="add-folder"+ className="relative flex w-full items-center gap-x-2 rounded-md px-2 py-1.5 transition-all duration-75 hover:bg-gray-100 active:bg-gray-200"+ onClick={() => {+ setOpenPopover(false);+ setShowAddFolderModal(true);+ }}+ >+ <FolderSquareIcon folder={{ id: "new", accessLevel: null }} />+ <span className="block truncate">Create new folder</span>+ </button>+ </div>+ }+ openPopover={openPopover}+ setOpenPopover={setOpenPopover}+ align="start"+ >+ <button className="group flex w-36 min-w-0 items-center gap-2 rounded-lg transition-colors duration-75 hover:bg-gray-100 data-[state=open]:bg-gray-200">+ {selectedFolder && <FolderSquareIcon folder={selectedFolder} />}+ <h1 className="min-w-0 truncate text-left text-lg font-medium leading-7 text-neutral-900">+ {selectedFolder?.name}+ </h1>+ </button>+ </Popover>+ </>+ );+};++const FolderSwitcherPlaceholder = () => {+ return (+ <div className="h-7 w-20 animate-pulse rounded-lg bg-gray-200 sm:w-32 md:h-9" />+ );+};diff --git a/apps/web/ui/folders/folder-switcher.tsx b/apps/web/ui/folders/folder-switcher.tsxindex 858274be0b..286baa16ba 100644--- a/apps/web/ui/folders/folder-switcher.tsx+++ b/apps/web/ui/folders/folder-switcher.tsx@@ -1,4 +1,4 @@-import { Folder } from "@/lib/folder/types";+import { allLinksOverview, FolderSummary } from "@/lib/folder/types";import { useCheckFolderPermission } from "@/lib/swr/use-folder-permissions";import useFolders from "@/lib/swr/use-folders";import useWorkspace from "@/lib/swr/use-workspace";@@ -6,39 +6,24 @@ import {Button,PenWriting,Popover,- Tick,useKeyboardShortcut,useRouterStuff,Users,} from "@dub/ui";-import { FolderPlus } from "@dub/ui/icons";import { cn } from "@dub/utils";-import { ChevronLeft, ChevronsUpDown } from "lucide-react";-import Link from "next/link";+import { ChevronLeft } from "lucide-react";import { useRouter, useSearchParams } from "next/navigation";import { useCallback, useEffect, useState } from "react";-import { useAddFolderModal } from "../modals/add-folder-modal";import { useDeleteFolderModal } from "../modals/delete-folder-modal";import { useRenameFolderModal } from "../modals/rename-folder-modal";import { Chart, Delete, ThreeDots } from "../shared/icons";-import { FolderAccessIcon } from "./folder-access-icon";+import { FolderDropdown } from "./folder-dropdown";import { useFolderPermissionsPanel } from "./folder-permissions-panel";-type FolderSummary = Pick<Folder, "id" | "name" | "accessLevel" | "linkCount">;--const allLinksOverview: FolderSummary = {- id: "unsorted",- name: "Links",- accessLevel: "edit",- linkCount: -1,-};-export const FolderSwitcher = () => {const searchParams = useSearchParams();const { queryParams } = useRouterStuff();const { folders, isLoading } = useFolders();- const [openPopover, setOpenPopover] = useState(false);- const { AddFolderModal, setShowAddFolderModal } = useAddFolderModal();const [selectedFolder, setSelectedFolder] = useState<FolderSummary | null>(allLinksOverview,@@ -83,8 +68,6 @@ export const FolderSwitcher = () => {return (<div className="flex items-center">- <AddFolderModal />-{!isAllLinksFolderSelected && (<buttononClick={() => {@@ -96,30 +79,9 @@ export const FolderSwitcher = () => {</button>)}- <Popover- content={- <FolderList- folders={[allLinksOverview, ...folders]}- setOpenPopover={setOpenPopover}- onFolderSelect={onFolderSelect}- selectedFolder={selectedFolder}- setShowAddFolderModal={setShowAddFolderModal}- />- }- openPopover={openPopover}- setOpenPopover={setOpenPopover}- align="start"- >- <button className="group -my-1 flex min-w-0 items-center gap-2 rounded-lg py-1 pl-2 pr-1 transition-colors duration-75 hover:bg-gray-100 data-[state=open]:bg-gray-200">- <h1 className="min-w-0 truncate text-left text-xl font-semibold leading-7 text-neutral-900 md:text-2xl">- {selectedFolder?.name}- </h1>- <ChevronsUpDown- className="size-5 shrink-0 text-gray-400"- aria-hidden="true"- />- </button>- </Popover>+ <FolderDropdown+ onFolderSelect={onFolderSelect}+ />{selectedFolder && !isAllLinksFolderSelected && (<FolderActions@@ -131,90 +93,6 @@ export const FolderSwitcher = () => {);};-const FolderList = ({- folders,- setOpenPopover,- onFolderSelect,- selectedFolder,- setShowAddFolderModal,-}: {- folders: FolderSummary[];- setOpenPopover: (open: boolean) => void;- onFolderSelect: (folder: FolderSummary) => void;- setShowAddFolderModal: (show: boolean) => void;- selectedFolder: FolderSummary | null;-}) => {- return (- <div className="relative mt-1 max-h-72 w-full space-y-0.5 overflow-auto rounded-md bg-white p-2 text-base sm:w-60 sm:text-sm sm:shadow-lg">- <div className="flex items-center justify-between px-2 pb-1">- <p className="text-xs font-medium text-gray-500">Folders</p>- {folders.length > 0 && (- <Link- href="/settings/library/folders"- onClick={() => setOpenPopover(false)}- className="rounded-md border border-gray-200 px-2 py-1 text-xs transition-colors hover:bg-gray-100"- >- View All- </Link>- )}- </div>-- {folders.map((folder) => {- return (- <button- key={folder.id}- className={cn(- "relative flex w-full items-center gap-x-2 rounded-md px-2 py-1.5 transition-all duration-75 hover:bg-gray-100 active:bg-gray-200",- {- "font-medium": selectedFolder?.id === folder.id,- },- )}- onClick={() => {- setOpenPopover(false);- onFolderSelect(folder);- }}- >- <FolderAccessIcon folder={folder} withBorder={false} />- <span- className={`flex items-center justify-start gap-1.5 text-sm sm:max-w-[140px] ${- selectedFolder?.id === folder.id ? "font-medium" : "font-normal"- }`}- >- <span className="truncate">{folder.name}</span>-- {folder.id === "unsorted" && (- <div className="rounded bg-gray-100 p-1">- <div className="text-xs font-normal text-black">Unsorted</div>- </div>- )}- </span>-- {selectedFolder?.id === folder.id && (- <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-black">- <Tick className="size-5" aria-hidden="true" />- </span>- )}- </button>- );- })}-- <button- key="add-folder"- className="relative flex w-full items-center gap-x-2 rounded-md px-2 py-1.5 transition-all duration-75 hover:bg-gray-100 active:bg-gray-200"- onClick={() => {- setOpenPopover(false);- setShowAddFolderModal(true);- }}- >- <div className="flex size-7 items-center justify-center rounded-full border border-gray-200 bg-gradient-to-t from-gray-100 group-hover:bg-white">- <FolderPlus className="size-4 text-gray-700" />- </div>- <span className="block truncate">Create new folder</span>- </button>- </div>- );-};-const FolderActions = ({folder,onDelete,diff --git a/apps/web/ui/modals/link-builder/index.tsx b/apps/web/ui/modals/link-builder/index.tsxindex 780fa04f43..438a40e967 100644--- a/apps/web/ui/modals/link-builder/index.tsx+++ b/apps/web/ui/modals/link-builder/index.tsx@@ -3,6 +3,7 @@import { mutatePrefix } from "@/lib/swr/mutate";import useWorkspace from "@/lib/swr/use-workspace";import { ExpandedLinkProps } from "@/lib/types";+import { FolderDropdown } from "@/ui/folders/folder-dropdown";import { DestinationUrlInput } from "@/ui/links/destination-url-input";import { ShortLinkInput } from "@/ui/links/short-link-input";import { useAvailableDomains } from "@/ui/links/use-available-domains";@@ -30,6 +31,7 @@ import {isValidUrl,linkConstructor,} from "@dub/utils";+import { ChevronRight } from "lucide-react";import { useParams, useSearchParams } from "next/navigation";import posthog from "posthog-js";import {@@ -133,14 +135,16 @@ function LinkBuilderInner({const formRef = useRef<HTMLFormElement>(null);const { handleKeyDown } = useEnterSubmit(formRef);- const [url, domain, key, title, description, trackConversion] = watch([+ const [url, domain, key, title, description] = watch(["url","domain","key","title","description",- "trackConversion",]);++ const [folderId, setFolderId] = useState<string | null>(null);+const [debouncedUrl] = useDebounce(getUrlWithoutUTMParams(url), 500);const endpoint = useMemo(@@ -215,6 +219,8 @@ function LinkBuilderInner({const [, copyToClipboard] = useCopyToClipboard();+ console.log({ folderId });+return (<><PasswordModal />@@ -246,13 +252,12 @@ function LinkBuilderInner({onSubmit={handleSubmit(async (data) => {// @ts-ignore – exclude extra attributes from `data` object before sending to APIconst { user, tags, tagId, ...rest } = data;- const folderId = searchParams.get("folderId");+ // const folderId = searchParams.get("folderId");const bodyData = {...rest,// Map tags to tagIdstagIds: tags.map(({ id }) => id),- // if folderId is available, add it to the body data- ...(folderId ? { folderId } : {}),+ ...(folderId && { folderId }),// Manually reset empty strings to nullexpiredUrl: rest.expiredUrl || null,@@ -330,7 +335,15 @@ function LinkBuilderInner({})}><div className="flex items-center justify-between px-6 py-4">- <div className="flex items-center gap-3">+ <div className="flex items-center gap-2">+ <div className="flex items-center rounded-md border border-neutral-200 p-1 hover:bg-gray-100">+ <FolderDropdown+ onFolderSelect={(folder) =>+ setFolderId(folder.id !== "unsorted" ? folder.id : null)+ }+ />+ </div>+ <ChevronRight className="size-4 text-neutral-500" /><LinkLogoapexDomain={getApexDomain(debouncedUrl)}className="size-6 sm:size-6 [&>*]:size-3 sm:[&>*]:size-4"