chore: use filter in url params

This commit is contained in:
Steven
2024-01-25 20:05:47 +08:00
parent 6d5e1def76
commit 70d1301dc3
3 changed files with 84 additions and 1 deletions

View File

@ -1,7 +1,7 @@
import { Button } from "@mui/joy";
import { useEffect, useState } from "react";
import { toast } from "react-hot-toast";
import { useParams } from "react-router-dom";
import { useLocation, useParams } from "react-router-dom";
import Empty from "@/components/Empty";
import Icon from "@/components/Icon";
import MemoFilter from "@/components/MemoFilter";
@ -18,6 +18,7 @@ import { useTranslate } from "@/utils/i18n";
const UserProfile = () => {
const t = useTranslate();
const location = useLocation();
const params = useParams();
const userStore = useUserStore();
const loadingState = useLoading();
@ -32,6 +33,18 @@ const UserProfile = () => {
.sort((a, b) => getTimeStampByDate(b.displayTime) - getTimeStampByDate(a.displayTime))
.sort((a, b) => Number(b.pinned) - Number(a.pinned));
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 username = params.username;
if (!username) {
@ -55,6 +68,20 @@ const UserProfile = () => {
return;
}
const urlParams = new URLSearchParams(location.search);
if (tagQuery) {
urlParams.set("tag", tagQuery);
} else {
urlParams.delete("tag");
}
if (textQuery) {
urlParams.set("text", textQuery);
} else {
urlParams.delete("text");
}
const params = urlParams.toString();
window.history.replaceState({}, "", `${location.pathname}${params?.length > 0 ? `?${params}` : ""}`);
memoList.reset();
fetchMemos();
}, [user, tagQuery, textQuery]);