import { useCallback } from "react"; import { IEditorCellProps } from "@src/components/fields/types"; import { useSetAtom } from "jotai"; import { findIndex } from "lodash-es"; import { useDropzone } from "react-dropzone"; import { format } from "date-fns"; import { alpha, Stack, Grid, Tooltip, Chip, IconButton } from "@mui/material"; import { Upload as UploadIcon } from "@src/assets/icons"; import ChipList from "@src/components/Table/ChipList"; import CircularProgressOptical from "@src/components/CircularProgressOptical"; import { projectScope, confirmDialogAtom } from "@src/atoms/projectScope"; import { tableScope, updateFieldAtom } from "@src/atoms/tableScope"; import useUploader from "@src/hooks/useFirebaseStorageUploader"; import { FileIcon } from "."; import { DATE_TIME_FORMAT } from "@src/constants/dates"; import { FileValue } from "@src/types/table"; export default function File_({ column, value, onChange, onSubmit, disabled, _rowy_ref, tabIndex, rowHeight, }: IEditorCellProps) { const confirm = useSetAtom(confirmDialogAtom, projectScope); const updateField = useSetAtom(updateFieldAtom, tableScope); const { uploaderState, upload, deleteUpload } = useUploader(); const { progress, isLoading } = uploaderState; const onDrop = useCallback( (acceptedFiles: File[]) => { const file = acceptedFiles[0]; if (file) { upload({ docRef: _rowy_ref, fieldName: column.key, files: [file], previousValue: value, onComplete: (newValue) => { updateField({ path: _rowy_ref.path, fieldName: column.key, value: newValue, }); }, }); } }, [value] ); const handleDelete = (ref: string) => { const newValue = [...value]; const index = findIndex(newValue, ["ref", ref]); const toBeDeleted = newValue.splice(index, 1); toBeDeleted.length && deleteUpload(toBeDeleted[0]); onChange(newValue); onSubmit(); }; const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop, multiple: false, }); const dropzoneProps = getRootProps(); return ( alpha( theme.palette.primary.main, theme.palette.action.hoverOpacity * 2 ), "& .row-hover-iconButton": { color: "primary.main" }, } : {}), }} {...dropzoneProps} tabIndex={tabIndex} onClick={undefined} > {Array.isArray(value) && value.map((file: FileValue) => ( 1 ? { maxWidth: `calc(100% - 12px)` } : {} } > } label={file.name} onClick={(e) => { window.open(file.downloadURL); e.stopPropagation(); }} onDelete={ disabled ? undefined : () => confirm({ handleConfirm: () => handleDelete(file.ref), title: "Delete file?", body: "This file cannot be recovered after", confirm: "Delete", confirmColor: "error", }) } tabIndex={tabIndex} style={{ width: "100%" }} /> ))} {!isLoading ? ( !disabled && ( { dropzoneProps.onClick!(e); e.stopPropagation(); }} style={{ display: "flex" }} className={_rowy_ref && "row-hover-iconButton end"} disabled={!_rowy_ref} tabIndex={tabIndex} > ) ) : (
)}
); }