import { useMemo } from 'react' ; import { useSelector , useDispatch , shallowEqual } from 'react-redux' ; import { setListSelectedIds , toggleListItem } from '../actions/listActions' ; import { Identifier , ReduxState } from '../types' ; const defaultRecords = [ ] ; param resource returns { Object } const useRecordSelection = ( resource ) : [ Identifier [ ] , { select : ( newIds : Identifier [ ] ) => void ; toggle : ( id : Identifier ) => void ; clearSelection : ( ) => void ; } ] => { const dispatch = useDispatch ( ) ; const selectedIds = useSelector < ReduxState , Identifier [ ] > ( ( reduxState ) => reduxState . admin . resources [ resource ] ? reduxState . admin . resources [ resource ] . list . selectedIds : defaultRecords , shallowEqual ) ; const selectionModifiers = useMemo ( ( ) => ( { select : ( newIds ) => { dispatch ( setListSelectedIds ( resource , newIds ) ) ; } , toggle : ( id ) => { dispatch ( toggleListItem ( resource , id ) ) ; } , clearSelection : ( ) => { dispatch ( setListSelectedIds ( resource , [ ] ) ) ; } , } ) , [ dispatch , resource ] ) ; return [ selectedIds , selectionModifiers ] ; } ; export default useRecordSelection ;