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 multi-tag filtering

Tim Wilson89c7d62eddef
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 cf0841c0ac..2d0f9f4a64 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
@@ -75,6 +75,7 @@ export default function WorkspaceLinksClient() {
<div className="grow basis-0 md:grow-0">
<Filter.Select
filters={filters}
+ activeFilters={activeFilters}
onSelect={onSelect}
onRemove={onRemove}
className="w-full"
diff --git a/apps/web/ui/links/use-link-filters.tsx b/apps/web/ui/links/use-link-filters.tsx
index 5d7b39ddb3..1ea9f9ff13 100644
--- a/apps/web/ui/links/use-link-filters.tsx
+++ b/apps/web/ui/links/use-link-filters.tsx
@@ -37,9 +37,10 @@ export function useLinkFilters() {
})),
},
{
- key: "tagId",
+ key: "tagIds",
icon: Tag,
label: "Tag",
+ multiple: true,
getOptionIcon: (value, props) => {
const tagColor =
props.option?.data?.color ??
@@ -91,32 +92,56 @@ export function useLinkFilters() {
];
}, [domains, tags]);
+ const selectedTagIds = useMemo(
+ () => searchParamsObj["tagIds"]?.split(",")?.filter(Boolean) ?? [],
+ [searchParamsObj],
+ );
+
const activeFilters = useMemo(() => {
- const { domain, tagId, userId } = searchParamsObj;
+ const { domain, tagIds, userId } = searchParamsObj;
return [
...(domain ? [{ key: "domain", value: domain }] : []),
- ...(tagId ? [{ key: "tagId", value: tagId }] : []),
+ ...(tagIds ? [{ key: "tagIds", value: selectedTagIds }] : []),
...(userId ? [{ key: "userId", value: userId }] : []),
];
}, [searchParamsObj]);
const onSelect = (key: string, value: any) => {
- queryParams({
- set: {
- [key]: value,
- },
- });
+ if (key === "tagIds") {
+ queryParams({
+ set: {
+ tagIds: selectedTagIds.concat(value).join(","),
+ },
+ });
+ } else {
+ queryParams({
+ set: {
+ [key]: value,
+ },
+ });
+ }
};
- const onRemove = (key: string) => {
- queryParams({
- del: key,
- });
+ const onRemove = (key: string, value: any) => {
+ if (
+ key === "tagIds" &&
+ !(selectedTagIds.length === 1 && selectedTagIds[0] === value)
+ ) {
+ queryParams({
+ set: {
+ tagIds: selectedTagIds.filter((id) => id !== value).join(","),
+ },
+ });
+ } else {
+ queryParams({
+ del: key,
+ });
+ }
};
const onRemoveAll = () => {
queryParams({
- del: ["domain", "tagId", "userId", "search"],
+ del: ["domain", "tagIds", "userId", "search"],
});
};
diff --git a/packages/ui/src/filter/filter-list.tsx b/packages/ui/src/filter/filter-list.tsx
index 6e2e123e11..d5f604decc 100644
--- a/packages/ui/src/filter/filter-list.tsx
+++ b/packages/ui/src/filter/filter-list.tsx
@@ -32,11 +32,11 @@ export function FilterList({
<div className={cn("flex w-full flex-wrap items-start gap-4", className)}>
<div className="flex grow flex-wrap gap-x-4 gap-y-2">
<AnimatePresence>
- {activeFilters?.map(({ key, value }) => {
+ {activeFilters?.map(({ key, value: filterValue }) => {
if (key === "loader") {
return (
<motion.div
- key={`loader-${value}`}
+ key={`loader-${filterValue}`}
initial={{ opacity: 0, y: 4 }}
animate={{ opacity: 1, y: 0 }}
className="h-9 w-48 animate-pulse rounded-md border border-gray-200 bg-white"
@@ -51,75 +51,81 @@ export function FilterList({
);
}
- const option = filter.options?.find((o) =>
- typeof o.value === "string" && typeof value === "string"
- ? o.value.toLowerCase() === value.toLowerCase()
- : o.value === value,
- );
+ return (
+ filter.multiple && Array.isArray(filterValue)
+ ? filterValue
+ : [filterValue]
+ ).map((value) => {
+ const option = filter.options?.find((o) =>
+ typeof o.value === "string" && typeof value === "string"
+ ? o.value.toLowerCase() === value.toLowerCase()
+ : o.value === value,
+ );
- const OptionIcon =
- option?.icon ??
- filter.getOptionIcon?.(value, {
- key: filter.key,
- option,
- }) ??
- filter.icon;
+ const OptionIcon =
+ option?.icon ??
+ filter.getOptionIcon?.(value, {
+ key: filter.key,
+ option,
+ }) ??
+ filter.icon;
- const optionLabel =
- option?.label ??
- filter.getOptionLabel?.(value, { key: filter.key, option }) ??
- value;
+ const optionLabel =
+ option?.label ??
+ filter.getOptionLabel?.(value, { key: filter.key, option }) ??
+ value;
- return (
- <motion.div
- key={key}
- initial={{ opacity: 0, y: 4 }}
- animate={{ opacity: 1, y: 0 }}
- className="flex items-center divide-x rounded-md border border-gray-200 bg-white text-sm text-black"
- >
- {/* Filter */}
- <div className="flex items-center gap-2.5 px-3 py-2">
- <span className="shrink-0 text-gray-600">
- {isReactNode(filter.icon) ? (
- filter.icon
- ) : (
- <filter.icon className="h-4 w-4" />
- )}
- </span>
- {filter.label}
- </div>
+ return (
+ <motion.div
+ key={key}
+ initial={{ opacity: 0, y: 4 }}
+ animate={{ opacity: 1, y: 0 }}
+ className="flex items-center divide-x rounded-md border border-gray-200 bg-white text-sm text-black"
+ >
+ {/* Filter */}
+ <div className="flex items-center gap-2.5 px-3 py-2">
+ <span className="shrink-0 text-gray-600">
+ {isReactNode(filter.icon) ? (
+ filter.icon
+ ) : (
+ <filter.icon className="h-4 w-4" />
+ )}
+ </span>
+ {filter.label}
+ </div>
- {/* is */}
- <div className="px-3 py-2 text-gray-500">is</div>
+ {/* is */}
+ <div className="px-3 py-2 text-gray-500">is</div>
- {/* Option */}
- <div className="flex items-center gap-2.5 px-3 py-2">
- {filter.options ? (
- <>
- <span className="shrink-0 text-gray-600">
- {isReactNode(OptionIcon) ? (
- OptionIcon
- ) : (
- <OptionIcon className="h-4 w-4" />
- )}
- </span>
- {truncate(optionLabel, 30)}
- </>
- ) : (
- <div className="h-5 w-12 animate-pulse rounded-md bg-gray-200" />
- )}
- </div>
+ {/* Option */}
+ <div className="flex items-center gap-2.5 px-3 py-2">
+ {filter.options ? (
+ <>
+ <span className="shrink-0 text-gray-600">
+ {isReactNode(OptionIcon) ? (
+ OptionIcon
+ ) : (
+ <OptionIcon className="h-4 w-4" />
+ )}
+ </span>
+ {truncate(optionLabel, 30)}
+ </>
+ ) : (
+ <div className="h-5 w-12 animate-pulse rounded-md bg-gray-200" />
+ )}
+ </div>
- {/* Remove */}
- <button
- type="button"
- className="h-full rounded-r-md p-2 text-gray-500 ring-inset ring-gray-500 hover:bg-gray-100 hover:text-gray-800 focus:outline-none focus-visible:ring-1"
- onClick={() => onRemove(key)}
- >
- <X className="h-3.5 w-3.5" />
- </button>
- </motion.div>
- );
+ {/* Remove */}
+ <button
+ type="button"
+ className="h-full rounded-r-md p-2 text-gray-500 ring-inset ring-gray-500 hover:bg-gray-100 hover:text-gray-800 focus:outline-none focus-visible:ring-1"
+ onClick={() => onRemove(key)}
+ >
+ <X className="h-3.5 w-3.5" />
+ </button>
+ </motion.div>
+ );
+ });
})}
</AnimatePresence>
</div>
diff --git a/packages/ui/src/filter/filter-select.tsx b/packages/ui/src/filter/filter-select.tsx
index 453aed5c3d..763e3267b3 100644
--- a/packages/ui/src/filter/filter-select.tsx
+++ b/packages/ui/src/filter/filter-select.tsx
@@ -1,6 +1,6 @@
import { cn, truncate } from "@dub/utils";
import { Command, useCommandState } from "cmdk";
-import { Check, ChevronDown, ListFilter } from "lucide-react";
+import { ChevronDown, ListFilter } from "lucide-react";
import {
PropsWithChildren,
ReactNode,
@@ -14,14 +14,14 @@ import {
} from "react";
import { AnimatedSizeContainer } from "../animated-size-container";
import { useMediaQuery, useResizeObserver } from "../hooks";
-import { LoadingSpinner, Magic } from "../icons";
+import { Check, LoadingSpinner, Magic } from "../icons";
import { Popover } from "../popover";
import { Filter, FilterOption } from "./types";
type FilterSelectProps = {
filters: Filter[];
onSelect: (key: string, value: FilterOption["value"]) => void;
- onRemove: (key: string) => void;
+ onRemove: (key: string, value: FilterOption["value"]) => void;
onOpenFilter?: (key: string) => void;
activeFilters?: {
key: Filter["key"];
@@ -87,16 +87,36 @@ export function FilterSelect({
onOpenFilter?.(key);
}, []);
+ const isOptionSelected = useCallback(
+ (value: FilterOption["value"]) => {
+ if (!selectedFilter || !activeFilters) return false;
+
+ const activeFilter = activeFilters.find(
+ ({ key }) => key === selectedFilterKey,
+ );
+
+ return (
+ activeFilter?.value === value ||
+ (activeFilter &&
+ selectedFilter.multiple &&
+ Array.isArray(activeFilter.value) &&
+ activeFilter.value.includes(value))
+ );
+ },
+ [selectedFilter],
+ );
+
const selectOption = useCallback(
(value: FilterOption["value"]) => {
if (selectedFilter) {
- activeFilters?.find(({ key }) => key === selectedFilterKey)?.value ===
- value
- ? onRemove(selectedFilter.key)
+ const isSelected = isOptionSelected(value);
+
+ isSelected
+ ? onRemove(selectedFilter.key, value)
: onSelect(selectedFilter.key, value);
- }
- setIsOpen(false);
+ if (!selectedFilter.multiple) setIsOpen(false);
+ }
},
[activeFilters, selectedFilter, askAI],
);
@@ -168,10 +188,7 @@ export function FilterSelect({
))
: // Filter options
selectedFilter.options?.map((option) => {
- const isSelected =
- activeFilters?.find(
- ({ key }) => key === selectedFilterKey,
- )?.value === option.value;
+ const isSelected = isOptionSelected(option.value);
return (
<FilterButton
diff --git a/packages/ui/src/filter/types.ts b/packages/ui/src/filter/types.ts
index 9a92dfb2b3..a464fe9f57 100644
--- a/packages/ui/src/filter/types.ts
+++ b/packages/ui/src/filter/types.ts
@@ -12,6 +12,7 @@ export type Filter = {
label: string;
separatorAfter?: boolean;
options: FilterOption[] | null;
+ multiple?: boolean;
getOptionIcon?: (
value: FilterOption["value"],
props: { key: Filter["key"]; option?: FilterOption },
diff --git a/packages/ui/src/icons/nucleo/check.tsx b/packages/ui/src/icons/nucleo/check.tsx
new file mode 100644
index 0000000000..dd89bee77f
--- /dev/null
+++ b/packages/ui/src/icons/nucleo/check.tsx
@@ -0,0 +1,24 @@
+import { SVGProps } from "react";
+
+export function Check(props: SVGProps<SVGSVGElement>) {
+ return (
+ <svg
+ height="18"
+ width="18"
+ viewBox="0 0 18 18"
+ xmlns="http://www.w3.org/2000/svg"
+ {...props}
+ >
+ <g fill="currentColor">
+ <polyline
+ fill="none"
+ points="2.75 9.25 6.75 14.25 15.25 3.75"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ </g>
+ </svg>
+ );
+}
diff --git a/packages/ui/src/icons/nucleo/index.ts b/packages/ui/src/icons/nucleo/index.ts
index 6d348ebe76..7e1f7d0a2f 100644
--- a/packages/ui/src/icons/nucleo/index.ts
+++ b/packages/ui/src/icons/nucleo/index.ts
@@ -9,6 +9,7 @@ export * from "./box-archive";
export * from "./bullet-list";
export * from "./cards";
export * from "./chart-line";
+export * from "./check";
export * from "./circle-check";
export * from "./circle-half-dotted-clock";
export * from "./circle-info";