Skip to content

Commit cf0aa3e

Browse files
committed
Refactor URL and version handling in Content component
- Simplify version ID and URL management logic to enhance clarity and performance. - Consolidate useEffect dependencies for better data handling and state updates. - Improve comments for better understanding of cache management and rendering logic.
1 parent 31bdc5e commit cf0aa3e

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

xp-archive/client/content/Content.tsx

+11-19
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ export const Content = () => {
4444
versionId: selectedVersion ?? '',
4545
});
4646

47+
// Simplified version ID and URL handling
4748
useEffect(() => {
48-
const versionId = selectedVersion ?? data?.versions?.[0]?.versionId;
49-
if (versionId) {
50-
if (!selectedVersion) {
51-
setSelectedVersion(versionId);
52-
}
53-
const newUrl = `${xpArchiveConfig.basePath}/${selectedContentId}/${selectedLocale}/${versionId}`;
49+
if (!selectedVersion && data?.versions?.[0]) {
50+
setSelectedVersion(data.versions[0].versionId);
51+
}
52+
if (selectedContentId && selectedLocale && selectedVersion) {
53+
const newUrl = `${xpArchiveConfig.basePath}/${selectedContentId}/${selectedLocale}/${selectedVersion}`;
5454
window.history.replaceState({}, '', newUrl);
5555
}
56-
}, [data, selectedContentId, selectedLocale, selectedVersion]);
56+
}, [data?.versions, selectedContentId, selectedLocale, selectedVersion]);
5757

5858
const isWebpage = !!data?.html && !data.json.attachment;
5959
const hasAttachment = !!data?.json.attachment;
@@ -71,7 +71,7 @@ export const Content = () => {
7171
};
7272
});
7373

74-
// Update cache when content ID changes or new versions arrive
74+
// Single effect for cache management
7575
useEffect(() => {
7676
if (prevContentIdRef.current && prevContentIdRef.current !== selectedContentId) {
7777
clearCachedVersionSelector(prevContentIdRef.current);
@@ -81,7 +81,7 @@ export const Content = () => {
8181
setVersionSelectorCache((prev) => ({
8282
component: null,
8383
versions: data.versions,
84-
isOpen: prev.isOpen, // Always preserve open state
84+
isOpen: prev.isOpen,
8585
}));
8686
}
8787

@@ -130,16 +130,11 @@ export const Content = () => {
130130
{getVersionDisplay()}
131131
</Button>
132132

133-
{/* Render either the cached component or a new VersionSelector */}
134133
{versionSelectorCache.component ? (
135134
versionSelectorCache.component
136135
) : (
137136
<VersionSelector
138-
versions={
139-
versionSelectorCache.versions.length > 0
140-
? versionSelectorCache.versions
141-
: data?.versions || []
142-
}
137+
versions={data?.versions || []}
143138
isOpen={versionSelectorCache.isOpen}
144139
onClose={() => {
145140
setVersionSelectorCache((prev) => ({
@@ -148,13 +143,10 @@ export const Content = () => {
148143
}));
149144
}}
150145
onMount={(component) => {
151-
// Cache the rendered component with the content ID
152146
setCachedVersionSelector(
153147
selectedContentId || '',
154148
component,
155-
versionSelectorCache.versions.length > 0
156-
? versionSelectorCache.versions
157-
: data?.versions || [],
149+
data?.versions || [],
158150
versionSelectorCache.isOpen
159151
);
160152
}}

0 commit comments

Comments
 (0)