Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: 🚸 Type: Refactoring, Improvement | Title: Refactoring file upload and handling in the application #12

Merged
merged 1 commit into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions app/api/uploadthing/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@ const f = createUploadthing();
export const ourFileRouter = {
imageUploader: f({ image: { maxFileSize: "4MB" } })
.middleware(async ({ req }) => {
const user = await getToken({ req, secret: process.env.SECRET });
const user = await getToken({ req });

if (!user) throw new Error("Unauthorized");

return { userId: user.id };
})
.onUploadComplete(async ({ metadata, file }) => {
console.log("Upload complete for userId:", metadata.userId);

console.log("file url", file.url);

return { uploadedBy: metadata.userId };
}),
.onUploadComplete(async ({ metadata, file }) => {}),
} satisfies FileRouter;

export type OurFileRouter = typeof ourFileRouter;
21 changes: 15 additions & 6 deletions components/rich-text-editor/editor-setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { BlockNoteEditor } from "@blocknote/core";
import { BlockNoteView, useBlockNote } from "@blocknote/react";
import "@blocknote/core/style.css";
import { Uploader } from "@/lib/uploadthing";
import { uploadFiles } from "@/lib/uploadthing";

type EditorSetupProps = {
onChange: (value: string) => void;
Expand All @@ -12,19 +12,28 @@ type EditorSetupProps = {
};

const EditorSetup = ({ onChange, editable }: EditorSetupProps) => {
const handleUpload = async (file: File) => {
const uploadData = { file: file, endpoint: "imageUploader" as const };
const res = await Uploader(uploadData);
const handleUploadFile = async (file: File): Promise<string> => {
try {
const endpoint = "imageUploader";
const options = {
files: [file],
};

return res.props.file;
const [res] = await uploadFiles(endpoint, options);

return res.url;
} catch (error) {
console.error("Error uploading file:", error);
throw new Error("File upload failed");
}
};

const editor: BlockNoteEditor = useBlockNote({
editable,
onEditorContentChange: (editor) => {
onChange(JSON.stringify(editor.topLevelBlocks, null, 2));
},
uploadFile: handleUpload,
uploadFile: handleUploadFile,
});

return (
Expand Down
3 changes: 3 additions & 0 deletions lib/uploadthing.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { generateComponents } from "@uploadthing/react";
import { generateReactHelpers } from "@uploadthing/react/hooks";

import type { OurFileRouter } from "@/app/api/uploadthing/core";

export const { UploadButton, UploadDropzone, Uploader } =
generateComponents<OurFileRouter>();

export const { uploadFiles } = generateReactHelpers<OurFileRouter>();
Loading