Skip to content

Commit

Permalink
[Discover][Unified Search] Clicking on an individual error should foc…
Browse files Browse the repository at this point in the history
…us the error on the monaco editor. (#148171)

## Summary
Completes part of #136950.
Closes #147986

### Basic case:

https://user-images.githubusercontent.com/22456368/209937498-c007be4e-4ced-46c4-bac5-5786b3d88074.mov

### Case with updating query, not running it, and clicking on errors:

https://user-images.githubusercontent.com/22456368/209937718-a55e13fd-5a7c-4ec2-ac6f-bfce9578d75a.mov

### Fixed bug related to clearing the previous errors:
#### Before:


https://user-images.githubusercontent.com/22456368/209938380-3fcd04a6-8c49-4bc7-82f8-9e2cccf7e369.mov

#### After


https://user-images.githubusercontent.com/22456368/209938517-53101589-206e-4c26-be59-6ddb499c6183.mov

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
Kuznietsov and kibanamachine authored Jan 3, 2023
1 parent 595ba8f commit fa3972b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function useDataState<T extends DataMsg>(data$: BehaviorSubject<T>) {
useEffect(() => {
const subscription = data$.subscribe((next) => {
if (next.fetchStatus !== fetchState.fetchStatus) {
setFetchState({ ...fetchState, ...next });
setFetchState({ ...fetchState, ...next, ...(next.error ? {} : { error: undefined }) });
}
});
return () => subscription.unsubscribe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@ import {
EuiDescriptionListDescription,
} from '@elastic/eui';
import { Interpolation, Theme, css } from '@emotion/react';
import { css as classNameCss } from '@emotion/css';

import type { MonacoError } from './helpers';

const isMac = navigator.platform.toLowerCase().indexOf('mac') >= 0;
const COMMAND_KEY = isMac ? '⌘' : '^';

interface EditorFooterProps {
lines: number;
containerCSS: Interpolation<Theme>;
errors?: Array<{ startLineNumber: number; message: string }>;
errors?: MonacoError[];
onErrorClick: (error: MonacoError) => void;
refreshErrors: () => void;
}

export const EditorFooter = memo(function EditorFooter({
lines,
containerCSS,
errors,
onErrorClick,
refreshErrors,
}: EditorFooterProps) {
const [isPopoverOpen, setIsPopoverOpen] = useState(false);
return (
Expand Down Expand Up @@ -75,7 +82,10 @@ export const EditorFooter = memo(function EditorFooter({
text-decoration: underline;
}
`}
onClick={() => setIsPopoverOpen(!isPopoverOpen)}
onClick={() => {
refreshErrors();
setIsPopoverOpen(!isPopoverOpen);
}}
>
<p>
{i18n.translate(
Expand Down Expand Up @@ -104,7 +114,15 @@ export const EditorFooter = memo(function EditorFooter({
<EuiDescriptionList>
{errors.map((error, index) => {
return (
<EuiDescriptionListDescription key={index}>
<EuiDescriptionListDescription
key={index}
className={classNameCss`
&:hover {
cursor: pointer;
}
`}
onClick={() => onErrorClick(error)}
>
<EuiFlexGroup gutterSize="xl" alignItems="flexStart">
<EuiFlexItem grow={false}>
<EuiFlexGroup gutterSize="s" alignItems="center">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import useDebounce from 'react-use/lib/useDebounce';
import { monaco } from '@kbn/monaco';
import { i18n } from '@kbn/i18n';

export interface MonacoError {
message: string;
startColumn: number;
startLineNumber: number;
endColumn: number;
endLineNumber: number;
severity: monaco.MarkerSeverity;
}

export const useDebounceWithOptions = (
fn: Function,
{ skipFirstRender }: { skipFirstRender: boolean } = { skipFirstRender: false },
Expand All @@ -33,7 +42,7 @@ export const useDebounceWithOptions = (
);
};

export const parseErrors = (errors: Error[], code: string) => {
export const parseErrors = (errors: Error[], code: string): MonacoError[] => {
return errors.map((error) => {
if (error.message.includes('line')) {
const text = error.message.split('line')[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
parseErrors,
getInlineEditorText,
getDocumentationSections,
MonacoError,
} from './helpers';
import { EditorFooter } from './editor_footer';
import { ResizableButton } from './resizable_button';
Expand Down Expand Up @@ -103,9 +104,7 @@ export const TextBasedLanguagesEditor = memo(function TextBasedLanguagesEditor({
const [isCompactFocused, setIsCompactFocused] = useState(isCodeEditorExpanded);
const [isCodeEditorExpandedFocused, setIsCodeEditorExpandedFocused] = useState(false);
const [isWordWrapped, setIsWordWrapped] = useState(true);
const [editorErrors, setEditorErrors] = useState<
Array<{ startLineNumber: number; message: string }>
>([]);
const [editorErrors, setEditorErrors] = useState<MonacoError[]>([]);
const [documentationSections, setDocumentationSections] =
useState<LanguageDocumentationSections>();
const kibana = useKibana<IUnifiedSearchPluginServices>();
Expand Down Expand Up @@ -241,6 +240,19 @@ export const TextBasedLanguagesEditor = memo(function TextBasedLanguagesEditor({
[errors]
);

const onErrorClick = useCallback(({ startLineNumber, startColumn }: MonacoError) => {
if (!editor1.current) {
return;
}

editor1.current.focus();
editor1.current.setPosition({
lineNumber: startLineNumber,
column: startColumn,
});
editor1.current.revealLine(startLineNumber);
}, []);

// Clean up the monaco editor and DOM on unmount
useEffect(() => {
const model = editorModel;
Expand Down Expand Up @@ -512,6 +524,8 @@ export const TextBasedLanguagesEditor = memo(function TextBasedLanguagesEditor({
lines={lines}
containerCSS={styles.bottomContainer}
errors={editorErrors}
onErrorClick={onErrorClick}
refreshErrors={onTextLangQuerySubmit}
/>
)}
</div>
Expand Down Expand Up @@ -577,7 +591,13 @@ export const TextBasedLanguagesEditor = memo(function TextBasedLanguagesEditor({
)}
</EuiFlexGroup>
{isCodeEditorExpanded && (
<EditorFooter lines={lines} containerCSS={styles.bottomContainer} errors={editorErrors} />
<EditorFooter
lines={lines}
containerCSS={styles.bottomContainer}
errors={editorErrors}
onErrorClick={onErrorClick}
refreshErrors={onTextLangQuerySubmit}
/>
)}
{isCodeEditorExpanded && (
<ResizableButton
Expand Down

0 comments on commit fa3972b

Please sign in to comment.