From 2b1b8729ee1f51d54f393e833643c619fbf1c2b8 Mon Sep 17 00:00:00 2001
From: canisminor1990
Date: Sun, 24 Nov 2024 22:18:43 +0800
Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20Fix=20location?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/pages/Docs/index.tsx | 13 +++++++------
src/slots/Header/LangSwitch.tsx | 11 +++++++----
src/slots/SearchResult/index.tsx | 11 ++++++++---
3 files changed, 22 insertions(+), 13 deletions(-)
diff --git a/src/pages/Docs/index.tsx b/src/pages/Docs/index.tsx
index d77646a..e95cd59 100644
--- a/src/pages/Docs/index.tsx
+++ b/src/pages/Docs/index.tsx
@@ -13,16 +13,17 @@ import { useStyles } from './styles';
const Documents = memo(() => {
const outlet = useOutlet();
const { mobile } = useResponsive();
- const { isApiPage, giscus } = useSiteStore((st) => ({
- giscus: siteSelectors.giscus(st),
- isApiPage: apiHeaderSelectors.isApiPage(st),
+ const { isApiPage, giscus, pathname } = useSiteStore((s) => ({
+ giscus: siteSelectors.giscus(s),
+ isApiPage: apiHeaderSelectors.isApiPage(s),
+ pathname: s.location.pathname,
}));
const { styles } = useStyles();
useEffect(() => {
window?.scrollTo(0, 0);
document?.body.scrollTo(0, 0);
- }, [location.pathname]);
+ }, [pathname]);
const Comment = useCallback(
() =>
@@ -31,14 +32,14 @@ const Documents = memo(() => {
),
- [giscus, location.pathname],
+ [giscus, pathname],
);
return (
diff --git a/src/slots/Header/LangSwitch.tsx b/src/slots/Header/LangSwitch.tsx
index f99502d..e366e45 100644
--- a/src/slots/Header/LangSwitch.tsx
+++ b/src/slots/Header/LangSwitch.tsx
@@ -80,8 +80,11 @@ const SingleSwitch = memo<{ current: ILocaleItem; locale: ILocaleItem }>(({ loca
});
const LangSwitch = memo(() => {
- const locales = useSiteStore((s) => s.siteData.locales);
- const current = useSiteStore((s) => s.locale);
+ const [locales, current, pathname] = useSiteStore((s) => [
+ s.siteData.locales,
+ s.locale,
+ s.location.pathname,
+ ]);
// do not render in single language
if (locales.length <= 1) return;
@@ -92,7 +95,7 @@ const LangSwitch = memo(() => {
console.log(
getTargetLocalePath({
current,
- pathname: location.pathname,
+ pathname,
target: locales[index],
}),
);
@@ -100,7 +103,7 @@ const LangSwitch = memo(() => {
history.push(
getTargetLocalePath({
current,
- pathname: location.pathname,
+ pathname,
target: locales[index],
}),
);
diff --git a/src/slots/SearchResult/index.tsx b/src/slots/SearchResult/index.tsx
index 1a1964f..98a373d 100644
--- a/src/slots/SearchResult/index.tsx
+++ b/src/slots/SearchResult/index.tsx
@@ -2,11 +2,13 @@ import { Icon } from '@lobehub/ui';
import animateScrollTo from 'animated-scroll-to';
import { Empty, Typography } from 'antd';
import { useTheme } from 'antd-style';
-import { FormattedMessage, Link, history, useLocation, type useSiteSearch } from 'dumi';
+import { FormattedMessage, Link, history, type useSiteSearch } from 'dumi';
import { FileBox, FileIcon, HeadingIcon, LetterText, LucideIcon } from 'lucide-react';
import React, { Fragment, memo, useCallback, useEffect, useState } from 'react';
import { Center, Flexbox } from 'react-layout-kit';
+import { siteSelectors, useSiteStore } from '@/store';
+
const ICONS_MAPPING: { [key: string]: LucideIcon } = {
content: LetterText,
demo: FileBox,
@@ -82,12 +84,15 @@ const SearchResult = memo<{
const theme = useTheme();
const [data, histsCount] = useFlatSearchData(props.data);
const [activeIndex, setActiveIndex] = useState(-1);
- const { pathname } = useLocation();
+ const [pathname, hostname] = useSiteStore((s) => [
+ s.location.pathname,
+ siteSelectors.hostname(s),
+ ]);
const onItemSelect = (item: ISearchResult[0]['hints'][0]) => {
props.onItemSelect?.(item);
- const url = new URL(item?.link, location.origin);
+ const url = new URL(item?.link, hostname || location?.origin);
if (url?.pathname === pathname && !url.hash) {
setTimeout(() => {
animateScrollTo(0, {