@@ -4,6 +4,7 @@ import { useEffect, useRef, useLayoutEffect, useState } from "react"
44import Prism from "prismjs"
55import "prismjs/components/prism-java"
66import "./prism-frc-theme.css" // Custom theme
7+ import { FileOutput } from "@/lib/types"
78
89function suppressResizeObserverErrors ( ) {
910 const originalConsoleError = console . error
@@ -19,15 +20,17 @@ function suppressResizeObserverErrors() {
1920}
2021
2122interface CodeDisplayProps {
22- code : string
23+ files : FileOutput [ ] ;
24+ activeIndex : number
2325 language : string
2426}
2527
26- export default function CodeDisplay ( { code , language } : CodeDisplayProps ) {
28+ export default function CodeDisplay ( { files , activeIndex , language } : CodeDisplayProps ) {
2729 const codeRef = useRef < HTMLPreElement > ( null )
2830 const [ displayCode , setDisplayCode ] = useState ( "" )
2931 const [ importCount , setImportCount ] = useState ( 0 )
3032 const [ lineOffset , setLineOffset ] = useState ( 0 )
33+ const [ lineNumbers , setLineNumbers ] = useState ( [ 0 ] )
3134 const [ hasImports , setHasImports ] = useState ( false )
3235
3336 useLayoutEffect ( ( ) => {
@@ -41,6 +44,8 @@ export default function CodeDisplay({ code, language }: CodeDisplayProps) {
4144
4245 // Process code whenever it changes
4346 useEffect ( ( ) => {
47+ // Get the code
48+ var code = files [ activeIndex ] . content ;
4449 // Split code into imports and implementation
4550 const lines = code . split ( "\n" )
4651
@@ -85,30 +90,21 @@ export default function CodeDisplay({ code, language }: CodeDisplayProps) {
8590
8691 // Create the display code - always hide imports
8792 setDisplayCode ( implementationLines . join ( "\n" ) )
88- } , [ code ] )
8993
90- // Optimize the highlighting to prevent excessive reflows
91- useEffect ( ( ) => {
92- if ( typeof window !== "undefined" && codeRef . current && displayCode ) {
93- // Use requestAnimationFrame to ensure DOM is ready
94- const highlightCode = ( ) => {
95- try {
96- Prism . highlightElement ( codeRef . current )
97- } catch ( e ) {
98- console . warn ( "Prism highlighting error:" , e )
94+ // Generate line numbers based on the current display code
95+ setLineNumbers ( displayCode . split ( "\n" ) . map ( ( _ , index ) => index + lineOffset + 1 ) ) ;
96+ requestAnimationFrame ( ( ) => {
97+ if ( codeRef . current )
98+ {
99+ try {
100+ Prism . highlightElement ( codeRef . current , false )
101+ } catch ( e ) {
102+ console . warn ( "Prism highlighting error:" , e ) ;
99103 }
100104 }
105+ } )
106+ } , [ files , activeIndex ] )
101107
102- const timeoutId = setTimeout ( ( ) => {
103- requestAnimationFrame ( highlightCode )
104- } , 0 )
105-
106- return ( ) => clearTimeout ( timeoutId )
107- }
108- } , [ displayCode ] )
109-
110- // Generate line numbers based on the current display code
111- const lineNumbers = displayCode . split ( "\n" ) . map ( ( _ , index ) => index + lineOffset + 1 )
112108
113109 return (
114110 < div className = "relative rounded-md overflow-hidden code-container" >
@@ -129,8 +125,8 @@ export default function CodeDisplay({ code, language }: CodeDisplayProps) {
129125 </ div >
130126
131127 { /* Code content */ }
132- < pre ref = { codeRef } className = { `language-${ language } p-4 overflow-x-auto flex-1 leading-6` } >
133- < code className = { `language-${ language } ` } > { displayCode } </ code >
128+ < pre className = { `language-${ language } p-4 overflow-x-auto flex-1 leading-6` } >
129+ < code ref = { codeRef } className = { `language-${ language } ` } > { displayCode } </ code >
134130 </ pre >
135131 </ div >
136132 </ div >
0 commit comments