import { useCallback } from 'react' ; import { useSelector , useDispatch } from 'react-redux' ; import { toggleListItemExpand } from '../actions/listActions' ; import { Identifier , ReduxState } from '../types' ; param { string } resource param { string | integer } id returns { Object } example const useExpanded = ( resource , id ) : [ boolean , ( ) => void ] => { const dispatch = useDispatch ( ) ; const expandedList = useSelector < ReduxState , Identifier [ ] > ( ( reduxState ) => reduxState . admin . resources [ resource ] ? reduxState . admin . resources [ resource ] . list . expanded : undefined ) ; const expanded = expandedList === undefined ? false : expandedList . map ( el => el == id ) . indexOf ( true ) !== - 1 ; const toggleExpanded = useCallback ( ( ) => { dispatch ( toggleListItemExpand ( resource , id ) ) ; } , [ dispatch , resource , id ] ) ; return [ expanded , toggleExpanded ] ; } ; export default useExpanded ;