2828 * approvals service still mounts the list route and answers it
2929 * `200 { data: [] }`: that is an answered read with no request, not an absent
3030 * route, and the result's `answered` says which one happened (objectui#12220).
31+ *
32+ * That list serves a request only to its participants (slate, submitter,
33+ * override actor), so for anyone else an answered empty read cannot tell "no
34+ * approval is running" from "one is running that you may not see". When the
35+ * list finds no pending request, the hook also asks the server whether the
36+ * record is held by a locking approval (`client.approvals.getRecordLock`,
37+ * answered to every caller who can read the record — objectstack#25934 ruling
38+ * A) and hands the answer back as `recordLock`.
3139 */
3240
3341import { useCallback , useEffect , useMemo , useRef , useState , useSyncExternalStore } from 'react' ;
42+ import { useAdapter } from '../providers/AdapterProvider.js' ;
3443import { bearerAuthHeaders } from '../utils/authToken.js' ;
3544import type { DecisionOutputDef } from '../utils/decisionOutputParams.js' ;
3645
@@ -278,6 +287,18 @@ interface UseRecordApprovalsResult {
278287 */
279288 pendingRequest : ApprovalRequestLite | null ;
280289 latestRequest : ApprovalRequestLite | null ;
290+ /**
291+ * The server's record lock (`GET /approvals/records/:object/:recordId/lock`,
292+ * objectstack#25934), read only when the list found no pending request:
293+ * `{ locked: true }` means a pending approval whose node locks the record is
294+ * running, though this viewer cannot see its request. `null` when there is
295+ * no answer to give: a pending request was found (it decides, with its own
296+ * `lock_record`), no adapter is mounted, or the read has never answered — a
297+ * composition without the approvals service answers it 501, so there the
298+ * record is not locked by this signal. A later failed read keeps the last
299+ * answer, as the list read does.
300+ */
301+ recordLock : { locked : boolean } | null ;
281302 refresh : ( ) => Promise < void > ;
282303}
283304
@@ -415,6 +436,8 @@ interface ApprovalsSnapshot {
415436 answered : boolean ;
416437 /** Rows as the server sent them, the pending one enriched. Unsorted. */
417438 rows : ApprovalRequestLite [ ] ;
439+ /** The server's record lock, when the rows hold no pending request. */
440+ recordLock : { locked : boolean } | null ;
418441}
419442
420443const EMPTY_ROWS : ApprovalRequestLite [ ] = [ ] ;
@@ -423,6 +446,7 @@ const INITIAL_SNAPSHOT: ApprovalsSnapshot = Object.freeze({
423446 available : true ,
424447 answered : false ,
425448 rows : EMPTY_ROWS ,
449+ recordLock : null ,
426450} ) as ApprovalsSnapshot ;
427451
428452interface ApprovalsEntry {
@@ -467,8 +491,16 @@ function holdEntry(key: string): () => void {
467491 } ;
468492}
469493
494+ /** Reads the server's record lock for the record a read is for. */
495+ type RecordLockReader = ( ) => Promise < { locked : boolean } > ;
496+
470497/** Send the record's read and publish its answer to every consumer of the key. */
471- function startRead ( key : string , objectName : string , recordId : string ) : Promise < void > {
498+ function startRead (
499+ key : string ,
500+ objectName : string ,
501+ recordId : string ,
502+ readLock : RecordLockReader | null ,
503+ ) : Promise < void > {
472504 const seq = ++ readSeq ;
473505 latestSeq . set ( key , seq ) ;
474506 const entry = entries . get ( key ) ;
@@ -484,11 +516,26 @@ function startRead(key: string, objectName: string, recordId: string): Promise<v
484516 // header — so exactly one follow-up read, never one per row.
485517 const pending = rows . find ( ( r ) => r . status === 'pending' ) ;
486518 const full = pending ? await fetchProgressEnrichment ( pending ) : null ;
519+ // A pending row the viewer can see decides the lock with its own node's
520+ // `lock_record`. Without one, ask the record itself (objectstack#25934):
521+ // the list hides a request from a non-participant, the lock does not.
522+ let recordLock : { locked : boolean } | null = null ;
523+ if ( ! pending && readLock ) {
524+ recordLock = entries . get ( key ) ?. snapshot . recordLock ?? null ;
525+ try {
526+ const lock = await readLock ( ) ;
527+ recordLock = { locked : lock ?. locked === true } ;
528+ } catch {
529+ // A read that never answered (501 without the approvals service)
530+ // leaves none; a failure after an answer keeps it, as the rows are.
531+ }
532+ }
487533 if ( latestSeq . get ( key ) !== seq ) return ;
488534 publish ( key , {
489535 rows : full ? rows . map ( ( r ) => ( r === pending ? full : r ) ) : rows ,
490536 available : true ,
491537 answered : true ,
538+ recordLock,
492539 } ) ;
493540 } catch ( err : any ) {
494541 if ( latestSeq . get ( key ) !== seq ) return ;
@@ -543,6 +590,18 @@ export function useRecordApprovals(
543590 // for the render, mirrored into a ref the mount effect and `refresh` read.
544591 const [ unavailable , setUnavailable ] = useState ( false ) ;
545592 const unavailableRef = useRef ( false ) ;
593+ // The record-lock read goes through the SDK (`client.approvals.getRecordLock`),
594+ // so it needs the adapter's client; with no adapter mounted it is skipped.
595+ const adapter = useAdapter ( ) ;
596+ const adapterRef = useRef ( adapter ) ;
597+ adapterRef . current = adapter ;
598+ const lockReader = useCallback (
599+ ( object : string , id : string ) : RecordLockReader | null =>
600+ adapterRef . current
601+ ? ( ) => adapterRef . current ! . getClient ( ) . approvals . getRecordLock ( object , id )
602+ : null ,
603+ [ ] ,
604+ ) ;
546605
547606 const subscribe = useCallback ( ( l : ( ) => void ) => subscribeKey ( readKey , l ) , [ readKey ] ) ;
548607 const getSnapshot = useCallback (
@@ -565,19 +624,19 @@ export function useRecordApprovals(
565624 // read only when neither exists. The entry may be new while a read is in
566625 // flight (StrictMode's replayed mount): that read publishes into it.
567626 if ( entry && ! entry . read && ! inFlight . has ( readKey ) && ! unavailableRef . current ) {
568- void startRead ( readKey , objectName , recordId ) ;
627+ void startRead ( readKey , objectName , recordId , lockReader ( objectName , recordId ) ) ;
569628 } else if ( entry && inFlight . has ( readKey ) ) {
570629 entry . read = true ;
571630 if ( ! entry . snapshot . loading ) publish ( readKey , { loading : true } ) ;
572631 }
573632 return release ;
574- } , [ readKey , objectName , recordId ] ) ;
633+ } , [ readKey , objectName , recordId , lockReader ] ) ;
575634
576635 const refresh = useCallback ( async ( ) => {
577636 if ( ! objectName || ! recordId || ! readKey ) return ;
578637 if ( unavailableRef . current ) return ;
579- await startRead ( readKey , objectName , recordId ) ;
580- } , [ objectName , recordId , readKey ] ) ;
638+ await startRead ( readKey , objectName , recordId , lockReader ( objectName , recordId ) ) ;
639+ } , [ objectName , recordId , readKey , lockReader ] ) ;
581640
582641 const requests = readKey === null ? EMPTY_ROWS : snapshot . rows ;
583642 const loading = snapshot . loading ;
@@ -613,6 +672,7 @@ export function useRecordApprovals(
613672 requests : sortedRequests ,
614673 pendingRequest,
615674 latestRequest,
675+ recordLock : readKey === null ? null : snapshot . recordLock ,
616676 refresh,
617677 } ;
618678}
0 commit comments