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 links filtering

Tim Wilson344180ef54a8
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/page-client.tsx
index e90c6ec9e0..8ff3258aff 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/page-client.tsx
@@ -21,9 +21,9 @@ export default function WorkspaceLinksClient() {
return (
<>
<AddEditLinkModal />
- <div className="flex h-36 items-center border-b border-gray-200 bg-white">
+ <div className="mt-10 flex items-center">
<MaxWidthWrapper>
- <div className="flex items-center justify-between">
+ <div className="flex items-center justify-between py-3">
<h1 className="text-2xl font-semibold tracking-tight text-black">
Links
</h1>
diff --git a/apps/web/ui/links/link-filters.tsx b/apps/web/ui/links/link-filters.tsx
index 7a44fb9b86..72a8ab2010 100644
--- a/apps/web/ui/links/link-filters.tsx
+++ b/apps/web/ui/links/link-filters.tsx
@@ -165,7 +165,7 @@ export const SearchBox = ({ searchInputRef }) => {
<input
ref={searchInputRef}
type="text"
- className="peer w-full rounded-md border border-gray-300 px-10 text-black placeholder:text-gray-400 focus:border-black focus:ring-0 sm:text-sm"
+ className="peer w-full rounded-md border border-gray-200 px-10 text-black outline-none transition-all placeholder:text-gray-400 focus:border-gray-500 focus:ring-4 focus:ring-gray-200 sm:text-sm"
placeholder="Search..."
defaultValue={searchParams?.get("search") || ""}
onChange={(e) => {
diff --git a/apps/web/ui/links/link-sort.tsx b/apps/web/ui/links/link-sort.tsx
index 649efac959..80b9d08fbe 100644
--- a/apps/web/ui/links/link-sort.tsx
+++ b/apps/web/ui/links/link-sort.tsx
@@ -1,8 +1,8 @@
-import { Sort } from "@/ui/shared/icons";
-import { IconMenu, Popover, Tick, useRouterStuff } from "@dub/ui";
+import { Button, IconMenu, Popover, Tick, useRouterStuff } from "@dub/ui";
import { ChevronDown, SortDesc } from "lucide-react";
import { useSearchParams } from "next/navigation";
import { useMemo, useState } from "react";
+import { Sort } from "../shared/icons";
const sortOptions = [
{
@@ -60,7 +60,28 @@ export default function LinkSort() {
openPopover={openPopover}
setOpenPopover={setOpenPopover}
>
- <button
+ <Button
+ variant="secondary"
+ onClick={() => setOpenPopover(!openPopover)}
+ text={
+ <span className="flex items-center gap-3">
+ <span>{sort ? selectedSort.display : "Sort"}</span>
+ <ChevronDown
+ className={`h-4 w-4 text-gray-400 ${
+ openPopover ? "rotate-180 transform" : ""
+ } transition-all duration-75`}
+ />
+ </span>
+ }
+ icon={
+ sort ? (
+ <SortDesc className="h-4 w-4" />
+ ) : (
+ <Sort className="h-4 w-4 shrink-0" />
+ )
+ }
+ />
+ {/* <button
onClick={() => setOpenPopover(!openPopover)}
className="flex w-48 items-center justify-between space-x-2 rounded-md bg-white px-3 py-2.5 shadow transition-all duration-75 hover:shadow-md"
>
@@ -79,7 +100,7 @@ export default function LinkSort() {
openPopover ? "rotate-180 transform" : ""
} transition-all duration-75`}
/>
- </button>
+ </button> */}
</Popover>
);
}
diff --git a/apps/web/ui/links/links-container.tsx b/apps/web/ui/links/links-container.tsx
index 70304c4921..83f7f1da04 100644
--- a/apps/web/ui/links/links-container.tsx
+++ b/apps/web/ui/links/links-container.tsx
@@ -2,15 +2,16 @@
import useLinks from "@/lib/swr/use-links";
import useLinksCount from "@/lib/swr/use-links-count";
-import { MaxWidthWrapper } from "@dub/ui";
+import { Filter, MaxWidthWrapper } from "@dub/ui";
import { Suspense, useRef } from "react";
import { useLinkFiltersModal } from "../modals/link-filters-modal";
import LinkCard from "./link-card";
import LinkCardPlaceholder from "./link-card-placeholder";
-import LinkFilters, { SearchBox } from "./link-filters";
+import { SearchBox } from "./link-filters";
import LinkPagination from "./link-pagination";
import LinkSort from "./link-sort";
import NoLinksPlaceholder from "./no-links-placeholder";
+import { useLinkFilters } from "./use-link-filters";
export default function LinksContainer({
AddEditLinkButton,
@@ -22,25 +23,39 @@ export default function LinksContainer({
const { LinkFiltersButton, LinkFiltersModal } = useLinkFiltersModal();
const searchInputRef = useRef();
+ const { filters, activeFilters, onSelect, onRemove, onRemoveAll } =
+ useLinkFilters();
+
return (
<>
<LinkFiltersModal />
- <MaxWidthWrapper className="flex flex-col space-y-3 py-3">
- <div className="flex h-10 w-full justify-center lg:justify-end">
- <LinkFiltersButton />
- <Suspense>
- <LinkSort />
- </Suspense>
- </div>
- <div className="block lg:hidden">
- <SearchBox searchInputRef={searchInputRef} />
- </div>
- <div className="grid grid-cols-1 gap-5 lg:grid-cols-7">
- <div className="scrollbar-hide sticky top-32 col-span-2 hidden max-h-[calc(100vh-150px)] self-start overflow-auto rounded-lg border border-gray-100 bg-white shadow lg:block">
+ <MaxWidthWrapper className="flex flex-col space-y-3 pb-3">
+ <div className="flex grow items-center justify-start gap-2">
+ <div className="w-72 max-w-full">
+ <SearchBox searchInputRef={searchInputRef} />
+ </div>
+ <div className="shrink-0">
+ <Filter.Select
+ filters={filters}
+ onSelect={onSelect}
+ onRemove={onRemove}
+ />
+ </div>
+ <div className="grow-0">
<Suspense>
- <LinkFilters />
+ <LinkSort />
</Suspense>
</div>
+ </div>
+ <div>
+ <Filter.List
+ filters={filters}
+ activeFilters={activeFilters}
+ onRemove={onRemove}
+ onRemoveAll={onRemoveAll}
+ />
+ </div>
+ <div className="grid grid-cols-1 gap-5 lg:grid-cols-5">
<div className="col-span-1 auto-rows-min grid-cols-1 lg:col-span-5">
<ul className="grid min-h-[66.5vh] auto-rows-min gap-3">
{links && !isValidating ? (
diff --git a/apps/web/ui/links/use-link-filters.tsx b/apps/web/ui/links/use-link-filters.tsx
new file mode 100644
index 0000000000..5c9518dd9b
--- /dev/null
+++ b/apps/web/ui/links/use-link-filters.tsx
@@ -0,0 +1,188 @@
+import useDomains from "@/lib/swr/use-domains";
+import useLinksCount from "@/lib/swr/use-links-count";
+import useTags from "@/lib/swr/use-tags";
+import useUser from "@/lib/swr/use-user";
+import useWorkspace from "@/lib/swr/use-workspace";
+import { BlurImage, Globe, Tag, Trash, User, useRouterStuff } from "@dub/ui";
+import { DUB_WORKSPACE_ID, GOOGLE_FAVICON_URL, cn } from "@dub/utils";
+import { useMemo } from "react";
+import { COLORS_LIST } from "./tag-badge";
+
+export function useLinkFilters() {
+ const domains = useDomainFilterOptions();
+ const tags = useTagFilterOptions();
+ const { user } = useUser();
+
+ const { queryParams, searchParamsObj } = useRouterStuff();
+
+ const filters = useMemo(() => {
+ return [
+ {
+ key: "domain",
+ icon: Globe,
+ label: "Domain",
+ getOptionIcon: (value) => (
+ <BlurImage
+ src={`${GOOGLE_FAVICON_URL}${value}`}
+ alt={value}
+ className="h-4 w-4 rounded-full"
+ width={16}
+ height={16}
+ />
+ ),
+ options: domains.map(({ slug, count }) => ({
+ value: slug,
+ label: slug,
+ right: count,
+ })),
+ },
+ {
+ key: "tagId",
+ icon: Tag,
+ label: "Tag",
+ getOptionIcon: (value, props) => {
+ const tagColor =
+ props.option?.data?.color ??
+ tags?.find(({ id }) => id === value)?.color;
+ return tagColor ? (
+ <div
+ className={cn(
+ "rounded-md p-1.5",
+ COLORS_LIST.find(({ color }) => color === tagColor)?.css,
+ )}
+ >
+ <Tag className="h-2.5 w-2.5" />
+ </div>
+ ) : null;
+ },
+ options:
+ tags?.map(({ id, name, color, count }) => ({
+ value: id,
+ icon: (
+ <div
+ className={cn(
+ "rounded-md p-1.5",
+ COLORS_LIST.find((c) => c.color === color)?.css,
+ )}
+ >
+ <Tag className="h-2.5 w-2.5" />
+ </div>
+ ),
+ label: name,
+ data: { color },
+ right: count,
+ })) ?? null,
+ },
+ ...(user?.id
+ ? [
+ {
+ key: "userId",
+ icon: User,
+ label: "Creator",
+ options: [
+ {
+ value: user?.id,
+ label: "Me",
+ },
+ ],
+ },
+ ]
+ : []),
+ {
+ key: "showArchived",
+ icon: Trash,
+ label: "Archived",
+ options: [
+ {
+ value: true,
+ label: "Include archived links",
+ },
+ ],
+ },
+ ];
+ }, [domains, tags]);
+
+ const activeFilters = useMemo(() => {
+ const { domain, tagId, userId, showArchived } = searchParamsObj;
+ return [
+ ...(domain ? [{ key: "domain", value: domain }] : []),
+ ...(tagId ? [{ key: "tagId", value: tagId }] : []),
+ ...(userId ? [{ key: "userId", value: userId }] : []),
+ ...(showArchived
+ ? [{ key: "root", value: showArchived === "true" }]
+ : []),
+ ];
+ }, [searchParamsObj]);
+
+ const onSelect = (key: string, value: any) => {
+ queryParams({
+ set: {
+ [key]: value,
+ },
+ });
+ };
+
+ const onRemove = (key: string) => {
+ queryParams({
+ del: key,
+ });
+ };
+
+ const onRemoveAll = () => {
+ queryParams({
+ del: ["domain", "tagId", "userId", "showArchived", "search"],
+ });
+ };
+
+ return { filters, activeFilters, onSelect, onRemove, onRemoveAll };
+}
+
+function useDomainFilterOptions() {
+ const { id: workspaceId } = useWorkspace();
+ const { data: domains } = useLinksCount({ groupBy: "domain" });
+ const { activeWorkspaceDomains, activeDefaultDomains } = useDomains();
+
+ return useMemo(() => {
+ if (domains?.length === 0) return [];
+
+ const workspaceDomains = activeWorkspaceDomains?.map((domain) => ({
+ ...domain,
+ count: domains?.find(({ domain: d }) => d === domain.slug)?._count || 0,
+ }));
+
+ const defaultDomains =
+ workspaceId === `ws_${DUB_WORKSPACE_ID}`
+ ? []
+ : activeDefaultDomains
+ ?.map((domain) => ({
+ ...domain,
+ count:
+ domains?.find(({ domain: d }) => d === domain.slug)?._count ||
+ 0,
+ }))
+ .filter((d) => d.count > 0);
+
+ const finalOptions = [
+ ...(workspaceDomains || []),
+ ...(defaultDomains || []),
+ ].sort((a, b) => b.count - a.count);
+
+ return finalOptions;
+ }, [activeWorkspaceDomains, activeDefaultDomains, domains, workspaceId]);
+}
+
+function useTagFilterOptions() {
+ const { tags } = useTags();
+ const { data: tagsCount } = useLinksCount({ groupBy: "tagId" });
+
+ return useMemo(
+ () =>
+ tags
+ ?.map((tag) => ({
+ ...tag,
+ count: tagsCount?.find(({ tagId }) => tagId === tag.id)?._count || 0,
+ }))
+ .sort((a, b) => b.count - a.count) ?? null,
+ [tags, tagsCount],
+ );
+}
diff --git a/packages/ui/src/filter/filter-select.tsx b/packages/ui/src/filter/filter-select.tsx
index 86a84a664c..449330133e 100644
--- a/packages/ui/src/filter/filter-select.tsx
+++ b/packages/ui/src/filter/filter-select.tsx
@@ -20,7 +20,7 @@ import { Filter, FilterOption } from "./types";
type FilterSelectProps = {
filters: Filter[];
- onSelect: (key: string, value: string) => void;
+ onSelect: (key: string, value: FilterOption["value"]) => void;
onRemove: (key: string) => void;
onOpenFilter?: (key: string) => void;
activeFilters?: {
diff --git a/packages/ui/src/icons/nucleo/index.ts b/packages/ui/src/icons/nucleo/index.ts
index f2b71e0a84..97b0f9e403 100644
--- a/packages/ui/src/icons/nucleo/index.ts
+++ b/packages/ui/src/icons/nucleo/index.ts
@@ -17,6 +17,8 @@ export * from "./referred-via";
export * from "./sliders";
export * from "./tablet";
export * from "./tag";
+export * from "./trash";
export * from "./tv";
+export * from "./user";
export * from "./watch";
export * from "./window";
diff --git a/packages/ui/src/icons/nucleo/trash.tsx b/packages/ui/src/icons/nucleo/trash.tsx
new file mode 100644
index 0000000000..b124b1fa7b
--- /dev/null
+++ b/packages/ui/src/icons/nucleo/trash.tsx
@@ -0,0 +1,37 @@
+import { SVGProps } from "react";
+
+export function Trash(props: SVGProps<SVGSVGElement>) {
+ return (
+ <svg viewBox="0 0 18 18" xmlns="http://www.w3.org/2000/svg" {...props}>
+ <g fill="currentColor">
+ <path
+ d="m13.474,7.25l-.374,7.105c-.056,1.062-.934,1.895-1.997,1.895h-4.205c-1.064,0-1.941-.833-1.997-1.895l-.374-7.105"
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ <path
+ d="m6.75,4.75v-2c0-.552.448-1,1-1h2.5c.552,0,1,.448,1,1v2"
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ <line
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ x1="2.75"
+ x2="15.25"
+ y1="4.75"
+ y2="4.75"
+ />
+ </g>
+ </svg>
+ );
+}
diff --git a/packages/ui/src/icons/nucleo/user.tsx b/packages/ui/src/icons/nucleo/user.tsx
new file mode 100644
index 0000000000..e9c7f78501
--- /dev/null
+++ b/packages/ui/src/icons/nucleo/user.tsx
@@ -0,0 +1,28 @@
+import { SVGProps } from "react";
+
+export function User(props: SVGProps<SVGSVGElement>) {
+ return (
+ <svg viewBox="0 0 18 18" xmlns="http://www.w3.org/2000/svg" {...props}>
+ <g fill="currentColor">
+ <circle
+ cx="9"
+ cy="4.5"
+ fill="none"
+ r="2.75"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ <path
+ d="M13.762,15.516c.86-.271,1.312-1.221,.947-2.045-.97-2.191-3.159-3.721-5.709-3.721s-4.739,1.53-5.709,3.721c-.365,.825,.087,1.774,.947,2.045,1.225,.386,2.846,.734,4.762,.734s3.537-.348,4.762-.734Z"
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ </g>
+ </svg>
+ );
+}