import { useCallback } from 'react' ; import { useKibana } from '../utils/kibana_react' ; export interface UseMessagesStorage { getMessages : ( plugin : string ) => string [ ] ; addMessage : ( plugin : string , id : string ) => void ; removeMessage : ( plugin : string , id : string ) => void ; clearAllMessages : ( plugin : string ) => void ; hasMessage : ( plugin : string , id : string ) => boolean ; } export const useMessagesStorage = ( ) : UseMessagesStorage => { const { storage } = useKibana ( ) . services ; const getMessages = useCallback ( ( plugin ) : string [ ] => storage . get ( `${ plugin }-messages` ) ?? [ ] , [ storage ] ) ; const addMessage = useCallback ( ( plugin , id ) => { const pluginStorage = storage . get ( `${ plugin }-messages` ) ?? [ ] ; storage . set ( `${ plugin }-messages` , [ ... pluginStorage , id ] ) ; } , [ storage ] ) ; const hasMessage = useCallback ( ( plugin , id ) : boolean => { const pluginStorage = storage . get ( `${ plugin }-messages` ) ?? [ ] ; return pluginStorage . includes ( ( val ) => val === id ) ; } , [ storage ] ) ; const removeMessage = useCallback ( ( plugin , id ) => { const pluginStorage = storage . get ( `${ plugin }-messages` ) ?? [ ] ; storage . set ( `${ plugin }-messages` , pluginStorage . filter ( ( val ) => val !== id ) ) ; } , [ storage ] ) ; const clearAllMessages = useCallback ( ( plugin ) : string [ ] => storage . remove ( `${ plugin }-messages` ) , [ storage ] ) ; return { getMessages , addMessage , clearAllMessages , removeMessage , hasMessage , } ; } ;