Skip to content

Commit 91cad60

Browse files
authored
Adjust TOC to support the same article linking from multiple locations (#653)
* feat: enhance navigation state management with session storage - Updated the LeftNavTree component to support saving and retrieving navigation item IDs from session storage, improving user experience by maintaining state across sessions. - Modified the calcExpandedIds function to accept an optional targetId for more precise matching. - Implemented helper functions to handle session storage interactions for navigation items. * refactor: simplify navigation state management in LeftNavTree component - Removed session storage interactions for expanded and selected navigation items, streamlining state management. - Updated the calcExpandedIds function calls to eliminate unnecessary parameters, enhancing clarity and performance. * refactor: simplify scroll behavior in scrollToElementIfInView function - Updated the scrollToElementIfInView function to directly call scrollIntoView with a "center" block option, removing unnecessary viewport checks and improving code clarity.
1 parent c35718b commit 91cad60

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

src/components/Layout/Navigation/LeftNavTree.tsx

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,18 @@ function StyledTreeItem(props: StyledTreeItemProps) {
7272
);
7373
}
7474

75-
const calcExpandedIds = (data: RepoNavLink[], targetLink: string) => {
75+
const calcExpandedIds = (
76+
data: RepoNavLink[],
77+
targetLink: string,
78+
targetId?: string
79+
) => {
7680
const ids: string[] = [];
7781
const treeForeach = (data: RepoNavLink[], parents: string[] = []): void => {
7882
data.forEach((item) => {
79-
if (item.link === targetLink) {
83+
const isMatch = targetId
84+
? item.link === targetLink && item.id === targetId
85+
: item.link === targetLink;
86+
if (isMatch) {
8087
ids.push(...parents);
8188
ids.push(item.id);
8289
return;
@@ -90,6 +97,29 @@ const calcExpandedIds = (data: RepoNavLink[], targetLink: string) => {
9097
return ids;
9198
};
9299

100+
// Session storage key prefix for nav item id
101+
const NAV_ITEM_ID_STORAGE_KEY = "nav_item_id_";
102+
103+
// Get nav item id from session storage for a given path
104+
const getNavItemIdFromStorage = (path: string): string | null => {
105+
if (typeof window === "undefined") return null;
106+
try {
107+
return sessionStorage.getItem(`${NAV_ITEM_ID_STORAGE_KEY}${path}`);
108+
} catch {
109+
return null;
110+
}
111+
};
112+
113+
// Save nav item id to session storage for a given path
114+
const saveNavItemIdToStorage = (path: string, id: string): void => {
115+
if (typeof window === "undefined") return;
116+
try {
117+
sessionStorage.setItem(`${NAV_ITEM_ID_STORAGE_KEY}${path}`, id);
118+
} catch {
119+
// Ignore storage errors
120+
}
121+
};
122+
93123
export default function ControlledTreeView(props: {
94124
data: RepoNav;
95125
current: string;
@@ -110,7 +140,12 @@ export default function ControlledTreeView(props: {
110140
const theme = useTheme();
111141

112142
React.useEffect(() => {
113-
const expandedIds = calcExpandedIds(data, currentUrl);
143+
const storedId = getNavItemIdFromStorage(currentUrl);
144+
const expandedIds = calcExpandedIds(
145+
data,
146+
currentUrl,
147+
storedId || undefined
148+
);
114149
setExpanded(expandedIds);
115150
expandedIds.length && setSelected([expandedIds[expandedIds.length - 1]]);
116151
}, [data, currentUrl]);
@@ -191,7 +226,13 @@ export default function ControlledTreeView(props: {
191226
key={item.id}
192227
to={item.link}
193228
style={{ width: "100%", color: "inherit" }}
194-
onClick={(e) => e.stopPropagation()}
229+
onClick={(e) => {
230+
e.stopPropagation();
231+
// Save nav item id to session storage when clicked
232+
if (item.link) {
233+
saveNavItemIdToStorage(item.link, item.id);
234+
}
235+
}}
195236
>
196237
<StyledTreeItem
197238
nodeId={item.id}

src/shared/utils/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,7 @@ export function isInViewport(element: HTMLElement) {
289289
export function scrollToElementIfInView(
290290
element: HTMLElement & { scrollIntoViewIfNeeded: () => void }
291291
) {
292-
const isVisiable = isInViewport(element);
293-
if (isVisiable) {
294-
return;
295-
}
296-
if (element.scrollIntoViewIfNeeded) {
297-
element.scrollIntoViewIfNeeded();
298-
} else {
299-
element.scrollIntoView({ block: "end" });
300-
}
292+
element.scrollIntoView({ block: "center" });
301293
}
302294

303295
export type PageType = "home" | "tidb" | "tidbcloud" | undefined;

0 commit comments

Comments
 (0)