import { Store } from 'shared/libs/stores' ; import { Document , DocumentSsh } from './types' ; interface State { items : Document [ ] ; } export default class StoreDocs extends Store < State > { state : State = { items : [ ] , } ; add ( doc ) { const item = { id : Math . floor ( Math . random ( ) * 100000 ) , ... doc , } ; this . setState ( { items : [ ... this . state . items , item ] , } ) ; return item ; } update ( id , partialDoc ) { const items = this . state . items . map ( doc => { if ( doc . id === id ) { return { ... doc , ... partialDoc , } ; } return doc ; } ) as Document [ ] ; this . setState ( { items , } ) ; } filter ( id ) { return this . state . items . filter ( i => i . id !== id ) ; } getNext ( id ) { const { items } = this . state ; for ( let i = 0 ; i < items . length ; i ++ ) { if ( items [ i ] . id === id ) { if ( items . length > i + 1 ) { return items [ i + 1 ] . id ; } if ( items . length === i + 1 && i !== 0 ) { return items [ i - 1 ] . id ; } } } return - 1 ; } find ( id ) { return this . state . items . find ( i => i . id === id ) ; } findByUrl ( url ) { return this . state . items . find ( i => i . url === encodeURI ( url ) ) ; } getNodeDocuments ( ) { return this . state . items . filter ( doc => doc . kind === 'nodes' ) ; } getSshDocuments ( ) { return this . state . items . filter ( doc => doc . kind === 'terminal' && doc . status === 'connected' ) as DocumentSsh [ ] ; } getDocuments ( ) { return this . state . items ; } }