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" ],
[ GRANT_SELECT ] : [ "Select" , "bi-check2-square" , "Select mode" , "Select: every grant button adds its item to the selection; Grant grants the selection under one passkey signature" ],
};
const PICKED_HINT = "In the selection: take it out with ✕ in the Grant menu" ;
2026-11-17 19:09:46 +00:00
const DIRECT_WHY = { rule : "a rule is accepted one at a time" , review : "a red gate is never granted in a selection" };
2026-11-18 19:28:37 +00:00
const grantView = { tray : null , sig : "" , open : false };
2026-11-17 19:09:46 +00:00
const grantMode = () => ( typeof ViewStore === "object" && ViewStore ? ViewStore . get ( "approval.mode" ) : "direct" );
const collecting = ( item ) => grantMode () === GRANT_SELECT && selectable ( item );
function regrant () {
render ( false );
drawStream ();
}
function collect ( item ) {
const key = keyOf ( item );
if ( ui . selected . has ( key )) {
ui . selected . delete ( key );
} else {
ui . selected . add ( key );
}
regrant ();
}
/**
* @param button - the grant button of item.
* @param item - a waiting inbox item.
2026-11-18 19:28:37 +00:00
* @returns button; a picked item's button turns pale green and disabled, and in Select it says why a grant still acts at once.
2026-11-17 19:09:46 +00:00
*/
function grantPress ( button , item ) {
2026-11-18 19:28:37 +00:00
const select = grantMode () === GRANT_SELECT ;
2026-11-17 19:09:46 +00:00
if ( ! selectable ( item )) {
2026-11-18 19:28:37 +00:00
if ( select ) {
button . setAttribute ( "title" , ` ${ button . getAttribute ( "title" ) } : acts at once, ${ DIRECT_WHY [ item . kind ] } ` );
}
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 );
}
if ( select ) {
button . setAttribute ( "aria-pressed" , String ( picked ));
if ( ! picked ) {
button . setAttribute ( "title" , "Select: press to add it to the selection; Grant grants it" );
}
}
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 ();
}