From 173dee8804546a641b178f84dd7ffd599f85e49e Mon Sep 17 00:00:00 2001 From: "yongen.loong" Date: Tue, 10 Sep 2024 23:50:59 +0800 Subject: [PATCH] feat: finalise share feature --- app/api/get-share/route.ts | 24 ++++++++++++++++----- app/share/[id]/_sharepagecomponent.tsx | 29 +++++++++++++++++++++++++- data/client.ts | 8 +++---- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/app/api/get-share/route.ts b/app/api/get-share/route.ts index 8699424..ba10106 100644 --- a/app/api/get-share/route.ts +++ b/app/api/get-share/route.ts @@ -1,5 +1,6 @@ +import { FileContent } from "@/data/db"; import { getBuildServerBaseUrl } from "@/lib/env"; -import { unzipSync } from "fflate"; +import { unzipSync, strFromU8 } from "fflate"; export async function GET(request: Request) { const { searchParams } = new URL(request.url); @@ -13,11 +14,24 @@ export async function GET(request: Request) { `${getBuildServerBaseUrl()}/playground/share/get/${id}` ); - const data = await res.text(); + const data = await res.arrayBuffer(); - const zipData = Buffer.from(data, "base64"); + try { + const unzipped = unzipSync(Buffer.from(data)); - const unzipped = unzipSync(zipData); + let files: FileContent[] = []; - return Response.json(unzipped); + Object.entries(unzipped).forEach(([k, v]) => { + files.push({ + path: k, + contents: strFromU8(v) + }) + }) + + return Response.json(files); + } catch (err) { + if (err instanceof Error) { + return Response.json({ message: err.message }, { status: 500 }); + } + } } diff --git a/app/share/[id]/_sharepagecomponent.tsx b/app/share/[id]/_sharepagecomponent.tsx index 08092a1..73f5248 100644 --- a/app/share/[id]/_sharepagecomponent.tsx +++ b/app/share/[id]/_sharepagecomponent.tsx @@ -1,12 +1,39 @@ "use client"; import { useShare } from "@/data/client"; +import { db } from "@/data/db"; +import { useRouter } from "next/navigation"; +import { useEffect } from "react"; export function SharePageComponent({ id }: { id: string }) { const { data, isLoading, error } = useShare(id); + const router = useRouter(); + + useEffect(() => { + async function importWorkspace() { + if (!data) return; + + const existing = await db.workspaces.get(id); + if (existing) { + router.push(`/workspace/${id}`); + } else { + await db.workspaces.add({name: id, template: id, dll: ''}); + + await db.files.bulkAdd( + data.map(({ path, contents }) => ({ + path: `/workspace/${id}/${encodeURIComponent(path)}`, + contents, + })) + ); + router.push(`/workspace/${id}`); + } + } + + importWorkspace(); + }, [id, data]) if (isLoading) return

Loading...

; else if (error) return

Error: {String(error)}

; - return

{JSON.stringify(data)}

; + return

Loading...

; } diff --git a/data/client.ts b/data/client.ts index 6fb8071..3b1f963 100644 --- a/data/client.ts +++ b/data/client.ts @@ -1,7 +1,7 @@ "use client"; import useSWR from "swr"; -import { db } from "./db"; +import { db, FileContent } from "./db"; import { ProposalInfo } from "./proposal-info-types"; import AElf from "aelf-sdk"; import { Transactions } from "./transactions-types"; @@ -144,13 +144,11 @@ export function useTutorialList() { } export function useShare(id: string) { - return useSWR< - Record - >(`get-share-${id}`, async () => { + return useSWR(`get-share-${id}`, async () => { const res = await fetch(`/api/get-share?id=${id}`); const data = await res.json(); return data; }); -} \ No newline at end of file +}