import { RootState } from '../..' ; import { DriveItemData } from '../../../drive/types' ; import itemsListService from '../../../drive/services/items-list.service' ; import { sessionSelectors } from '../session/session.selectors' ; const rootFolderId = ( state ) : number => { const { team } = state . team ; const { user } = state . user ; const isTeam = sessionSelectors . isTeam ( state ) ; return ( isTeam ? team ?. root_folder_id : user ?. root_folder_id ) || 0 ; } ; const storageSelectors = { rootFolderId , currentFolderId ( state ) { const { namePath } = state . storage ; return namePath . length > 0 ? namePath [ namePath . length - 1 ] . id : rootFolderId ( state ) ; } , currentFolderPath ( state ) { return state . storage . namePath . reduce ( ( t , path ) => `${ t }${ path . name }/` , '/' ) ; } , bucket ( state ) { const { team } = state . team ; const isTeam = sessionSelectors . isTeam ( state ) ; return ( isTeam ? team ?. bucket : state . user . user ?. bucket ) || '' ; } , isCurrentFolderEmpty ( state ) { const currentFolderId = this . currentFolderId ( state ) ; return this . levelItems ( state ) ( currentFolderId ) . length === 0 ; } , isFolderInNamePath ( state ) { return ( folderId ) => state . storage . namePath . map ( ( p ) => p . id ) . includes ( folderId ) ; } , currentFolderItems ( state ) { const currentFolderId = this . currentFolderId ( state ) ; return this . levelItems ( state ) ( currentFolderId ) ; } , levelItems ( state ) { return ( folderId ) => state . storage . levels [ folderId ] || [ ] ; } , filteredItems ( state ) { return ( items ) => { const filteredItems = items . filter ( ( item ) => { const filters = state . storage . filters ; const fullName = item . isFolder ? item . name : item . name + `.${ item . type }` ; return fullName . toLowerCase ( ) . match ( filters . text . toLowerCase ( ) ) ; } ) ; itemsListService . sort ( filteredItems , state . storage . order . by as 'name' | 'type' | 'updatedAt' | 'size' , state . storage . order . direction ) ; return filteredItems ; } ; } , isItemSelected ( state ) { return ( item ) => state . storage . selectedItems . some ( ( i ) => item . id === i . id && item . isFolder === i . isFolder ) ; } , isSomeItemSelected : ( state ) : boolean => state . storage . selectedItems . length > 0 , } ; export default storageSelectors ;