import { useState , useEffect , useRef } from 'react' ; import { QuickReplyItemProps } from '../components/QuickReplies' ; type QuickReplies = QuickReplyItemProps [ ] ; export default function useQuickReplies ( initialState = [ ] ) { const [ quickReplies , setQuickReplies ] = useState ( initialState ) ; const [ visible , setVisible ] = useState ( true ) ; const savedRef = useRef < QuickReplies > ( ) ; const stashRef = useRef < QuickReplies > ( ) ; useEffect ( ( ) => { savedRef . current = quickReplies ; } , [ quickReplies ] ) ; const prepend = ( list ) => { setQuickReplies ( ( prev ) => [ ... list , ... prev ] ) ; } ; const save = ( ) => { stashRef . current = savedRef . current ; } ; const pop = ( ) => { if ( stashRef . current ) { setQuickReplies ( stashRef . current ) ; } } ; return { quickReplies , prepend , replace : setQuickReplies , visible , setVisible , save , pop , } ; }