|
| 1 | +import { |
| 2 | + BAIArtifactRevisionTableArtifactRevisionFragment$data, |
| 3 | + BAIArtifactRevisionTableArtifactRevisionFragment$key, |
| 4 | +} from '../../__generated__/BAIArtifactRevisionTableArtifactRevisionFragment.graphql'; |
| 5 | +import { BAIArtifactRevisionTableLatestRevisionFragment$key } from '../../__generated__/BAIArtifactRevisionTableLatestRevisionFragment.graphql'; |
| 6 | +import { convertToDecimalUnit, filterOutEmpty } from '../../helper'; |
| 7 | +import BAIFlex from '../BAIFlex'; |
| 8 | +import BAITag from '../BAITag'; |
| 9 | +import BAIText from '../BAIText'; |
| 10 | +import { BAIColumnsType, BAITable, BAITableProps } from '../Table'; |
| 11 | +import { Button, Tag } from 'antd'; |
| 12 | +import dayjs from 'dayjs'; |
| 13 | +import relativeTime from 'dayjs/plugin/relativeTime'; |
| 14 | +import { Download } from 'lucide-react'; |
| 15 | +import { useTranslation } from 'react-i18next'; |
| 16 | +import { graphql, useFragment } from 'react-relay'; |
| 17 | + |
| 18 | +dayjs.extend(relativeTime); |
| 19 | + |
| 20 | +export type ArtifactRevision = NonNullable< |
| 21 | + NonNullable<BAIArtifactRevisionTableArtifactRevisionFragment$data>[number] |
| 22 | +>; |
| 23 | + |
| 24 | +export interface BAIArtifactRevisionTableProps |
| 25 | + extends Omit< |
| 26 | + BAITableProps<ArtifactRevision>, |
| 27 | + 'dataSource' | 'columns' | 'rowKey' |
| 28 | + > { |
| 29 | + artifactRevisionFrgmt: BAIArtifactRevisionTableArtifactRevisionFragment$key; |
| 30 | + onClickDownload: (revisionId: string) => void; |
| 31 | + latestRevisionFrgmt: |
| 32 | + | BAIArtifactRevisionTableLatestRevisionFragment$key |
| 33 | + | null |
| 34 | + | undefined; |
| 35 | +} |
| 36 | + |
| 37 | +const BAIArtifactRevisionTable = ({ |
| 38 | + artifactRevisionFrgmt, |
| 39 | + onClickDownload, |
| 40 | + latestRevisionFrgmt, |
| 41 | + ...tableProps |
| 42 | +}: BAIArtifactRevisionTableProps) => { |
| 43 | + const { t } = useTranslation(); |
| 44 | + |
| 45 | + const artifactRevision = |
| 46 | + useFragment<BAIArtifactRevisionTableArtifactRevisionFragment$key>( |
| 47 | + graphql` |
| 48 | + fragment BAIArtifactRevisionTableArtifactRevisionFragment on ArtifactRevision |
| 49 | + @relay(plural: true) { |
| 50 | + id |
| 51 | + version |
| 52 | + size |
| 53 | + status |
| 54 | + updatedAt |
| 55 | + } |
| 56 | + `, |
| 57 | + artifactRevisionFrgmt, |
| 58 | + ); |
| 59 | + const latestRevision = |
| 60 | + useFragment<BAIArtifactRevisionTableLatestRevisionFragment$key>( |
| 61 | + graphql` |
| 62 | + fragment BAIArtifactRevisionTableLatestRevisionFragment on ArtifactRevision { |
| 63 | + id |
| 64 | + } |
| 65 | + `, |
| 66 | + latestRevisionFrgmt, |
| 67 | + ); |
| 68 | + |
| 69 | + const columns: BAIColumnsType<ArtifactRevision> = [ |
| 70 | + { |
| 71 | + title: t('comp:BAIArtifactRevisionTable.Version'), |
| 72 | + dataIndex: 'version', |
| 73 | + key: 'version', |
| 74 | + width: '30%', |
| 75 | + render: (version: string, record: ArtifactRevision) => ( |
| 76 | + <div> |
| 77 | + <BAIFlex align="center" gap={'xs'}> |
| 78 | + <BAIText monospace strong> |
| 79 | + {version} |
| 80 | + </BAIText> |
| 81 | + {latestRevision && latestRevision.id === record.id && ( |
| 82 | + <Tag color="blue">Latest</Tag> |
| 83 | + )} |
| 84 | + {record.status === 'PULLED' && <BAITag>{record.status}</BAITag>} |
| 85 | + </BAIFlex> |
| 86 | + </div> |
| 87 | + ), |
| 88 | + }, |
| 89 | + { |
| 90 | + title: t('comp:BAIArtifactRevisionTable.Status'), |
| 91 | + dataIndex: 'status', |
| 92 | + key: 'status', |
| 93 | + width: '15%', |
| 94 | + render: (value: string) => { |
| 95 | + return <BAITag>{value}</BAITag>; |
| 96 | + }, |
| 97 | + }, |
| 98 | + { |
| 99 | + title: t('comp:BAIArtifactRevisionTable.Action'), |
| 100 | + key: 'action', |
| 101 | + width: '15%', |
| 102 | + render: (_, record: ArtifactRevision) => { |
| 103 | + const status = record.status; |
| 104 | + const isDownloadable = status === 'SCANNED'; |
| 105 | + const isLoading = status === 'PULLING' || status === 'VERIFYING'; |
| 106 | + |
| 107 | + return ( |
| 108 | + <Button |
| 109 | + icon={<Download size={16} />} |
| 110 | + type={'primary'} |
| 111 | + size="small" |
| 112 | + onClick={() => { |
| 113 | + if (isDownloadable) { |
| 114 | + onClickDownload(record.id); |
| 115 | + } |
| 116 | + }} |
| 117 | + disabled={isLoading || !isDownloadable} |
| 118 | + loading={isLoading} |
| 119 | + > |
| 120 | + Pull |
| 121 | + </Button> |
| 122 | + ); |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + title: t('comp:BAIArtifactRevisionTable.Size'), |
| 127 | + dataIndex: 'size', |
| 128 | + key: 'size', |
| 129 | + width: '15%', |
| 130 | + render: (size: number) => { |
| 131 | + if (!size) return <BAIText monospace>N/A</BAIText>; |
| 132 | + return ( |
| 133 | + <BAIText monospace> |
| 134 | + {convertToDecimalUnit(size, 'auto')?.displayValue} |
| 135 | + </BAIText> |
| 136 | + ); |
| 137 | + }, |
| 138 | + }, |
| 139 | + { |
| 140 | + title: t('comp:BAIArtifactTable.Updated'), |
| 141 | + dataIndex: 'updatedAt', |
| 142 | + key: 'updatedAt', |
| 143 | + width: '15%', |
| 144 | + render: (updated_at: string) => ( |
| 145 | + <BAIText type="secondary" title={dayjs(updated_at).toString()}> |
| 146 | + {dayjs(updated_at).fromNow()} |
| 147 | + </BAIText> |
| 148 | + ), |
| 149 | + }, |
| 150 | + ]; |
| 151 | + |
| 152 | + return ( |
| 153 | + <BAITable<ArtifactRevision> |
| 154 | + rowKey={(record) => record.id} |
| 155 | + resizable |
| 156 | + columns={filterOutEmpty(columns)} |
| 157 | + dataSource={artifactRevision} |
| 158 | + scroll={{ x: 'max-content' }} |
| 159 | + {...tableProps} |
| 160 | + ></BAITable> |
| 161 | + ); |
| 162 | +}; |
| 163 | + |
| 164 | +export default BAIArtifactRevisionTable; |
0 commit comments