import { useEffect, useState } from 'react'; import { useHistory } from 'react-router-dom'; import { Transition } from '@headlessui/react'; import { fadeTransition } from 'src/utils/transitions'; import { DownloadIcon, XIcon, ArrowLeftIcon, ArrowRightIcon } from '@heroicons/react/outline'; import { addShortcut, removeShortcut } from '@features/global/services/shortcut-service'; import RouterServices from '@features/router/services/router-service'; import useRouterCompany from '@features/router/hooks/use-router-company'; import { useDrivePreview, useDrivePreviewDisplayData, useDrivePreviewLoading, } from '@features/drive/hooks/use-drive-preview'; import { formatSize } from '@features/global/utils/format-file-size'; import { formatDate } from '@features/global/utils/format-date'; import { DriveItem } from 'app/features/drive/types'; import { Modal } from '@atoms/modal'; import { Button } from '@atoms/button/button'; import { Loader } from '@atoms/loader'; import * as Text from '@atoms/text'; import DriveDisplay from './drive-display'; import Controls from './controls'; interface DrivePreviewProps { items: DriveItem[]; } export const DrivePreview: React.FC = ({ items }) => { const history = useHistory(); const company = useRouterCompany(); const { status, isOpen, open, close, loading } = useDrivePreview(); const [modalLoading, setModalLoading] = useState(true); const { loading: loadingData } = useDrivePreviewLoading(); let animationTimeout: number = setTimeout(() => undefined); const { download, extension } = useDrivePreviewDisplayData(); const { type = '' } = useDrivePreviewDisplayData(); const name = status.details?.item.name; const handleSwitchRight = () => { const currentItem = status.item; if (currentItem) { const currentItemPos = items.findIndex(x => x.id === currentItem.id); switchPreview(items[(currentItemPos + 1) % items.length]); } }; const handleSwitchLeft = () => { const currentItem = status.item; if (currentItem) { const currentItemPos = items.findIndex(x => x.id === currentItem.id); switchPreview(items[(currentItemPos - 1 + items.length) % items.length]); } }; useEffect(() => { addShortcut({ shortcut: 'esc', handler: close }); return () => { removeShortcut({ shortcut: 'esc', handler: close }); }; }, []); useEffect(() => { if (items.length < 2) return () => {}; addShortcut({ shortcut: 'Right', handler: handleSwitchRight }); addShortcut({ shortcut: 'Left', handler: handleSwitchLeft }); return () => { removeShortcut({ shortcut: 'Right', handler: handleSwitchRight }); removeShortcut({ shortcut: 'Left', handler: handleSwitchLeft }); }; }, [status]); useEffect(() => { clearTimeout(animationTimeout); if (loading) { animationTimeout = window.setTimeout(() => { setModalLoading(false); }, 100); } }, [loading]); const switchPreview = async (item: DriveItem) => { close(); //TODO[ASH] fix state management for this component //right now changing the routing leads to a lot of components rerender //and galery become unusable // history.push( // RouterServices.generateRouteFromState({ companyId: company, itemId: item.id, }), // ); open(item); }; return ( { close(); // small delay to allow the modal to close history.push(RouterServices.generateRouteFromState({ companyId: company, itemId: '' })); }} />
{name} {formatDate( +(status.details?.item.added || '') || status.details?.item.last_version_cache.date_added, )}{' '} • {extension?.toLocaleUpperCase()},{' '} {formatSize( status.details?.item.last_version_cache.file_metadata.size || status.details?.item.size, )}
{items.length > 1 && <>
); };