import { action , computed , makeObservable , observable } from 'mobx' ; type Key = string | string [ ] ; export class TableState { selected : Map < string , boolean > ; expanded : Map < string , boolean > ; get itemsSelected ( ) : boolean { return Array . from ( this . selected . values ( ) ) . some ( v => v ) ; } get selectedList ( ) : string [ ] { return Array . from ( this . selected ) . filter ( ( [ _ , value ] ) => value ) . map ( ( [ key ] ) => key ) ; } constructor ( ) { this . selected = new Map ( ) ; this . expanded = new Map ( ) ; makeObservable ( this , { selected : observable , expanded : observable , itemsSelected : computed , selectedList : computed , unselect : action , unexpand : action , } ) ; } unselect ( key ? ) { if ( key === undefined ) { this . selected . clear ( ) ; } else { if ( typeof key === 'string' ) { this . selected . delete ( key ) ; } else { for ( const id of key ) { this . selected . delete ( id ) ; } } } return this . selected ; } unexpand ( key ? ) { if ( key === undefined ) { this . expanded . clear ( ) ; } else { if ( typeof key === 'string' ) { this . expanded . delete ( key ) ; } else { for ( const id of key ) { this . expanded . delete ( id ) ; } } } return this . expanded ; } }