Skip to content

Commit a64848c

Browse files
authored
Fix performance issue on promptfiddle codemirror editor (#2083)
<!-- ELLIPSIS_HIDDEN --> > [!IMPORTANT] > Optimize `CodeMirrorViewer` performance by adjusting `useEffect` dependencies and commenting out unused code. > > - **Performance Optimization**: > - Commented out unused `useEffect` in `code-mirror-viewer.tsx` to prevent unnecessary re-renders. > - Changed `useEffect` dependency from `fileContent.code` to `fileContent.id` to reduce updates. > - Updated `useEffect` dependency from `generatedFiles?.map((f) => f.content)` to `generatedFiles?.map((f) => f.path)` for more efficient comparison. > - **Imports**: > - Added `useAtom` import from `jotai` in `code-mirror-viewer.tsx`. > - Added `filesAtom` import in `code-mirror-viewer.tsx`. > > <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup> for eaa659d. You can [customize](https://app.ellipsis.dev/BoundaryML/settings/summaries) this summary. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN -->
1 parent 350914c commit a64848c

1 file changed

Lines changed: 27 additions & 24 deletions

File tree

typescript/playground-common/src/shared/baml-project-panel/codemirror-panel/code-mirror-viewer.tsx

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { StateField, StateEffect, RangeSet } from '@codemirror/state'
1818

1919
import { hyperLink } from '@uiw/codemirror-extensions-hyper-link'
2020
import { langs } from '@uiw/codemirror-extensions-langs'
21-
import { useSetAtom, useStore, useAtomValue } from 'jotai'
21+
import { useSetAtom, useStore, useAtomValue, useAtom } from 'jotai'
2222
import { type ICodeBlock } from '../types'
2323
import { CodeMirrorDiagnosticsAtom } from './atoms'
2424

@@ -30,6 +30,7 @@ import { createDefaultMapFromCDN, createSystem, createVirtualTypeScriptEnvironme
3030
import { useTheme } from 'next-themes'
3131
import ts from 'typescript'
3232
import { FlashRange, flashRangesAtom, updateCursorAtom } from '../playground-panel/atoms'
33+
import { filesAtom } from '../atoms'
3334

3435
const extensionMap = {
3536
js: [langs.javascript()],
@@ -130,6 +131,8 @@ export const CodeMirrorViewer = ({
130131
onContentChange: (content: string) => void
131132
onAutocompleteTrigger?: (content: string) => Promise<string>
132133
}) => {
134+
// const [files, setFiles] = useAtom(filesAtom)
135+
133136
const ref = useRef<ReactCodeMirrorRef>({})
134137
const store = useStore()
135138
const flashRanges = useAtomValue(flashRangesAtom)
@@ -189,27 +192,27 @@ export const CodeMirrorViewer = ({
189192

190193
const [extensions, setExtensions] = useState<Extension[]>([])
191194

192-
useEffect(() => {
193-
// const interval = setInterval(() => {
194-
if (ref.current?.view?.contentDOM) {
195-
const line = ref.current.view.state.doc.lineAt(ref.current.view.state.doc.length)
196-
if (line) {
197-
if (shouldScrollDown) {
198-
ref.current.view?.dispatch({
199-
selection: { anchor: line.from, head: undefined },
200-
scrollIntoView: true,
201-
})
202-
}
203-
}
204-
// // Scroll to the bottom of the container
205-
// containerRef.current.contentDOM.scrollIntoView({
206-
// behavior: "smooth",
207-
// });
208-
}
209-
// }, 1000); // Adjust the interval time (in milliseconds) as needed
210-
211-
// return () => clearInterval(interval); // Clean up the interval on component unmount
212-
}, [fileContent, ref, shouldScrollDown])
195+
// useEffect(() => {
196+
// // const interval = setInterval(() => {
197+
// if (ref.current?.view?.contentDOM) {
198+
// const line = ref.current.view.state.doc.lineAt(ref.current.view.state.doc.length)
199+
// if (line) {
200+
// if (shouldScrollDown) {
201+
// ref.current.view?.dispatch({
202+
// selection: { anchor: line.from, head: undefined },
203+
// scrollIntoView: true,
204+
// })
205+
// }
206+
// }
207+
// // // Scroll to the bottom of the container
208+
// // containerRef.current.contentDOM.scrollIntoView({
209+
// // behavior: "smooth",
210+
// // });
211+
// }
212+
// // }, 1000); // Adjust the interval time (in milliseconds) as needed
213+
214+
// // return () => clearInterval(interval); // Clean up the interval on component unmount
215+
// }, [fileContent, ref, shouldScrollDown])
213216

214217
const setUpdateCursor = useSetAtom(updateCursorAtom)
215218

@@ -267,7 +270,7 @@ export const CodeMirrorViewer = ({
267270
}
268271

269272
void initializeExtensions()
270-
}, [JSON.stringify(generatedFiles?.map((f) => f.content)), compartment, lang, makeLinter])
273+
}, [JSON.stringify(generatedFiles?.map((f) => f.path)), compartment, lang, makeLinter])
271274

272275
const inlineCopilotExtension = useMemo(
273276
() => [
@@ -339,7 +342,7 @@ export const CodeMirrorViewer = ({
339342

340343
useEffect(() => {
341344
onContentChange?.(fileContent.code)
342-
}, [fileContent.code])
345+
}, [fileContent.id])
343346

344347
useEffect(() => {
345348
if (lang !== 'baml') {

0 commit comments

Comments
 (0)