-
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.
- Loading branch information
1 parent
cf40d8b
commit 6880bbb
Showing
4 changed files
with
126 additions
and
0 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
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,70 @@ | ||
"use client"; | ||
|
||
import { db, FileContent } from "@/data/db"; | ||
import { useCallback } from "react"; | ||
import { useDropzone, DropzoneOptions } from "react-dropzone"; | ||
import { kebabCase } from "es-toolkit/string"; | ||
import { useRouter } from "next/navigation"; | ||
|
||
export function FileUpload() { | ||
const router = useRouter(); | ||
const onDrop = useCallback<NonNullable<DropzoneOptions["onDrop"]>>( | ||
async (acceptedFiles) => { | ||
let all: FileContent[] = []; | ||
|
||
acceptedFiles.forEach((file) => { | ||
const reader = new FileReader(); | ||
|
||
reader.onabort = () => console.log("file reading was aborted"); | ||
reader.onerror = () => console.log("file reading has failed"); | ||
reader.onload = () => { | ||
// Do whatever you want with the file contents | ||
const binaryStr = reader.result; | ||
if (binaryStr instanceof ArrayBuffer) { | ||
const contents = Buffer.from(binaryStr).toString("ascii"); | ||
// @ts-expect-error | ||
const filename = file.path!; | ||
|
||
all.push({ path: filename.slice(1), contents }); | ||
} | ||
}; | ||
reader.readAsArrayBuffer(file); | ||
}); | ||
|
||
const input = window.prompt("Enter the workspace name: "); | ||
|
||
if (!!input) { | ||
const name = kebabCase(input); | ||
|
||
try { | ||
await db.workspaces.add({ | ||
name, | ||
template: "file-upload", | ||
dll: "", | ||
}); | ||
|
||
const templateData: { path: string; contents: string }[] = all; | ||
|
||
await db.files.bulkAdd( | ||
templateData.map(({ path, contents }) => ({ | ||
path: `/workspace/${name}/${path}`, | ||
contents, | ||
})) | ||
); | ||
await router.push(`/workspace/${name}`); | ||
} catch (err) { | ||
alert(String(err)); | ||
} | ||
} | ||
}, | ||
[] | ||
); | ||
const { getRootProps, getInputProps } = useDropzone({ onDrop }); | ||
|
||
return ( | ||
<div {...getRootProps()} className="p-8 border"> | ||
<input {...getInputProps()} /> | ||
<p>Drag 'n' drop some files here, or click to select files</p> | ||
</div> | ||
); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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