Skip to content

Commit

Permalink
feat: Updated frontend/src/components/ImageUpload.
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] committed Nov 18, 2023
1 parent 183ddde commit 8bbee0b
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions frontend/src/components/ImageUpload.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useEffect, useMemo, useCallback } from "react";
import { useDropzone } from "react-dropzone";
// import { PromptImage } from "../../../types";
import { toast } from "react-hot-toast";
import axios from 'axios';

const baseStyle = {
flex: 1,
Expand Down Expand Up @@ -48,23 +48,36 @@ function fileToDataURL(file: File) {
type FileWithPreview = {
preview: string;
} & File;

interface Props {
setReferenceImages: (referenceImages: string[]) => void;
}

function ImageUpload({ setReferenceImages }: Props) {
const [files, setFiles] = useState<FileWithPreview[]>([]);
const { getRootProps, getInputProps, isFocused, isDragAccept, isDragReject } =
useDropzone({
maxFiles: 1,
);

// Convert images to data URLs and set the prompt images state
Promise.all(acceptedFiles.map((file) => fileToDataURL(file)))
.then((dataUrls) => {
setReferenceImages(dataUrls.map((dataUrl) => dataUrl as string));
// Make a POST request to the /extract-colors endpoint with the uploaded image
axios.post('/extract-colors', { image: dataUrls[0] })
.then((response) => {
// Store the returned colors in the component's state
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
accept: {
"image/png": [".png"],
"image/jpeg": [".jpeg"],
"image/jpg": [".jpg"],
},
onDrop: (acceptedFiles) => {
const [colors, setColors] = useState<string[]>([]);
// Set up the preview thumbnail images
setFiles(
acceptedFiles.map((file: File) =>
Expand Down Expand Up @@ -147,3 +160,8 @@ function ImageUpload({ setReferenceImages }: Props) {
}

export default ImageUpload;
<div className="colors">
{colors.map((color, index) => (
<div key={index} style={{ backgroundColor: color, width: '50px', height: '50px' }}></div>
))}
</div>

0 comments on commit 8bbee0b

Please sign in to comment.