import { Box, Flex, Text, useColorModeValue, IconButton, chakra, Tooltip } from '@chakra-ui/react'; import React from 'react'; import type { IconName } from 'ui/shared/IconSvg'; import IconSvg from 'ui/shared/IconSvg'; const FILE_ICONS: Record = { '.json': 'files/json', '.sol': 'files/sol', '.yul': 'files/yul', }; function getFileExtension(fileName: string) { const chunks = fileName.split('.'); if (chunks.length === 1) { return ''; } return '.' + chunks[chunks.length - 1]; } interface Props { file: File; className?: string; index?: number; onRemove?: (index?: number) => void; isDisabled?: boolean; error?: string; } const FileSnippet = ({ file, className, index, onRemove, isDisabled, error }: Props) => { const handleRemove = React.useCallback((event: React.MouseEvent) => { event.stopPropagation(); onRemove?.(index); }, [ index, onRemove ]); const handleErrorHintIconClick = React.useCallback((event: React.MouseEvent) => { event.stopPropagation(); }, []); const fileExtension = getFileExtension(file.name); const fileIcon = FILE_ICONS[fileExtension] || 'files/placeholder'; const iconColor = useColorModeValue('gray.600', 'gray.400'); return ( { file.name } { Boolean(error) && ( ) } } boxSize={ 6 } variant="simple" display="inline-block" flexShrink={ 0 } ml="auto" onClick={ handleRemove } isDisabled={ isDisabled } alignSelf="flex-start" /> { file.size.toLocaleString(undefined, { notation: 'compact', maximumFractionDigits: 2, unit: 'byte', unitDisplay: 'narrow', style: 'unit' }) } ); }; export default React.memo(chakra(FileSnippet));