feat: Updated frontend/src/components/ImageUpload.

This commit is contained in:
sweep-ai[bot] 2023-11-18 20:59:30 +00:00 committed by GitHub
parent 183ddde941
commit 8bbee0b60e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,7 @@
import { useState, useEffect, useMemo, useCallback } from "react"; import { useState, useEffect, useMemo, useCallback } from "react";
import { useDropzone } from "react-dropzone"; import { useDropzone } from "react-dropzone";
// import { PromptImage } from "../../../types";
import { toast } from "react-hot-toast"; import { toast } from "react-hot-toast";
import axios from 'axios';
const baseStyle = { const baseStyle = {
flex: 1, flex: 1,
@ -48,16 +48,28 @@ function fileToDataURL(file: File) {
type FileWithPreview = { type FileWithPreview = {
preview: string; preview: string;
} & File; } & File;
);
interface Props { // Convert images to data URLs and set the prompt images state
setReferenceImages: (referenceImages: string[]) => void; Promise.all(acceptedFiles.map((file) => fileToDataURL(file)))
} .then((dataUrls) => {
setReferenceImages(dataUrls.map((dataUrl) => dataUrl as string));
function ImageUpload({ setReferenceImages }: Props) { // Make a POST request to the /extract-colors endpoint with the uploaded image
const [files, setFiles] = useState<FileWithPreview[]>([]); axios.post('/extract-colors', { image: dataUrls[0] })
const { getRootProps, getInputProps, isFocused, isDragAccept, isDragReject } = .then((response) => {
useDropzone({ // Store the returned colors in the component's state
maxFiles: 1, setColors(response.data.colors);
})
.catch((error) => {
toast.error("Error extracting colors: " + error);
console.error("Error extracting colors:", error);
});
})
.catch((error) => {
toast.error("Error reading files" + error);
console.error("Error reading files:", error);
});
},
maxSize: 1024 * 1024 * 5, // 5 MB maxSize: 1024 * 1024 * 5, // 5 MB
accept: { accept: {
"image/png": [".png"], "image/png": [".png"],
@ -65,6 +77,7 @@ function ImageUpload({ setReferenceImages }: Props) {
"image/jpg": [".jpg"], "image/jpg": [".jpg"],
}, },
onDrop: (acceptedFiles) => { onDrop: (acceptedFiles) => {
const [colors, setColors] = useState<string[]>([]);
// Set up the preview thumbnail images // Set up the preview thumbnail images
setFiles( setFiles(
acceptedFiles.map((file: File) => acceptedFiles.map((file: File) =>
@ -147,3 +160,8 @@ function ImageUpload({ setReferenceImages }: Props) {
} }
export default ImageUpload; export default ImageUpload;
<div className="colors">
{colors.map((color, index) => (
<div key={index} style={{ backgroundColor: color, width: '50px', height: '50px' }}></div>
))}
</div>