@@ -17,7 +17,13 @@ import {
17
17
useEditDepth ,
18
18
useUploadEdits ,
19
19
} from '@payloadcms/ui'
20
- import { formatAdminURL , getFormState } from '@payloadcms/ui/shared'
20
+ import {
21
+ formatAdminURL ,
22
+ getFormState ,
23
+ handleBackToDashboard ,
24
+ handleGoBack ,
25
+ handleTakeOver ,
26
+ } from '@payloadcms/ui/shared'
21
27
import { useRouter , useSearchParams } from 'next/navigation.js'
22
28
import React , { Fragment , useCallback , useEffect , useRef , useState } from 'react'
23
29
@@ -151,89 +157,6 @@ export const DefaultEditView: React.FC = () => {
151
157
return false
152
158
} )
153
159
154
- const handleTakeOver = useCallback ( ( ) => {
155
- if ( ! isLockingEnabled ) {
156
- return
157
- }
158
-
159
- try {
160
- // Call updateDocumentEditor to update the document's owner to the current user
161
- void updateDocumentEditor ( id , collectionSlug ?? globalSlug , user )
162
-
163
- documentLockStateRef . current . hasShownLockedModal = true
164
-
165
- // Update the locked state to reflect the current user as the owner
166
- documentLockStateRef . current = {
167
- hasShownLockedModal : documentLockStateRef . current ?. hasShownLockedModal ,
168
- isLocked : true ,
169
- user,
170
- }
171
- setCurrentEditor ( user )
172
- } catch ( error ) {
173
- // eslint-disable-next-line no-console
174
- console . error ( 'Error during document takeover:' , error )
175
- }
176
- } , [
177
- updateDocumentEditor ,
178
- id ,
179
- collectionSlug ,
180
- globalSlug ,
181
- user ,
182
- setCurrentEditor ,
183
- isLockingEnabled ,
184
- ] )
185
-
186
- const handleTakeOverWithinDoc = useCallback ( ( ) => {
187
- if ( ! isLockingEnabled ) {
188
- return
189
- }
190
-
191
- try {
192
- // Call updateDocumentEditor to update the document's owner to the current user
193
- void updateDocumentEditor ( id , collectionSlug ?? globalSlug , user )
194
-
195
- // Update the locked state to reflect the current user as the owner
196
- documentLockStateRef . current = {
197
- hasShownLockedModal : documentLockStateRef . current ?. hasShownLockedModal ,
198
- isLocked : true ,
199
- user,
200
- }
201
- setCurrentEditor ( user )
202
-
203
- // Ensure the document is editable for the incoming user
204
- setIsReadOnlyForIncomingUser ( false )
205
- } catch ( error ) {
206
- // eslint-disable-next-line no-console
207
- console . error ( 'Error during document takeover:' , error )
208
- }
209
- } , [
210
- updateDocumentEditor ,
211
- id ,
212
- collectionSlug ,
213
- globalSlug ,
214
- user ,
215
- setCurrentEditor ,
216
- isLockingEnabled ,
217
- ] )
218
-
219
- const handleGoBack = useCallback ( ( ) => {
220
- const redirectRoute = formatAdminURL ( {
221
- adminRoute,
222
- path : collectionSlug ? `/collections/${ collectionSlug } ` : '/' ,
223
- } )
224
- router . push ( redirectRoute )
225
- } , [ adminRoute , collectionSlug , router ] )
226
-
227
- const handleBackToDashboard = useCallback ( ( ) => {
228
- setShowTakeOverModal ( false )
229
- const redirectRoute = formatAdminURL ( {
230
- adminRoute,
231
- path : '/' ,
232
- } )
233
-
234
- router . push ( redirectRoute )
235
- } , [ adminRoute , router ] )
236
-
237
160
const onSave = useCallback (
238
161
( json ) => {
239
162
reportUpdate ( {
@@ -373,7 +296,19 @@ export const DefaultEditView: React.FC = () => {
373
296
return
374
297
}
375
298
376
- if ( ( id || globalSlug ) && documentIsLocked ) {
299
+ const currentPath = window . location . pathname
300
+
301
+ const documentId = id || globalSlug
302
+
303
+ // Routes where we do NOT want to unlock the document
304
+ const stayWithinDocumentPaths = [ 'preview' , 'api' , 'versions' ]
305
+
306
+ const isStayingWithinDocument = stayWithinDocumentPaths . some ( ( path ) =>
307
+ currentPath . includes ( path ) ,
308
+ )
309
+
310
+ // Unlock the document only if we're actually navigating away from the document
311
+ if ( documentId && documentIsLocked && ! isStayingWithinDocument ) {
377
312
// Check if this user is still the current editor
378
313
if ( documentLockStateRef . current ?. user ?. id === user . id ) {
379
314
void unlockDocument ( id , collectionSlug ?? globalSlug )
@@ -421,20 +356,32 @@ export const DefaultEditView: React.FC = () => {
421
356
{ BeforeDocument }
422
357
{ isLockingEnabled && shouldShowDocumentLockedModal && ! isReadOnlyForIncomingUser && (
423
358
< DocumentLocked
424
- handleGoBack = { handleGoBack }
359
+ handleGoBack = { ( ) => handleGoBack ( { adminRoute , collectionSlug , router } ) }
425
360
isActive = { shouldShowDocumentLockedModal }
426
361
onReadOnly = { ( ) => {
427
362
setIsReadOnlyForIncomingUser ( true )
428
363
setShowTakeOverModal ( false )
429
364
} }
430
- onTakeOver = { handleTakeOver }
365
+ onTakeOver = { ( ) =>
366
+ handleTakeOver (
367
+ id ,
368
+ collectionSlug ,
369
+ globalSlug ,
370
+ user ,
371
+ false ,
372
+ updateDocumentEditor ,
373
+ setCurrentEditor ,
374
+ documentLockStateRef ,
375
+ isLockingEnabled ,
376
+ )
377
+ }
431
378
updatedAt = { lastUpdateTime }
432
379
user = { currentEditor }
433
380
/>
434
381
) }
435
382
{ isLockingEnabled && showTakeOverModal && (
436
383
< DocumentTakeOver
437
- handleBackToDashboard = { handleBackToDashboard }
384
+ handleBackToDashboard = { ( ) => handleBackToDashboard ( { adminRoute , router } ) }
438
385
isActive = { showTakeOverModal }
439
386
onReadOnly = { ( ) => {
440
387
setIsReadOnlyForIncomingUser ( true )
@@ -469,7 +416,20 @@ export const DefaultEditView: React.FC = () => {
469
416
onDrawerCreate = { onDrawerCreate }
470
417
onDuplicate = { onDuplicate }
471
418
onSave = { onSave }
472
- onTakeOver = { handleTakeOverWithinDoc }
419
+ onTakeOver = { ( ) =>
420
+ handleTakeOver (
421
+ id ,
422
+ collectionSlug ,
423
+ globalSlug ,
424
+ user ,
425
+ true ,
426
+ updateDocumentEditor ,
427
+ setCurrentEditor ,
428
+ documentLockStateRef ,
429
+ isLockingEnabled ,
430
+ setIsReadOnlyForIncomingUser ,
431
+ )
432
+ }
473
433
permissions = { docPermissions }
474
434
readOnlyForIncomingUser = { isReadOnlyForIncomingUser }
475
435
redirectAfterDelete = { redirectAfterDelete }
@@ -494,6 +454,7 @@ export const DefaultEditView: React.FC = () => {
494
454
requirePassword = { ! id }
495
455
setSchemaPath = { setSchemaPath }
496
456
setValidateBeforeSubmit = { setValidateBeforeSubmit }
457
+ // eslint-disable-next-line react-compiler/react-compiler
497
458
useAPIKey = { auth . useAPIKey }
498
459
username = { data ?. username }
499
460
verify = { auth . verify }
0 commit comments