mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: tweak url filters
This commit is contained in:
44
web/src/hooks/useFilterWithUrlParams.ts
Normal file
44
web/src/hooks/useFilterWithUrlParams.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import { useEffect } from "react";
|
||||
import { useLocation } from "react-router-dom";
|
||||
import { useFilterStore } from "@/store/module";
|
||||
|
||||
const useFilterWithUrlParams = () => {
|
||||
const location = useLocation();
|
||||
const filterStore = useFilterStore();
|
||||
const { tag, text } = filterStore.state;
|
||||
|
||||
useEffect(() => {
|
||||
const urlParams = new URLSearchParams(location.search);
|
||||
const tag = urlParams.get("tag");
|
||||
const text = urlParams.get("text");
|
||||
if (tag) {
|
||||
filterStore.setTagFilter(tag);
|
||||
}
|
||||
if (text) {
|
||||
filterStore.setTextFilter(text);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const urlParams = new URLSearchParams(location.search);
|
||||
if (tag) {
|
||||
urlParams.set("tag", tag);
|
||||
} else {
|
||||
urlParams.delete("tag");
|
||||
}
|
||||
if (text) {
|
||||
urlParams.set("text", text);
|
||||
} else {
|
||||
urlParams.delete("text");
|
||||
}
|
||||
const params = urlParams.toString();
|
||||
window.history.replaceState({}, "", `${location.pathname}${params?.length > 0 ? `?${params}` : ""}`);
|
||||
}, [tag, text]);
|
||||
|
||||
return {
|
||||
tag,
|
||||
text,
|
||||
};
|
||||
};
|
||||
|
||||
export default useFilterWithUrlParams;
|
Reference in New Issue
Block a user