import type AppRegistry from 'hadron-app-registry'; import React, { useCallback, useRef } from 'react'; import { SegmentedControl, SegmentedControlOption, Overline, css, spacing, useId, useDarkMode, WarningSummary, ErrorSummary, } from '@mongodb-js/compass-components'; const explainToolbarStyles = css({ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: spacing[3], padding: spacing[3], zIndex: 10, }); const explainQueryBarStyles = css({ width: '100%', position: 'relative', }); const explainActionsToolbarStyles = css({ display: 'flex', alignItems: 'center', width: '100%', justifyContent: 'flex-end', gap: spacing[2], }); const READ_ONLY_WARNING_MESSAGE = 'Explain plans on readonly views are not supported.'; const OUTDATED_WARNING_MESSAGE = `The explain content is outdated and no longer in sync with the documents view. Press "Explain" again to see the explain plan for the current query.`; type ExplainView = 'json' | 'tree'; type ExplainToolbarProps = { localAppRegistry: AppRegistry; explainErrorMessage?: string; resultId: string; hasExplainResults: boolean; onExecuteExplainClicked: (queryBarStoreState: any) => void; showOutdatedWarning: boolean; showReadonlyWarning: boolean; switchToTreeView: () => void; switchToJSONView: () => void; viewType: ExplainView; }; function ExplainToolbar({ localAppRegistry, resultId, explainErrorMessage, hasExplainResults, onExecuteExplainClicked, showOutdatedWarning, showReadonlyWarning, switchToTreeView, switchToJSONView, viewType, }: ExplainToolbarProps) { const darkMode = useDarkMode(); const labelId = useId(); const controlId = useId(); const queryBarRole = localAppRegistry.getRole('Query.QueryBar')![0]; const queryBarRef = useRef<{ component: React.ComponentType; store: any; // Query bar store is not currently typed. actions: any; // Query bar actions are not typed. }>({ component: queryBarRole.component, store: localAppRegistry.getStore(queryBarRole.storeName!), actions: localAppRegistry.getAction(queryBarRole.actionName!), }); const toggleView = useCallback( (newViewType: ExplainView) => { if (newViewType === 'tree') { switchToTreeView(); } else { switchToJSONView(); } }, [switchToJSONView, switchToTreeView] ); const QueryBarComponent = queryBarRef.current.component; return (
{ }
{hasExplainResults && (
View toggleView(value as ExplainView)} > Visual Tree Raw Json
)} {(showOutdatedWarning || showReadonlyWarning) && ( )} {explainErrorMessage && }
); } export { ExplainToolbar };