-
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
c80e1b3
commit 566c806
Showing
5 changed files
with
187 additions
and
53 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
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,62 @@ | ||
"use client"; | ||
|
||
import { useCallback } from "react"; | ||
import { useWebContainer } from "./use-web-container"; | ||
import { useWorkspaceId } from "../workspace/use-workspace-id"; | ||
import { usePathname } from "next/navigation"; | ||
import { db } from "@/data/db"; | ||
import { DirectoryNode, FileSystemTree } from "@webcontainer/api"; | ||
|
||
export function useLoadFiles() { | ||
const id = useWorkspaceId(); | ||
const pathname = usePathname(); | ||
|
||
const webContainer = useWebContainer(); | ||
|
||
const loadFiles = useCallback(async () => { | ||
if (typeof id !== "string") throw new Error("id is not string"); | ||
const start = `${pathname}/`; | ||
const files = ( | ||
await db.files.filter((file) => file.path.startsWith(start)).toArray() | ||
).map((file) => ({ | ||
path: decodeURIComponent(file.path.replace(start, "")), | ||
contents: file.contents, | ||
})); | ||
|
||
const root: FileSystemTree = {}; | ||
|
||
files.forEach((file) => { | ||
const parts = file.path.split("/"); | ||
let current = root; | ||
|
||
parts.forEach((part, index) => { | ||
if (index === parts.length - 1) { | ||
// Last part, assign file content under the "file" key | ||
current[part] = { | ||
file: { | ||
contents: file.contents, | ||
}, | ||
}; | ||
} else { | ||
// Traverse or create sub-directory under "directory" key | ||
if (!current[part]) { | ||
current[part] = { | ||
directory: {}, | ||
}; | ||
} | ||
current = (current[part] as DirectoryNode).directory; | ||
} | ||
}); | ||
}); | ||
|
||
if (!!webContainer) { | ||
webContainer.fs.rm(webContainer.workdir, { | ||
recursive: true, | ||
force: true, | ||
}); | ||
webContainer.mount(root); | ||
} | ||
}, [id, pathname]); | ||
|
||
return loadFiles; | ||
} |
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,107 @@ | ||
"use client"; | ||
|
||
import { Button } from "@/components/ui/button"; | ||
import { Upload } from "lucide-react"; | ||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogDescription, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from "@/components/ui/dialog"; | ||
|
||
import { db, FileContent } from "@/data/db"; | ||
import { useCallback, useState } from "react"; | ||
import { useDropzone, DropzoneOptions } from "react-dropzone"; | ||
import { usePathname } from "next/navigation"; | ||
import { useRefreshFileExplorer } from "./file-explorer"; | ||
import { useLoadFiles } from "../webcontainer/use-load-files"; | ||
|
||
export default function UploadModal() { | ||
const refreshFileExplorer = useRefreshFileExplorer(); | ||
const pathname = usePathname(); | ||
const [isOpen, setIsOpen] = useState(false); | ||
const loadFiles = useLoadFiles(); | ||
|
||
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); | ||
}); | ||
|
||
try { | ||
await db.workspaces.delete(pathname); | ||
await db.files.bulkDelete( | ||
(await db.files.toArray()) | ||
.map((i) => i.path) | ||
.filter((i) => i.startsWith(pathname + "/")) | ||
); | ||
|
||
await db.workspaces.add({ | ||
name: pathname, | ||
template: "file-upload", | ||
dll: "", | ||
}); | ||
|
||
const templateData: { path: string; contents: string }[] = all; | ||
|
||
await db.files.bulkAdd( | ||
templateData.map(({ path, contents }) => ({ | ||
path: `${pathname}/${encodeURIComponent(path)}`, | ||
contents, | ||
})) | ||
); | ||
|
||
await refreshFileExplorer(); | ||
loadFiles(); | ||
setIsOpen(false); | ||
} catch (err) { | ||
alert(String(err)); | ||
} | ||
}, | ||
[] | ||
); | ||
const { getRootProps, getInputProps } = useDropzone({ onDrop }); | ||
|
||
return ( | ||
<Dialog open={isOpen}> | ||
<DialogTrigger asChild> | ||
<Button variant="ghost" size="icon" onClick={() => setIsOpen(true)}> | ||
<Upload className="h-4 w-4" /> | ||
</Button> | ||
</DialogTrigger> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Upload Files</DialogTitle> | ||
<DialogDescription> | ||
<div {...getRootProps()} className="p-8 border"> | ||
<input {...getInputProps()} /> | ||
<p> | ||
Drag 'n' drop some files here, or click to select | ||
files | ||
</p> | ||
</div> | ||
</DialogDescription> | ||
</DialogHeader> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
} |