The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Support multiple tags in link add/edit modal
Tim Wilsonbc000c1683d2
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.tsindex a3fe056b57..a3fb6849f4 100644--- a/apps/web/lib/api/links.ts+++ b/apps/web/lib/api/links.ts@@ -807,5 +807,6 @@ function combineTagIds({tagId?: string | null;tagIds?: string[];}) {- return (tagIds ?? []).concat(tagId ?? []);+ // Using Set to remove duplicates+ return [...new Set((tagIds ?? []).concat(tagId ?? []))];}diff --git a/apps/web/lib/swr/use-links.ts b/apps/web/lib/swr/use-links.tsindex 074d53fbb4..a6aeef72f0 100644--- a/apps/web/lib/swr/use-links.ts+++ b/apps/web/lib/swr/use-links.ts@@ -1,8 +1,7 @@import { fetcher } from "@dub/utils";import { useRouterStuff } from "@dub/ui";-import { type Link as LinkProps } from "@prisma/client";import useSWR from "swr";-import { TagProps, UserProps } from "../types";+import { LinkWithTagsProps, TagProps, UserProps } from "../types";import { useParams } from "next/navigation";import { useEffect, useState } from "react";@@ -18,9 +17,8 @@ export default function useLinks() {}, []);const { data: links, isValidating } = useSWR<- (LinkProps & {+ (LinkWithTagsProps & {user: UserProps;- tags: TagProps[];})[]>(slugdiff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex 30e8a9c352..649d60220c 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -3,6 +3,10 @@ import { Link, Tag } from "@prisma/client";export type LinkProps = Link;+export interface LinkWithTagsProps extends LinkProps {+ tags: TagProps[];+}+export interface LinkWithTagIdsProps extends LinkProps {tagIds?: string[];}diff --git a/apps/web/ui/links/link-card.tsx b/apps/web/ui/links/link-card.tsxindex e7c5e8982b..33aeb0730c 100644--- a/apps/web/ui/links/link-card.tsx+++ b/apps/web/ui/links/link-card.tsx@@ -1,6 +1,6 @@import useDomains from "@/lib/swr/use-domains";import useProject from "@/lib/swr/use-project";-import { TagProps, UserProps } from "@/lib/types";+import { LinkWithTagsProps, UserProps } from "@/lib/types";import TagBadge from "@/ui/links/tag-badge";import { useAddEditLinkModal } from "@/ui/modals/add-edit-link-modal";import { useArchiveLinkModal } from "@/ui/modals/archive-link-modal";@@ -51,8 +51,7 @@ import { toast } from "sonner";export default function LinkCard({props,}: {- props: LinkProps & {- tags: TagProps[];+ props: LinkWithTagsProps & {user: UserProps;};}) {diff --git a/apps/web/ui/modals/add-edit-link-modal/index.tsx b/apps/web/ui/modals/add-edit-link-modal/index.tsxindex 2fddd13cf7..a647d9a33d 100644--- a/apps/web/ui/modals/add-edit-link-modal/index.tsx+++ b/apps/web/ui/modals/add-edit-link-modal/index.tsx@@ -52,6 +52,7 @@ import Preview from "./preview";import RewriteSection from "./rewrite-section";import UTMSection from "./utm-section";import TagsSection from "./tags-section";+import { LinkWithTagsProps } from "@/lib/types";function AddEditLinkModal({showAddEditLinkModal,@@ -62,8 +63,8 @@ function AddEditLinkModal({}: {showAddEditLinkModal: boolean;setShowAddEditLinkModal: Dispatch<SetStateAction<boolean>>;- props?: LinkProps;- duplicateProps?: LinkProps;+ props?: LinkWithTagsProps;+ duplicateProps?: LinkWithTagsProps;homepageDemo?: boolean;}) {const params = useParams() as { slug?: string };@@ -82,10 +83,10 @@ function AddEditLinkModal({defaultDomains,} = useDomains();- const [data, setData] = useState<LinkProps>(+ const [data, setData] = useState<LinkWithTagsProps>(props ||duplicateProps || {- ...(DEFAULT_LINK_PROPS as LinkProps),+ ...(DEFAULT_LINK_PROPS as LinkWithTagsProps),domain: primaryDomain,key: "",url: "",@@ -344,13 +345,18 @@ function AddEditLinkModal({e.preventDefault();setSaving(true);// @ts-ignore – exclude the extra `user` attribute from `data` object before sending to API- const { user, ...rest } = data;+ const { user, tags, ...rest } = data;+ const bodyData = {+ ...rest,+ // Map tags to tagIds+ tagIds: tags.map(({ id }) => id),+ };fetch(endpoint.url, {method: endpoint.method,headers: {"Content-Type": "application/json",},- body: JSON.stringify(rest),+ body: JSON.stringify(bodyData),}).then(async (res) => {if (res.status === 200) {await mutate(@@ -701,8 +707,8 @@ export function useAddEditLinkModal({duplicateProps,homepageDemo,}: {- props?: LinkProps;- duplicateProps?: LinkProps;+ props?: LinkWithTagsProps;+ duplicateProps?: LinkWithTagsProps;homepageDemo?: boolean;} = {}) {const [showAddEditLinkModal, setShowAddEditLinkModal] = useState(false);diff --git a/apps/web/ui/modals/add-edit-link-modal/tags-section.tsx b/apps/web/ui/modals/add-edit-link-modal/tags-section.tsxindex 6bf3760edf..0567f8c7d6 100644--- a/apps/web/ui/modals/add-edit-link-modal/tags-section.tsx+++ b/apps/web/ui/modals/add-edit-link-modal/tags-section.tsx@@ -1,19 +1,12 @@import useTags from "@/lib/swr/use-tags";+import { LinkWithTagsProps } from "@/lib/types";import TagBadge from "@/ui/links/tag-badge";import { LoadingCircle, SimpleTooltipContent, Tooltip } from "@dub/ui";import { HOME_DOMAIN } from "@dub/utils";-import { type Link as LinkProps } from "@prisma/client";import { Command, useCommandState } from "cmdk";import { Check, ChevronDown, Tag, X } from "lucide-react";import { useParams } from "next/navigation";-import {- Dispatch,- SetStateAction,- useEffect,- useMemo,- useRef,- useState,-} from "react";+import { Dispatch, SetStateAction, useEffect, useRef, useState } from "react";import { toast } from "sonner";import { mutate } from "swr";@@ -21,18 +14,12 @@ export default function TagsSection({data,setData,}: {- data: LinkProps;- setData: Dispatch<SetStateAction<LinkProps>>;+ data: LinkWithTagsProps;+ setData: Dispatch<SetStateAction<LinkWithTagsProps>>;}) {const { slug } = useParams() as { slug?: string };- const { tagId } = data;- const { tags } = useTags();-- const selectedTag = useMemo(() => {- if (tagId) {- return tags?.find((tag) => tag.id === tagId);- }- }, [tagId, tags]);+ const { tags: availableTags } = useTags();+ const tags = data.tags;const [inputValue, setInputValue] = useState("");const [creatingTag, setCreatingTag] = useState(false);@@ -78,7 +65,7 @@ export default function TagsSection({const isEmpty = useCommandState((state) => state.filtered.count === 0);return (<Command.Input- placeholder="Choose a tag"+ placeholder="Select tags..."// hack to focus on the input when the dropdown opensautoFocus={openCommandList}// when focus on the input. only show the dropdown if there are tags and the tagValue is not empty@@ -124,16 +111,16 @@ export default function TagsSection({</Tooltip>)}</div>- <div className="flex h-9 px-8">- {selectedTag ? (- <TagBadge key={selectedTag.id} {...selectedTag} />- ) : (- <CommandInput />- )}- {selectedTag ? (+ <div className="flex h-9 gap-1.5 px-8">+ {tags.map((tag) => (+ <TagBadge key={tag.id} {...tag} />+ ))}+ <CommandInput />+ {tags.length ? (<button+ type="button"onClick={() => {- setData({ ...data, tagId: null });+ setData({ ...data, tags: [] });setInputValue("");}}className="absolute inset-y-0 right-0 my-auto"@@ -147,7 +134,7 @@ export default function TagsSection({</div>{openCommandList && (<Command.List className="absolute z-20 mt-2 h-[calc(var(--cmdk-list-height)+17px)] max-h-[300px] w-full overflow-auto rounded-md border border-gray-200 bg-white p-2 shadow-md transition-all">- {tags?.length === 0 && inputValue.length === 0 && (+ {availableTags?.length === 0 && inputValue.length === 0 && (<p className="px-4 py-2.5 text-sm text-gray-900">Start typing to create tag...</p>@@ -166,18 +153,25 @@ export default function TagsSection({</button>)}</Command.Empty>- {tags?.map((tag) => (+ {availableTags?.map((tag) => (<Command.Itemkey={tag.id}value={tag.name}onSelect={() => {- setData({ ...data, tagId: tag.id });- setOpenCommandList(false);+ const isRemoving = data.tags+ .map(({ id }) => id)+ .includes(tag.id);+ setData({+ ...data,+ tags: isRemoving+ ? data.tags.filter(({ id }) => id !== tag.id)+ : [...data.tags, tag],+ });}}className="aria-selected:bg-gray-100 aria-selected:text-gray-900 group flex cursor-pointer items-center justify-between rounded-md px-4 py-2 text-sm text-gray-900 hover:bg-gray-100 hover:text-gray-900 active:bg-gray-200"><TagBadge {...tag} />- {selectedTag?.id === tag.id && (+ {tags.map(({ id }) => id).includes(tag.id) && (<Check className="h-5 w-5 text-gray-500" />)}</Command.Item>diff --git a/packages/utils/src/constants/index.ts b/packages/utils/src/constants/index.tsindex 0eed71df7e..765725940a 100644--- a/packages/utils/src/constants/index.ts+++ b/packages/utils/src/constants/index.ts@@ -230,6 +230,8 @@ export const DEFAULT_LINK_PROPS = {userId: "",proxy: false,++ tags: [] as any,};export const SAML_PROVIDERS = [