@@ -55,77 +55,88 @@ export const Content = () => {
55
55
}
56
56
} , [ data , selectedContentId , selectedLocale , selectedVersion ] ) ;
57
57
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 > (
64
61
getDefaultView ( isWebpage , hasAttachment )
65
62
) ;
66
63
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
+ } ) ;
78
72
79
- // Display data
73
+ // Add this new state to cache display values
74
+ const [ cachedDisplayData , setCachedDisplayData ] = useState ( {
80
75
displayName : '' ,
81
76
path : '' ,
82
- } ) ) ;
77
+ } ) ;
83
78
84
- // Update cache when content changes
79
+ // Update this useEffect to also cache display data when data loads
85
80
useEffect ( ( ) => {
86
81
if ( prevContentIdRef . current && prevContentIdRef . current !== selectedContentId ) {
87
82
clearCachedVersionSelector ( prevContentIdRef . current ) ;
88
83
}
89
84
90
85
if ( data ?. versions && selectedContentId ) {
91
- setContentCache ( ( prev ) => ( {
92
- ... prev ,
86
+ setVersionSelectorCache ( ( prev ) => ( {
87
+ component : null ,
93
88
versions : data . versions ,
94
- versionComponent : null ,
95
- displayName : data . json ?. displayName || prev . displayName ,
96
- path : data . json ?. _path || prev . path ,
89
+ isOpen : prev . isOpen ,
97
90
} ) ) ;
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
+ }
98
99
}
99
100
100
101
prevContentIdRef . current = selectedContentId ;
101
102
} , [ selectedContentId , data ?. versions , data ?. json ] ) ;
102
103
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
+
104
112
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 (
107
116
( v ) => v . versionId === selectedVersion
108
117
) ;
109
118
if ( cachedVersion ?. timestamp ) {
110
119
return formatTimestamp ( cachedVersion . timestamp ) ;
111
120
}
112
121
}
113
122
123
+ // Fall back to data if cache doesn't have it
114
124
if ( selectedVersion && data ?. versions ) {
115
125
return formatTimestamp (
116
126
data . versions . find ( ( v ) => v . versionId === selectedVersion ) ?. timestamp ?? ''
117
127
) ;
118
128
}
119
-
120
129
return 'Laster...' ;
121
130
} ;
122
131
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
+ } ;
125
136
126
- const htmlPath = ` ${ xpArchiveConfig . basePath } /html/ ${ selectedContentId } / ${ selectedLocale } / $ {
127
- data ?. json . _versionKey
128
- } ` ;
137
+ const getPath = ( ) => {
138
+ return data ?. json . _path || cachedDisplayData . path || '' ;
139
+ } ;
129
140
130
141
if ( ! selectedContentId ) {
131
142
return < EmptyState /> ;
@@ -143,45 +154,40 @@ export const Content = () => {
143
154
icon = { < SidebarRightIcon /> }
144
155
iconPosition = { 'right' }
145
156
onClick = { ( ) => {
146
- setContentCache ( ( prev ) => ( {
157
+ setVersionSelectorCache ( ( prev ) => ( {
147
158
...prev ,
148
- isVersionPanelOpen : true ,
159
+ isOpen : true ,
149
160
} ) ) ;
150
161
} }
151
162
>
152
163
{ getVersionDisplay ( ) }
153
164
</ Button >
154
165
155
- { contentCache . versionComponent ? (
156
- contentCache . versionComponent
166
+ { versionSelectorCache . component ? (
167
+ versionSelectorCache . component
157
168
) : (
158
169
< VersionSelector
159
170
versions = {
160
- contentCache . versions . length > 0
161
- ? contentCache . versions
171
+ versionSelectorCache . versions . length > 0
172
+ ? versionSelectorCache . versions
162
173
: data ?. versions || [ ]
163
174
}
164
- isOpen = { contentCache . isVersionPanelOpen }
175
+ isOpen = { versionSelectorCache . isOpen }
165
176
onClose = { ( ) => {
166
- setContentCache ( ( prev ) => ( {
177
+ setVersionSelectorCache ( ( prev ) => ( {
167
178
...prev ,
168
- isVersionPanelOpen : false ,
179
+ isOpen : false ,
169
180
} ) ) ;
170
181
} }
171
182
onMount = { ( component ) => {
172
183
setCachedVersionSelector (
173
184
selectedContentId ?? '' ,
174
185
component ,
175
- contentCache . versions . length > 0
176
- ? contentCache . versions
186
+ versionSelectorCache . versions . length > 0
187
+ ? versionSelectorCache . versions
177
188
: data ?. versions || [ ] ,
178
- contentCache . isVersionPanelOpen
189
+ versionSelectorCache . isOpen
179
190
) ;
180
-
181
- setContentCache ( ( prev ) => ( {
182
- ...prev ,
183
- versionComponent : component ,
184
- } ) ) ;
185
191
} }
186
192
/>
187
193
) }
0 commit comments