2026-11-17 19:09:46 +00:00
const GRANT_SELECT = "select" ;
2026-11-18 19:28:37 +00:00
const GRANT_MODES = {
direct : [ "Direct" , "bi-lightning-charge" , "Direct mode" , "Direct: every grant button grants at once" ],
2026-11-22 15:55:43 +00:00
[ GRANT_SELECT ] : [ "Select" , "bi-check2-square" , "Select mode" , "Select: every grant button and option adds its item to the selection; Grant grants the selection under one passkey signature, each rule and red-gate review under its own" ],
2026-11-18 19:28:37 +00:00
};
const PICKED_HINT = "In the selection: take it out with ✕ in the Grant menu" ;
2026-11-22 15:55:43 +00:00
const SOLO_HINT = "Select: press to add it to the selection; Grant asks its own passkey signature for it" ;
2026-11-18 19:28:37 +00:00
const grantView = { tray : null , sig : "" , open : false };
2026-11-22 15:55:43 +00:00
const choices = new Map ();
2026-11-17 19:09:46 +00:00
const grantMode = () => ( typeof ViewStore === "object" && ViewStore ? ViewStore . get ( "approval.mode" ) : "direct" );
2026-11-22 15:55:43 +00:00
const chosenLabel = ( item ) => ( ui . selected . has ( keyOf ( item )) ? choices . get ( keyOf ( item )) : undefined );
const solo = ( item ) => ! item . held && ( item . kind === "rule" || ( item . kind === "review" && item . gate !== "pass" ));
const batchable = ( item ) => selectable ( item ) || ( ! item . held && chosenLabel ( item ) !== undefined );
const collectable = ( item ) => batchable ( item ) || solo ( item );
const collecting = ( item ) => grantMode () === GRANT_SELECT && collectable ( item );
const picking = ( item ) => grantMode () === GRANT_SELECT && ! item . held ;
/**
* @param {Object} item a batchable inbox item.
* @returns {string} the label its grant carries: the picked option, else a decision's recommendation, else "".
*/
2026-11-22 23:28:53 +00:00
function grantLabel ( item ) {
2026-11-22 15:55:43 +00:00
const label = chosenLabel ( item );
if ( label !== undefined ) {
return label ;
}
return item . kind === "decision" ? item . recommended : "" ;
}
2026-11-17 19:09:46 +00:00
function regrant () {
render ( false );
drawStream ();
}
2026-11-22 15:55:43 +00:00
function flip ( key ) {
choices . delete ( key );
2026-11-17 19:09:46 +00:00
if ( ui . selected . has ( key )) {
ui . selected . delete ( key );
} else {
ui . selected . add ( key );
}
2026-11-22 15:55:43 +00:00
}
function collect ( item ) {
flip ( keyOf ( item ));
regrant ();
}
/**
* @param {Object} item a decision or a decision row.
* @param {string} label the picked answer; picking the same answer again takes the item out of the selection.
*/
function collectPick ( item , label ) {
const key = keyOf ( item );
if ( chosenLabel ( item ) === label ) {
collect ( item );
return ;
}
choices . set ( key , label );
ui . selected . add ( key );
2026-11-17 19:09:46 +00:00
regrant ();
}
/**
* @param button - the grant button of item.
* @param item - a waiting inbox item.
2026-11-22 15:55:43 +00:00
* @returns button; a picked item's button turns pale green and disabled, and in Select a rule or red-gate review says it gets its own signature.
2026-11-17 19:09:46 +00:00
*/
function grantPress ( button , item ) {
2026-11-22 15:55:43 +00:00
if ( ! collectable ( item )) {
2026-11-17 19:09:46 +00:00
return button ;
}
const picked = ui . selected . has ( keyOf ( item ));
2026-11-18 19:28:37 +00:00
if ( picked ) {
button . disabled = true ;
button . setAttribute ( "class" , ` ${ button . getAttribute ( "class" ) } picked` );
button . setAttribute ( "title" , PICKED_HINT );
}
2026-11-22 15:55:43 +00:00
if ( grantMode () === GRANT_SELECT ) {
2026-11-18 19:28:37 +00:00
button . setAttribute ( "aria-pressed" , String ( picked ));
if ( ! picked ) {
2026-11-22 15:55:43 +00:00
button . setAttribute ( "title" , solo ( item ) ? SOLO_HINT : "Select: press to add it to the selection; Grant grants it" );
2026-11-18 19:28:37 +00:00
}
}
2026-11-17 19:09:46 +00:00
return button ;
}
2026-11-18 19:28:37 +00:00
function modeItem ( mode ) {
const [, name , text , hint ] = GRANT_MODES [ mode ];
return h ( "button" , { type : "button" , id : `grant- ${ mode } ` , class : "menu-item" , "data-mode" : mode , ... named ( GRANT_MODES [ mode ][ 0 ], hint ), on : { click : () => {
grantView . open = false ;
ViewStore . set ( "approval.mode" , mode );
}}}, icon ( name ), text );
}
function pickedItem ( item ) {
return h ( "li" , {}, h ( "button" , { type : "button" , class : "menu-item" , ... named ( `Remove ${ item . title } ` , "Remove: take it out of the selection" ), on : { click : () => collect ( item )}},
icon ( "bi-x-lg" ), h ( "span" , { class : "grant-picked-title" }, revealed ( item . title ))));
}
/**
* @returns {Node} the one selection control: Select, or Grant N once something is picked or Select mode is on, with a menu of the rest.
*/
function grantSplit ( mode , actions ) {
const selecting = mode === GRANT_SELECT || actions . chosen . length > 0 ;
const [ label , name , , hint ] = GRANT_MODES [ GRANT_SELECT ];
const main = selecting ? actions . grant : h ( "button" , { type : "button" , id : "grant-select" , class : "btn small" , "data-mode" : GRANT_SELECT , ... named ( label , hint ), on : { click : () => ViewStore . set ( "approval.mode" , GRANT_SELECT )}},
icon ( name ), h ( "span" , { class : "grant-label" }, label ));
const clear = h ( "button" , { type : "button" , class : "menu-item" , ... named ( "Clear" , "Clear: empty the selection" ), disabled : ! actions . chosen . length , on : { click : () => {
ui . selected . clear ();
regrant ();
}}}, icon ( "bi-x-lg" ), "Clear selection" );
const menu = h ( "details" , { class : "menu grant-menu" , open : grantView . open , on : { toggle : ( event ) => {
grantView . open = event . currentTarget . open ;
}}}, h ( "summary" , { class : "grant-count" , ... named ( ` ${ actions . chosen . length } selected` , "More: deny, grant all green, the picked items, clear, mode" )}, caret ()),
h ( "div" , { class : "menu-box end grant-menu-box" }, actions . deny , actions . green ,
actions . chosen . length ? h ( "ul" , { class : "grant-picked" , "aria-label" : "Picked" }, actions . chosen . map ( pickedItem )) : null ,
clear , selecting ? modeItem ( mode === GRANT_SELECT ? "direct" : GRANT_SELECT ) : null ));
return h ( "div" , { class : "grant-split" , role : "group" , "aria-label" : "Selection" }, main , menu );
}
2026-11-17 19:09:46 +00:00
function drawGrant () {
if ( ! grantView . tray ) {
return ;
}
const mode = grantMode ();
2026-11-18 19:28:37 +00:00
const actions = selectionActions ( state . inbox );
const sig = JSON . stringify ([ mode , actions . chosen . map ( keyOf ), state . inbox . map (( item ) => [ keyOf ( item ), isGreen ( item ), item . held ]), ui . busy ]);
2026-11-17 19:09:46 +00:00
if ( sig === grantView . sig ) {
return ;
}
grantView . sig = sig ;
2026-11-18 19:28:37 +00:00
grantView . tray . replaceChildren ( grantSplit ( mode , actions ));
2026-11-17 19:09:46 +00:00
}
function grantBoot () {
const bar = document . getElementById ( "grant-bar" );
2026-11-18 19:28:37 +00:00
grantView . tray = h ( "div" , { class : "grant-tray" });
bar . replaceChildren ( grantView . tray );
2026-11-17 19:09:46 +00:00
ViewStore . on ( "approval" , regrant );
drawGrant ();
}