import { type ChangeEvent, type DragEvent, useState } from 'react'; import Image from 'next/image'; import { faUpload } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import Button from '../basic/buttons/Button'; import Error from '../basic/Error'; import parse from '../utilities/file'; import guidGenerator from '../utilities/randomId'; interface DropZoneProps { // TODO: change Data type from any setData: (data: any) => void; setErrorDragAndDrop: (hasError: boolean) => void; createNewFile: () => void; errorDragAndDrop: boolean; setButtonReady: (isReady: boolean) => void; keysExist: boolean; numCurrentRows: number; } const DropZone = ({ setData, setErrorDragAndDrop, createNewFile, errorDragAndDrop, setButtonReady, keysExist, numCurrentRows }: DropZoneProps) => { const handleDragEnter = (e: DragEvent) => { e.preventDefault(); e.stopPropagation(); }; const handleDragLeave = (e: DragEvent) => { e.preventDefault(); e.stopPropagation(); }; const handleDragOver = (e: DragEvent) => { e.preventDefault(); e.stopPropagation(); // set dropEffect to copy i.e copy of the source item e.dataTransfer.dropEffect = 'copy'; }; const [loading, setLoading] = useState(false); // This function function immediately parses the file after it is dropped const handleDrop = async (e: DragEvent) => { setLoading(true); setTimeout(() => setLoading(false), 5000); e.preventDefault(); e.stopPropagation(); e.dataTransfer.dropEffect = 'copy'; const file = e.dataTransfer.files[0]; const reader = new FileReader(); reader.onload = (event) => { if (event.target === null || event.target.result === null) return; // parse function's argument looks like to be ArrayBuffer const keyPairs = parse(event.target.result as Buffer); const newData = Object.keys(keyPairs).map((key, index) => { return { id: guidGenerator(), pos: numCurrentRows + index, key: key, value: keyPairs[key as keyof typeof keyPairs], type: 'shared' }; }); setData(newData); setButtonReady(true); }; // If something is wrong show an error try { reader.readAsText(file); setLoading(false); } catch (error) { setErrorDragAndDrop(true); setLoading(false); } }; // This function is used when the user manually selects a file from the in-browser dircetory (not drag and drop) const handleFileSelect = (e: ChangeEvent) => { setLoading(true); setTimeout(() => setLoading(false), 5000); if (e.currentTarget.files === null) return; const file = e.currentTarget.files[0]; const reader = new FileReader(); reader.onload = (event) => { if (event.target === null || event.target.result === null) return; const { result } = event.target; if (typeof result === 'string') { const newData = result .split('\n') .map((line: string, index: number) => { return { id: guidGenerator(), pos: numCurrentRows + index, key: line.split('=')[0], value: line.split('=').slice(1, line.split('=').length).join('='), type: 'shared' }; }); setData(newData); setButtonReady(true); } }; reader.readAsText(file); }; return loading ? (
google logo
) : keysExist ? (
{errorDragAndDrop ? (
) : (
)}

Drag and drop your .env file here to add more keys.

{errorDragAndDrop ? (
) : ( <> )}
) : (

Drag and drop your .env file here.

OR

{errorDragAndDrop ? (
) : (
{/*

If you are expecting to see a file here, contact your administrator for permission.

*/}
)}
); }; export default DropZone;