Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Improve editor UX

Tomas Valenta9242f57129d1
diff --git a/components/AuthForm.tsx b/components/AuthForm.tsx
index 2546238f3..22de838fb 100644
--- a/components/AuthForm.tsx
+++ b/components/AuthForm.tsx
@@ -114,7 +114,7 @@ function AuthForm({ authType }: Props) {
items-center
space-y-8
self-start
- rounded-md
+ rounded-lg
border
border-slate-200
bg-white
@@ -141,7 +141,7 @@ function AuthForm({ authType }: Props) {
'w-full',
'px-2.5',
'py-2',
- 'rounded-md',
+ 'rounded-lg',
'border',
'border-slate-200',
{
@@ -168,7 +168,7 @@ function AuthForm({ authType }: Props) {
className={clsx(
'px-2.5',
'py-2',
- 'rounded-md',
+ 'rounded-lg',
'border',
'flex',
'min-w-0',
diff --git a/components/Button.tsx b/components/Button.tsx
index ecae216af..982d8f475 100644
--- a/components/Button.tsx
+++ b/components/Button.tsx
@@ -43,7 +43,7 @@ function Button({
'items-center',
'justify-center',
'transition-all',
- 'rounded-md',
+ 'rounded-lg',
'border',
'space-x-1.5',
'py-1.5',
diff --git a/components/Editor/AddBlockButton.tsx b/components/Editor/AddBlockButton.tsx
new file mode 100644
index 000000000..671ec3294
--- /dev/null
+++ b/components/Editor/AddBlockButton.tsx
@@ -0,0 +1,34 @@
+import clsx from 'clsx'
+import { Plus } from 'lucide-react'
+import { nanoid } from 'nanoid'
+
+import { Block } from 'state/store'
+
+export interface Props {
+ addBlock: (block: Block) => void
+}
+
+export default function AddBlockButton({ addBlock }: Props) {
+ return (
+ <button
+ onMouseDown={() => addBlock({
+ prompt: '',
+ id: nanoid(),
+ })}
+ type="submit"
+ className={clsx(
+ 'items-center',
+ 'justify-center',
+ 'transition-all',
+ 'rounded-full',
+ 'bg-green-800/60',
+ 'hover:bg-green-800',
+ 'text-white',
+ 'py-1.5',
+ 'px-1.5',
+ )}
+ >
+ <Plus size="18px" />
+ </button>
+ )
+}
diff --git a/components/Editor/BodyBlock.tsx b/components/Editor/BlockEditor.tsx
similarity index 89%
rename from components/Editor/BodyBlock.tsx
rename to components/Editor/BlockEditor.tsx
index 46ee9bb6b..bd2f8eb4e 100644
--- a/components/Editor/BodyBlock.tsx
+++ b/components/Editor/BlockEditor.tsx
@@ -1,6 +1,6 @@
import clsx from 'clsx'
import { Wrench } from 'lucide-react'
-import { useState, useEffect, MouseEvent } from 'react'
+import { useState, useEffect, MouseEvent, useRef } from 'react'
import Text from 'components/typography/Text'
@@ -10,10 +10,17 @@ export interface Props {
onChange: (block: Block) => void
onDelete: () => void
block: Block
+ isLast?: boolean
}
-export default function BodyBlock({ onChange, onDelete, block }: Props) {
+export default function BlockEditor({
+ onChange,
+ onDelete,
+ block,
+ isLast,
+}: Props) {
const [confirmDelete, setConfirmDelete] = useState(false)
+ const ref = useRef<HTMLTextAreaElement>(null)
useEffect(
function expireConfirm() {
@@ -27,6 +34,12 @@ export default function BodyBlock({ onChange, onDelete, block }: Props) {
[confirmDelete],
)
+ useEffect(function autofocus() {
+ if (isLast) {
+ ref.current?.focus()
+ }
+ }, [isLast])
+
function handleDelete(
e: MouseEvent<HTMLButtonElement, globalThis.MouseEvent>,
) {
@@ -49,7 +62,6 @@ export default function BodyBlock({ onChange, onDelete, block }: Props) {
<div className="
flex
relative
- items-center
w-[65ch]
">
<div className="
@@ -93,6 +105,7 @@ export default function BodyBlock({ onChange, onDelete, block }: Props) {
placeholder="Prompt"
value={block.prompt}
onChange={e => onChange({ ...block, prompt: e.target.value })}
+ ref={ref}
className={clsx(
'w-full',
'px-4',
@@ -100,8 +113,7 @@ export default function BodyBlock({ onChange, onDelete, block }: Props) {
'rounded-lg',
'transition-colors',
'border',
- 'hover:border-green-800/40',
- 'border-slate-200',
+ 'border-green-800/40',
'bg-white',
'outline-none',
'focus:border-green-800',
diff --git a/components/Editor/DeployButton.tsx b/components/Editor/DeployButton.tsx
deleted file mode 100644
index 0d3e12e7a..000000000
--- a/components/Editor/DeployButton.tsx
+++ /dev/null
@@ -1,45 +0,0 @@
-import useSWRMutation from 'swr/mutation'
-import { shallow } from 'zustand/shallow'
-
-import Button from '../Button'
-import { Block, State } from 'state/store'
-import { getStoreContext } from 'state/StoreProvider'
-
-const selector = (state: State) => ({
- blocks: state.blocks,
-})
-
-async function handlePostGenerate(url: string, { arg }: { arg: { blocks: Block[] } }) {
- return await fetch(url, {
- method: 'POST',
- body: JSON.stringify(arg),
-
- headers: {
- 'Content-Type': 'application/json',
- },
- }).then(r => r.json())
-}
-
-export default function DeployButton() {
- const useStore = getStoreContext()
-
- const {
- blocks,
- } = useStore(selector, shallow)
-
- const { trigger: generate } = useSWRMutation('/api/generate', handlePostGenerate)
-
- async function deploy() {
- generate({
- blocks,
- })
- }
-
- return (
- <Button
- text="Deploy"
- onClick={deploy}
- variant={Button.variant.Full}
- />
- )
-}
diff --git a/components/Editor/HeadBlock.tsx b/components/Editor/HeadBlock.tsx
deleted file mode 100644
index 0a95d0f23..000000000
--- a/components/Editor/HeadBlock.tsx
+++ /dev/null
@@ -1,66 +0,0 @@
-import clsx from 'clsx'
-import { useEffect, useRef, useState } from 'react'
-import { toast } from 'sonner'
-
-import { Block } from 'state/store'
-
-export interface Props {
- onConfirm: (block: Block) => void
-}
-
-export default function HeadBlock({ onConfirm }: Props) {
- const ref = useRef<HTMLInputElement>(null)
- const [prompt, setPrompt] = useState<string>('')
-
- useEffect(function autofocus() {
- ref.current?.focus()
- }, [])
-
- return (
- <div className="flex flex-col flex-1 min-w-[500px]">
- <form
- autoComplete="on"
- onSubmit={e => {
- e.preventDefault()
- if (!prompt) {
- toast('Prompt must not be empty')
- ref.current?.focus()
- return
- }
- onConfirm({
- prompt,
- })
- setPrompt('')
- }}
- >
- <input
- autoCapitalize="off"
- autoComplete="off"
- autoCorrect="off"
- name="block"
- placeholder="What to do..."
- type="text"
- ref={ref}
- value={prompt}
- onChange={e => setPrompt(e.target.value)}
- className={clsx(
- 'w-full',
- 'px-4',
- 'py-2',
- 'hover:border-green-800/40',
- 'rounded-full',
- 'transition-all',
- 'border',
- 'border-slate-200',
- 'bg-white',
- 'outline-none',
- 'focus:border-green-800',
- 'text-sm',
- 'placeholder:text-slate-300',
- )}
- required
- />
- </form>
- </div>
- )
-}
diff --git a/components/Editor/index.tsx b/components/Editor/index.tsx
index ad24482fd..a446179ab 100644
--- a/components/Editor/index.tsx
+++ b/components/Editor/index.tsx
@@ -1,26 +1,34 @@
import { shallow } from 'zustand/shallow'
import useSWRMutation from 'swr/mutation'
+import { Fragment } from 'react'
-import { State, Block } from 'state/store'
+import { State, Block, methods, Method } from 'state/store'
import { getStoreContext } from 'state/StoreProvider'
+import Select from 'components/Select'
import Text from '../typography/Text'
-import HeadBlock from './HeadBlock'
-import BodyBlock from './BodyBlock'
+import BlockEditor from './BlockEditor'
import Button from '../Button'
import ConnectionLine from './ConnectionLine'
-import { Fragment } from 'react'
+import AddBlockButton from './AddBlockButton'
const selector = (state: State) => ({
blocks: state.blocks,
+ method: state.method,
addBlock: state.addBlock,
removeBlock: state.deleteBlock,
changeBlock: state.changeBlock,
+ changeMethod: state.changeMethod,
})
export interface Props { }
-async function handlePostGenerate(url: string, { arg }: { arg: { blocks: Block[] } }) {
+async function handlePostGenerate(url: string, { arg }: {
+ arg: {
+ blocks: Block[],
+ method: Method,
+ }
+}) {
return await fetch(url, {
method: 'POST',
body: JSON.stringify(arg),
@@ -37,8 +45,10 @@ export default function Editor({ }: Props) {
const {
addBlock,
blocks,
+ method,
changeBlock,
removeBlock,
+ changeMethod,
} = useStore(selector, shallow)
const { trigger: generate } = useSWRMutation('/api/generate', handlePostGenerate)
@@ -46,6 +56,7 @@ export default function Editor({ }: Props) {
async function deploy() {
const response = await generate({
blocks,
+ method,
})
console.log(response)
}
@@ -58,37 +69,47 @@ export default function Editor({ }: Props) {
flex-col
items-center
overflow-auto
+ scroller
relative
">
- <div className="flex">
+ <div className="flex items-center space-x-2">
<Text
- text="Incoming Request"
+ text="Incoming"
+ className='font-bold'
+ />
+ <Select
+ direction="left"
+ selectedValue={{ key: method, title: method }}
+ values={methods.map(m => ({ key: m, title: m }))}
+ onChange={m => changeMethod(m.title as Method)}
+ />
+ <Text
+ text="Request"
className='font-bold'
/>
</div>
- <ConnectionLine className='h-4' />
<div className="
flex
flex-col
items-center
transition-all
">
- {
- blocks.map((b, i, a) =>
- <Fragment
- key={i.toString() + ' ' + a.length}
- >
- <BodyBlock
- block={b}
- onDelete={() => removeBlock(i)}
- onChange={(b) => changeBlock(i, b)}
- />
- <ConnectionLine className='h-4' />
- </Fragment>
- )
- }
+ {blocks.map((b, i, a) =>
+ <Fragment
+ key={b.id}
+ >
+ <ConnectionLine className='h-4' />
+ <BlockEditor
+ block={b}
+ onDelete={() => removeBlock(i)}
+ onChange={(b) => changeBlock(i, b)}
+ isLast={i === a.length - 1}
+ />
+ </Fragment>
+ )}
</div>
- <HeadBlock onConfirm={addBlock} />
+ <ConnectionLine className='h-4' />
+ <AddBlockButton addBlock={addBlock} />
<div className="absolute right-4 top-4">
<Button
text="Deploy"
diff --git a/components/Select/SelectValue.tsx b/components/Select/SelectValue.tsx
new file mode 100644
index 000000000..3ccaa4cde
--- /dev/null
+++ b/components/Select/SelectValue.tsx
@@ -0,0 +1,64 @@
+import { Check as CheckIcon } from 'lucide-react'
+
+import Text from 'components/typography/Text'
+import clsx from 'clsx'
+
+export interface Value {
+ title: string
+ key: string
+}
+
+export interface Props {
+ value: Value
+ onSelect: (v: Value) => void
+ isSelected: boolean
+}
+
+function SelectValue({
+ value,
+ onSelect,
+ isSelected,
+}: Props) {
+ return (
+ <div
+ className={clsx(
+ `
+ px-2
+ cursor-pointer
+ border-transparent
+ hover:bg-green-200
+ flex
+ items-center
+ rounded
+ justify-start
+ space-x-2
+ py-1
+ `,
+ { 'text-green-500': isSelected },
+ )}
+ onClick={() => onSelect(value)}
+ >
+ <div className={clsx(
+ `w-[14px]
+ h-[16px]
+ flex
+ items-center
+ justify-center
+ `,
+ )}>
+ {isSelected &&
+ <CheckIcon
+ className="self-center"
+ size={14}
+ />
+ }
+ </div>
+ <Text
+ className="whitespace-nowrap font-mono"
+ text={value.title}
+ />
+ </div>
+ )
+}
+
+export default SelectValue
diff --git a/components/Select/index.tsx b/components/Select/index.tsx
new file mode 100644
index 000000000..c23e55e06
--- /dev/null
+++ b/components/Select/index.tsx
@@ -0,0 +1,121 @@
+import {
+ useCallback,
+ useRef,
+ useState,
+} from 'react'
+import { useOnClickOutside } from 'usehooks-ts'
+import clsx from 'clsx'
+import { ChevronDown as ChevronDownIcon } from 'lucide-react'
+
+import Text from 'components/typography/Text'
+
+import SelectValue, { Value } from './SelectValue'
+
+export interface Props {
+ values: Value[]
+ selectedValue: Value
+ onChange: (v: Value) => void
+ // Where to align the modal
+ direction: 'left' | 'right'
+}
+
+function Select({
+ values,
+ selectedValue,
+ onChange,
+ direction,
+}: Props) {
+ const ref = useRef<HTMLDivElement>(null)
+ const [isOpened, setIsOpened] = useState(false)
+
+ const handleClickOutside = useCallback(() => {
+ setIsOpened(false)
+ }, [])
+
+ const handleValueSelect = useCallback((val: Value) => {
+ onChange(val)
+ setIsOpened(false)
+ }, [onChange])
+
+ useOnClickOutside(ref, handleClickOutside)
+
+ return (
+ <div className="
+ flex
+ items-center
+ ">
+ <div
+ className="
+ relative
+ select-none
+ "
+ ref={ref}
+ >
+ <div
+ className={clsx(
+ `transition-all
+ cursor-pointer
+ flex
+ hover:border-green-600
+ hover:text-green-600
+ border-b-4
+ items-center
+ justify-between
+ space-x-1`,
+ {
+ 'text-green-600 border-green-600': isOpened,
+ 'text-green-500 border-green-500': !isOpened,
+ }
+ )}
+ onClick={() => setIsOpened(val => !val)}
+ >
+ <Text
+ className="font-mono"
+ text={selectedValue.title}
+ />
+ <ChevronDownIcon
+ className="self-center"
+ size={14}
+ />
+ </div>
+
+ {/* Modal */}
+ {isOpened && (
+ <div className={clsx(
+ {
+ 'left-0': direction === 'left',
+ 'right-0': direction === 'right',
+ },
+ `z-[999]
+ top-[28px]
+
+ absolute
+ p-1
+ mt-0.5
+ border border-green-500
+ rounded
+ bg-white
+ space-y-2`
+ )}>
+ <div className="
+ flex
+ flex-col
+ space-y-1
+ ">
+ {values.map(val => (
+ <SelectValue
+ isSelected={val.key === selectedValue.key}
+ key={val.key}
+ value={val}
+ onSelect={handleValueSelect}
+ />
+ ))}
+ </div>
+ </div>
+ )}
+ </div>
+ </div>
+ )
+}
+
+export default Select
\ No newline at end of file
diff --git a/package.json b/package.json
index f716274db..1e4c1a333 100644
--- a/package.json
+++ b/package.json
@@ -24,16 +24,16 @@
"clsx": "^1.2.1",
"immer": "^9.0.19",
"lucide-react": "^0.122.0",
+ "nanoid": "^4.0.1",
"next": "13.2.3",
"next-superjson-plugin": "^0.5.6",
"randomcolor": "^0.6.2",
"react": "18.2.0",
"react-dom": "18.2.0",
- "server-only": "^0.0.1",
"sonner": "^0.1.6",
"superjson": "^1.12.2",
"swr": "^2.0.4",
- "tailwind-merge": "^1.10.0",
+ "usehooks-ts": "^2.9.1",
"zustand": "^4.3.5"
},
"devDependencies": {
diff --git a/pages/sign.tsx b/pages/sign.tsx
index 6df23be58..3b70c6eaa 100644
--- a/pages/sign.tsx
+++ b/pages/sign.tsx
@@ -32,7 +32,7 @@ function SignIn() {
flex-col
items-center
space-y-4
- rounded-md
+ rounded-lg
"
>
{!isCreatingNewAccount && (
diff --git a/state/store.ts b/state/store.ts
index 7b2b9c9d2..adba66a14 100644
--- a/state/store.ts
+++ b/state/store.ts
@@ -1,11 +1,12 @@
import { create } from 'zustand'
import { immer } from 'zustand/middleware/immer'
-import { createJSONStorage, persist } from 'zustand/middleware'
+import { persist } from 'zustand/middleware'
import { SupabaseClient } from '@supabase/supabase-js'
import { api_deployments } from '@prisma/client'
import { apiDeploymentsTable } from 'db/tables'
import { Database } from 'db/supabase'
+import { nanoid } from 'nanoid'
export interface Tool {
name: string
@@ -13,19 +14,46 @@ export interface Tool {
export interface Block {
tools?: Tool[]
+ id: string
prompt: string
}
+export enum Method {
+ POST = 'POST',
+ GET = 'GET',
+ PUT = 'PUT',
+ DELETE = 'DELETE',
+ PATCH = 'PATCH',
+}
+
+export const methods = Object.keys(Method).filter((item) => {
+ return isNaN(Number(item))
+});
+
export interface State {
blocks: Block[]
+ method: Method
changeBlock: (index: number, block: Partial<Block>) => void
deleteBlock: (index: number) => void
addBlock: (block: Block) => void
+ changeMethod: (method: Method) => void
+}
+
+const defaultBlock: Block = { prompt: '', id: nanoid() }
+const defaultState: Pick<State, 'blocks' | 'method'> = {
+ blocks: [defaultBlock],
+ method: Method.POST,
}
export function createStore(deployment?: api_deployments, client?: SupabaseClient<Database>) {
+ const state = (deployment?.data as any).state as Pick<State, 'blocks' | 'method'> || defaultState
+
+ if (state.blocks.length === 0) {
+ state.blocks.push(defaultBlock)
+ }
+
const immerStore = immer<State>((set) => ({
- blocks: (deployment?.data as any)?.state?.blocks || [],
+ ...state,
changeBlock: (index, block) =>
set(state => {
state.blocks[index] = { ...state.blocks[index], ...block }
@@ -38,18 +66,19 @@ export function createStore(deployment?: api_deployments, client?: SupabaseClien
set(state => {
state.blocks.push(block)
}),
+ changeMethod: (method) =>
+ set(state => {
+ state.method = method
+ }),
}))
const persistent = persist(immerStore, {
name: 'supabase-storage',
partialize: (state) => state,
- storage: client && deployment ? createJSONStorage(() => ({
+ storage: client && deployment ? {
getItem: async (name) => {
- const res = await client.from(apiDeploymentsTable).select('data').eq('id', deployment.id).single()
- if (res.error) {
- throw res.error
- }
- return JSON.stringify(res.data.data)
+ // We retrieve the data on the server
+ return null
},
removeItem: async () => {
const res = await client.from(apiDeploymentsTable).update({ data: {} }).eq('id', deployment.id).single()
@@ -58,12 +87,12 @@ export function createStore(deployment?: api_deployments, client?: SupabaseClien
}
},
setItem: async (name, value) => {
- const res = await client.from(apiDeploymentsTable).update({ data: JSON.parse(value) }).eq('id', deployment.id).single()
+ const res = await client.from(apiDeploymentsTable).update({ data: value as any }).eq('id', deployment.id).single()
if (res.error) {
throw res.error
}
},
- })) : undefined,
+ } : undefined,
})
const useStore = create<State, [["zustand/persist", unknown], ["zustand/immer", never]]>(persistent)
diff --git a/styles/global.css b/styles/global.css
index a29c78709..b1e673ada 100644
--- a/styles/global.css
+++ b/styles/global.css
@@ -24,4 +24,10 @@
#__next {
@apply flex h-inherit flex-col items-center;
}
+
+ .scroller {
+ scrollbar-color: #cbd5e1 transparent;
+ scrollbar-width: thin;
+ scrollbar-gutter: stable both-edges;
+ }
}
diff --git a/yarn.lock b/yarn.lock
index 6a456f161..8ac7ad3b6 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5867,6 +5867,11 @@ nanoid@^3.3.4:
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.4.tgz#730b67e3cd09e2deacf03c027c81c9d9dbc5e8ab"
integrity sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==
+nanoid@^4.0.1:
+ version "4.0.1"
+ resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-4.0.1.tgz#398d7ccfdbf9faf2231b2ca7e8fff5dbca6a509b"
+ integrity sha512-udKGtCCUafD3nQtJg9wBhRP3KMbPglUsgV5JVsXhvyBs/oefqb4sqMEhKBBgqZncYowu58p1prsZQBYvAj/Gww==
+
natural-compare@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
@@ -7196,11 +7201,6 @@ [email protected]:
parseurl "~1.3.3"
send "0.18.0"
-server-only@^0.0.1:
- version "0.0.1"
- resolved "https://registry.yarnpkg.com/server-only/-/server-only-0.0.1.tgz#0f366bb6afb618c37c9255a314535dc412cd1c9e"
- integrity sha512-qepMx2JxAa5jjfzxG79yPPq+8BuFToHd1hm7kI+Z4zAq1ftQiP7HcxMhDDItrbtwVeLg/cY2JnKnrcFkmiswNA==
-
set-blocking@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"
@@ -7530,11 +7530,6 @@ synckit@^0.8.4:
"@pkgr/utils" "^2.3.1"
tslib "^2.5.0"
-tailwind-merge@^1.10.0:
- version "1.10.0"
- resolved "https://registry.yarnpkg.com/tailwind-merge/-/tailwind-merge-1.10.0.tgz#535541c7373d8a31afa81a4267e2ab9b4afa9429"
- integrity sha512-WFnDXSS4kFTZwjKg5/oZSGzBRU/l+qcbv5NVTzLUQvJ9yovDAP05h0F2+ZFW0Lw9EcgRoc2AfURUdZvnEFrXKg==
-
tailwindcss@^3.2.7:
version "3.2.7"
resolved "https://registry.yarnpkg.com/tailwindcss/-/tailwindcss-3.2.7.tgz#5936dd08c250b05180f0944500c01dce19188c07"
@@ -7975,6 +7970,11 @@ [email protected], use-sync-external-store@^1.2.0:
resolved "https://registry.yarnpkg.com/use-sync-external-store/-/use-sync-external-store-1.2.0.tgz#7dbefd6ef3fe4e767a0cf5d7287aacfb5846928a"
integrity sha512-eEgnFxGQ1Ife9bzYs6VLi8/4X6CObHMw9Qr9tPY43iKwsPw8xE8+EFsf/2cFZ5S3esXgpWgtSCtLNS41F+sKPA==
+usehooks-ts@^2.9.1:
+ version "2.9.1"
+ resolved "https://registry.yarnpkg.com/usehooks-ts/-/usehooks-ts-2.9.1.tgz#953d3284851ffd097432379e271ce046a8180b37"
+ integrity sha512-2FAuSIGHlY+apM9FVlj8/oNhd+1y+Uwv5QNkMQz1oSfdHk4PXo1qoCw9I5M7j0vpH8CSWFJwXbVPeYDjLCx9PA==
+
utf-8-validate@^5.0.2:
version "5.0.10"
resolved "https://registry.yarnpkg.com/utf-8-validate/-/utf-8-validate-5.0.10.tgz#d7d10ea39318171ca982718b6b96a8d2442571a2"