@@ -94,14 +94,16 @@ const DocumentInfo: React.FC<
94
94
hasPublishPermissionFromProps ,
95
95
)
96
96
const isInitializing = initialState === undefined || data === undefined
97
- const hasInitializedDocPermissions = useRef ( false )
98
97
const [ unpublishedVersions , setUnpublishedVersions ] =
99
98
useState < PaginatedDocs < TypeWithVersion < any > > > ( null )
100
99
101
100
const { getPreference, setPreference } = usePreferences ( )
102
101
const { permissions } = useAuth ( )
103
102
const { code : locale } = useLocale ( )
104
103
const prevLocale = useRef ( locale )
104
+ const hasInitializedDocPermissions = useRef ( false )
105
+ // Separate locale cache used for handling permissions
106
+ const prevLocalePermissions = useRef ( locale )
105
107
106
108
const versionsConfig = docConfig ?. versions
107
109
@@ -262,11 +264,12 @@ const DocumentInfo: React.FC<
262
264
locale : locale || undefined ,
263
265
}
264
266
265
- const newIsEditing = getIsEditing ( { id : data ?. id , collectionSlug, globalSlug } )
267
+ const idToUse = data ?. id || id
268
+ const newIsEditing = getIsEditing ( { id : idToUse , collectionSlug, globalSlug } )
266
269
267
270
if ( newIsEditing ) {
268
271
const docAccessURL = collectionSlug
269
- ? `/${ collectionSlug } /access/${ data . id } `
272
+ ? `/${ collectionSlug } /access/${ idToUse } `
270
273
: globalSlug
271
274
? `/globals/${ globalSlug } /access`
272
275
: null
@@ -329,7 +332,7 @@ const DocumentInfo: React.FC<
329
332
)
330
333
}
331
334
} ,
332
- [ serverURL , api , permissions , i18n . language , locale , collectionSlug , globalSlug ] ,
335
+ [ serverURL , api , id , permissions , i18n . language , locale , collectionSlug , globalSlug ] ,
333
336
)
334
337
335
338
const getDocPreferences = useCallback ( ( ) => {
@@ -485,26 +488,24 @@ const DocumentInfo: React.FC<
485
488
} , [ collectionConfig , data , dateFormat , i18n , id , globalConfig ] )
486
489
487
490
useEffect ( ( ) => {
488
- const loadDocPermissions = async ( ) => {
489
- const docPermissions : DocumentPermissions = docPermissionsFromProps
490
- const hasSavePermission : boolean = hasSavePermissionFromProps
491
- const hasPublishPermission : boolean = hasPublishPermissionFromProps
492
-
493
- if (
494
- ! docPermissions ||
495
- hasSavePermission === undefined ||
496
- hasSavePermission === null ||
497
- hasPublishPermission === undefined ||
498
- hasPublishPermission === null
491
+ const localeChanged = locale !== prevLocalePermissions . current
492
+
493
+ if ( data && ( collectionSlug || globalSlug ) ) {
494
+ if ( localeChanged ) {
495
+ prevLocalePermissions . current = locale
496
+ void getDocPermissions ( data )
497
+ } else if (
498
+ hasInitializedDocPermissions . current === false &&
499
+ ( ! docPermissions ||
500
+ hasSavePermission === undefined ||
501
+ hasSavePermission === null ||
502
+ hasPublishPermission === undefined ||
503
+ hasPublishPermission === null )
499
504
) {
500
- await getDocPermissions ( data )
505
+ hasInitializedDocPermissions . current = true
506
+ void getDocPermissions ( data )
501
507
}
502
508
}
503
-
504
- if ( ! hasInitializedDocPermissions . current && data && ( collectionSlug || globalSlug ) ) {
505
- hasInitializedDocPermissions . current = true
506
- void loadDocPermissions ( )
507
- }
508
509
} , [
509
510
getDocPermissions ,
510
511
docPermissionsFromProps ,
@@ -514,6 +515,10 @@ const DocumentInfo: React.FC<
514
515
collectionSlug ,
515
516
globalSlug ,
516
517
data ,
518
+ locale ,
519
+ docPermissions ,
520
+ hasSavePermission ,
521
+ hasPublishPermission ,
517
522
] )
518
523
519
524
const action : string = React . useMemo ( ( ) => {
0 commit comments