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: allow selecting multiple tags #482

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
51 changes: 47 additions & 4 deletions src/components/Tags/Tags.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Link, { LinkProps } from "next/link";
import { useRouter } from "next/router";
import { ComponentPropsWithoutRef } from "react";

import styles from "./Tags.module.css";
Expand All @@ -14,13 +15,49 @@ type TagProps = {
} & Omit<LinkProps, "href"> &
ComponentPropsWithoutRef<"a">;

function QueryString(tag: string, isActive: boolean | undefined) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review:
I could find a quick way to convert router.query to URLSearchParams and had to write the QueryString function.
I've tried new URLSearchParams(router.query as Record<string, string>),. It converts arrays to coma-separated strings, and I lose query parameters type-safety when working with string.

const router = useRouter();

// Convert router.query dict to URLSearchParams.
const queryParams = new URLSearchParams();
for (const key in router.query) {
const value = router.query[key];
if (Array.isArray(value)) {
value.forEach((v) => queryParams.append(key, v));
} else {
queryParams.append(key, router.query[key] as string);
}
}

// Convert tag query parameter to tag[].
if (queryParams.has("tag")) {
queryParams.append("tag[]", queryParams.get("tag") as string);
queryParams.delete("tag");
}

// Update the tag query parameter.
// If tag is active, remove it from the query string.
if (isActive) {
queryParams.delete("tag[]", tag);
} else {
queryParams.append("tag[]", tag);
}

// Convert URLSearchParams to query string.
// Replace %5B%5D= with []= to be more readable.
const queryString = queryParams.toString().replaceAll("%5B%5D=", "[]=");

// Return the query string.
return queryString.length ? `/?${queryString}` : "/";
}

export function Tag({ tag, isActive, className, ...props }: TagProps) {
const Icon = isActive ? IconRemove : IconTag;
return (
<Link
{...props}
className={cn(styles.tag, className, isActive && styles.active)}
href={isActive ? "/" : `/?tag=${tag}`}
href={QueryString(tag, isActive)}
>
<Icon className={cn(styles.icon)} />
<span className={styles.label}>{tag}</span>
Expand All @@ -30,17 +67,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");
const activeTagsList = activeTags || [];
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={activeTagsList.includes(tag)}
scroll={false}
/>
))}
</div>
);
Expand Down
11 changes: 8 additions & 3 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import { CustomPage } from "@/pages/_app";

const Home: CustomPage = () => {
const router = useRouter();
const tag = router.query.tag as string | undefined;
const tag = router.query.tag ? router.query.tag : router.query["tag[]"];
const activeTags = Array.isArray(tag) ? tag : tag ? [tag] : [];

const appName = getAppName();
const metaDescription = getLabel("metaDescription");
const chartConfig = getChartConfig();
Expand All @@ -30,7 +32,10 @@ const Home: CustomPage = () => {
const quadrants = getQuadrants();
const tags = getTags();
const items = getItems(undefined, true).filter(
(item) => !tag || item.tags?.includes(tag),
(item) =>
!tag ||
item.tags?.filter((itemTag) => activeTags.includes(itemTag)).length ===
activeTags.length,
);

return (
Expand Down Expand Up @@ -65,7 +70,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