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 tag management to settings

Tim Wilson3072b736baae
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx
index 12fab91219..b860d5fc57 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx
@@ -5,6 +5,7 @@ import {
Key,
Receipt2,
ShieldCheck,
+ Tag,
Users6,
} from "@dub/ui/src/icons";
import { ReactNode } from "react";
@@ -25,6 +26,11 @@ export default function WorkspaceSettingsLayout({
icon: Globe,
segment: "domains",
},
+ {
+ name: "Tags",
+ icon: Tag,
+ segment: "tags",
+ },
{
name: "Billing",
icon: Receipt2,
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tags/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tags/page-client.tsx
new file mode 100644
index 0000000000..be224763b9
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tags/page-client.tsx
@@ -0,0 +1,66 @@
+"use client";
+
+import useLinksCount from "@/lib/swr/use-links-count";
+import useTags from "@/lib/swr/use-tags";
+import useWorkspace from "@/lib/swr/use-workspace";
+import { useAddEditTagModal } from "@/ui/modals/add-edit-tag-modal";
+import EmptyState from "@/ui/shared/empty-state";
+import { CardList } from "@dub/ui";
+import { Tag } from "@dub/ui/src/icons";
+import { InfoTooltip, TooltipContent } from "@dub/ui/src/tooltip";
+import { TagCard } from "./tag-card";
+import { TagCardPlaceholder } from "./tag-card-placeholder";
+
+export default function WorkspaceTagsClient() {
+ const { id: workspaceId } = useWorkspace();
+
+ const { AddEditTagModal, AddTagButton } = useAddEditTagModal();
+
+ const { tags, loading } = useTags();
+ const { data: tagsCount } = useLinksCount({ groupBy: "tagId" });
+
+ return (
+ <>
+ <div className="grid gap-5">
+ <div className="flex flex-wrap justify-between gap-6">
+ <div className="flex items-center gap-x-2">
+ <h1 className="text-2xl font-semibold tracking-tight text-black">
+ Tags
+ </h1>
+ <InfoTooltip
+ content={
+ <TooltipContent
+ title="Learn more about how to use tags on Dub."
+ href="https://dub.co/help/article/how-to-use-tags"
+ target="_blank"
+ cta="Learn more"
+ />
+ }
+ />
+ </div>
+ <div className="flex w-full flex-wrap items-center gap-3 sm:w-auto">
+ <AddTagButton />
+ </div>
+ </div>
+ {workspaceId && <AddEditTagModal />}
+
+ {loading || tags?.length ? (
+ <CardList variant="compact" loading={loading}>
+ {tags?.length
+ ? tags.map((tag) => (
+ <TagCard key={tag.id} tag={tag} tagsCount={tagsCount} />
+ ))
+ : Array.from({ length: 6 }).map((_, idx) => (
+ <TagCardPlaceholder key={idx} />
+ ))}
+ </CardList>
+ ) : (
+ <div className="flex flex-col items-center gap-4 rounded-xl border border-gray-200 py-10">
+ <EmptyState icon={Tag} title="No tags found for this workspace" />
+ <AddTagButton />
+ </div>
+ )}
+ </div>
+ </>
+ );
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tags/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tags/page.tsx
new file mode 100644
index 0000000000..79834030d7
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tags/page.tsx
@@ -0,0 +1,10 @@
+import { Suspense } from "react";
+import WorkspaceTagsClient from "./page-client";
+
+export default function WorkspaceDomains() {
+ return (
+ <Suspense>
+ <WorkspaceTagsClient />
+ </Suspense>
+ );
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tags/tag-card-placeholder.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tags/tag-card-placeholder.tsx
new file mode 100644
index 0000000000..91a3f68493
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tags/tag-card-placeholder.tsx
@@ -0,0 +1,12 @@
+import { CardList } from "@dub/ui";
+
+export function TagCardPlaceholder() {
+ return (
+ <CardList.Card>
+ <div className="flex h-8 grow items-center gap-5 sm:gap-8 md:gap-12">
+ <div className="hidden h-5 w-32 animate-pulse rounded-md bg-gray-200 sm:block" />
+ <div className="hidden h-5 w-12 animate-pulse rounded-md bg-gray-200 sm:block" />
+ </div>
+ </CardList.Card>
+ );
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tags/tag-card.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tags/tag-card.tsx
new file mode 100644
index 0000000000..d29da1e05f
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tags/tag-card.tsx
@@ -0,0 +1,138 @@
+"use client";
+
+import useWorkspace from "@/lib/swr/use-workspace";
+import { TagProps } from "@/lib/types";
+import TagBadge from "@/ui/links/tag-badge";
+import { useAddEditTagModal } from "@/ui/modals/add-edit-tag-modal";
+import { Delete, ThreeDots } from "@/ui/shared/icons";
+import { Button, CardList, Popover } from "@dub/ui";
+import { LoadingSpinner, PenWriting } from "@dub/ui/src/icons";
+import { cn } from "@dub/utils";
+import Link from "next/link";
+import { useState } from "react";
+import { toast } from "sonner";
+import { mutate } from "swr";
+
+export function TagCard({
+ tag,
+ tagsCount,
+}: {
+ tag: TagProps;
+ tagsCount?: { tagId: string; _count: number }[];
+}) {
+ const { id, slug } = useWorkspace();
+
+ const linksCount = tagsCount?.find(({ tagId }) => tagId === tag.id)?._count;
+
+ const [openPopover, setOpenPopover] = useState(false);
+ const [processing, setProcessing] = useState(false);
+
+ const { AddEditTagModal, setShowAddEditTagModal } = useAddEditTagModal({
+ props: tag,
+ });
+
+ const handleDelete = async () => {
+ setProcessing(true);
+ fetch(`/api/tags/${tag.id}?workspaceId=${id}`, {
+ method: "DELETE",
+ })
+ .then(async (res) => {
+ if (res.ok) {
+ await Promise.all([
+ mutate(`/api/tags?workspaceId=${id}`),
+ mutate(
+ (key) => typeof key === "string" && key.startsWith("/api/links"),
+ undefined,
+ { revalidate: true },
+ ),
+ ]);
+ toast.success("Tag deleted");
+ } else {
+ const { error } = await res.json();
+ toast.error(error.message);
+ }
+ })
+ .finally(() => setProcessing(false));
+ };
+
+ return (
+ <>
+ <AddEditTagModal />
+
+ <CardList.Card
+ key={tag.id}
+ onClick={() => setShowAddEditTagModal(true)}
+ innerClassName={cn(
+ "flex items-center justify-between gap-5 sm:gap-8 md:gap-12 text-sm transition-opacity",
+ processing && "opacity-50",
+ )}
+ >
+ <div className="flex min-w-0 grow items-center gap-5 sm:gap-8 md:gap-12">
+ <TagBadge {...tag} withIcon primary className="min-w-0 truncate" />
+ {tagsCount !== undefined && (
+ <Link
+ href={`/${slug}?tagIds=${tag.id}`}
+ className="whitespace-nowrap rounded-md border border-gray-200 bg-gray-50 px-2 py-0.5 text-sm text-gray-800 transition-colors hover:bg-gray-100"
+ >
+ {linksCount || 0} link{linksCount !== 1 && "s"}
+ </Link>
+ )}
+ </div>
+
+ <div>
+ <Popover
+ content={
+ <div className="grid w-full gap-px p-2 sm:w-48">
+ <Button
+ text="Edit"
+ variant="outline"
+ onClick={() => {
+ setOpenPopover(false);
+ setShowAddEditTagModal(true);
+ }}
+ icon={<PenWriting className="h-4 w-4" />}
+ shortcut="E"
+ className="h-9 px-2 font-medium"
+ />
+ <Button
+ text="Delete"
+ variant="danger-outline"
+ onClick={() => {
+ setOpenPopover(false);
+ confirm(
+ "Are you sure you want to delete this tag? All tagged links will be untagged, but they won't be deleted.",
+ ) && handleDelete();
+ }}
+ icon={<Delete className="h-4 w-4" />}
+ shortcut="X"
+ className="h-9 px-2 font-medium"
+ />
+ </div>
+ }
+ align="end"
+ openPopover={openPopover}
+ setOpenPopover={setOpenPopover}
+ >
+ <Button
+ variant="secondary"
+ className={cn(
+ "h-8 px-1.5 outline-none transition-all duration-200",
+ "border-transparent data-[state=open]:border-gray-500 sm:group-hover/card:data-[state=closed]:border-gray-200",
+ )}
+ icon={
+ processing ? (
+ <LoadingSpinner className="h-5 w-5 shrink-0" />
+ ) : (
+ <ThreeDots className="h-5 w-5 shrink-0" />
+ )
+ }
+ onClick={() => {
+ setOpenPopover(!openPopover);
+ }}
+ />
+ </Popover>
+ </div>
+ </CardList.Card>
+ </>
+ );
+}
diff --git a/apps/web/ui/links/tag-badge.tsx b/apps/web/ui/links/tag-badge.tsx
index 6d16a4f6d4..e53c9e8ffc 100644
--- a/apps/web/ui/links/tag-badge.tsx
+++ b/apps/web/ui/links/tag-badge.tsx
@@ -1,6 +1,6 @@
import { TagColorProps } from "@/lib/types";
import { useMediaQuery } from "@dub/ui";
-import { cn, truncate } from "@dub/utils";
+import { cn, truncate as truncateFn } from "@dub/utils";
import { Tag } from "lucide-react";
export default function TagBadge({
@@ -8,32 +8,43 @@ export default function TagBadge({
color,
withIcon,
plus,
+ /** Whether the tag badge is primary information and should avoid truncating/collapsing */
+ primary = false,
+ className,
}: {
name: string;
color: TagColorProps;
withIcon?: boolean;
plus?: number;
+ primary?: boolean;
+ className?: string;
}) {
const { isDesktop } = useMediaQuery();
return (
<span
className={cn(
- "my-auto whitespace-nowrap rounded-md border px-2 py-0.5 text-sm",
- (withIcon || plus) &&
- "flex items-center gap-x-1.5 rounded-full p-1.5 sm:rounded-md sm:px-2 sm:py-0.5",
+ "my-auto flex items-center gap-x-1.5 whitespace-nowrap rounded-md border px-2 py-0.5 text-sm",
+ ((withIcon && !primary) || plus) &&
+ "rounded-full p-1.5 sm:rounded-md sm:px-2 sm:py-0.5",
color === "red" && "border-red-300 bg-red-100 text-red-600",
color === "yellow" && "border-yellow-300 bg-yellow-100 text-yellow-600",
color === "green" && "border-green-300 bg-green-100 text-green-600",
color === "blue" && "border-blue-300 bg-blue-100 text-blue-600",
color === "purple" && "border-purple-300 bg-purple-100 text-purple-600",
color === "brown" && "border-brown-300 bg-brown-100 text-brown-600",
+ className,
)}
>
{withIcon && <Tag className="h-3 w-3" />}
- <p {...(withIcon && { className: "hidden sm:inline-block" })}>
- {truncate(name || "", !isDesktop ? 20 : 24)}
- </p>
+ <span
+ className={cn(
+ withIcon && !primary && "hidden sm:inline-block",
+ "min-w-0 truncate",
+ )}
+ >
+ {primary ? name : truncateFn(name || "", !isDesktop ? 20 : 24)}
+ </span>
{!!plus && (
<span className="hidden sm:block">
<span className="pr-1.5 opacity-30 md:pl-1 md:pr-2.5">|</span>+{plus}
diff --git a/apps/web/ui/modals/add-edit-tag-modal.tsx b/apps/web/ui/modals/add-edit-tag-modal.tsx
index 834431300b..b5114d89d2 100644
--- a/apps/web/ui/modals/add-edit-tag-modal.tsx
+++ b/apps/web/ui/modals/add-edit-tag-modal.tsx
@@ -218,9 +218,9 @@ function AddTagButton({
return (
<div>
<Button
- variant="secondary"
- text="Add"
- className="h-7 px-2"
+ variant="primary"
+ text="Add Tag"
+ className="h-9 rounded-lg"
disabledTooltip={
exceededTags ? (
<TooltipContent