Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Fix focus change bug

Tomas Valenta9975daf6279c
diff --git a/components/Editor/BlockEditor.tsx b/components/Editor/BlockEditor.tsx
index 637ddadde..775bd1644 100644
--- a/components/Editor/BlockEditor.tsx
+++ b/components/Editor/BlockEditor.tsx
@@ -1,6 +1,6 @@
import clsx from 'clsx'
import { Wrench } from 'lucide-react'
-import { useState, useEffect, MouseEvent, useRef } from 'react'
+import { useState, useLayoutEffect, useEffect, MouseEvent, useRef } from 'react'
import TextareaAutosize from 'react-textarea-autosize'
import Text from 'components/typography/Text'
@@ -10,19 +10,17 @@ export interface Props {
onChange: (block: Block) => void
onDelete: () => void
block: Block
- isLast?: boolean
- isFirst?: boolean
- isFocused?: boolean
- onFocus?: () => void
+ index: number,
+ focus: { index: number }
+ onFocus: () => void
}
export default function BlockEditor({
onChange,
onDelete,
block,
- isLast,
- isFirst,
- isFocused,
+ index,
+ focus,
onFocus,
}: Props) {
const [confirmDelete, setConfirmDelete] = useState(false)
@@ -40,11 +38,11 @@ export default function BlockEditor({
[confirmDelete],
)
- useEffect(function autofocus() {
- if (isFocused) {
+ useLayoutEffect(function autofocus() {
+ if (focus.index === index) {
ref.current?.focus()
}
- }, [isFocused])
+ }, [focus, index])
function handleDelete(
e: MouseEvent<HTMLButtonElement, globalThis.MouseEvent>,
diff --git a/components/Editor/index.tsx b/components/Editor/index.tsx
index 4bf69d770..de6a8707b 100644
--- a/components/Editor/index.tsx
+++ b/components/Editor/index.tsx
@@ -58,7 +58,7 @@ export default function Editor({ }: Props) {
changeMethod,
} = useStore(selector, shallow)
- const [focusedBlock, setFocusedBlock] = useState(0)
+ const [focusedBlock, setFocusedBlock] = useState({ index: 0 })
const { trigger: generate } = useSWRMutation('/api/generate', handlePostGenerate)
@@ -78,13 +78,13 @@ export default function Editor({ }: Props) {
onKeyDown={(s) => {
if (s === 'command+enter' || s === 'control+enter') {
setFocusedBlock(b => {
- if (blocks.length === 0 || b === blocks.length - 1) {
+ if (blocks.length === 0 || b.index === blocks.length - 1) {
addBlock({ prompt: '', id: nanoid() })
}
- return b + 1
+ return { index: b.index + 1 }
})
} else if (s === 'shift+command+enter' || s === 'shift+control+enter') {
- setFocusedBlock(b => b > 0 ? b - 1 : b)
+ setFocusedBlock(b => ({ index: b.index > 0 ? b.index - 1 : b.index }))
}
}}
filter={() => {
@@ -130,18 +130,29 @@ export default function Editor({ }: Props) {
<ConnectionLine className='h-4' />
<BlockEditor
block={b}
- onDelete={() => removeBlock(i)}
+ onDelete={() => {
+ removeBlock(i)
+ setTimeout(() => {
+ if (i <= focusedBlock.index) {
+ setFocusedBlock(b => ({ index: b.index - 1 }))
+ } else {
+ setFocusedBlock(b => ({ index: b.index }))
+ }
+ }, 0)
+ }}
onChange={(b) => changeBlock(i, b)}
- isLast={i === a.length - 1}
- isFirst={i === 0}
- isFocused={i === focusedBlock}
- onFocus={() => setFocusedBlock(i)}
+ index={i}
+ focus={focusedBlock}
+ onFocus={() => setFocusedBlock({ index: i })}
/>
</Fragment>
)}
</div>
<ConnectionLine className='h-4' />
- <AddBlockButton addBlock={addBlock} />
+ <AddBlockButton addBlock={(block) => {
+ addBlock(block)
+ setTimeout(() => setFocusedBlock({ index: blocks.length }), 0)
+ }} />
<div className="absolute right-4 top-4">
<Button
text="Deploy"