Skip to content

Commit

Permalink
feat: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
yongenaelf committed Sep 10, 2024
1 parent 4783729 commit ededc76
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 26 deletions.
25 changes: 15 additions & 10 deletions app/api/get-share/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,33 @@ export async function GET(request: Request) {
throw new Error("no id");
}

const res = await fetch(
`${getBuildServerBaseUrl()}/playground/share/get/${id}`
);
try {
const res = await fetch(
`${getBuildServerBaseUrl()}/playground/share/get/${id}`
);

const data = await res.arrayBuffer();
const data = await res.arrayBuffer();

try {
const unzipped = unzipSync(Buffer.from(data));

let files: FileContent[] = [];

Object.entries(unzipped).forEach(([k, v]) => {
files.push({
path: k,
contents: strFromU8(v)
})
})
contents: strFromU8(v),
});
});

return Response.json(files);
return Response.json({ files, success: true });
} catch (err) {
if (err instanceof Error) {
return Response.json({ message: err.message }, { status: 500 });
let error = err.message;

if (error === "invalid zip data")
error = "This share ID is not available.";

return Response.json({ message: error, success: false });
}
}
}
34 changes: 27 additions & 7 deletions app/share/[id]/_sharepagecomponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@ import { useShare } from "@/data/client";
import { db } from "@/data/db";
import { useRouter } from "next/navigation";
import { useEffect } from "react";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";

export function SharePageComponent({ id }: { id: string }) {
const { data, isLoading, error } = useShare(id);
const { data, isLoading } = useShare(id);
const router = useRouter();

useEffect(() => {
async function importWorkspace() {
if (!data) return;
if (!data?.files) 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.workspaces.add({ name: id, template: id, dll: "" });

await db.files.bulkAdd(
data.map(({ path, contents }) => ({
data.files.map(({ path, contents }) => ({
path: `/workspace/${id}/${encodeURIComponent(path)}`,
contents,
}))
Expand All @@ -30,10 +38,22 @@ export function SharePageComponent({ id }: { id: string }) {
}

importWorkspace();
}, [id, data])

}, [id, data]);

if (data?.success === false)
return (
<Dialog open onOpenChange={() => router.push("/")}>
<DialogContent>
<DialogHeader>
<DialogTitle>Error</DialogTitle>
<DialogDescription>
{data.message}
</DialogDescription>
</DialogHeader>
</DialogContent>
</Dialog>
);
if (isLoading) return <p>Loading...</p>;
else if (error) return <p>Error: {String(error)}</p>;

return <p>Loading...</p>;
}
5 changes: 1 addition & 4 deletions app/share/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { SharePageComponent } from "./_sharepagecomponent";



export default function Page({ params: {id} }: { params: { id: string } }) {

export default function Page({ params: { id } }: { params: { id: string } }) {
return (
<SharePageComponent id={id} />
);
Expand Down
13 changes: 8 additions & 5 deletions data/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,14 @@ export function useTutorialList() {
}

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

const data = await res.json();
const data = await res.json();

return data;
});
return data;
}
);
}

0 comments on commit ededc76

Please sign in to comment.