import { useCallback , useMemo } from 'react' ; import useObservable from 'react-use/lib/useObservable' ; import { useOpenSearchDashboards } from '../context' ; /**
 * Returns the current UI-settings value.
 *
 * Usage:
 *
 * ```js
 * const darkMode = useUiSetting('theme:darkMode');
 * ```
 */ export const useUiSetting = < T > ( key , defaultValue ? ) : T => { const { services } = useOpenSearchDashboards ( ) ; if ( typeof services . uiSettings !== 'object' ) { throw new TypeError ( 'uiSettings service not available in opensearch-dashboards-react context.' ) ; } return services . uiSettings . get ( key , defaultValue ) ; } ; type Setter < T > = ( newValue : T ) => Promise < boolean > ; /**
 * Returns a 2-tuple, where first entry is the setting value and second is a
 * function to update the setting value.
 *
 * Synchronously returns the most current value of the setting and subscribes
 * to all subsequent updates, which will re-render your component on new values.
 *
 * Usage:
 *
 * ```js
 * const [darkMode, setDarkMode] = useUiSetting$('theme:darkMode');
 * ```
 */ export const useUiSetting$ = < T > ( key , defaultValue ? ) : [ T , Setter < T > ] => { const { services } = useOpenSearchDashboards ( ) ; if ( typeof services . uiSettings !== 'object' ) { throw new TypeError ( 'uiSettings service not available in opensearch-dashboards-react context.' ) ; } const observable$ = useMemo ( ( ) => services . uiSettings ! . get$ ( key , defaultValue ) , [ key , defaultValue , services . uiSettings , ] ) ; const value = useObservable < T > ( observable$ , services . uiSettings ! . get ( key , defaultValue ) ) ; const set = useCallback ( ( newValue ) => services . uiSettings ! . set ( key , newValue ) , [ key ] ) ; return [ value , set ] ; } ;