diff --git a/actions/threads.tsx b/actions/threads.tsx index a642e7a7..56cd9973 100644 --- a/actions/threads.tsx +++ b/actions/threads.tsx @@ -1,6 +1,6 @@ "use server" -import {THREADS_DIR} from "@/config/env"; +import {THREADS_DIR, WORKSPACE_DIR} from "@/config/env"; import {gpt} from "@/config/env"; import fs from "fs/promises"; import path from 'path'; @@ -20,6 +20,7 @@ export type ThreadMeta = { updated: Date; id: string; script: string; + workspace: string; } export async function init() { @@ -67,7 +68,11 @@ export async function getThreads() { export async function getThread(id: string) { const threads = await getThreads(); - return threads.find(thread => thread.meta.id === id); + const thread = threads.find(thread => thread.meta.id === id); + if (!thread) return null; + // falsy check for workspace to account for old threads that don't have a workspace + if (thread.meta.workspace == undefined) thread.meta.workspace = WORKSPACE_DIR(); + return thread; } async function newThreadName(): Promise { @@ -96,8 +101,9 @@ export async function createThread(script: string, firstMessage?: string): Promi description: '', created: new Date(), updated: new Date(), + workspace: WORKSPACE_DIR(), id, - script + script, } const threadState = ''; @@ -129,3 +135,12 @@ export async function renameThread(id: string, name: string) { threadMeta.name = name; await fs.writeFile(path.join(threadPath, META_FILE), JSON.stringify(threadMeta)); } + +export async function updateThreadWorkspace(id: string, workspace: string) { + const threadsDir = THREADS_DIR(); + const threadPath = path.join(threadsDir,id); + const meta = await fs.readFile(path.join(threadPath, META_FILE), "utf-8"); + const threadMeta = JSON.parse(meta) as ThreadMeta; + threadMeta.workspace = workspace; + await fs.writeFile(path.join(threadPath, META_FILE), JSON.stringify(threadMeta)); +} diff --git a/actions/upload.tsx b/actions/upload.tsx index b739ae99..ce583188 100644 --- a/actions/upload.tsx +++ b/actions/upload.tsx @@ -2,19 +2,16 @@ import fs from "node:fs/promises"; import path from "node:path"; -import {revalidatePath} from "next/cache"; -import {WORKSPACE_DIR} from '@/config/env'; -import {Dirent} from 'fs'; - -export async function uploadFile(formData: FormData) { - const workspaceDir = WORKSPACE_DIR() - await fs.mkdir(workspaceDir, {recursive: true}) +import { revalidatePath } from "next/cache"; +import { Dirent } from 'fs'; +export async function uploadFile(workspace: string, formData: FormData) { const file = formData.get("file") as File; + await fs.mkdir(workspace, { recursive: true }) + const arrayBuffer = await file.arrayBuffer(); const buffer = new Uint8Array(arrayBuffer); - await fs.writeFile(path.join(workspaceDir, file.name), buffer); - + await fs.writeFile(path.join(workspace,file.name), buffer); revalidatePath("/"); } @@ -27,10 +24,10 @@ export async function deleteFile(path: string) { } } -export async function lsWorkspaceFiles(): Promise { +export async function lsFiles(dir: string): Promise { let files: Dirent[] = [] try { - const dirents = await fs.readdir(WORKSPACE_DIR(), {withFileTypes: true}); + const dirents = await fs.readdir(dir, { withFileTypes: true }); files = dirents.filter((dirent: Dirent) => !dirent.isDirectory()); } catch (e) { if ((e as NodeJS.ErrnoException).code !== 'ENOENT') { @@ -39,4 +36,4 @@ export async function lsWorkspaceFiles(): Promise { } return JSON.stringify(files); -} +} \ No newline at end of file diff --git a/app/edit/page.tsx b/app/edit/page.tsx index ccbcd0b1..1f10f626 100644 --- a/app/edit/page.tsx +++ b/app/edit/page.tsx @@ -5,6 +5,7 @@ import {useSearchParams} from "next/navigation"; import Script from "@/components/script"; import Configure from "@/components/edit/configure"; import {EditContextProvider} from "@/contexts/edit"; +import { ScriptContextProvider } from "@/contexts/script"; import New from "@/components/edit/new"; import ScriptNav from "@/components/edit/scriptNav"; @@ -25,15 +26,17 @@ function EditFile() { return ( -
-
- + +
+
+ +
+
+ +
+