-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from AElfProject/feature/share
Feature/share
- Loading branch information
Showing
13 changed files
with
281 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { FileContent } from "@/data/db"; | ||
import { getBuildServerBaseUrl } from "@/lib/env"; | ||
import { unzipSync, strFromU8 } from "fflate"; | ||
|
||
export async function GET(request: Request) { | ||
const { searchParams } = new URL(request.url); | ||
const id = searchParams.get("id"); | ||
|
||
if (!id) { | ||
throw new Error("no id"); | ||
} | ||
|
||
const res = await fetch( | ||
`${getBuildServerBaseUrl()}/playground/share/get/${id}` | ||
); | ||
|
||
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) | ||
}) | ||
}) | ||
|
||
return Response.json(files); | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
return Response.json({ message: err.message }, { status: 500 }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { FileContent } from "@/data/db"; | ||
import { getBuildServerBaseUrl } from "@/lib/env"; | ||
import { fileContentToZip } from "@/lib/file-content-to-zip"; | ||
import { type NextRequest } from "next/server"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
export async function POST(request: NextRequest) { | ||
const { files } = (await request.json()) as { files: FileContent[] }; | ||
|
||
const zippedData = fileContentToZip(files); | ||
|
||
const formData = new FormData(); | ||
const filePath = uuidv4() + ".zip"; | ||
formData.append( | ||
"file", | ||
new File([zippedData], filePath, { type: "application/zip" }), | ||
filePath | ||
); | ||
|
||
const requestInit: RequestInit = { | ||
method: "POST", | ||
body: formData, | ||
redirect: "follow", | ||
}; | ||
|
||
const response = await fetch( | ||
`${getBuildServerBaseUrl()}/playground/share/create`, | ||
requestInit | ||
); | ||
|
||
if (!response.ok) { | ||
const { message } = await response.json(); | ||
return Response.json({ error: message }, { status: response.status }); | ||
} | ||
|
||
return Response.json(await response.json()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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>Loading...</p>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { SharePageComponent } from "./_sharepagecomponent"; | ||
|
||
|
||
|
||
export default function Page({ params: {id} }: { params: { id: string } }) { | ||
|
||
return ( | ||
<SharePageComponent id={id} /> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"use client"; | ||
import { Copy, CopyCheck } from "lucide-react"; | ||
import Link from "next/link"; | ||
import { useMemo, useState } from "react"; | ||
import { CopyToClipboard } from "react-copy-to-clipboard"; | ||
import { useToast } from "@/components/ui/use-toast"; | ||
|
||
export function ShareLink({ id }: { id: string }) { | ||
const { toast } = useToast(); | ||
const origin = | ||
typeof window !== "undefined" && window.location.origin | ||
? window.location.origin | ||
: ""; | ||
const [copied, setCopied] = useState(false); | ||
|
||
const url = useMemo(() => { | ||
return `${origin}/share/${id}`; | ||
}, [id, origin]); | ||
|
||
return ( | ||
<div className="flex"> | ||
<Link href={url} className="mr-4"> | ||
{url} | ||
</Link> | ||
<CopyToClipboard | ||
text={url} | ||
onCopy={() => { | ||
setCopied(true); | ||
|
||
toast({ | ||
title: "Link Copied", | ||
description: "Share link has been copied to your clipboard.", | ||
|
||
}); | ||
}} | ||
> | ||
{copied ? ( | ||
<CopyCheck className="w-4 h-4 cursor-pointer" /> | ||
) : ( | ||
<Copy className="w-4 h-4 cursor-pointer" /> | ||
)} | ||
</CopyToClipboard> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.