connect to uploader
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { useEditorStore, type ToolType } from "@/pages/editor/store/editorStore";
|
||||
import { useSingleUpload } from "@/pages/uploader/hooks/useUploaderData";
|
||||
import {
|
||||
ImageUpload,
|
||||
VideoInput,
|
||||
@@ -18,31 +19,33 @@ type ToolInstructionsProps = {
|
||||
|
||||
const ToolInstructions = ({ tool }: ToolInstructionsProps) => {
|
||||
const { addObject, setSelectedObjectId, setTool } = useEditorStore();
|
||||
const { mutateAsync: uploadFile } = useSingleUpload();
|
||||
|
||||
const handleImageUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const handleImageUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (file) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = (event) => {
|
||||
const imageUrl = event.target?.result as string;
|
||||
const img = new Image();
|
||||
img.onload = () => {
|
||||
const newImage = {
|
||||
id: `image-${Date.now()}`,
|
||||
type: "image" as ToolType,
|
||||
x: 100,
|
||||
y: 100,
|
||||
width: img.width,
|
||||
height: img.height,
|
||||
imageUrl,
|
||||
};
|
||||
addObject(newImage);
|
||||
setSelectedObjectId(newImage.id);
|
||||
setTool("select");
|
||||
if (!file) return;
|
||||
try {
|
||||
const result = await uploadFile(file);
|
||||
const imageUrl = result?.data?.url;
|
||||
if (!imageUrl) return;
|
||||
const img = new Image();
|
||||
img.onload = () => {
|
||||
const newImage = {
|
||||
id: `image-${Date.now()}`,
|
||||
type: "image" as ToolType,
|
||||
x: 100,
|
||||
y: 100,
|
||||
width: img.width,
|
||||
height: img.height,
|
||||
imageUrl,
|
||||
};
|
||||
img.src = imageUrl;
|
||||
addObject(newImage);
|
||||
setSelectedObjectId(newImage.id);
|
||||
setTool("select");
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
img.src = imageUrl;
|
||||
} catch {
|
||||
// TODO: show error toast
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user