Skip to content

Commit 1083c4d

Browse files
committed
forsøk cache visning
1 parent e44edce commit 1083c4d

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

xp-archive/client/content/Content.tsx

+29-8
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ export const Content = () => {
7070
};
7171
});
7272

73-
// Add this new state to cache display values
73+
// Add view type to cached display data
7474
const [cachedDisplayData, setCachedDisplayData] = useState({
7575
displayName: '',
7676
path: '',
77+
view: undefined as ViewVariant | undefined,
7778
});
7879

79-
// Update this useEffect to also cache display data when data loads
80+
// Update this useEffect to also cache display data and view when data loads
8081
useEffect(() => {
8182
if (prevContentIdRef.current && prevContentIdRef.current !== selectedContentId) {
8283
clearCachedVersionSelector(prevContentIdRef.current);
@@ -91,20 +92,40 @@ export const Content = () => {
9192

9293
// Cache display data when it's available
9394
if (data.json?.displayName || data.json?._path) {
94-
setCachedDisplayData({
95-
displayName: data.json.displayName || '',
96-
path: data.json._path || '',
97-
});
95+
setCachedDisplayData((prev) => ({
96+
displayName: data.json?.displayName || '',
97+
path: data.json?._path || '',
98+
view: prev.view, // Keep existing view
99+
}));
98100
}
99101
}
100102

101103
prevContentIdRef.current = selectedContentId;
102104
}, [selectedContentId, data?.versions, data?.json]);
103105

106+
// Update view selector effect to store the selected view in cache
104107
useEffect(() => {
105-
setSelectedView(getDefaultView(isWebpage, hasAttachment));
108+
const newView = getDefaultView(isWebpage, hasAttachment);
109+
setSelectedView(newView);
110+
111+
// Cache the view
112+
setCachedDisplayData((prev) => ({
113+
...prev,
114+
view: newView,
115+
}));
106116
}, [isWebpage, hasAttachment, selectedContentId]);
107117

118+
// Add handler to update cached view
119+
const handleViewChange = (view: ViewVariant) => {
120+
setSelectedView(view);
121+
122+
// Update cached view
123+
setCachedDisplayData((prev) => ({
124+
...prev,
125+
view,
126+
}));
127+
};
128+
108129
const htmlPath = `${xpArchiveConfig.basePath}/html/${selectedContentId}/${selectedLocale}/${
109130
data?.json._versionKey
110131
}`;
@@ -197,7 +218,7 @@ export const Content = () => {
197218
<div className={style.viewSelectorWrapper}>
198219
<ViewSelector
199220
selectedView={selectedView}
200-
setSelectedView={setSelectedView}
221+
setSelectedView={handleViewChange}
201222
hasAttachment={hasAttachment}
202223
isWebpage={isWebpage}
203224
/>

0 commit comments

Comments
 (0)