|
|
@@ -1,14 +1,20 @@
|
|
|
-import { useEffect } from 'react'
|
|
|
+import { useCallback, useEffect } from 'react'
|
|
|
import { useLayoutContext } from '@/shared/context/layout-context'
|
|
|
+import { useIsNewEditorEnabled } from '@/features/ide-redesign/utils/new-editor-utils'
|
|
|
+import { useRailContext } from '@/features/ide-redesign/contexts/rail-context'
|
|
|
+import { useEditorContext } from '@/shared/context/editor-context'
|
|
|
|
|
|
/**
|
|
|
* This hook adds an event listener for events dispatched from the editor to the compile logs pane
|
|
|
*/
|
|
|
export const useLogEvents = (setShowLogs: (show: boolean) => void) => {
|
|
|
const { pdfLayout, setView } = useLayoutContext()
|
|
|
+ const newEditor = useIsNewEditorEnabled()
|
|
|
+ const { openTab: openRailTab } = useRailContext()
|
|
|
+ const { hasPremiumSuggestion } = useEditorContext()
|
|
|
|
|
|
- useEffect(() => {
|
|
|
- const listener = (event: Event) => {
|
|
|
+ const handleViewCompileLogEntryEventOldEditor = useCallback(
|
|
|
+ (event: Event) => {
|
|
|
const { id, suggestFix } = (
|
|
|
event as CustomEvent<{ id: string; suggestFix?: boolean }>
|
|
|
).detail
|
|
|
@@ -52,6 +58,68 @@ export const useLogEvents = (setShowLogs: (show: boolean) => void) => {
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
+ },
|
|
|
+ [pdfLayout, setView, setShowLogs]
|
|
|
+ )
|
|
|
+
|
|
|
+ const handleViewCompileLogEntryEventNewEditor = useCallback(
|
|
|
+ (event: Event) => {
|
|
|
+ const { id, suggestFix } = (
|
|
|
+ event as CustomEvent<{ id: string; suggestFix?: boolean }>
|
|
|
+ ).detail
|
|
|
+
|
|
|
+ openRailTab('errors')
|
|
|
+
|
|
|
+ window.setTimeout(() => {
|
|
|
+ const logEntry = document.querySelector(
|
|
|
+ `.log-entry[data-log-entry-id="${id}"]`
|
|
|
+ )
|
|
|
+
|
|
|
+ if (logEntry) {
|
|
|
+ logEntry.scrollIntoView({
|
|
|
+ block: 'start',
|
|
|
+ inline: 'nearest',
|
|
|
+ })
|
|
|
+
|
|
|
+ const expandCollapseButton =
|
|
|
+ logEntry.querySelector<HTMLButtonElement>(
|
|
|
+ 'button[data-action="expand-collapse"]'
|
|
|
+ )
|
|
|
+
|
|
|
+ const collapsed = expandCollapseButton?.dataset.collapsed === 'true'
|
|
|
+
|
|
|
+ if (collapsed) {
|
|
|
+ expandCollapseButton.click()
|
|
|
+ }
|
|
|
+
|
|
|
+ if (suggestFix) {
|
|
|
+ if (hasPremiumSuggestion) {
|
|
|
+ logEntry
|
|
|
+ .querySelector<HTMLButtonElement>(
|
|
|
+ 'button[data-action="suggest-fix"]'
|
|
|
+ )
|
|
|
+ ?.click()
|
|
|
+ } else {
|
|
|
+ window.dispatchEvent(
|
|
|
+ new CustomEvent('aiAssist:showPaywall', {
|
|
|
+ detail: { origin: 'suggest-fix' },
|
|
|
+ })
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ [openRailTab, hasPremiumSuggestion]
|
|
|
+ )
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ const listener = (event: Event) => {
|
|
|
+ if (newEditor) {
|
|
|
+ handleViewCompileLogEntryEventNewEditor(event)
|
|
|
+ } else {
|
|
|
+ handleViewCompileLogEntryEventOldEditor(event)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
window.addEventListener('editor:view-compile-log-entry', listener)
|
|
|
@@ -59,5 +127,9 @@ export const useLogEvents = (setShowLogs: (show: boolean) => void) => {
|
|
|
return () => {
|
|
|
window.removeEventListener('editor:view-compile-log-entry', listener)
|
|
|
}
|
|
|
- }, [pdfLayout, setView, setShowLogs])
|
|
|
+ }, [
|
|
|
+ handleViewCompileLogEntryEventNewEditor,
|
|
|
+ handleViewCompileLogEntryEventOldEditor,
|
|
|
+ newEditor,
|
|
|
+ ])
|
|
|
}
|