Skip to content

Commit 31bdc5e

Browse files
committed
Refactor cache and search management in Content and VersionSelector components
- Update cache handling to clear and reset based on content ID changes, ensuring accurate version data. - Simplify the search query reset logic in VersionSelector when versions change, improving user experience. - Streamline the useEffect hooks for better clarity and performance in both components.
1 parent 313e142 commit 31bdc5e

File tree

2 files changed

+15
-55
lines changed

2 files changed

+15
-55
lines changed

xp-archive/client/content/Content.tsx

+4-16
Original file line numberDiff line numberDiff line change
@@ -71,34 +71,22 @@ export const Content = () => {
7171
};
7272
});
7373

74-
// Update this effect to clear the cache immediately when content ID changes
74+
// Update cache when content ID changes or new versions arrive
7575
useEffect(() => {
76-
// Clear the cache for the previous content ID when it changes
7776
if (prevContentIdRef.current && prevContentIdRef.current !== selectedContentId) {
7877
clearCachedVersionSelector(prevContentIdRef.current);
7978
}
8079

81-
prevContentIdRef.current = selectedContentId;
82-
83-
// Also reset the local cache state when content ID changes, but preserve isOpen state
8480
if (data?.versions && selectedContentId) {
8581
setVersionSelectorCache((prev) => ({
8682
component: null,
8783
versions: data.versions,
88-
isOpen: prev.isOpen, // Preserve the open state
84+
isOpen: prev.isOpen, // Always preserve open state
8985
}));
9086
}
91-
}, [selectedContentId, data?.versions]);
9287

93-
// Add a separate effect to handle data loading without affecting panel state
94-
useEffect(() => {
95-
if (data?.versions && selectedContentId) {
96-
setVersionSelectorCache((prev) => ({
97-
...prev,
98-
versions: data.versions,
99-
}));
100-
}
101-
}, [data?.versions, selectedContentId]);
88+
prevContentIdRef.current = selectedContentId;
89+
}, [selectedContentId, data?.versions]);
10290

10391
useEffect(() => {
10492
setSelectedView(getDefaultView(isWebpage, hasAttachment));

xp-archive/client/versionSelector/VersionSelector.tsx

+11-39
Original file line numberDiff line numberDiff line change
@@ -51,44 +51,8 @@ export const VersionSelector = ({ versions, isOpen, onClose, onMount }: Props) =
5151
formatTimestamp(version.timestamp).toLowerCase().includes(searchQuery.toLowerCase())
5252
);
5353

54-
// Call onMount with the rendered component
54+
// Reset search when versions change completely
5555
useEffect(() => {
56-
if (onMount) {
57-
const component = (
58-
<SlidePanel isOpen={isOpen} onClose={handleClose}>
59-
<Heading size="medium" spacing>
60-
Versjoner
61-
</Heading>
62-
<Search
63-
label="Søk i versjoner"
64-
variant="simple"
65-
value={searchQuery}
66-
onChange={setSearchQuery}
67-
className={style.search}
68-
/>
69-
<div className={style.versionList}>
70-
{filteredVersions.map((version, index) => (
71-
<VersionButton
72-
key={version.versionId}
73-
isSelected={version.versionId === selectedVersion}
74-
onClick={() => selectVersion(version.versionId)}
75-
>
76-
{formatTimestamp(version.timestamp)}
77-
{index === 0 && (
78-
<span style={{ fontWeight: 'normal' }}> (Siste versjon)</span>
79-
)}
80-
</VersionButton>
81-
))}
82-
</div>
83-
</SlidePanel>
84-
);
85-
onMount(component);
86-
}
87-
}, [versions, isOpen, searchQuery, selectedVersion]);
88-
89-
// Add a useEffect to update the filtered versions when the input versions change
90-
useEffect(() => {
91-
// Reset search query when versions change completely (different content)
9256
if (
9357
versions.length > 0 &&
9458
filteredVersions.length > 0 &&
@@ -98,8 +62,7 @@ export const VersionSelector = ({ versions, isOpen, onClose, onMount }: Props) =
9862
}
9963
}, [versions]);
10064

101-
// Return the same component
102-
return (
65+
const component = (
10366
<SlidePanel isOpen={isOpen} onClose={handleClose}>
10467
<Heading size="medium" spacing>
10568
Versjoner
@@ -127,4 +90,13 @@ export const VersionSelector = ({ versions, isOpen, onClose, onMount }: Props) =
12790
</div>
12891
</SlidePanel>
12992
);
93+
94+
// Call onMount with the component
95+
useEffect(() => {
96+
if (onMount) {
97+
onMount(component);
98+
}
99+
}, [versions, isOpen, searchQuery, selectedVersion]);
100+
101+
return component;
130102
};

0 commit comments

Comments
 (0)