import React, { useCallback } from 'react'; import { translate } from '@aws-amplify/ui'; import { humanFileSize } from '@aws-amplify/ui'; import { FileState, TrackerProps } from '../types'; import { View, Image, Text, Button, TextField, Loader, ComponentClassNames, VisuallyHidden, } from '../../../../primitives'; import { IconClose, IconEdit, IconFile, } from '../../../../primitives/Icon/internal'; import { UploadMessage } from './UploadMessage'; export function UploadTracker({ errorMessage, file, fileState, hasImage, name, onCancel, onCancelEdit, onPause, onResume, onSaveEdit, onStartEdit, percentage, isResumable, showImage, }: TrackerProps): JSX.Element | null { const [tempName, setTempName] = React.useState(name); const inputRef = React.useRef(null); const url = URL.createObjectURL(file); // Focus the input after pressing the edit button React.useEffect(() => { if (fileState === FileState.EDITING && inputRef.current) { inputRef.current.focus(); } }, [fileState]); const { size } = file; const icon = hasImage ? {file.name} : ; const isDeterminate = isResumable ? percentage > 0 : true; const showEditButton = fileState === FileState.INIT || (fileState === FileState.ERROR && errorMessage === translate('Extension not allowed')); const DisplayView = useCallback( () => ( <> {name} {showEditButton ? ( ) : null} {humanFileSize(size, true)} ), [file.name, name, onStartEdit, showEditButton, size] ); const Actions = useCallback(() => { switch (fileState) { case FileState.EDITING: return ( <> ); case FileState.RESUME: case FileState.LOADING: if (!isResumable) return null; return ( ); case FileState.PAUSED: return ( ); case FileState.SUCCESS: return null; default: return ( ); } }, [ file.name, fileState, isResumable, name, onCancel, onCancelEdit, onPause, onResume, onSaveEdit, tempName, ]); if (!file) return null; return ( {showImage ? ( {icon} ) : null} {/* Main View */} {fileState === FileState.EDITING ? ( // Wrapping this text field in a form with onSubmit will allow keyboard // users to press enter to save changes. { onSaveEdit(tempName); }} > ) => { setTempName(e.target.value); }} value={tempName} /> ) : ( )} {fileState === FileState.LOADING ? ( ) : null} ); }