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