Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Add block describing request's body (only frontend so far)

Vasek Mlejnsky44ce3c7b2f00
diff --git a/components/Editor/BlockEditor.tsx b/components/Block/Basic.tsx
similarity index 97%
rename from components/Editor/BlockEditor.tsx
rename to components/Block/Basic.tsx
index 0300e7e4c..34ade61d3 100644
--- a/components/Editor/BlockEditor.tsx
+++ b/components/Block/Basic.tsx
@@ -17,7 +17,7 @@ export interface Props {
onFocus: () => void
}
-function BlockEditor({
+function Basic({
onChange,
onDelete,
block,
@@ -87,4 +87,4 @@ function BlockEditor({
)
}
-export default BlockEditor
+export default Basic
diff --git a/components/Block/RequestBody.tsx b/components/Block/RequestBody.tsx
new file mode 100644
index 000000000..1cd4ec27b
--- /dev/null
+++ b/components/Block/RequestBody.tsx
@@ -0,0 +1,115 @@
+import clsx from 'clsx'
+import {
+ useEffect,
+ useRef,
+} from 'react'
+import TextareaAutosize from 'react-textarea-autosize'
+
+import { Block } from 'state/store'
+import DeleteButton from 'components/DeleteButton'
+
+export interface Props {
+ onChange: (block: Block) => void
+ onDelete: () => void
+ block: Block
+ index: number,
+ focus: { index: number }
+ onFocus: () => void
+}
+
+function BlockEditor({
+ onChange,
+ onDelete,
+ block,
+ index,
+ focus,
+ onFocus,
+}: Props) {
+ const ref = useRef<HTMLTextAreaElement>(null)
+
+ useEffect(function autofocus() {
+ if (focus.index === index) {
+ ref.current?.focus()
+ }
+ }, [focus, index])
+
+ return (
+ <div className="
+ flex
+ flex-col
+ items-start
+ relative
+ w-[65ch]
+ border
+ border-green-800/40
+ rounded-lg
+ ">
+ <div className="
+ self-stretch
+ rounded-t-lg
+ py-1
+ px-4
+ font-mono
+ text-slate-400
+ text-sm
+ bg-gray-50
+ ">
+ {'{'}
+ </div>
+ <TextareaAutosize
+ autoCapitalize="off"
+ autoComplete="off"
+ autoCorrect="off"
+ spellCheck="false"
+ name="block"
+ placeholder="fieldName: Type,"
+ value={block.prompt}
+ onChange={e => onChange({ ...block, prompt: e.target.value })}
+ onFocus={onFocus}
+ ref={ref}
+ className={clsx(
+ 'w-full',
+ 'pl-8',
+ 'pr-4',
+ 'py-2',
+ 'leading-6',
+ 'tracking-wide',
+ 'font-mono',
+ 'text-slate-500',
+ 'focus:text-slate-600',
+ 'no-scroller',
+ 'transition-[shadow,colors]',
+ 'focus:shadow',
+ 'bg-white',
+ 'outline-none',
+ 'text-sm',
+ 'placeholder:text-slate-300',
+ )}
+ />
+ <div className="
+ self-stretch
+ rounded-b-lg
+ py-1
+ px-4
+ font-mono
+ text-slate-400
+ text-sm
+ bg-gray-50
+ ">
+ {'}'}
+ </div>
+ <div className="
+ absolute
+ translate-x-full
+ right-0
+ pl-1
+ ">
+ <DeleteButton
+ onDelete={onDelete}
+ />
+ </div>
+ </div>
+ )
+}
+
+export default BlockEditor
diff --git a/components/Block/index.tsx b/components/Block/index.tsx
new file mode 100644
index 000000000..67051d2b6
--- /dev/null
+++ b/components/Block/index.tsx
@@ -0,0 +1,24 @@
+import { BlockType } from 'state/store'
+import Basic, { Props as BaseProps } from './Basic'
+import RequestBody, { Props as RequestBodyProps } from './RequestBody'
+
+export interface Props {
+ type: BlockType
+ props: BaseProps | RequestBodyProps
+}
+
+function Block({
+ type,
+ props,
+}: Props) {
+ switch (type) {
+ case 'Basic':
+ return <Basic {...props} />
+ case 'RequestBody':
+ return <RequestBody {...props} />
+ default:
+ return <Basic {...props} />
+ }
+}
+
+export default Block
diff --git a/components/Editor/index.tsx b/components/Editor/index.tsx
index f6a395e6b..bc60abacf 100644
--- a/components/Editor/index.tsx
+++ b/components/Editor/index.tsx
@@ -9,8 +9,9 @@ import Select from 'components/Select'
import Text from 'components/Text'
import { useLatestDeployment } from 'hooks/useLatestDeployment'
import { Log } from 'db/types'
+import Block from 'components/Block'
+
-import BlockEditor from './BlockEditor'
import ConnectionLine from './ConnectionLine'
import AddBlockButton from './AddBlockButton'
import Logs from './Logs'
@@ -107,7 +108,7 @@ function Editor({ project }: Props) {
if (!selectedRoute) return
setFocusedBlock(b => {
if (selectedRoute.blocks.length === 0 || b.index === selectedRoute?.blocks.length - 1) {
- addBlock(selectedRoute.id)
+ addBlock(selectedRoute.id, 'Basic')
}
return { index: b.index + 1 }
})
@@ -164,28 +165,33 @@ function Editor({ project }: Props) {
flex-col
items-center
transition-all
- ">
+ ">
{selectedRoute.blocks.map((b, i) =>
<Fragment
key={b.id}
>
<ConnectionLine className='h-4' />
- <BlockEditor
- block={b}
- onDelete={() => {
- deleteBlock(selectedRoute.id, i)
- setTimeout(() => {
- if (i <= focusedBlock.index) {
- setFocusedBlock(b => ({ index: b.index - 1 }))
- } else {
- setFocusedBlock(b => ({ index: b.index }))
- }
- }, 0)
+ <Block
+ type={b.type}
+ props={{
+ block: b,
+ onDelete() {
+ deleteBlock(selectedRoute.id, i)
+ setTimeout(() => {
+ if (i <= focusedBlock.index) {
+ setFocusedBlock(b => ({ index: b.index - 1 }))
+ } else {
+ setFocusedBlock(b => ({ index: b.index }))
+ }
+ }, 0)
+ },
+ onChange(b) {
+ changeBlock(selectedRoute.id, i, b)
+ },
+ index: i,
+ focus: focusedBlock,
+ onFocus() { setFocusedBlock({ index: i }) }
}}
- onChange={(b) => changeBlock(selectedRoute.id, i, b)}
- index={i}
- focus={focusedBlock}
- onFocus={() => setFocusedBlock({ index: i })}
/>
</Fragment>
)}
@@ -193,7 +199,7 @@ function Editor({ project }: Props) {
<ConnectionLine className='min-h-[16px]' />
<AddBlockButton
addBlock={() => {
- addBlock(selectedRoute.id)
+ addBlock(selectedRoute.id, 'Basic')
setTimeout(() => setFocusedBlock({ index: selectedRoute.blocks.length }), 0)
}}
/>
diff --git a/state/store.ts b/state/store.ts
index 94fb2e59d..ef69b931e 100644
--- a/state/store.ts
+++ b/state/store.ts
@@ -8,8 +8,11 @@ import { projects, Prisma } from '@prisma/client'
import { Database } from 'db/supabase'
import { projectsTable } from 'db/tables'
+export type BlockType = 'Basic' | 'RequestBody'
+
export interface Block {
id: string
+ type: BlockType
prompt: string
}
@@ -41,7 +44,7 @@ export interface SerializedState {
export interface State extends SerializedState {
changeBlock: (routeID: string, index: number, block: Partial<Omit<Block, 'id'>>) => void
deleteBlock: (routeID: string, index: number) => void
- addBlock: (routeID: string) => void
+ addBlock: (routeID: string, type: BlockType) => void
changeRoute: (id: string, route: Partial<Omit<Route, 'id'>>) => void
deleteRoute: (id: string) => void
addRoute: () => void
@@ -49,8 +52,9 @@ export interface State extends SerializedState {
changeEnv: (pair: { key: string, value: string }, idx: number) => void
}
-function getDefaultBlock(): Block {
+function createBlock(type: BlockType): Block {
return {
+ type,
prompt: '',
id: nanoid(),
}
@@ -58,7 +62,7 @@ function getDefaultBlock(): Block {
function getDefaultRoute(): Route {
return {
- blocks: [getDefaultBlock()],
+ blocks: [createBlock('RequestBody')],
method: Method.POST,
route: '/',
id: nanoid(),
@@ -128,7 +132,7 @@ export function createStore(project: projects, client?: SupabaseClient<Database>
addBlock: (routeID) => set(state => {
const idx = state.routes.findIndex(r => r.id === routeID)
if (idx !== -1) {
- state.routes[idx].blocks.push(getDefaultBlock())
+ state.routes[idx].blocks.push(createBlock('Basic'))
}
}),
setEnvs: (envs) => set(state => {