| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- 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()
- const handleViewCompileLogEntryEventOldEditor = useCallback(
- (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) {
- // if they are paywalled, click that instead
- const paywall = document.querySelector<HTMLButtonElement>(
- 'button[data-action="assistant-paywall-show"]'
- )
- if (paywall) {
- paywall.scrollIntoView({
- block: 'start',
- inline: 'nearest',
- })
- paywall.click()
- } else {
- element
- .querySelector<HTMLButtonElement>(
- 'button[data-action="suggest-fix"]'
- )
- ?.click()
- }
- }
- }
- })
- },
- [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)
- return () => {
- window.removeEventListener('editor:view-compile-log-entry', listener)
- }
- }, [
- handleViewCompileLogEntryEventNewEditor,
- handleViewCompileLogEntryEventOldEditor,
- newEditor,
- ])
- }
|