import Uploady, { useRequestPreSend, UPLOADER_EVENTS } from "@rpldy/uploady"; import { asUploadButton } from "@rpldy/upload-button"; // import { fetchUploadUrl } from "./fetch-upload-img-url"; import ImagePreviews from "./ImagePreviews"; import UploadDropZone from "@rpldy/upload-drop-zone"; import { forwardRef, useCallback, useState } from "react"; import styles from './styles.module.scss' import { AiOutlineCloudUpload } from "react-icons/ai"; import { motion } from "framer-motion"; import { getMockSenderEnhancer } from "@rpldy/mock-sender"; import ScreenshotThumbnail from "./ScreenshotThumbnail"; import { FiCamera } from "react-icons/fi"; import { Control, Path, useController } from "react-hook-form"; import { ImageInput } from "src/graphql"; import { fetchUploadImageUrl } from "src/api/uploading"; const mockSenderEnhancer = getMockSenderEnhancer({ delay: 1500, }); const MAX_UPLOAD_COUNT = 4 as const; interface Props { value: ImageInput[], onChange: (new_value: ImageInput[]) => void } export default function ScreenshotsInput(props: Props) { const { value: uploadedFiles, onChange } = props; const [uploadingCount, setUploadingCount] = useState(0) const canUploadMore = uploadingCount + uploadedFiles.length < MAX_UPLOAD_COUNT; const placeholdersCount = (MAX_UPLOAD_COUNT - (uploadingCount + uploadedFiles.length + 1)); return ( { setUploadingCount(v => v + batch.items.length) }, [UPLOADER_EVENTS.ITEM_FINALIZE]: () => setUploadingCount(v => v - 1), [UPLOADER_EVENTS.ITEM_FINISH]: (item) => { // Just for mocking purposes const dataUrl = URL.createObjectURL(item.file); const { id, filename, variants } = item?.uploadResponse?.data?.result ?? { id: Math.random().toString(), filename: item.file.name, variants: [ "", dataUrl ] } if (id) { onChange([...uploadedFiles, { id, name: filename, url: variants[1] }].slice(-MAX_UPLOAD_COUNT)) } } }} > {canUploadMore && } {uploadedFiles.map(f => { onChange(uploadedFiles.filter(file => file.id !== f.id)) }} />)} {(placeholdersCount > 0) && Array(placeholdersCount).fill(0).map((_, idx) => )} ) } const DropZone = forwardRef((props, ref) => { const { onClick, ...buttonProps } = props; useRequestPreSend(async (data) => { const filename = data.items?.[0].file.name ?? '' const res = await fetchUploadImageUrl({ filename }); return { options: { destination: { url: res.uploadURL }, } } }) const onZoneClick = useCallback( (e: any) => { if (onClick) { onClick(e); } }, [onClick] ); return Browse images or drop them here Drop to upload }) const DropZoneButton = asUploadButton(DropZone);