import { ReactElement, useEffect, useMemo, useRef, useState } from 'react' import './FileProgressNotification.scss' import UpIcon from 'remixicon-react/ArrowUpSLineIcon' import DownIcon from 'remixicon-react/ArrowDownSLineIcon' import { FileProgressWindow } from '../FileProgressWindow/FileProgressWindow' import { FileTransferType, TransferStatus, ProgressItem } from '../../constants/transfers' interface FileProgressNotificationProps { label?: string type: FileTransferType open?: boolean items?: ProgressItem[] onRowClose?: (uuid: string) => void onCloseAll?: () => void } export function FileProgressNotification({ label, type, open, items, onRowClose, onCloseAll, }: FileProgressNotificationProps): ReactElement | null { const [showFileProgressWindow, setShowFileProgressWindow] = useState(Boolean(open)) const [openedByUser, setOpenedByUser] = useState(false) const autoHideTimer = useRef(null) const allDone = useMemo(() => { if (!items || items.length === 0) return false return items.every(i => (typeof i.percent === 'number' ? i.percent >= 100 : i.status === TransferStatus.Done)) }, [items]) useEffect(() => { if (open) { setShowFileProgressWindow(true) setOpenedByUser(false) } }, [open]) useEffect(() => { if (autoHideTimer.current) { window.clearTimeout(autoHideTimer.current) autoHideTimer.current = null } if (showFileProgressWindow && allDone && !openedByUser) { autoHideTimer.current = window.setTimeout(() => { setShowFileProgressWindow(false) autoHideTimer.current = null }, 3000) as unknown as number } return () => { if (autoHideTimer.current) { window.clearTimeout(autoHideTimer.current) autoHideTimer.current = null } } }, [showFileProgressWindow, allDone, openedByUser]) const handleOpenClick = () => { setOpenedByUser(true) setShowFileProgressWindow(true) } return (
{label} {type === FileTransferType.Upload && } {type === FileTransferType.Download && }
{showFileProgressWindow && ( setShowFileProgressWindow(false)} onRowClose={onRowClose} onCloseAll={() => { onCloseAll?.() setShowFileProgressWindow(false) }} /> )}
) }