import variables from 'config/variables'; import { PureComponent, createRef } from 'react'; import { toast } from 'react-toastify'; import { MdCancel, MdAddLink, MdAddPhotoAlternate, MdOutlineFileUpload, MdFolder, } from 'react-icons/md'; import { addCustomImage, getCustomImages, deleteCustomImage } from 'utils/indexedDB'; import { Tooltip, Button } from 'components/Elements'; import Modal from 'react-modal'; import CustomURLModal from './CustomURLModal'; import { motion, AnimatePresence } from 'framer-motion'; export default class CustomSettings extends PureComponent { getMessage = (text, obj) => variables.getMessage(text, obj || {}); constructor() { super(); this.state = { customBackground: [], customURLModal: false, urlError: '', isDragging: false, }; this.customDnd = createRef(null); } componentDidMount() { this.loadCustomImages(); const dnd = this.customDnd.current; dnd.ondragover = dnd.ondragenter = (e) => { e.preventDefault(); this.setState({ isDragging: true }); }; dnd.ondragleave = (e) => { e.preventDefault(); this.setState({ isDragging: false }); }; dnd.ondrop = (e) => { e.preventDefault(); this.setState({ isDragging: false }); const files = Array.from(e.dataTransfer.files); this.handleFileUpload(files); }; } loadCustomImages = () => { getCustomImages().then((images) => { this.setState({ customBackground: images }); }); }; addCustomImage = (image) => { addCustomImage({ url: image }).then(() => { this.loadCustomImages(); toast(variables.getMessage('toasts.upload_success')); }); }; removeCustomImage = (id) => { deleteCustomImage(id).then(() => { this.loadCustomImages(); toast(variables.getMessage('toasts.remove_success')); }); }; uploadCustomBackground = () => { document.getElementById('bg-input').click(); }; handleFileUpload = (files) => { const maxSize = 5 * 1024 * 1024; // 5MB size limit files.forEach((file) => { if (file.size > maxSize) { toast.error(variables.getMessage('toasts.upload_size_error')); return; } const reader = new FileReader(); reader.onload = (event) => { this.addCustomImage(event.target.result); }; reader.readAsDataURL(file); }); }; render() { return ( <>
{variables.getMessage('settings:sections.background.source.custom_title')} {variables.getMessage('settings:sections.background.source.custom_description')}
{this.state.customBackground.length > 0 ? ( {this.state.customBackground.map((image, index) => ( {'Custom
)}
{ const files = Array.from(e.target.files); this.handleFileUpload(files); }} /> this.setState({ customURLModal: false })} isOpen={this.state.customURLModal} className="Modal resetmodal mainModal" overlayClassName="Overlay resetoverlay" ariaHideApp={false} > this.addCustomImage(e)} urlError={this.state.urlError} modalCloseOnly={() => this.setState({ customURLModal: false })} /> ); } }