import { memo } from 'react'
import { Button, ButtonProps } from 'antd'
import { FileWordOutlined } from '@ant-design/icons'

import { FileInfoDto } from '@api'
import { downloadFile } from './factory'

export type DownloadLinkProps = ButtonProps & {
    file?: FileInfoDto
    name?: string
}

export const DownloadLink = memo<DownloadLinkProps>(({ file, name, ...other }) => (
    <Button
        type={'link'}
        icon={<FileWordOutlined />}
        onClick={file && (() => downloadFile(file))}
        {...other}
    >{name ?? file?.name ?? '-'}</Button>
))

export default DownloadLink
