3232
3333import React , { useCallback , useMemo , useState } from 'react' ;
3434import {
35+ Badge ,
3536 Button ,
37+ DropdownMenu ,
38+ DropdownMenuContent ,
39+ DropdownMenuItem ,
40+ DropdownMenuTrigger ,
3641 Separator ,
3742 cn ,
3843 hasDeclaredVisibilityGate ,
@@ -54,7 +59,7 @@ import {
5459import type { ActionDef } from '@object-ui/core' ;
5560import type { ConsoleActionDispatch } from '../consoleActionDispatch.js' ;
5661import { useObjectTranslation } from '@object-ui/i18n' ;
57- import { Loader2 , ShieldAlert } from 'lucide-react' ;
62+ import { Loader2 , MoreHorizontal , ShieldAlert } from 'lucide-react' ;
5863import { useConsoleActionRuntime } from '../hooks/useConsoleActionRuntime.js' ;
5964import { useAdapter } from '../providers/AdapterProvider.js' ;
6065// Straight from `@object-ui/react`, NOT through `../providers/MetadataProvider`
@@ -74,7 +79,12 @@ import {
7479 readDecisionOutputRegistry ,
7580} from '../utils/decisionOutputParams.js' ;
7681import { getIcon } from '../utils/getIcon.js' ;
77- import { isOverrideDecision , bypassedApproverNames } from '../utils/approvalOverride.js' ;
82+ import {
83+ actionAdmittedByOverride ,
84+ bypassedApproverNames ,
85+ isOverrideDecision ,
86+ isOverrideOnlyViewer ,
87+ } from '../utils/approvalOverride.js' ;
7888import {
7989 approverCopyFrom ,
8090 approverDisplay ,
@@ -117,6 +127,26 @@ export interface DeclaredActionsBarProps {
117127 * returns null when empty), so the host never gets an orphan divider.
118128 */
119129 label ?: string ;
130+ /**
131+ * Fold the admin-override levers into one menu for a viewer who holds no
132+ * approver slot but may override (objectstack#25877).
133+ *
134+ * For that viewer every lever the override flag admits — approve, reject,
135+ * reassign, and recall, whose gate ORs the flag in too — is the exception
136+ * path, and a row of them drawn as buttons is the most prominent thing on
137+ * the page. With this set, such a viewer sees one status line instead
138+ * (pending, and who the step waits on), the levers the override flag does
139+ * NOT admit (reply) as buttons, and the folded ones behind a single marked
140+ * menu. The set is read off each action's own `visible` gate
141+ * (`actionAdmittedByOverride`), not off a name list, so a lever shipped as
142+ * metadata alone folds the same way.
143+ *
144+ * Placement only: every folded lever keeps its own `visible` / `disabled` /
145+ * capability gates, its override framing and its dialog, and the request it
146+ * sends is unchanged. A viewer who CAN act (the approver) and any viewer the
147+ * server sent no `viewer` block for render exactly as without it.
148+ */
149+ foldOverrides ?: boolean ;
120150}
121151
122152/**
@@ -148,7 +178,13 @@ const DeclaredActionButton: React.FC<{
148178 action : ActionDef ;
149179 objectName : string ;
150180 record : any ;
151- } > = ( { action, objectName, record } ) => {
181+ /**
182+ * Draw the lever as an item of the override menu instead of a button
183+ * (objectstack#25877). Every gate, label and dispatch above the render is
184+ * shared, so the two forms cannot disagree about what the lever does.
185+ */
186+ asMenuItem ?: boolean ;
187+ } > = ( { action, objectName, record, asMenuItem = false } ) => {
152188 const { execute } = useAction ( ) ;
153189 const [ loading , setLoading ] = useState ( false ) ;
154190 // Localize the SERVER-DECLARED strings through the `_actions.<name>.*`
@@ -227,6 +263,7 @@ const DeclaredActionButton: React.FC<{
227263 const isDisabledPred = useCondition ( toPredicateInput ( action . disabled ) , predicateRecord ) ;
228264 // Called up here with the other hooks: the `visible` gate below returns early.
229265 const reasonId = React . useId ( ) ;
266+ const labelId = React . useId ( ) ;
230267
231268 /**
232269 * Is the button this viewer is looking at an ADMIN OVERRIDE (objectui#5178)?
@@ -513,6 +550,48 @@ const DeclaredActionButton: React.FC<{
513550 ? String ( t ( 'actions.notAvailableForRecord' , { defaultValue : 'Not available for this record' } ) )
514551 : undefined ;
515552
553+ if ( asMenuItem ) {
554+ // The menu form of the same lever (objectstack#25877). Shaped like the
555+ // page header's overflow items (`PageHeaderActions`): `preventDefault` on
556+ // select keeps the menu mounted while the action's own dialog takes over,
557+ // and a disabled item states its reason as a visible second line, because
558+ // a tooltip inside a menu is unreachable from the keyboard (a disabled
559+ // item is skipped by arrow-key focus).
560+ return (
561+ < DropdownMenuItem
562+ disabled = { disabledByPredicate || loading }
563+ aria-labelledby = { disabledReason ? labelId : undefined }
564+ aria-describedby = { disabledReason ? reasonId : undefined }
565+ onSelect = { ( event ) => {
566+ event . preventDefault ( ) ;
567+ void handleClick ( ) ;
568+ } }
569+ className = { cn (
570+ 'gap-2' ,
571+ isOverride && 'text-amber-700 focus:text-amber-800 dark:text-amber-400 dark:focus:text-amber-300' ,
572+ ) }
573+ data-testid = { `declared-action-${ action . name } ` }
574+ data-override-decision = { isOverride ? 'true' : undefined }
575+ >
576+ { loading
577+ ? < Loader2 className = "h-4 w-4 animate-spin" />
578+ : isOverride
579+ ? < ShieldAlert className = "h-4 w-4" />
580+ : iconName
581+ ? React . createElement ( getIcon ( iconName ) , { className : 'h-4 w-4' } )
582+ : null }
583+ { disabledReason ? (
584+ < span className = "flex min-w-0 flex-col" >
585+ < span id = { labelId } > { label } </ span >
586+ < span id = { reasonId } className = "text-xs text-muted-foreground" > { disabledReason } </ span >
587+ </ span >
588+ ) : (
589+ < span > { label } </ span >
590+ ) }
591+ </ DropdownMenuItem >
592+ ) ;
593+ }
594+
516595 const button = (
517596 < Button
518597 type = "button"
@@ -578,6 +657,51 @@ const DeclaredActionButton: React.FC<{
578657 ) ;
579658} ;
580659
660+ /**
661+ * The status line that replaces the override levers for a viewer who holds no
662+ * approver slot (objectstack#25877): the request is pending, and who the step
663+ * waits on, by name.
664+ *
665+ * The names are the same slate, resolved the same way, as the override notice
666+ * each folded lever shows (`bypassedApproverNames` over the shared approver
667+ * directory), so the line and the dialog never name different people. An
668+ * unstaffed step has no one to name, and then the line says pending alone.
669+ */
670+ const ApprovalWaitingStatus : React . FC < { record : any } > = ( { record } ) => {
671+ const { t } = useObjectTranslation ( ) ;
672+ const refs = useMemo ( ( ) => unresolvedApproverRefs ( record ) , [ record ] ) ;
673+ const directory = useApproverDirectory ( refs ) ;
674+ const names = useMemo ( ( ) => {
675+ const copy = approverCopyFrom ( t as unknown as Parameters < typeof approverCopyFrom > [ 0 ] ) ;
676+ const shown = bypassedApproverNames ( record , ( id ) => {
677+ const display = approverDisplay ( id , { resolved : directory [ id ] , copy } ) ;
678+ return display . detail ? `${ display . label } · ${ display . detail } ` : display . label ;
679+ } ) ;
680+ return Array . from ( new Set ( shown ) ) ;
681+ } , [ record , directory , t ] ) ;
682+ return (
683+ < div
684+ className = "flex min-w-0 flex-wrap items-center gap-x-2 gap-y-1 text-sm"
685+ data-testid = "declared-actions-approval-status"
686+ >
687+ < Badge
688+ variant = "outline"
689+ className = "border-amber-300 bg-amber-50 text-amber-800 dark:border-amber-500/40 dark:bg-amber-500/10 dark:text-amber-300"
690+ >
691+ { String ( t ( 'approvalsInbox.statusPending' , { defaultValue : 'Pending' } ) ) }
692+ </ Badge >
693+ { names . length > 0 && (
694+ < span className = "min-w-0 text-muted-foreground" >
695+ { String ( t ( 'approvalsInbox.waitingOn' , { defaultValue : 'Waiting on' } ) ) } { ' ' }
696+ < span className = "text-foreground" >
697+ { names . join ( String ( t ( 'approvalsInbox.approverNameSeparator' , { defaultValue : ', ' } ) ) ) }
698+ </ span >
699+ </ span >
700+ ) }
701+ </ div >
702+ ) ;
703+ } ;
704+
581705/**
582706 * The located set, capability-gated — and the toolbar it draws.
583707 *
@@ -629,22 +753,95 @@ const DeclaredActionButton: React.FC<{
629753 * declared capability, and gets no new exposure either.
630754 */
631755const DeclaredActionsToolbar : React . FC <
632- Pick < DeclaredActionsBarProps , 'objectName' | 'record' | 'className' | 'label' > & {
756+ Pick < DeclaredActionsBarProps , 'objectName' | 'record' | 'className' | 'label' | 'foldOverrides' > & {
633757 /** The located set, pre-filtered by `location` / `exclude`, ungated. */
634758 actions : ActionDef [ ] ;
635759 }
636- > = ( { actions, objectName, record, className, label } ) => {
760+ > = ( { actions, objectName, record, className, label, foldOverrides } ) => {
637761 const { t } = useObjectTranslation ( ) ;
638762 const mayInvoke = useCapabilityGate ( ) ;
639763 const permitted = useMemo (
640764 ( ) => actions . filter ( ( action ) => mayInvoke ( action . requiredPermissions ) ) ,
641765 [ actions , mayInvoke ] ,
642766 ) ;
767+ // objectstack#25877 — which of the permitted levers fold into the override
768+ // menu. Only for a viewer admitted through the override door alone
769+ // (`can_act: false`, `can_override: true`), and only the levers whose own
770+ // gate ORs that flag in; everything else stays a button. The split runs on
771+ // the capability-gated set, so a lever the gate removed is in neither half.
772+ const folded = useMemo (
773+ ( ) => ( foldOverrides && isOverrideOnlyViewer ( record )
774+ ? permitted . filter ( ( action ) => actionAdmittedByOverride ( action ) )
775+ : [ ] ) ,
776+ [ foldOverrides , record , permitted ] ,
777+ ) ;
643778
644779 // Nothing this viewer may invoke renders nothing — no toolbar chrome, no
645780 // orphan divider, same degrade as "nothing declared at this location".
646781 if ( permitted . length === 0 ) return null ;
647782
783+ if ( folded . length > 0 ) {
784+ const inline = permitted . filter ( ( action ) => ! folded . includes ( action ) ) ;
785+ const overrideMenuLabel = String ( t ( 'approvalsInbox.viaOverrideChip' , { defaultValue : 'Admin override' } ) ) ;
786+ return (
787+ < div className = { cn ( 'space-y-2' , className ) } data-approval-override-folded = "" >
788+ { label && (
789+ < >
790+ < Separator />
791+ < div className = "text-xs font-medium text-muted-foreground" > { label } </ div >
792+ </ >
793+ ) }
794+ < div className = "flex flex-row flex-wrap items-center gap-2" >
795+ < ApprovalWaitingStatus record = { record } />
796+ < div
797+ role = "toolbar"
798+ aria-label = { label || t ( 'common.actions' ) }
799+ className = "ml-auto flex flex-row flex-wrap items-center gap-2"
800+ >
801+ { inline . map ( ( action ) => (
802+ < DeclaredActionButton
803+ key = { action . name }
804+ action = { action }
805+ objectName = { objectName }
806+ record = { record }
807+ />
808+ ) ) }
809+ < DropdownMenu >
810+ < DropdownMenuTrigger asChild >
811+ { /* One marked control for the whole exception path: the
812+ override shield the folded levers wear, in the same amber,
813+ beside the overflow glyph. */ }
814+ < Button
815+ type = "button"
816+ variant = "outline"
817+ size = "sm"
818+ className = "gap-1 px-2 border-amber-500/60 text-amber-700 hover:bg-amber-50 hover:text-amber-800 dark:text-amber-400 dark:hover:bg-amber-500/10 dark:hover:text-amber-300"
819+ aria-label = { overrideMenuLabel }
820+ title = { overrideMenuLabel }
821+ data-testid = "declared-actions-override-menu"
822+ >
823+ < ShieldAlert className = "h-4 w-4" />
824+ < MoreHorizontal className = "h-4 w-4" />
825+ </ Button >
826+ </ DropdownMenuTrigger >
827+ < DropdownMenuContent align = "end" className = "min-w-48" >
828+ { folded . map ( ( action ) => (
829+ < DeclaredActionButton
830+ key = { action . name }
831+ action = { action }
832+ objectName = { objectName }
833+ record = { record }
834+ asMenuItem
835+ />
836+ ) ) }
837+ </ DropdownMenuContent >
838+ </ DropdownMenu >
839+ </ div >
840+ </ div >
841+ </ div >
842+ ) ;
843+ }
844+
648845 return (
649846 < div className = { cn ( 'space-y-2' , className ) } >
650847 { label && (
@@ -676,6 +873,7 @@ export function DeclaredActionsBar({
676873 exclude,
677874 className,
678875 label,
876+ foldOverrides,
679877} : DeclaredActionsBarProps ) {
680878 const dataSource = useAdapter ( ) ;
681879 // Fetch the object def (and its declared actions) unless the host passed
@@ -730,6 +928,7 @@ export function DeclaredActionsBar({
730928 record = { record }
731929 className = { className }
732930 label = { label }
931+ foldOverrides = { foldOverrides }
733932 />
734933 { runtime . dialogs }
735934 </ ActionProvider >
0 commit comments