Skip to content

Commit

Permalink
feat: finalise share feature
Browse files Browse the repository at this point in the history
  • Loading branch information
yongenaelf committed Sep 10, 2024
1 parent 5a8ccda commit 173dee8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
24 changes: 19 additions & 5 deletions app/api/get-share/route.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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 });
}
}
}
29 changes: 28 additions & 1 deletion app/share/[id]/_sharepagecomponent.tsx
Original file line number Diff line number Diff line change
@@ -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 <p>Loading...</p>;
else if (error) return <p>Error: {String(error)}</p>;

return <p>{JSON.stringify(data)}</p>;
return <p>Loading...</p>;
}
8 changes: 3 additions & 5 deletions data/client.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -144,13 +144,11 @@ export function useTutorialList() {
}

export function useShare(id: string) {
return useSWR<
Record<string, string>
>(`get-share-${id}`, async () => {
return useSWR<FileContent[]>(`get-share-${id}`, async () => {
const res = await fetch(`/api/get-share?id=${id}`);

const data = await res.json();

return data;
});
}
}

0 comments on commit 173dee8

Please sign in to comment.