|
| 1 | +import classNames from "classnames"; |
| 2 | +import { reaction } from "mobx"; |
| 3 | +import { observer } from "mobx-react"; |
| 4 | +import { FC, useEffect, useRef, useState } from "react"; |
| 5 | +import { Trans } from "react-i18next"; |
| 6 | +import Icon from "../Styled/Icon"; |
| 7 | +import { useViewState } from "./Context"; |
| 8 | +import Styles from "./drag-drop-notification.scss"; |
| 9 | + |
| 10 | +const DragDropNotification: FC = observer(() => { |
| 11 | + const viewState = useViewState(); |
| 12 | + const [showNotification, setShowNotification] = useState(false); |
| 13 | + const notificationTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>( |
| 14 | + null |
| 15 | + ); |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + const disposer = reaction( |
| 19 | + () => viewState.lastUploadedFiles, |
| 20 | + () => { |
| 21 | + if (notificationTimeoutRef.current) { |
| 22 | + clearTimeout(notificationTimeoutRef.current); |
| 23 | + } |
| 24 | + // show notification, restart timer |
| 25 | + setShowNotification(true); |
| 26 | + // initialise new time out |
| 27 | + notificationTimeoutRef.current = setTimeout(() => { |
| 28 | + setShowNotification(false); |
| 29 | + }, 5000); |
| 30 | + } |
| 31 | + ); |
| 32 | + |
| 33 | + return () => { |
| 34 | + if (notificationTimeoutRef.current) { |
| 35 | + clearTimeout(notificationTimeoutRef.current); |
| 36 | + } |
| 37 | + disposer(); |
| 38 | + }; |
| 39 | + }, [viewState.lastUploadedFiles]); |
| 40 | + |
| 41 | + const handleHover = () => { |
| 42 | + // reset timer on hover |
| 43 | + if (notificationTimeoutRef.current) { |
| 44 | + clearTimeout(notificationTimeoutRef.current); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + const handleMouseLeave = () => { |
| 49 | + notificationTimeoutRef.current = setTimeout(() => { |
| 50 | + setShowNotification(false); |
| 51 | + }, 4000); |
| 52 | + }; |
| 53 | + |
| 54 | + const handleClick = () => { |
| 55 | + viewState.openUserData(); |
| 56 | + }; |
| 57 | + |
| 58 | + const fileNames = viewState.lastUploadedFiles.join(","); |
| 59 | + |
| 60 | + return ( |
| 61 | + <button |
| 62 | + className={classNames(Styles.notification, { |
| 63 | + [Styles.isActive]: showNotification && fileNames.length > 0 |
| 64 | + })} |
| 65 | + onMouseEnter={handleHover} |
| 66 | + onMouseLeave={handleMouseLeave} |
| 67 | + onClick={handleClick} |
| 68 | + > |
| 69 | + <div className={Styles.icon}> |
| 70 | + <Icon glyph={Icon.GLYPHS.upload} /> |
| 71 | + </div> |
| 72 | + <div className={Styles.info}> |
| 73 | + <Trans |
| 74 | + i18nKey="dragDrop.notification" |
| 75 | + count={viewState.lastUploadedFiles.length} |
| 76 | + > |
| 77 | + <span className={Styles.filename}>"{{ fileNames }}"</span> |
| 78 | + {{ count: viewState.lastUploadedFiles.length }} been added to{" "} |
| 79 | + <span className={Styles.action}>My data</span> |
| 80 | + </Trans> |
| 81 | + </div> |
| 82 | + </button> |
| 83 | + ); |
| 84 | +}); |
| 85 | + |
| 86 | +DragDropNotification.displayName = "DragDropNotification"; |
| 87 | + |
| 88 | +export default DragDropNotification; |
0 commit comments