Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Fix adding projects [WIP]; Add logs subscription [WIP]; Cleanup

Tomas Valenta75efbe7f61bb
diff --git a/components/AuthForm.tsx b/components/AuthForm.tsx
index eef07e06a..11c6e524e 100644
--- a/components/AuthForm.tsx
+++ b/components/AuthForm.tsx
@@ -1,12 +1,13 @@
import clsx from 'clsx'
import { useEffect, useRef, useState } from 'react'
+import { useSupabaseClient } from '@supabase/auth-helpers-react'
+import { useRouter } from 'next/router'
import Button from 'components/Button'
import Text from 'components/Text'
+import { Database } from 'db/supabase'
-import SpinnerIcon from './icons/Spinner'
-import { useSupabaseClient } from '@supabase/auth-helpers-react'
-import { useRouter } from 'next/router'
+import SpinnerIcon from './Spinner'
export enum AuthFormType {
SignIn,
@@ -20,7 +21,7 @@ export interface Props {
function AuthForm({ authType }: Props) {
const [isLoading, setIsLoading] = useState(false)
const [errMessage, setErrMessage] = useState('')
- const supabaseClient = useSupabaseClient()
+ const supabaseClient = useSupabaseClient<Database>()
const router = useRouter()
const emailRef = useRef<HTMLInputElement>(null)
diff --git a/components/Button.tsx b/components/Button.tsx
index 4a2187920..0123224ab 100644
--- a/components/Button.tsx
+++ b/components/Button.tsx
@@ -1,5 +1,8 @@
import clsx from 'clsx'
-import { MouseEvent, ReactNode } from 'react'
+import {
+ MouseEvent,
+ ReactNode,
+} from 'react'
import Text from 'components/Text'
diff --git a/components/Editor/BlockEditor.tsx b/components/Editor/BlockEditor.tsx
index 7afb4e022..d4a6cbb18 100644
--- a/components/Editor/BlockEditor.tsx
+++ b/components/Editor/BlockEditor.tsx
@@ -1,6 +1,11 @@
import clsx from 'clsx'
import { Wrench } from 'lucide-react'
-import { useState, useEffect, MouseEvent, useRef } from 'react'
+import {
+ useState,
+ useEffect,
+ MouseEvent,
+ useRef,
+} from 'react'
import TextareaAutosize from 'react-textarea-autosize'
import Text from 'components/Text'
diff --git a/components/Editor/Logs.tsx b/components/Editor/Logs.tsx
index 1d6b4d6bd..b85f5c45e 100644
--- a/components/Editor/Logs.tsx
+++ b/components/Editor/Logs.tsx
@@ -7,6 +7,6 @@ export interface Props {
export default function Logs({ logs }: Props) {
return (
-
+ <div></div>
)
}
diff --git a/components/Input.tsx b/components/Input.tsx
new file mode 100644
index 000000000..ccbbf1cb7
--- /dev/null
+++ b/components/Input.tsx
@@ -0,0 +1,83 @@
+import clsx from 'clsx'
+import {
+ useEffect,
+ useRef,
+} from 'react'
+
+import Text from 'components/Text'
+
+export interface Props {
+ value?: string
+ placeholder?: string
+ onChange: (value: string) => void
+ isTransparent?: boolean
+ autofocus?: boolean
+ label?: string
+ pattern?: string
+ title?: string
+ required?: boolean
+}
+
+export default function Input({
+ value,
+ required,
+ pattern,
+ title,
+ isTransparent,
+ autofocus,
+ onChange,
+ placeholder,
+ label,
+}: Props) {
+ const ref = useRef<HTMLInputElement>(null)
+
+ useEffect(
+ function focus() {
+ if (!ref.current) return
+ if (autofocus) {
+ ref.current.focus()
+ }
+ }, [autofocus])
+
+ return (
+ <div className="
+ flex
+ flex-col
+ space-y-1
+ flex-1
+ ">
+ {label &&
+ <Text text={label} size={Text.size.S3} />
+ }
+ <input
+ required={required}
+ autoCapitalize="off"
+ autoCorrect="off"
+ autoComplete="off"
+ spellCheck="false"
+ title={title}
+ value={value}
+ onChange={e => onChange(e.target.value)}
+ placeholder={placeholder}
+ ref={ref}
+ pattern={pattern}
+ type="text"
+ className={clsx(
+ { 'bg-transparent': isTransparent },
+ 'w-full',
+ 'px-2',
+ 'focus:bg-white',
+ 'py-1',
+ 'rounded',
+ 'border',
+ 'border-slate-200',
+ 'hover:border-slate-300',
+ 'outline-none',
+ 'focus:border-green-800',
+ 'text-sm',
+ 'placeholder:text-slate-300',
+ )}
+ />
+ </div>
+ )
+}
diff --git a/components/ItemList/Item.tsx b/components/ItemList/Item.tsx
index 78dd43959..718bc5ba2 100644
--- a/components/ItemList/Item.tsx
+++ b/components/ItemList/Item.tsx
@@ -1,8 +1,12 @@
import clsx from 'clsx'
import Link from 'next/link'
-import { MouseEvent, useEffect, useState } from 'react'
+import {
+ MouseEvent,
+ useEffect,
+ useState,
+} from 'react'
-import SpinnerIcon from 'components/icons/Spinner'
+import SpinnerIcon from 'components/Spinner'
import Text from 'components/Text'
export interface ItemSetup {
diff --git a/components/icons/Spinner.tsx b/components/Spinner.tsx
similarity index 100%
rename from components/icons/Spinner.tsx
rename to components/Spinner.tsx
diff --git a/db/supabase.ts b/db/supabase.ts
index 4f83066a4..ffd59fe55 100644
--- a/db/supabase.ts
+++ b/db/supabase.ts
@@ -38,21 +38,21 @@ export interface Database {
data: Json | null
id: string
name: string
- team_id: string | null
+ team_id: string
}
Insert: {
created_at?: string
data?: Json | null
id?: string
name?: string
- team_id?: string | null
+ team_id: string
}
Update: {
created_at?: string
data?: Json | null
id?: string
name?: string
- team_id?: string | null
+ team_id?: string
}
}
teams: {
diff --git a/hooks/useLatestDeployment.tsx b/hooks/useLatestDeployment.tsx
index 07e050598..dd2389bb6 100644
--- a/hooks/useLatestDeployment.tsx
+++ b/hooks/useLatestDeployment.tsx
@@ -1,30 +1,32 @@
import { deployments, projects } from '@prisma/client'
import { useSupabaseClient } from '@supabase/auth-helpers-react'
-import { deploymentsTable } from 'db/tables'
import { useState, useEffect } from 'react'
+import { deploymentsTable } from 'db/tables'
+import { Database } from 'db/supabase'
+
export function useLatestDeployment(project: projects) {
const [deployment, setDeployment] = useState<deployments>()
- const client = useSupabaseClient()
+ const client = useSupabaseClient<Database>()
- // useEffect(function subscribe() {
- // const sub = client.channel('any')
- // .on('postgres_changes',
- // {
- // event: 'UPDATE',
- // schema: 'public',
- // table: deploymentsTable
- // filter: `project_id=eq.${project.id}`,
- // }, payload => {
- // console.log('Change received!', payload)
- // setDeployment(payload.new)
- // })
- // .subscribe()
+ useEffect(function subscribe() {
+ const sub = client.channel('any')
+ .on('postgres_changes',
+ {
+ event: 'UPDATE',
+ schema: 'public',
+ table: deploymentsTable
+ filter: `project_id=eq.${project.id}`,
+ }, payload => {
+ console.log('Change received!', payload)
+ setDeployment(payload.new)
+ })
+ .subscribe()
- // return () => {
- // sub.unsubscribe()
- // }
- // }, [deployment, client])
+ return () => {
+ sub.unsubscribe()
+ }
+ }, [deployment, client])
return deployment
}
diff --git a/pages/[projectID].tsx b/pages/[projectID].tsx
index 716f0c75e..bef6e6ccd 100644
--- a/pages/[projectID].tsx
+++ b/pages/[projectID].tsx
@@ -6,6 +6,8 @@ import type { ParsedUrlQuery } from 'querystring'
import { prisma, projects } from 'db/prisma'
import Editor from 'components/Editor'
import { StoreProvider } from 'state/StoreProvider'
+import { Database } from 'db/supabase'
+import { inspect } from 'util'
interface PathProps extends ParsedUrlQuery {
projectID: string
@@ -56,6 +58,9 @@ export const getServerSideProps: GetServerSideProps<Props, PathProps> = async (c
},
})
+
+
+ console.log(inspect(user, { depth: null }))
const project = user
?.users_teams
.flatMap(t => t.teams.projects)
@@ -79,7 +84,7 @@ interface Props {
}
function EditorPage({ project }: Props) {
- const client = useSupabaseClient()
+ const client = useSupabaseClient<Database>()
return (
<StoreProvider
diff --git a/pages/index.tsx b/pages/index.tsx
index 8c9c20ee5..54185b335 100644
--- a/pages/index.tsx
+++ b/pages/index.tsx
@@ -9,6 +9,7 @@ import Text from 'components/Text'
import { prisma, projects } from 'db/prisma'
import Button from 'components/Button'
import { deploymentsTable, projectsTable } from 'db/tables'
+import { Database } from 'db/supabase'
export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
const supabase = createServerSupabaseClient(ctx)
@@ -87,7 +88,7 @@ interface Props {
function Home({ projects }: Props) {
const router = useRouter()
- const client = useSupabaseClient()
+ const client = useSupabaseClient<Database>()
async function handleDelete(id: string) {
await client.from(deploymentsTable).delete().eq('project_id', id)
@@ -124,7 +125,7 @@ function Home({ projects }: Props) {
icon={<Plus size="16px" />}
text="New"
variant={Button.variant.Full}
- onClick={() => router.push('/new/project')}
+ onClick={() => router.push('/new')}
/>
</div>
diff --git a/pages/new.tsx b/pages/new.tsx
new file mode 100644
index 000000000..0708fc97f
--- /dev/null
+++ b/pages/new.tsx
@@ -0,0 +1,149 @@
+import {
+ useState,
+} from 'react'
+import {
+ LayoutGrid,
+} from 'lucide-react'
+import { useRouter } from 'next/router'
+import { useSupabaseClient } from '@supabase/auth-helpers-react'
+import { nanoid } from 'nanoid'
+
+import Text from 'components/Text'
+import Input from 'components/Input'
+import Button from 'components/Button'
+import SpinnerIcon from 'components/Spinner'
+import { projectsTable } from 'db/tables'
+import { Database } from 'db/supabase'
+
+const re = /[^a-zA-Z0-9\-]/
+
+export default function NewProject() {
+ const [isCreating, setIsCreating] = useState(false)
+ const [projectID, setProjectID] = useState(() => nanoid())
+
+ const trimmedID = projectID.trim()
+ const validatedID = re.test(trimmedID)
+
+ const [err, setErr] = useState<string>()
+ const router = useRouter()
+
+
+
+ const client = useSupabaseClient<Database>()
+
+ async function handleCreateProject() {
+ setErr(undefined)
+ setIsCreating(true)
+
+ if (validatedID) {
+ setErr('Project name must be a combination of letters, numbers and dashes')
+ setIsCreating(false)
+ return
+ }
+
+ if (trimmedID.length > 63) {
+ setErr('Project name is too long. It must have 63 or less characters')
+ setIsCreating(false)
+ return
+ }
+
+ try {
+ const res = await client.from(projectsTable).insert({ id: trimmedID })
+
+ if (res.error) {
+ setErr(res.statusText)
+ setIsCreating(false)
+ return
+ }
+
+ router.push({
+ pathname: '/[projectID]',
+ query: {
+ projectID: trimmedID,
+ },
+ })
+ } catch (err) {
+ console.error(err)
+ setIsCreating(false)
+ }
+ }
+
+ return (
+ <div
+ className="
+ flex
+ flex-1
+ flex-col
+ space-x-0
+ space-y-8
+ overflow-hidden
+ p-8
+ lg:p-12
+ "
+ >
+ <div className="flex">
+ <div className="items-center flex space-x-2">
+ <LayoutGrid size="30px" strokeWidth="1.5" />
+ <Text
+ size={Text.size.S1}
+ text="Create New Project"
+ />
+ </div>
+ </div>
+
+ <div className="flex flex-1 justify-center overflow-hidden min-h-[200px]">
+ <div className="w-[450px] max-h-[600px] flex flex-col space-y-2 overflow-hidden">
+ <div className="flex space-x-2 items-center transition-all">
+ <Text
+ text="Configure project"
+ className="text-base"
+ />
+ </div>
+ <div className="space-y-2 flex-1 flex flex-col">
+ <div
+ className="space-y-6 rounded border p-8 flex flex-col bg-white"
+ >
+ <div className="flex flex-col space-y-6">
+ <div className="flex flex-col flex-1 space-y-1">
+ <Input
+ title="Must be a combination of letters, numbers and dashes"
+ pattern="[^a-zA-Z0-9\-]"
+ label="Project name"
+ placeholder="Project name"
+ value={projectID}
+ onChange={v => setProjectID(v)}
+ required
+ autofocus
+ />
+ <Text
+ text="Combination of letters, numbers and dashes"
+ size={Text.size.S3}
+ className="text-slate-400"
+ />
+ </div>
+
+ </div>
+ <div className="flex justify-center flex-col space-y-4">
+ <Button
+ isDisabled={!projectID || isCreating || validatedID}
+ onClick={handleCreateProject}
+ text={isCreating ? 'Creating...' : 'Create project'}
+ variant={Button.variant.Full}
+ className="self-center whitespace-nowrap"
+ icon={isCreating ? <SpinnerIcon className="text-white" /> : null}
+ />
+ {!isCreating && err && (
+ <Text
+ className="self-center text-red-500"
+ size={Text.size.S3}
+ text={err}
+ />
+ )}
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ )
+}
diff --git a/pages/sign.tsx b/pages/sign.tsx
index 3b70c6eaa..680ff74ca 100644
--- a/pages/sign.tsx
+++ b/pages/sign.tsx
@@ -3,7 +3,7 @@ import { useUser } from '@supabase/auth-helpers-react'
import AuthForm from 'components/AuthForm'
import TitleLink from 'components/TitleLink'
-import SpinnerIcon from 'components/icons/Spinner'
+import SpinnerIcon from 'components/Spinner'
function SignIn() {
const router = useRouter()
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index eb652c748..d68c0108d 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -266,10 +266,10 @@ model projects {
id String @id(map: "ai_api_pkey") @default("")
created_at DateTime @default(now()) @db.Timestamptz(6)
data Json?
- team_id String? @db.Uuid
+ team_id String @db.Uuid
name String @default("")
deployments deployments[]
- teams teams? @relation(fields: [team_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
+ teams teams @relation(fields: [team_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
@@schema("public")
}