Skip to content

Commit 73f4d40

Browse files
committed
Revert "cache "Visning" og prøv å forenkle det hele"
This reverts commit 86eae8e.
1 parent 86eae8e commit 73f4d40

File tree

1 file changed

+57
-51
lines changed

1 file changed

+57
-51
lines changed

xp-archive/client/content/Content.tsx

+57-51
Original file line numberDiff line numberDiff line change
@@ -55,77 +55,88 @@ export const Content = () => {
5555
}
5656
}, [data, selectedContentId, selectedLocale, selectedVersion]);
5757

58-
// Calculate derived properties
59-
const isWebpage = !!data?.html && !data?.json?.attachment;
60-
const hasAttachment = !!data?.json?.attachment;
61-
62-
// State for view selector
63-
const [selectedView, setSelectedView] = useState<ViewVariant | undefined>(() =>
58+
const isWebpage = !!data?.html && !data.json.attachment;
59+
const hasAttachment = !!data?.json.attachment;
60+
const [selectedView, setSelectedView] = useState<ViewVariant | undefined>(
6461
getDefaultView(isWebpage, hasAttachment)
6562
);
6663

67-
// Update view when content type changes
68-
useEffect(() => {
69-
setSelectedView(getDefaultView(isWebpage, hasAttachment));
70-
}, [isWebpage, hasAttachment, selectedContentId]);
71-
72-
// Single cache for content-related data
73-
const [contentCache, setContentCache] = useState(() => ({
74-
// Version selector
75-
versions: getCachedVersionSelector(selectedContentId ?? '').versions,
76-
versionComponent: getCachedVersionSelector(selectedContentId ?? '').component,
77-
isVersionPanelOpen: getCachedVersionSelector(selectedContentId ?? '').isOpen,
64+
const [versionSelectorCache, setVersionSelectorCache] = useState(() => {
65+
const cache = getCachedVersionSelector(selectedContentId ?? '');
66+
return {
67+
component: cache.component,
68+
versions: cache.versions,
69+
isOpen: cache.isOpen,
70+
};
71+
});
7872

79-
// Display data
73+
// Add this new state to cache display values
74+
const [cachedDisplayData, setCachedDisplayData] = useState({
8075
displayName: '',
8176
path: '',
82-
}));
77+
});
8378

84-
// Update cache when content changes
79+
// Update this useEffect to also cache display data when data loads
8580
useEffect(() => {
8681
if (prevContentIdRef.current && prevContentIdRef.current !== selectedContentId) {
8782
clearCachedVersionSelector(prevContentIdRef.current);
8883
}
8984

9085
if (data?.versions && selectedContentId) {
91-
setContentCache((prev) => ({
92-
...prev,
86+
setVersionSelectorCache((prev) => ({
87+
component: null,
9388
versions: data.versions,
94-
versionComponent: null,
95-
displayName: data.json?.displayName || prev.displayName,
96-
path: data.json?._path || prev.path,
89+
isOpen: prev.isOpen,
9790
}));
91+
92+
// Cache display data when it's available
93+
if (data.json?.displayName || data.json?._path) {
94+
setCachedDisplayData({
95+
displayName: data.json.displayName || '',
96+
path: data.json._path || '',
97+
});
98+
}
9899
}
99100

100101
prevContentIdRef.current = selectedContentId;
101102
}, [selectedContentId, data?.versions, data?.json]);
102103

103-
// Helper functions to get data with fallbacks
104+
useEffect(() => {
105+
setSelectedView(getDefaultView(isWebpage, hasAttachment));
106+
}, [isWebpage, hasAttachment, selectedContentId]);
107+
108+
const htmlPath = `${xpArchiveConfig.basePath}/html/${selectedContentId}/${selectedLocale}/${
109+
data?.json._versionKey
110+
}`;
111+
104112
const getVersionDisplay = () => {
105-
if (selectedVersion && contentCache.versions.length > 0) {
106-
const cachedVersion = contentCache.versions.find(
113+
// First check if we have the version in our cache
114+
if (selectedVersion && versionSelectorCache.versions.length > 0) {
115+
const cachedVersion = versionSelectorCache.versions.find(
107116
(v) => v.versionId === selectedVersion
108117
);
109118
if (cachedVersion?.timestamp) {
110119
return formatTimestamp(cachedVersion.timestamp);
111120
}
112121
}
113122

123+
// Fall back to data if cache doesn't have it
114124
if (selectedVersion && data?.versions) {
115125
return formatTimestamp(
116126
data.versions.find((v) => v.versionId === selectedVersion)?.timestamp ?? ''
117127
);
118128
}
119-
120129
return 'Laster...';
121130
};
122131

123-
const getDisplayName = () => data?.json?.displayName || contentCache.displayName || 'Laster...';
124-
const getPath = () => data?.json?._path || contentCache.path || '';
132+
// Add helper functions to get title and URL with fallbacks
133+
const getDisplayName = () => {
134+
return data?.json.displayName || cachedDisplayData.displayName || 'Laster...';
135+
};
125136

126-
const htmlPath = `${xpArchiveConfig.basePath}/html/${selectedContentId}/${selectedLocale}/${
127-
data?.json._versionKey
128-
}`;
137+
const getPath = () => {
138+
return data?.json._path || cachedDisplayData.path || '';
139+
};
129140

130141
if (!selectedContentId) {
131142
return <EmptyState />;
@@ -143,45 +154,40 @@ export const Content = () => {
143154
icon={<SidebarRightIcon />}
144155
iconPosition={'right'}
145156
onClick={() => {
146-
setContentCache((prev) => ({
157+
setVersionSelectorCache((prev) => ({
147158
...prev,
148-
isVersionPanelOpen: true,
159+
isOpen: true,
149160
}));
150161
}}
151162
>
152163
{getVersionDisplay()}
153164
</Button>
154165

155-
{contentCache.versionComponent ? (
156-
contentCache.versionComponent
166+
{versionSelectorCache.component ? (
167+
versionSelectorCache.component
157168
) : (
158169
<VersionSelector
159170
versions={
160-
contentCache.versions.length > 0
161-
? contentCache.versions
171+
versionSelectorCache.versions.length > 0
172+
? versionSelectorCache.versions
162173
: data?.versions || []
163174
}
164-
isOpen={contentCache.isVersionPanelOpen}
175+
isOpen={versionSelectorCache.isOpen}
165176
onClose={() => {
166-
setContentCache((prev) => ({
177+
setVersionSelectorCache((prev) => ({
167178
...prev,
168-
isVersionPanelOpen: false,
179+
isOpen: false,
169180
}));
170181
}}
171182
onMount={(component) => {
172183
setCachedVersionSelector(
173184
selectedContentId ?? '',
174185
component,
175-
contentCache.versions.length > 0
176-
? contentCache.versions
186+
versionSelectorCache.versions.length > 0
187+
? versionSelectorCache.versions
177188
: data?.versions || [],
178-
contentCache.isVersionPanelOpen
189+
versionSelectorCache.isOpen
179190
);
180-
181-
setContentCache((prev) => ({
182-
...prev,
183-
versionComponent: component,
184-
}));
185191
}}
186192
/>
187193
)}

0 commit comments

Comments
 (0)