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.

Sort LinkCard tags to display tags matching filter first

Tim Wilson45595e982c72
diff --git a/apps/web/ui/links/link-card.tsx b/apps/web/ui/links/link-card.tsx
index 19b58df38e..7f0043b2d6 100644
--- a/apps/web/ui/links/link-card.tsx
+++ b/apps/web/ui/links/link-card.tsx
@@ -47,7 +47,7 @@ import {
TimerOff,
} from "lucide-react";
import Link from "next/link";
-import { useParams } from "next/navigation";
+import { useParams, useSearchParams } from "next/navigation";
import punycode from "punycode/";
import { useEffect, useMemo, useRef, useState } from "react";
import { toast } from "sonner";
@@ -78,14 +78,31 @@ export default function LinkCard({
user,
} = props;
+ const searchParams = useSearchParams();
+
const [primaryTags, additionalTags] = useMemo(() => {
const primaryTagsCount = 1;
+ const filteredTagIds =
+ searchParams?.get("tagId")?.split(",")?.filter(Boolean) ?? [];
+
+ /*
+ Sort tags so that the filtered tags are first. The most recently selected
+ filtered tag (last in array) should be displayed first.
+ */
+ const sortedTags =
+ filteredTagIds.length > 0
+ ? [...tags].sort(
+ (a, b) =>
+ filteredTagIds.indexOf(b.id) - filteredTagIds.indexOf(a.id),
+ )
+ : tags;
+
return [
- tags.filter((_, idx) => idx < primaryTagsCount),
- tags.filter((_, idx) => idx >= primaryTagsCount),
+ sortedTags.filter((_, idx) => idx < primaryTagsCount),
+ sortedTags.filter((_, idx) => idx >= primaryTagsCount),
];
- }, [tags]);
+ }, [tags, searchParams]);
const apexDomain = getApexDomain(url);