Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add multi-tag filtering with AND logic #505

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/components/Tags/Tags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,34 @@ import { cn } from "@/lib/utils";
type TagProps = {
tag: string;
isActive?: boolean;
activeTags?: string[];
} & Omit<LinkProps, "href"> &
ComponentPropsWithoutRef<"a">;

export function Tag({ tag, isActive, className, ...props }: TagProps) {
export function Tag({
tag,
isActive,
activeTags = [],
className,
...props
}: TagProps) {
const Icon = isActive ? IconRemove : IconTag;

// Update URL based on tag state
const remainingTags = isActive
? activeTags.filter((t) => t !== tag)
: [...activeTags, tag];

const searchParams = new URLSearchParams(
remainingTags.map((tag) => ["tag", encodeURIComponent(tag)]),
);
const href = searchParams.toString() ? `/?${searchParams}` : "/";

return (
<Link
{...props}
className={cn(styles.tag, className, isActive && styles.active)}
href={isActive ? "/" : `/?tag=${encodeURIComponent(tag)}`}
href={href}
>
<Icon className={cn(styles.icon)} />
<span className={styles.label}>{tag}</span>
Expand All @@ -30,17 +48,23 @@ export function Tag({ tag, isActive, className, ...props }: TagProps) {

interface TagsProps {
tags: string[];
activeTag?: string;
activeTags: string[];
className?: string;
}

export function Tags({ tags, activeTag, className }: TagsProps) {
export function Tags({ tags, activeTags, className }: TagsProps) {
const label = getLabel("filterByTag");
return (
<div className={cn(styles.tags, className)}>
{!!label && <h3>{label}</h3>}
{tags.map((tag) => (
<Tag key={tag} tag={tag} isActive={activeTag == tag} scroll={false} />
<Tag
key={tag}
tag={tag}
isActive={activeTags.includes(tag)}
activeTags={activeTags}
scroll={false}
/>
))}
</div>
);
Expand Down
26 changes: 21 additions & 5 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { CustomPage } from "@/pages/_app";

const Home: CustomPage = () => {
const router = useRouter();
const tag = router.query.tag as string | undefined;
const tagQuery = router.query.tag;
const appName = getAppName();
const metaDescription = getLabel("metaDescription");
const chartConfig = getChartConfig();
Expand All @@ -29,9 +29,25 @@ const Home: CustomPage = () => {
const rings = getRings();
const quadrants = getQuadrants();
const tags = getTags();
const items = getItems(undefined, true).filter(
(item) => !tag || item.tags?.includes(tag),
);

// Convert URL tag parameter(s) to array of decoded tags
const activeTags = (Array.isArray(tagQuery) ? tagQuery : [tagQuery])
.filter((tag): tag is string => Boolean(tag))
.map((tag) => decodeURIComponent(tag));

// Clean up URL if needed
if (tagQuery === "") {
router.replace("/", undefined, { shallow: true });
}

// Filter items by selected tags (AND logic)
const items = getItems(undefined, true).filter((item) => {
if (!activeTags.length) return true;
const itemTags = item.tags ?? [];
return (
itemTags.length > 0 && activeTags.every((tag) => itemTags.includes(tag))
);
});

return (
<>
Expand Down Expand Up @@ -65,7 +81,7 @@ const Home: CustomPage = () => {
return (
getToggle("showTagFilter") &&
tags.length > 0 && (
<Tags key={section} tags={tags} activeTag={tag} />
<Tags key={section} tags={tags} activeTags={activeTags} />
)
);
case "list":
Expand Down