Skip to content

Commit

Permalink
🐛 fix: Fix location
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Nov 24, 2024
1 parent d9e9007 commit 2b1b872
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
13 changes: 7 additions & 6 deletions src/pages/Docs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
() =>
Expand All @@ -31,14 +32,14 @@ const Documents = memo(() => {
<Giscus
category={giscus.category}
categoryId={giscus.categoryId}
id="lobehub"
id="giscus"
mapping="title"
repo={giscus.repo}
repoId={giscus.repoId}
/>
</div>
),
[giscus, location.pathname],
[giscus, pathname],
);
return (
<Center className={styles.content} style={{ marginBottom: 48, padding: mobile ? 0 : 24 }}>
Expand Down
11 changes: 7 additions & 4 deletions src/slots/Header/LangSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -92,15 +95,15 @@ const LangSwitch = memo(() => {
console.log(
getTargetLocalePath({
current,
pathname: location.pathname,
pathname,
target: locales[index],
}),
);

history.push(
getTargetLocalePath({
current,
pathname: location.pathname,
pathname,
target: locales[index],
}),
);
Expand Down
11 changes: 8 additions & 3 deletions src/slots/SearchResult/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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, {
Expand Down

0 comments on commit 2b1b872

Please sign in to comment.