import React, { useCallback } from 'react'; import { Button, Icon, MoreOptionsToggle, css, cx, spacing, palette, useDarkMode, Label, Link, } from '@mongodb-js/compass-components'; import type { Listenable } from 'reflux'; import type AppRegistry from 'hadron-app-registry'; import type { QueryOption, QueryBarOptionProps, } from '../constants/query-option-definition'; import { QueryOption as QueryOptionComponent, documentEditorLabelContainerStyles, } from './query-option'; import { QueryHistoryButtonPopover } from './query-history-button-popover'; import { QueryBarRow } from './query-bar-row'; import type { CompletionWithServerInfo } from '@mongodb-js/compass-editor'; const queryBarFormStyles = css({ display: 'flex', flexDirection: 'column', flexGrow: 1, background: palette.white, border: `1px solid ${palette.gray.light2}`, borderRadius: '6px', padding: spacing[2], }); const queryBarFormDarkStyles = css({ background: palette.gray.dark3, borderColor: palette.gray.dark2, }); const queryBarFirstRowStyles = css({ display: 'flex', // NOTE: To keep the elements in the query bar from re-positioning // vertically when the filter input is multi-line we use // `flex-start` here. It is more brittle as it does require the other elements // to account for their height individually. alignItems: 'flex-start', gap: spacing[2], paddingLeft: spacing[2], }); const moreOptionsContainerStyles = css({ // We explicitly offset this element so we can use // `alignItems: 'flex-start'` on the first row of the query bar. paddingTop: 2, paddingBottom: 2, }); const filterContainerStyles = css({ flexGrow: 1, }); const filterLabelStyles = css({ padding: 0, }); const queryOptionsContainerStyles = css({ display: 'flex', flexDirection: 'column', marginTop: spacing[2], padding: `0 ${spacing[2]}px`, gap: spacing[2], }); const queryBarDocumentationLink = 'https://docs.mongodb.com/compass/current/query/filter/'; type QueryBarProps = { buttonLabel?: string; expanded: boolean; globalAppRegistry: AppRegistry; localAppRegistry: AppRegistry; onApply: () => void; onChangeQueryOption: (queryOption: QueryOption, value: string) => void; onOpenExportToLanguage: () => void; onReset: () => void; queryOptionsLayout?: (QueryOption | QueryOption[])[]; queryState: 'apply' | 'reset'; refreshEditorAction: Listenable; resultId: string | number; schemaFields: CompletionWithServerInfo[]; serverVersion: string; showExportToLanguageButton?: boolean; showQueryHistoryButton?: boolean; toggleExpandQueryOptions: () => void; valid: boolean; } & QueryBarOptionProps; const QueryBar: React.FunctionComponent = ({ buttonLabel = 'Apply', expanded: isQueryOptionsExpanded = false, globalAppRegistry, localAppRegistry, onApply: _onApply, onChangeQueryOption, onOpenExportToLanguage, onReset, // Used to specify which query options to show and where they are positioned. queryOptionsLayout = [ 'project', ['sort', 'maxTimeMS'], ['collation', 'skip', 'limit'], ], queryState, refreshEditorAction, resultId, schemaFields, serverVersion, showExportToLanguageButton = true, showQueryHistoryButton = true, toggleExpandQueryOptions, valid: isQueryValid, ...queryOptionProps }) => { const darkMode = useDarkMode(); const onApply = useCallback(() => { if (isQueryValid) { _onApply(); } }, [_onApply, isQueryValid]); const onFormSubmit = useCallback( (evt: React.FormEvent) => { evt.preventDefault(); onApply(); }, [onApply] ); const filterQueryOptionId = 'query-bar-option-input-filter'; return (
{showQueryHistoryButton && ( )}
onChangeQueryOption('filter', value)} onApply={onApply} placeholder={queryOptionProps.filterPlaceholder} refreshEditorAction={refreshEditorAction} schemaFields={schemaFields} serverVersion={serverVersion} value={queryOptionProps.filterString} />
{showExportToLanguageButton && ( )} {queryOptionsLayout && queryOptionsLayout.length > 0 && (
)}
{isQueryOptionsExpanded && queryOptionsLayout && queryOptionsLayout.length > 0 && (
{queryOptionsLayout.map((queryOptionRowLayout, rowIndex) => ( ))}
)}
); }; export { QueryBar };