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";
|
||||
|
||||
import { redirect, useSearchParams } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import TagSelector from "../tag-selector";
|
||||
|
||||
export default function FilterSelect() {
|
||||
const searchParams = useSearchParams();
|
||||
|
||||
const rawTags = searchParams.get("tags");
|
||||
const preexistingTags = rawTags
|
||||
? rawTags
|
||||
.split(",")
|
||||
.map((tag) => tag.trim())
|
||||
.filter((tag) => tag.length > 0)
|
||||
: [];
|
||||
const preexistingTags = useMemo(
|
||||
() =>
|
||||
rawTags
|
||||
? rawTags
|
||||
.split(",")
|
||||
.map((tag) => tag.trim())
|
||||
.filter((tag) => tag.length > 0)
|
||||
: [],
|
||||
[rawTags]
|
||||
);
|
||||
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [tags, setTags] = useState<string[]>(preexistingTags);
|
||||
|
|
@ -21,6 +26,10 @@ export default function FilterSelect() {
|
|||
redirect(`/?tags=${encodeURIComponent(tags.join(","))}`);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
setTags(preexistingTags);
|
||||
}, [preexistingTags]);
|
||||
|
||||
return (
|
||||
<div className="relative">
|
||||
<button onClick={() => setIsOpen((prev) => !prev)} className="pill button gap-1 text-nowrap">
|
||||
|
|
|
|||
Loading…
Reference in a new issue