fix: update filter select when redirected
This commit is contained in:
parent
00096552ce
commit
865b5bc31a
1 changed files with 16 additions and 7 deletions
|
|
@ -1,18 +1,23 @@
|
||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { redirect, useSearchParams } from "next/navigation";
|
import { redirect, useSearchParams } from "next/navigation";
|
||||||
import { useState } from "react";
|
import { useEffect, useMemo, useState } from "react";
|
||||||
import TagSelector from "../tag-selector";
|
import TagSelector from "../tag-selector";
|
||||||
|
|
||||||
export default function FilterSelect() {
|
export default function FilterSelect() {
|
||||||
const searchParams = useSearchParams();
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
const rawTags = searchParams.get("tags");
|
const rawTags = searchParams.get("tags");
|
||||||
const preexistingTags = rawTags
|
const preexistingTags = useMemo(
|
||||||
? rawTags
|
() =>
|
||||||
.split(",")
|
rawTags
|
||||||
.map((tag) => tag.trim())
|
? rawTags
|
||||||
.filter((tag) => tag.length > 0)
|
.split(",")
|
||||||
: [];
|
.map((tag) => tag.trim())
|
||||||
|
.filter((tag) => tag.length > 0)
|
||||||
|
: [],
|
||||||
|
[rawTags]
|
||||||
|
);
|
||||||
|
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const [tags, setTags] = useState<string[]>(preexistingTags);
|
const [tags, setTags] = useState<string[]>(preexistingTags);
|
||||||
|
|
@ -21,6 +26,10 @@ export default function FilterSelect() {
|
||||||
redirect(`/?tags=${encodeURIComponent(tags.join(","))}`);
|
redirect(`/?tags=${encodeURIComponent(tags.join(","))}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setTags(preexistingTags);
|
||||||
|
}, [preexistingTags]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="relative">
|
<div className="relative">
|
||||||
<button onClick={() => setIsOpen((prev) => !prev)} className="pill button gap-1 text-nowrap">
|
<button onClick={() => setIsOpen((prev) => !prev)} className="pill button gap-1 text-nowrap">
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue