import { css } from '@emotion/css'; import React, { useMemo, useState } from 'react'; import { HorizontalGroup, Icon, IconButton, Tooltip, useTheme2 } from '@grafana/ui'; import { QueryValidator, QueryValidatorProps } from './QueryValidator'; interface QueryToolboxProps extends Omit { showTools?: boolean; isExpanded?: boolean; onFormatCode?: () => void; onExpand?: (expand: boolean) => void; onValidate?: (isValid: boolean) => void; } export function QueryToolbox({ showTools, onFormatCode, onExpand, isExpanded, ...validatorProps }: QueryToolboxProps) { const theme = useTheme2(); const [validationResult, setValidationResult] = useState(); const styles = useMemo(() => { return { container: css` border: 1px solid ${theme.colors.border.medium}; border-top: none; padding: ${theme.spacing(0.5, 0.5, 0.5, 0.5)}; display: flex; flex-grow: 1; justify-content: space-between; font-size: ${theme.typography.bodySmall.fontSize}; `, error: css` color: ${theme.colors.error.text}; font-size: ${theme.typography.bodySmall.fontSize}; font-family: ${theme.typography.fontFamilyMonospace}; `, valid: css` color: ${theme.colors.success.text}; `, info: css` color: ${theme.colors.text.secondary}; `, hint: css` color: ${theme.colors.text.disabled}; white-space: nowrap; cursor: help; `, }; }, [theme]); let style = {}; if (!showTools && validationResult === undefined) { style = { height: 0, padding: 0, visibility: 'hidden' }; } return (
{validatorProps.onValidate && ( { setValidationResult(result); validatorProps.onValidate!(result); }} /> )}
{showTools && (
{onFormatCode && ( )} {onExpand && ( onExpand(!isExpanded)} name={isExpanded ? 'angle-up' : 'angle-down'} size="xs" tooltip={isExpanded ? 'Collapse editor' : 'Expand editor'} /> )}
)}
); }