fix: hasHistory

This commit is contained in:
hamster1963 2024-10-22 20:51:07 +08:00
parent 1b868a90b5
commit 11abe9255a
2 changed files with 14 additions and 12 deletions

View File

@ -30,11 +30,10 @@ export default function ServerDetailClient({
useEffect(() => { useEffect(() => {
const previousPath = sessionStorage.getItem("lastPath"); const previousPath = sessionStorage.getItem("lastPath");
const currentPath = window.location.pathname; if (previousPath) {
if (previousPath && previousPath !== currentPath) {
setHasHistory(true); setHasHistory(true);
} else { } else {
const currentPath = window.location.pathname;
sessionStorage.setItem("lastPath", currentPath); sessionStorage.setItem("lastPath", currentPath);
} }
}, []); }, []);

View File

@ -12,11 +12,18 @@ import useSWR from "swr";
export default function ServerListClient() { export default function ServerListClient() {
const t = useTranslations("ServerListClient"); const t = useTranslations("ServerListClient");
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null);
const defaultTag = t("defaultTag"); const defaultTag = t("defaultTag");
const [tag, setTag] = useState<string>(
sessionStorage.getItem("selectedTag") || defaultTag, const [tag, setTag] = useState<string>(defaultTag);
);
const [isMounted, setIsMounted] = useState(false);
useEffect(() => {
const savedTag = sessionStorage.getItem("selectedTag") || defaultTag;
setTag(savedTag);
restoreScrollPosition();
setIsMounted(true);
}, []);
const handleTagChange = (newTag: string) => { const handleTagChange = (newTag: string) => {
setTag(newTag); setTag(newTag);
@ -34,10 +41,6 @@ export default function ServerListClient() {
} }
}; };
useEffect(() => {
restoreScrollPosition();
}, [tag]);
useEffect(() => { useEffect(() => {
const handleRouteChange = () => { const handleRouteChange = () => {
restoreScrollPosition(); restoreScrollPosition();
@ -61,7 +64,7 @@ export default function ServerListClient() {
</div> </div>
); );
if (!data?.result) return null; if (!data?.result || !isMounted) return null;
const { result } = data; const { result } = data;
const sortedServers = result.sort((a, b) => { const sortedServers = result.sort((a, b) => {