From 26d7524c93bff8802a6d27d91759caa876593005 Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Wed, 26 Jun 2024 09:06:51 +0100 Subject: [PATCH] Updates to the AI error assistant (#19107) GitOrigin-RevId: 7ffc1e32d331fa8bab1ea25919e706bf8b59800f --- .../web/frontend/extracted-translations.json | 1 + .../context/editor-manager-context.tsx | 6 ++- .../context/file-tree-open-context.tsx | 2 +- .../pdf-preview/components/pdf-log-entry.jsx | 21 +------- .../pdf-preview/hooks/use-log-events.ts | 50 +++++++++++++++++++ .../shared/context/detach-compile-context.tsx | 2 + .../shared/context/local-compile-context.tsx | 5 +- services/web/locales/en.json | 1 + .../pdf-preview/pdf-logs-entries.spec.tsx | 2 + 9 files changed, 66 insertions(+), 24 deletions(-) create mode 100644 services/web/frontend/js/features/pdf-preview/hooks/use-log-events.ts diff --git a/services/web/frontend/extracted-translations.json b/services/web/frontend/extracted-translations.json index dd45f8437f..85cdf0aace 100644 --- a/services/web/frontend/extracted-translations.json +++ b/services/web/frontend/extracted-translations.json @@ -617,6 +617,7 @@ "in_order_to_match_institutional_metadata_associated": "", "include_caption": "", "include_label": "", + "include_the_error_message_and_ai_response": "", "increased_compile_timeout": "", "inr_discount_modal_info": "", "inr_discount_modal_title": "", diff --git a/services/web/frontend/js/features/ide-react/context/editor-manager-context.tsx b/services/web/frontend/js/features/ide-react/context/editor-manager-context.tsx index 5272eb90c4..f96eba9aa3 100644 --- a/services/web/frontend/js/features/ide-react/context/editor-manager-context.tsx +++ b/services/web/frontend/js/features/ide-react/context/editor-manager-context.tsx @@ -42,6 +42,7 @@ interface OpenDocOptions Partial { gotoOffset?: number forceReopen?: boolean + keepCurrentView?: boolean } export type EditorManager = { @@ -431,7 +432,10 @@ export const EditorManagerProvider: FC = ({ children }) => { // store position of previous doc before switching docs eventEmitter.emit('store-doc-position') } - setView('editor') + + if (!options.keepCurrentView) { + setView('editor') + } const done = (isNewDoc: boolean) => { window.dispatchEvent( diff --git a/services/web/frontend/js/features/ide-react/context/file-tree-open-context.tsx b/services/web/frontend/js/features/ide-react/context/file-tree-open-context.tsx index 9975b028b2..d1b30deb59 100644 --- a/services/web/frontend/js/features/ide-react/context/file-tree-open-context.tsx +++ b/services/web/frontend/js/features/ide-react/context/file-tree-open-context.tsx @@ -73,7 +73,7 @@ export const FileTreeOpenProvider: FC = ({ children }) => { setOpenEntity(selected) if (selected.type === 'doc' && fileTreeReady) { - openDocWithId(selected.entity._id) + openDocWithId(selected.entity._id, { keepCurrentView: true }) if (selected.entity.name.endsWith('.bib')) { sendMB('open-bib-file', { projectOwner: owner._id, diff --git a/services/web/frontend/js/features/pdf-preview/components/pdf-log-entry.jsx b/services/web/frontend/js/features/pdf-preview/components/pdf-log-entry.jsx index ffe5458107..dd678b798c 100644 --- a/services/web/frontend/js/features/pdf-preview/components/pdf-log-entry.jsx +++ b/services/web/frontend/js/features/pdf-preview/components/pdf-log-entry.jsx @@ -48,31 +48,12 @@ function PdfLogEntry({ [level, onSourceLocationClick, ruleId, sourceLocation] ) - const logEntryRef = useCallback( - element => { - if (element) { - window.addEventListener('editor:view-compile-log-entry', event => { - if (event.detail.id === id) { - element.scrollIntoView({ block: 'start', inline: 'nearest' }) - - if (event.detail.suggestFix) { - element - .querySelector('button[data-action="suggest-fix"]') - ?.click() - } - } - }) - } - }, - [id] - ) - return (
void) => { + const { pdfLayout, setView } = useLayoutContext() + + useEffect(() => { + const listener = (event: Event) => { + const { id, suggestFix } = ( + event as CustomEvent<{ id: string; suggestFix?: boolean }> + ).detail + + setShowLogs(true) + + if (pdfLayout === 'flat') { + setView('pdf') + } + + window.setTimeout(() => { + const element = document.querySelector( + `.log-entry[data-log-entry-id="${id}"]` + ) + + if (element) { + element.scrollIntoView({ + block: 'start', + inline: 'nearest', + }) + + if (suggestFix) { + element + .querySelector( + 'button[data-action="suggest-fix"]' + ) + ?.click() + } + } + }) + } + + window.addEventListener('editor:view-compile-log-entry', listener) + + return () => { + window.removeEventListener('editor:view-compile-log-entry', listener) + } + }, [pdfLayout, setView, setShowLogs]) +} diff --git a/services/web/frontend/js/shared/context/detach-compile-context.tsx b/services/web/frontend/js/shared/context/detach-compile-context.tsx index e9111300b1..9bc4c58280 100644 --- a/services/web/frontend/js/shared/context/detach-compile-context.tsx +++ b/services/web/frontend/js/shared/context/detach-compile-context.tsx @@ -3,6 +3,7 @@ import { CompileContext, useLocalCompileContext } from './local-compile-context' import useDetachStateWatcher from '../hooks/use-detach-state-watcher' import useDetachAction from '../hooks/use-detach-action' import useCompileTriggers from '../../features/pdf-preview/hooks/use-compile-triggers' +import { useLogEvents } from '@/features/pdf-preview/hooks/use-log-events' export const DetachCompileContext = createContext( undefined @@ -363,6 +364,7 @@ export const DetachCompileProvider: FC = ({ children }) => { ) useCompileTriggers(startCompile, setChangedAt) + useLogEvents(setShowLogs) const value = useMemo( () => ({ diff --git a/services/web/frontend/js/shared/context/local-compile-context.tsx b/services/web/frontend/js/shared/context/local-compile-context.tsx index 58db07a1c4..38a65659d4 100644 --- a/services/web/frontend/js/shared/context/local-compile-context.tsx +++ b/services/web/frontend/js/shared/context/local-compile-context.tsx @@ -96,7 +96,7 @@ export type CompileContext = { stopCompile: () => void setChangedAt: (value: any) => void clearCache: () => void - syncToEntry: (value: any) => void + syncToEntry: (value: any, keepCurrentView?: boolean) => void } export const LocalCompileContext = createContext( @@ -573,13 +573,14 @@ export const LocalCompileProvider: FC = ({ children }) => { }, [compiler]) const syncToEntry = useCallback( - entry => { + (entry, keepCurrentView = false) => { const result = findEntityByPath(entry.file) if (result && result.type === 'doc') { openDocId(result.entity._id, { gotoLine: entry.line ?? undefined, gotoColumn: entry.column ?? undefined, + keepCurrentView, }) } }, diff --git a/services/web/locales/en.json b/services/web/locales/en.json index 14038640e1..1fe4f43c83 100644 --- a/services/web/locales/en.json +++ b/services/web/locales/en.json @@ -888,6 +888,7 @@ "in_order_to_match_institutional_metadata_associated": "In order to match your institutional metadata, your account is associated with the email __email__.", "include_caption": "Include caption", "include_label": "Include label", + "include_the_error_message_and_ai_response": "Include the error message and AI response", "increased_compile_timeout": "Increased compile timeout", "individuals": "Individuals", "indvidual_plans": "Individual Plans", diff --git a/services/web/test/frontend/components/pdf-preview/pdf-logs-entries.spec.tsx b/services/web/test/frontend/components/pdf-preview/pdf-logs-entries.spec.tsx index 704e2729be..30f9d1c69e 100644 --- a/services/web/test/frontend/components/pdf-preview/pdf-logs-entries.spec.tsx +++ b/services/web/test/frontend/components/pdf-preview/pdf-logs-entries.spec.tsx @@ -92,6 +92,7 @@ describe('', function () { { gotoLine: 9, gotoColumn: 8, + keepCurrentView: false, } ) }) @@ -130,6 +131,7 @@ describe('', function () { { gotoLine: 7, gotoColumn: 6, + keepCurrentView: false, } ) })