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.

Update link cards to support multiple tags

Tim Wilsonea3c8815f585
diff --git a/apps/web/app/api/route.ts b/apps/web/app/api/route.ts
index 648daf14f4..228928be96 100644
--- a/apps/web/app/api/route.ts
+++ b/apps/web/app/api/route.ts
@@ -822,14 +822,12 @@ export function GET(): NextResponse<OpenAPIV3.Document> {
nullable: true,
deprecated: true,
},
- tagIds: {
+ tags: {
type: "array",
items: {
- type: "string",
- format: "cuid",
+ $ref: "#/components/schemas/Tag",
},
- description:
- "The unique IDs of the tags assigned to the short link.",
+ description: "The tags assigned to the short link.",
default: [],
},
comments: {
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index e38797cd1f..a3fe056b57 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -73,6 +73,17 @@ export async function getLinksForProject({
},
include: {
user: true,
+ tags: {
+ include: {
+ tag: {
+ select: {
+ id: true,
+ name: true,
+ color: true,
+ },
+ },
+ },
+ },
},
orderBy: {
[sort]: "desc",
@@ -90,6 +101,7 @@ export async function getLinksForProject({
});
return {
...link,
+ tags: link.tags.map(({ tag }) => tag),
shortLink,
qrCode: `https://api.dub.co/qr?url=${shortLink}`,
};
diff --git a/apps/web/lib/swr/use-links.ts b/apps/web/lib/swr/use-links.ts
index 697039293c..074d53fbb4 100644
--- a/apps/web/lib/swr/use-links.ts
+++ b/apps/web/lib/swr/use-links.ts
@@ -2,7 +2,7 @@ import { fetcher } from "@dub/utils";
import { useRouterStuff } from "@dub/ui";
import { type Link as LinkProps } from "@prisma/client";
import useSWR from "swr";
-import { UserProps } from "../types";
+import { TagProps, UserProps } from "../types";
import { useParams } from "next/navigation";
import { useEffect, useState } from "react";
@@ -20,6 +20,7 @@ export default function useLinks() {
const { data: links, isValidating } = useSWR<
(LinkProps & {
user: UserProps;
+ tags: TagProps[];
})[]
>(
slug
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 781ea30878..30e8a9c352 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -1,5 +1,5 @@
import { DirectorySyncProviders } from "@boxyhq/saml-jackson";
-import { Link } from "@prisma/client";
+import { Link, Tag } from "@prisma/client";
export type LinkProps = Link;
diff --git a/apps/web/ui/links/link-card.tsx b/apps/web/ui/links/link-card.tsx
index fc30d4e063..e7c5e8982b 100644
--- a/apps/web/ui/links/link-card.tsx
+++ b/apps/web/ui/links/link-card.tsx
@@ -1,7 +1,6 @@
import useDomains from "@/lib/swr/use-domains";
import useProject from "@/lib/swr/use-project";
-import useTags from "@/lib/swr/use-tags";
-import { UserProps } from "@/lib/types";
+import { TagProps, 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";
@@ -45,7 +44,7 @@ import Link from "next/link";
import { useParams } from "next/navigation";
import Linkify from "linkify-react";
import punycode from "punycode/";
-import { useEffect, useMemo, useRef, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import useSWR, { mutate } from "swr";
import { toast } from "sonner";
@@ -53,6 +52,7 @@ export default function LinkCard({
props,
}: {
props: LinkProps & {
+ tags: TagProps[];
user: UserProps;
};
}) {
@@ -66,12 +66,10 @@ export default function LinkCard({
createdAt,
lastClicked,
archived,
- tagId,
+ tags,
comments,
user,
} = props;
- const { tags } = useTags();
- const tag = useMemo(() => tags?.find((t) => t.id === tagId), [tags, tagId]);
const apexDomain = getApexDomain(url);
@@ -257,7 +255,7 @@ export default function LinkCard({
it messes up the tooltip positioning.
*/}
<div className="ml-2 sm:ml-4">
- <div className="flex max-w-fit items-center space-x-2">
+ <div className="flex max-w-fit flex-wrap items-center gap-x-2">
{!verified && !loading ? (
<Tooltip
content={
@@ -268,7 +266,7 @@ export default function LinkCard({
/>
}
>
- <div className="w-24 -translate-x-2 cursor-not-allowed truncate text-sm font-semibold text-gray-400 line-through sm:w-full sm:text-base">
+ <div className="max-w-[140px] -translate-x-2 cursor-not-allowed truncate text-sm font-semibold text-gray-400 line-through sm:max-w-[300px] sm:text-base md:max-w-[360px] xl:max-w-[500px]">
{linkConstructor({
key,
domain: punycode.toUnicode(domain || ""),
@@ -279,7 +277,7 @@ export default function LinkCard({
) : (
<a
className={cn(
- "w-full max-w-[140px] truncate text-sm font-semibold text-blue-800 sm:max-w-[300px] sm:text-base md:max-w-[360px] xl:max-w-[500px]",
+ "max-w-[140px] truncate text-sm font-semibold text-blue-800 sm:max-w-[300px] sm:text-base md:max-w-[360px] xl:max-w-[500px]",
{
"text-gray-500": archived || expired,
},
@@ -324,20 +322,22 @@ export default function LinkCard({
</button>
</Tooltip>
)}
- {tag?.color && (
- <button
- onClick={() => {
- queryParams({
- set: {
- tagId: tag.id,
- },
- });
- }}
- className="transition-all duration-75 hover:scale-105 active:scale-100"
- >
- <TagBadge {...tag} withIcon />
- </button>
- )}
+ {tags
+ .filter(({ color }) => color)
+ .map((tag) => (
+ <button
+ onClick={() => {
+ queryParams({
+ set: {
+ tagId: tag.id,
+ },
+ });
+ }}
+ className="transition-all duration-75 hover:scale-105 active:scale-100"
+ >
+ <TagBadge {...tag} withIcon />
+ </button>
+ ))}
</div>
<div className="flex max-w-fit items-center space-x-1">
<Tooltip