Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Update db; Add project management [WIP]

Tomas Valenta552819dbf197
diff --git a/components/Editor/Logs.tsx b/components/Editor/Logs.tsx
index f1bc1ef3b..1d6b4d6bd 100644
--- a/components/Editor/Logs.tsx
+++ b/components/Editor/Logs.tsx
@@ -1,9 +1,12 @@
-import { useDeployment } from 'hooks/useDeployment';
-
-export interface Logs {
+import { Log } from '.';
+export interface Props {
+ logs: Log[]
}
-function Logs() {
+export default function Logs({ logs }: Props) {
+
+ return (
-}
\ No newline at end of file
+ )
+}
diff --git a/components/Editor/index.tsx b/components/Editor/index.tsx
index ec045bcaa..aa41880ee 100644
--- a/components/Editor/index.tsx
+++ b/components/Editor/index.tsx
@@ -2,7 +2,7 @@ import { shallow } from 'zustand/shallow'
import useSWRMutation from 'swr/mutation'
import { Fragment, useState } from 'react'
import Hotkeys from 'react-hot-keys'
-import { api_deployments } from '@prisma/client'
+import { projects } from '@prisma/client'
import { State, Block, methods, Method } from 'state/store'
import { getStoreContext } from 'state/StoreProvider'
@@ -10,6 +10,7 @@ import Select from 'components/Select'
import { nanoid } from 'nanoid'
import Button from 'components/Button'
import Text from 'components/Text'
+import { useLatestDeployment } from 'hooks/useLatestDeployment'
import BlockEditor from './BlockEditor'
import ConnectionLine from './ConnectionLine'
@@ -24,15 +25,19 @@ const selector = (state: State) => ({
changeMethod: state.changeMethod,
})
+export interface Log {
+
+}
+
export interface Props {
- deployment: api_deployments
+ project: projects
}
async function handlePostGenerate(url: string, { arg }: {
arg: {
blocks: Block[],
method: Method,
- }
+ },
}) {
return await fetch(url, {
method: 'POST',
@@ -47,11 +52,11 @@ async function handlePostGenerate(url: string, { arg }: {
}).then(r => r.json())
}
-export default function Editor({ deployment }: Props) {
+export default function Editor({ project }: Props) {
const useStore = getStoreContext()
- const realtimeDeployment = useDeployment()
-
+ const deployment = useLatestDeployment(project)
+ const logs = deployment?.logs as Log[] | undefined
const {
addBlock,
@@ -63,7 +68,6 @@ export default function Editor({ deployment }: Props) {
} = useStore(selector, shallow)
const [focusedBlock, setFocusedBlock] = useState({ index: 0 })
-
const { trigger: generate } = useSWRMutation('/api/generate', handlePostGenerate)
async function deploy() {
diff --git a/db/supabase.ts b/db/supabase.ts
index 9151a2918..4f83066a4 100644
--- a/db/supabase.ts
+++ b/db/supabase.ts
@@ -12,73 +12,82 @@ export interface Database {
deployments: {
Row: {
code: string | null
- created_at: string | null
+ created_at: string
id: string
logs: Json[] | null
+ project_id: string
}
Insert: {
code?: string | null
- created_at?: string | null
+ created_at?: string
id?: string
logs?: Json[] | null
+ project_id: string
}
Update: {
code?: string | null
- created_at?: string | null
+ created_at?: string
id?: string
logs?: Json[] | null
+ project_id?: string
}
}
projects: {
Row: {
- created_at: string | null
+ created_at: string
data: Json | null
id: string
+ name: string
+ team_id: string | null
}
Insert: {
- created_at?: string | null
+ created_at?: string
data?: Json | null
id?: string
+ name?: string
+ team_id?: string | null
}
Update: {
- created_at?: string | null
+ created_at?: string
data?: Json | null
id?: string
+ name?: string
+ team_id?: string | null
}
}
teams: {
Row: {
- created_at: string | null
+ created_at: string
id: string
is_default: boolean | null
- name: string | null
+ name: string
}
Insert: {
- created_at?: string | null
+ created_at?: string
id?: string
is_default?: boolean | null
- name?: string | null
+ name?: string
}
Update: {
- created_at?: string | null
+ created_at?: string
id?: string
is_default?: boolean | null
- name?: string | null
+ name?: string
}
}
users_teams: {
Row: {
- created_at: string | null
+ created_at: string
team_id: string
user_id: string
}
Insert: {
- created_at?: string | null
+ created_at?: string
team_id: string
user_id: string
}
Update: {
- created_at?: string | null
+ created_at?: string
team_id?: string
user_id?: string
}
diff --git a/hooks/useDeployment.tsx b/hooks/useDeployment.tsx
deleted file mode 100644
index 852dad776..000000000
--- a/hooks/useDeployment.tsx
+++ /dev/null
@@ -1,29 +0,0 @@
-import { api_deployments } from '@prisma/client'
-import { useSupabaseClient } from '@supabase/auth-helpers-react'
-import { useState, useEffect } from 'react'
-
-export function useDeployment(deployment: api_deployments) {
- const [latest, setLatest] = useState<api_deployments>(deployment)
- const client = useSupabaseClient()
-
- useEffect(function subscribe() {
- const sub = client.channel('any')
- .on('postgres_changes',
- {
- event: 'UPDATE',
- schema: 'public',
- table: 'api_deployments',
- filter: `id=eq.${deployment.id}`,
- }, payload => {
- console.log('Change received!', payload)
- setLatest(l => ({ ...l, code: payload.new.code, logs: payload.new.logs }))
- })
- .subscribe()
-
- return () => {
- sub.unsubscribe()
- }
- }, [deployment, client])
-
- return latest
-}
diff --git a/hooks/useLatestDeployment.tsx b/hooks/useLatestDeployment.tsx
new file mode 100644
index 000000000..07e050598
--- /dev/null
+++ b/hooks/useLatestDeployment.tsx
@@ -0,0 +1,30 @@
+import { deployments, projects } from '@prisma/client'
+import { useSupabaseClient } from '@supabase/auth-helpers-react'
+import { deploymentsTable } from 'db/tables'
+import { useState, useEffect } from 'react'
+
+export function useLatestDeployment(project: projects) {
+ const [deployment, setDeployment] = useState<deployments>()
+ const client = useSupabaseClient()
+
+ // 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 deployment
+}
diff --git a/pages/[projectID].tsx b/pages/[projectID].tsx
index 13ce79638..716f0c75e 100644
--- a/pages/[projectID].tsx
+++ b/pages/[projectID].tsx
@@ -3,19 +3,17 @@ import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs'
import { useSupabaseClient } from '@supabase/auth-helpers-react'
import type { ParsedUrlQuery } from 'querystring'
-import { prisma, api_deployments } from 'db/prisma'
-import DeploymentEditor from 'components/Editor'
-import { StoreProvider } from 'state/StoreProvider'
+import { prisma, projects } from 'db/prisma'
import Editor from 'components/Editor'
+import { StoreProvider } from 'state/StoreProvider'
interface PathProps extends ParsedUrlQuery {
- deploymentID: string
+ projectID: string
}
export const getServerSideProps: GetServerSideProps<Props, PathProps> = async (ctx) => {
- const deploymentID = parseInt(ctx.params?.deploymentID || '')
-
- if (!deploymentID) {
+ const projectID = ctx.params?.projectID
+ if (!projectID) {
return {
notFound: true,
}
@@ -44,13 +42,13 @@ export const getServerSideProps: GetServerSideProps<Props, PathProps> = async (c
select: {
teams: {
include: {
- api_deployments: {
+ projects: {
where: {
id: {
- equals: deploymentID,
+ equals: projectID,
},
- }
- }
+ },
+ },
},
},
},
@@ -58,8 +56,12 @@ export const getServerSideProps: GetServerSideProps<Props, PathProps> = async (c
},
})
- const deployment = user?.users_teams.flatMap(t => t.teams.api_deployments).find(d => d.id === BigInt(deploymentID))
- if (!deployment) {
+ const project = user
+ ?.users_teams
+ .flatMap(t => t.teams.projects)
+ .find(p => p.id === projectID)
+
+ if (!project) {
return {
notFound: true,
}
@@ -67,24 +69,24 @@ export const getServerSideProps: GetServerSideProps<Props, PathProps> = async (c
return {
props: {
- deployment,
+ project,
}
}
}
interface Props {
- deployment: api_deployments
+ project: projects
}
-function EditorPage({ deployment }: Props) {
+function EditorPage({ project }: Props) {
const client = useSupabaseClient()
return (
<StoreProvider
client={client}
- deployment={deployment}
+ initialState={project}
>
- <Editor />
+ <Editor project={project} />
</StoreProvider>
)
}
diff --git a/pages/_app.tsx b/pages/_app.tsx
index 64ec034ac..c88cfc56a 100644
--- a/pages/_app.tsx
+++ b/pages/_app.tsx
@@ -6,7 +6,6 @@ import { useState } from 'react'
import { useRouter } from 'next/router'
import clsx from 'clsx'
import { Toaster } from 'sonner'
-import { api_deployments } from '@prisma/client'
import 'styles/global.css'
@@ -23,7 +22,7 @@ const jetBrains = JetBrains_Mono({
variable: '--font-jet-brains',
})
-export default function App({ Component, pageProps }: AppProps<{ initialSession: Session, deployment?: api_deployments }>) {
+export default function App({ Component, pageProps }: AppProps<{ initialSession: Session }>) {
const router = useRouter()
const [supabaseClient] = useState(() => createBrowserSupabaseClient<Database>())
@@ -33,8 +32,8 @@ export default function App({ Component, pageProps }: AppProps<{ initialSession:
return (
<main className={clsx(
inter.variable,
- 'font-sans',
jetBrains.variable,
+ 'font-sans',
'flex',
'h-inherit',
'w-full',
diff --git a/pages/index.tsx b/pages/index.tsx
index 06ab33932..8c9c20ee5 100644
--- a/pages/index.tsx
+++ b/pages/index.tsx
@@ -1,13 +1,14 @@
import type { GetServerSideProps } from 'next'
import { LayoutGrid, Plus } from 'lucide-react'
import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs'
-import useSWRMutation from 'swr/mutation'
import { useRouter } from 'next/router'
+import { useSupabaseClient } from '@supabase/auth-helpers-react'
import ItemList from 'components/ItemList'
import Text from 'components/Text'
-import { api_deployments, prisma } from 'db/prisma'
+import { prisma, projects } from 'db/prisma'
import Button from 'components/Button'
+import { deploymentsTable, projectsTable } from 'db/tables'
export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
const supabase = createServerSupabaseClient(ctx)
@@ -33,11 +34,11 @@ export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
include: {
teams: {
include: {
- api_deployments: true,
+ projects: true,
},
- }
- }
- }
+ },
+ },
+ },
},
})
@@ -59,55 +60,38 @@ export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
},
},
include: {
- api_deployments: true
+ projects: true,
},
})
return {
props: {
- deployments: team.api_deployments,
+ projects: team.projects,
}
}
}
- // Show deployments from all teams for now.
- const deployments = user.users_teams.flatMap(t => t.teams.api_deployments)
+ // Show projects from all teams.
+ const projects = user.users_teams.flatMap(t => t.teams.projects)
return {
props: {
- deployments,
+ projects,
}
}
}
-// interface DeleteProjectBody {
-// id: string
-// }
-
-// async function handleDeleteProject(url: string, { arg }: { arg: DeleteProjectBody }) {
-// return await fetch(url, {
-// method: 'DELETE',
-// body: JSON.stringify(arg),
-
-// headers: {
-// 'Content-Type': 'application/json',
-// },
-// }).then(r => r.json())
-// }
-
-
interface Props {
- deployments: api_deployments[]
+ projects: projects[]
}
-function Home({ deployments }: Props) {
+function Home({ projects }: Props) {
const router = useRouter()
- const {
- trigger: deleteProject,
- } = useSWRMutation('/api/project', handleDeleteProject)
+ const client = useSupabaseClient()
async function handleDelete(id: string) {
- await deleteProject({ id })
+ await client.from(deploymentsTable).delete().eq('project_id', id)
+ await client.from(projectsTable).delete().eq('id', id)
router.replace(router.asPath)
}
@@ -156,9 +140,9 @@ function Home({ deployments }: Props) {
<div className="flex flex-1 justify-center overflow-hidden">
<ItemList
deleteItem={handleDelete}
- items={deployments.map(i => ({
+ items={projects.map(i => ({
...i,
- title: i.title || i.id,
+ title: i.name || i.id,
path: '/[id]',
type: 'Project',
icon: <LayoutGrid size="22px" strokeWidth="1.7" />,
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 09d518cca..eb652c748 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -231,9 +231,10 @@ model auth_users {
model teams {
id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
- created_at DateTime? @default(now()) @db.Timestamptz(6)
+ created_at DateTime @default(now()) @db.Timestamptz(6)
is_default Boolean?
- name String?
+ name String @default("")
+ projects projects[]
users_teams users_teams[]
@@schema("public")
@@ -241,7 +242,7 @@ model teams {
model users_teams {
user_id String @db.Uuid
- created_at DateTime? @default(now()) @db.Timestamptz(6)
+ created_at DateTime @default(now()) @db.Timestamptz(6)
team_id String @db.Uuid
teams teams @relation(fields: [team_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
users auth_users @relation(fields: [user_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
@@ -251,18 +252,24 @@ model users_teams {
}
model deployments {
- id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
- created_at DateTime? @default(now()) @db.Timestamptz(6)
+ id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
+ created_at DateTime @default(now()) @db.Timestamptz(6)
logs Json[]
code String?
+ project_id String
+ projects projects @relation(fields: [project_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
@@schema("public")
}
model projects {
- id String @id(map: "ai_api_pkey") @default("")
- created_at DateTime? @default(now()) @db.Timestamptz(6)
- data Json?
+ id String @id(map: "ai_api_pkey") @default("")
+ created_at DateTime @default(now()) @db.Timestamptz(6)
+ data Json?
+ team_id String? @db.Uuid
+ name String @default("")
+ deployments deployments[]
+ teams teams? @relation(fields: [team_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
@@schema("public")
}
diff --git a/state/StoreProvider.tsx b/state/StoreProvider.tsx
index cd4f5e9f0..453474105 100644
--- a/state/StoreProvider.tsx
+++ b/state/StoreProvider.tsx
@@ -5,24 +5,25 @@ import {
useMemo
} from 'react'
import { SupabaseClient } from '@supabase/supabase-js'
-import { api_deployments } from '@prisma/client'
+import { projects } from '@prisma/client'
-import { createStore } from './store'
import { Database } from 'db/supabase'
+import { createStore } from './store'
+
const storeContext = createContext<ReturnType<typeof createStore> | null>(null)
export interface Props extends PropsWithChildren {
- deployment?: api_deployments
+ initialState?: projects
client: SupabaseClient<Database>
}
export function StoreProvider({
- deployment,
+ initialState,
client,
children,
}: Props) {
- const store = useMemo(() => createStore(deployment, client), [deployment, client])
+ const store = useMemo(() => createStore(initialState, client), [initialState, client])
return (
<storeContext.Provider value={store}>