import React, { useCallback, useRef, useState } from 'react'; import { Icon, InteractivePopover, css, cx, focusRing, spacing, } from '@mongodb-js/compass-components'; import { createLoggerAndTelemetry } from '@mongodb-js/compass-logging'; import type AppRegistry from 'hadron-app-registry'; const { track } = createLoggerAndTelemetry('COMPASS-QUERY-BAR-UI'); const openQueryHistoryButtonStyles = cx( css({ border: 'none', backgroundColor: 'transparent', display: 'inline-flex', alignItems: 'center', padding: spacing[2] - 2, // -2px for border. marginLeft: spacing[1], '&:hover': { cursor: 'pointer', }, }), focusRing ); const queryHistoryPopoverStyles = css({ // We want the popover to open almost to the shell at the bottom of Compass. maxHeight: 'calc(100vh - 270px)', marginTop: spacing[1], display: 'flex', }); type QueryHistoryProps = { globalAppRegistry: AppRegistry; localAppRegistry: AppRegistry; }; export const QueryHistoryButtonPopover: React.FunctionComponent< QueryHistoryProps > = ({ globalAppRegistry, localAppRegistry }) => { const queryHistoryRef = useRef<{ component?: React.ComponentType; store: any; // Query history store is not currently typed. actions: any; // Query history actions are not typed. }>({ component: globalAppRegistry.getRole('Query.QueryHistory')?.[0].component, store: localAppRegistry.getStore('Query.History'), actions: localAppRegistry.getAction('Query.History.Actions'), }); const [showQueryHistory, setShowQueryHistory] = useState(false); const onSetShowQueryHistory = useCallback( (newShowQueryHistory: boolean) => { if (newShowQueryHistory) { track('Query History Opened'); } else { track('Query History Closed'); } setShowQueryHistory(newShowQueryHistory); }, [setShowQueryHistory] ); const QueryHistoryComponent = queryHistoryRef.current.component; if (!QueryHistoryComponent) { return null; } return ( ( <> {children} )} open={showQueryHistory} setOpen={onSetShowQueryHistory} > ); };