Skip to content

Commit 056012c

Browse files
committed
Refactor Content and VersionSelector components to streamline URL management. Removed the updateContentUrl function and added separate effects for URL updates in Content.tsx to prevent unnecessary history entries. Updated VersionSelector to only manage state without modifying the URL directly.
1 parent e8a95cd commit 056012c

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

xp-archive/client/content/Content.tsx

+15-9
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ const getDefaultView = (isWebpage: boolean, hasAttachment: boolean): ViewVariant
1818
return undefined;
1919
};
2020

21-
const updateContentUrl = (nodeId: string, locale: string, versionId?: string) => {
22-
const newUrl = `${xpArchiveConfig.basePath}/${nodeId}/${locale}/${versionId ?? ''}`;
23-
window.history.pushState({}, '', newUrl);
24-
};
25-
2621
export const Content = () => {
2722
const { selectedContentId, selectedLocale, selectedVersion, setSelectedVersion } =
2823
useAppState();
@@ -42,16 +37,27 @@ export const Content = () => {
4237
versionId: selectedVersion ?? '',
4338
});
4439

40+
// Effect to set the initial version if none is selected
4541
useEffect(() => {
46-
if (selectedVersion) {
47-
updateContentUrl(selectedContentId ?? '', selectedLocale, selectedVersion);
48-
} else if (data?.versions?.[0]) {
42+
if (!selectedVersion && data?.versions?.[0]) {
4943
const latestVersionId = data.versions[0].versionId;
5044
setSelectedVersion(latestVersionId);
51-
updateContentUrl(selectedContentId ?? '', selectedLocale, latestVersionId);
5245
}
5346
}, [data, selectedContentId, selectedLocale, selectedVersion]);
5447

48+
// Separate effect for URL updates that doesn't trigger component reloads
49+
useEffect(() => {
50+
if (selectedContentId && selectedLocale) {
51+
const versionPart = selectedVersion ? `/${selectedVersion}` : '';
52+
const newUrl = `${xpArchiveConfig.basePath}/${selectedContentId}/${selectedLocale}${versionPart}`;
53+
54+
// Only update if URL is different to avoid unnecessary history entries
55+
if (window.location.pathname !== newUrl) {
56+
window.history.replaceState({}, '', newUrl);
57+
}
58+
}
59+
}, [selectedContentId, selectedLocale, selectedVersion]);
60+
5561
const isWebpage = !!data?.html && !data.json.attachment;
5662
const hasAttachment = !!data?.json.attachment;
5763
const [selectedView, setSelectedView] = useState<ViewVariant | undefined>(

xp-archive/client/versionSelector/VersionSelector.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ const VersionButton = ({ isSelected, onClick, children }: VersionButtonProps) =>
3434

3535
export const VersionSelector = ({ versions, isOpen, onClose }: Props) => {
3636
const [searchQuery, setSearchQuery] = useState('');
37-
const { setSelectedContentId, selectedVersion, setSelectedVersion } = useAppState();
37+
const { setSelectedVersion, selectedVersion } = useAppState();
3838

3939
const handleClose = () => {
4040
setSearchQuery('');
4141
onClose();
4242
};
4343

4444
const selectVersion = (versionId: string) => {
45-
const nodeId = versions.find((v) => v.versionId === versionId)?.nodeId;
46-
if (nodeId) setSelectedContentId(nodeId);
45+
// Just update the state, don't modify the URL here
46+
// The URL update will be handled by the effect in Content.tsx
4747
setSelectedVersion(versionId);
4848
};
4949

0 commit comments

Comments
 (0)