Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Fix logs rerendering

Tomas Valentac04b171408a3
diff --git a/components/Editor/RightSidebar/DeployButton.tsx b/components/Editor/RightSidebar/DeployButton.tsx
index ae361a25c..317d4cd5c 100644
--- a/components/Editor/RightSidebar/DeployButton.tsx
+++ b/components/Editor/RightSidebar/DeployButton.tsx
@@ -1,5 +1,5 @@
import { deployment_state } from '@prisma/client'
-import { ReactNode } from 'react'
+import { ReactNode, memo } from 'react'
import Button from 'components/Button'
import Spinner from 'components/Spinner'
@@ -64,4 +64,4 @@ function DeployButton({
)
}
-export default DeployButton
+export default memo(DeployButton)
diff --git a/components/Editor/RightSidebar/Envs.tsx b/components/Editor/RightSidebar/Envs.tsx
new file mode 100644
index 000000000..19b6ff127
--- /dev/null
+++ b/components/Editor/RightSidebar/Envs.tsx
@@ -0,0 +1,110 @@
+import { memo, useCallback } from 'react'
+
+import Button from 'components/Button'
+import Text from 'components/Text'
+import { useStateStore } from 'state/StoreProvider'
+
+export interface Props {
+
+}
+
+function Envs({ }: Props) {
+ const store = useStateStore()
+
+ const envs = store.use.envs()
+ const setEnvs = store.use.setEnvs()
+ const changeEnv = store.use.changeEnv()
+
+ const handleEnvKeyChange = useCallback((event: any, idx: number) => {
+ changeEnv({ key: event.target.value.trim(), value: envs[idx].value }, idx)
+ }, [envs, changeEnv])
+
+ const handleEnvValueChange = useCallback((event: any, idx: number) => {
+ changeEnv({ key: envs[idx].key, value: event.target.value.trim() }, idx)
+ }, [envs, changeEnv])
+
+ const addNewEnv = useCallback(() => {
+ setEnvs([...envs, { key: '', value: '' }])
+ }, [envs, setEnvs])
+
+ const deleteEnv = useCallback((delIdx: number) => {
+ const newEnvs = [...envs.filter((_, idx) => idx !== delIdx)]
+ setEnvs(newEnvs)
+ }, [envs, setEnvs])
+
+
+ return (
+ <div className="
+ max-w-full
+ flex
+ flex-col
+ border-b
+ p-4
+ space-y-4
+ ">
+ <Text
+ text="Envs"
+ size={Text.size.S2}
+ className="
+ uppercase
+ text-slate-400
+ font-semibold
+ "
+ />
+ {envs.map((env, idx) =>
+ <div
+ key={idx}
+ className="
+ flex
+ items-center
+ justify-evenly
+ space-x-2
+ "
+ >
+ <input
+ className="
+ flex-1
+ p-1.5
+ text-xs
+ font-mono
+ rounded
+ border
+ outline-none
+ focus:border-green-600
+ "
+ placeholder="KEY"
+ value={env.key}
+ onChange={event => handleEnvKeyChange(event, idx)}
+ />
+ <input
+ className="
+ flex-1
+ p-1.5
+ text-xs
+ font-mono
+ rounded
+ border
+ outline-none
+ focus:border-green-600
+ "
+ placeholder="VALUE"
+ value={env.value}
+ onChange={event => handleEnvValueChange(event, idx)}
+ />
+ {envs.length > 1 &&
+ <Button
+ text="Delete"
+ onClick={() => deleteEnv(idx)}
+ />
+ }
+ </div>
+ )}
+ <Button
+ text="Add another"
+ onClick={addNewEnv}
+ />
+ </div>
+ )
+}
+
+export default memo(Envs)
diff --git a/components/Editor/RightSidebar/Logs/LogEntry.tsx b/components/Editor/RightSidebar/Logs/LogEntry.tsx
index dc817e63e..a6da22bb1 100644
--- a/components/Editor/RightSidebar/Logs/LogEntry.tsx
+++ b/components/Editor/RightSidebar/Logs/LogEntry.tsx
@@ -6,8 +6,6 @@ import {
Loader,
} from 'lucide-react'
import ReactMarkdown from 'react-markdown'
-import hljs from 'highlight.js'
-import { useEffect } from 'react'
import { Log } from 'db/types'
@@ -30,11 +28,6 @@ export interface Props {
}
function LogEntry({ log }: Props) {
- useEffect(function highlightCode() {
- if (log.type === 'tool' && log.name === 'RunJavaScriptCode') {
- hljs.highlightAll()
- }
- }, [log])
return (
<div className="
rounded-lg
diff --git a/components/Editor/RightSidebar/Logs/index.tsx b/components/Editor/RightSidebar/Logs/index.tsx
index 90aa399b2..ad7f6f938 100644
--- a/components/Editor/RightSidebar/Logs/index.tsx
+++ b/components/Editor/RightSidebar/Logs/index.tsx
@@ -1,6 +1,8 @@
import {
Fragment,
useEffect,
+ useLayoutEffect,
+ useRef,
useState,
} from 'react'
import hljs from 'highlight.js'
@@ -10,9 +12,10 @@ import Text from 'components/Text'
import { useTabs } from 'components/Tabs/useTabs'
import Tabs from 'components/Tabs'
import { Log } from 'db/types'
-import LogEntry from './LogEntry'
import ConnectionLine from 'components/Editor/ConnectionLine'
+import LogEntry from './LogEntry'
+
export interface Props {
logs?: Log[]
logsRaw?: string
@@ -35,21 +38,27 @@ function Logs({
},
]
})
-
+ const ref = useRef<HTMLDivElement>(null)
const tabsCss = useTabs(tabsProps)
- useEffect(function highlightCode() {
- if (selectedTab === 1) {
- hljs.highlightAll()
- }
- }, [logs, selectedTab])
+ // useEffect(function highlightCode() {
+ // if (selectedTab === 1) {
+ // hljs.highlightAll()
+ // }
+ // }, [logs, selectedTab])
+
+
+ useEffect(function scrollLogs() {
+ if (!ref.current) return
+ ref.current.scrollIntoView({ behavior: 'smooth' })
+ }, [logs])
return (
<div className="
max-w-full
flex
flex-col
- overflow-auto
+ overflow-hidden
">
<div className="
flex
@@ -80,14 +89,17 @@ function Logs({
</div>
<div
+ ref={ref}
className="
flex-1
overflow-auto
text-xs
tracking-wide
font-sans
+ scroller
whitespace-pre-wrap
- p-4
+ py-4
+ px-2
">
{selectedTab === 0 && logs?.map((l, i, a) =>
<Fragment key={i}>
@@ -110,6 +122,7 @@ function Logs({
}
</>
)}
+ <div ref={ref} />
</div>
</div>
)
diff --git a/components/Editor/RightSidebar/index.tsx b/components/Editor/RightSidebar/index.tsx
index 3db9407cf..c294c108b 100644
--- a/components/Editor/RightSidebar/index.tsx
+++ b/components/Editor/RightSidebar/index.tsx
@@ -1,60 +1,52 @@
-import {
- useCallback,
-} from 'react'
-import { deployment_state } from '@prisma/client'
+import { projects } from '@prisma/client'
import Link from 'next/link'
+import { useEffect, useState } from 'react'
import { Log } from 'db/types'
-import { useStateStore } from 'state/StoreProvider'
import Sidebar from 'components/Sidebar'
import Text from 'components/Text'
-import Button from 'components/Button'
+import { Route } from 'state/store'
+import { useLatestDeployment } from 'hooks/useLatestDeployment'
import DeployButton from './DeployButton'
import Logs from './Logs'
+import Envs from './Envs'
export interface Props {
- logs?: Log[]
- logsRaw?: string
deploy: () => void
- deployedURL?: string | null
- deployStatus?: deployment_state | null
- isInitializingDeploy?: boolean
isDeployRequestRunning?: boolean
+ setDeployURL: (url: string | undefined) => void
+
+ project: projects
+ route?: Route
}
function RightSidebar({
- logs,
- logsRaw,
isDeployRequestRunning,
deploy,
- deployedURL,
- deployStatus,
- isInitializingDeploy,
+ project,
+ route,
+ setDeployURL,
}: Props) {
- const store = useStateStore()
-
- const envs = store.use.envs()
- const setEnvs = store.use.setEnvs()
- const changeEnv = store.use.changeEnv()
+ const deployment = useLatestDeployment(project, route)
+ const logs = deployment?.logs as Log[] | undefined
+ const logsRaw = deployment?.logs_raw as string | undefined
- const handleEnvKeyChange = useCallback((event: any, idx: number) => {
- changeEnv({ key: event.target.value.trim(), value: envs[idx].value }, idx)
- }, [envs, changeEnv])
+ useEffect(function updateURL() {
+ setDeployURL(deployment?.url ? deployment.url : undefined)
+ }, [deployment?.url, setDeployURL])
- const handleEnvValueChange = useCallback((event: any, idx: number) => {
- changeEnv({ key: envs[idx].key, value: event.target.value.trim() }, idx)
- }, [envs, changeEnv])
+ const [isInitializingDeploy, setIsInitializingDeploy] = useState(false)
- const addNewEnv = useCallback(() => {
- setEnvs([...envs, { key: '', value: '' }])
- }, [envs, setEnvs])
-
- const deleteEnv = useCallback((delIdx: number) => {
- const newEnvs = [...envs.filter((_, idx) => idx !== delIdx)]
- setEnvs(newEnvs)
- }, [envs, setEnvs])
+ useEffect(function handleDeployState() {
+ if (!deployment) return
+ setIsInitializingDeploy(false)
+ }, [deployment])
+ useEffect(function handleDeployState() {
+ if (!isDeployRequestRunning) return
+ setIsInitializingDeploy(true)
+ }, [isDeployRequestRunning])
return (
<Sidebar
@@ -96,23 +88,23 @@ function RightSidebar({
deploy={deploy}
isDeployRequestRunning={isDeployRequestRunning}
isInitializingDeploy={isInitializingDeploy}
- deployStatus={deployStatus}
+ deployStatus={deployment?.state}
/>
</div>
- {deployedURL &&
+ {deployment?.url &&
<Link
- href={deployedURL}
+ href={deployment.url}
className="underline"
target="_blank"
rel="noopener noreferrer"
>
<Text
size={Text.size.S3}
- text={deployedURL.substring('https://'.length)}
+ text={deployment.url.substring('https://'.length)}
/>
</Link>
}
- {!deployedURL &&
+ {!deployment?.url &&
<Text
text="No deployment URL found"
size={Text.size.S3}
@@ -120,76 +112,7 @@ function RightSidebar({
/>
}
</div>
- <div className="
- max-w-full
- flex
- flex-col
- border-b
- p-4
- space-y-4
- ">
- <Text
- text="Envs"
- size={Text.size.S2}
- className="
- uppercase
- text-slate-400
- font-semibold
- "
- />
- {envs.map((env, idx) =>
- <div
- key={idx}
- className="
- flex
- items-center
- justify-evenly
- space-x-2
- "
- >
- <input
- className="
- flex-1
- p-1.5
- text-xs
- font-mono
- rounded
- border
- outline-none
- focus:border-green-600
- "
- placeholder="KEY"
- value={env.key}
- onChange={event => handleEnvKeyChange(event, idx)}
- />
- <input
- className="
- flex-1
- p-1.5
- text-xs
- font-mono
- rounded
- border
- outline-none
- focus:border-green-600
- "
- placeholder="VALUE"
- value={env.value}
- onChange={event => handleEnvValueChange(event, idx)}
- />
- {envs.length > 1 &&
- <Button
- text="Delete"
- onClick={() => deleteEnv(idx)}
- />
- }
- </div>
- )}
- <Button
- text="Add another"
- onClick={addNewEnv}
- />
- </div>
+ <Envs />
<Logs
logs={logs}
logsRaw={logsRaw}
diff --git a/components/Editor/index.tsx b/components/Editor/index.tsx
index 5fe995541..cbaf2b46e 100644
--- a/components/Editor/index.tsx
+++ b/components/Editor/index.tsx
@@ -1,7 +1,7 @@
import useSWRMutation from 'swr/mutation'
import { Fragment, useEffect, useState } from 'react'
import Hotkeys from 'react-hot-keys'
-import { projects } from '@prisma/client'
+import { deployment_state, projects } from '@prisma/client'
import { Route, methods, Method } from 'state/store'
import { useStateStore } from 'state/StoreProvider'
@@ -62,9 +62,9 @@ function Editor({ project }: Props) {
const addRoute = store.use.addRoute()
const envs = store.use.envs()
- const [isInitializingDeploy, setIsInitializingDeploy] = useState(false)
const [selectedRouteID, setSelectedRouteID] = useState(() => routes.length > 0 ? routes[0].id : undefined)
const selectedRoute = routes.find(s => s.id === selectedRouteID)
+ const [deploymentURL, setDeploymentURL] = useState<string>()
useEffect(function selectDefaultRoute() {
if (selectedRoute?.id || routes.length === 0) return
@@ -80,25 +80,14 @@ function Editor({ project }: Props) {
})
}
- const deployment = useLatestDeployment(project, selectedRoute)
- const logs = deployment?.logs as Log[] | undefined
- const logsRaw = deployment?.logs_raw as string | undefined
-
- useEffect(function resetInitializing() {
- if (!deployment) return
- setIsInitializingDeploy(false)
- }, [deployment])
-
const [focusedBlock, setFocusedBlock] = useState({ index: 0 })
const {
trigger: generate,
isMutating: isDeployRequestRunning,
- error,
} = useSWRMutation(`${apiHost}/generate`, handlePostGenerate)
async function deploy() {
if (!selectedRoute) return
- setIsInitializingDeploy(true)
await generate({
projectID: project.id,
route: selectedRoute,
@@ -209,9 +198,9 @@ function Editor({ project }: Props) {
setTimeout(() => setFocusedBlock({ index: selectedRoute.blocks.length }), 0)
}}
/>
- {deployment?.url &&
+ {deploymentURL &&
<Link
- href={deployment.url}
+ href={deploymentURL}
className="
mt-6
underline
@@ -219,20 +208,18 @@ function Editor({ project }: Props) {
target="_blank" rel="noopener noreferrer"
>
<Text
- text={deployment.url.substring('https://'.length)}
+ text={deploymentURL.substring('https://'.length)}
size={Text.size.S3}
/>
</Link>
}
</div>
<RightSidebar
- deployStatus={deployment?.state}
- isInitializingDeploy={isInitializingDeploy}
- logs={logs}
+ project={project}
+ route={selectedRoute}
isDeployRequestRunning={isDeployRequestRunning}
- logsRaw={logsRaw}
deploy={deploy}
- deployedURL={deployment?.url}
+ setDeployURL={setDeploymentURL}
/>
</>
}
diff --git a/hooks/useLatestDeployment.tsx b/hooks/useLatestDeployment.tsx
index e8cbb20f5..6cc8af467 100644
--- a/hooks/useLatestDeployment.tsx
+++ b/hooks/useLatestDeployment.tsx
@@ -26,7 +26,6 @@ export function useLatestDeployment(project: projects, route?: Route) {
.single()
if (deployment.error) return
- console.log(deployment.data)
setInitDeployment(deployment.data as unknown as deployments)
}())
}, [client, project.id, route?.id])
diff --git a/styles/global.css b/styles/global.css
index 9142c2d39..f1f271cf4 100644
--- a/styles/global.css
+++ b/styles/global.css
@@ -63,3 +63,16 @@ code::-webkit-scrollbar-thumb {
height: 6px;
@apply rounded-full;
}
+
+.scroller::-webkit-scrollbar {
+ /* display: none; */
+ scrollbar-track-color: transparent;
+ background-color: transparent;
+ width: 8px;
+ height: 8px;
+}
+
+.scroller::-webkit-scrollbar-thumb {
+ @apply bg-slate-300;
+ @apply rounded-full;
+}