|
1 | | -import { FC } from 'react' |
| 1 | +import { FC, useCallback, useMemo, useState } from 'react' |
| 2 | +import { noop } from 'lodash' |
| 3 | +import classNames from 'classnames' |
| 4 | +import moment from 'moment' |
2 | 5 |
|
3 | | -// import styles from './ScorecardAttachments.module.scss' |
| 6 | +import { IconOutline, Table, TableColumn } from '~/libs/ui' |
| 7 | +import { useReviewsContext } from '~/apps/review/src/pages/reviews/ReviewsContext' |
| 8 | +import { useWindowSize, WindowSize } from '~/libs/shared' |
| 9 | + |
| 10 | +import { AiWorkflowRunArtifact, |
| 11 | + AiWorkflowRunArtifactDownloadResponse, |
| 12 | + AiWorkflowRunAttachmentsResponse, |
| 13 | + useDownloadAiWorkflowsRunArtifact, useFetchAiWorkflowsRunAttachments } from '../../../hooks' |
| 14 | +import { TableWrapper } from '../../TableWrapper' |
| 15 | +import { TABLE_DATE_FORMAT } from '../../../constants' |
| 16 | +import { formatFileSize } from '../../common' |
| 17 | +import { ReviewsContextModel } from '../../../models' |
| 18 | + |
| 19 | +import styles from './ScorecardAttachments.module.scss' |
4 | 20 |
|
5 | 21 | interface ScorecardAttachmentsProps { |
6 | 22 | className?: string |
7 | 23 | } |
8 | 24 |
|
9 | | -const ScorecardAttachments: FC<ScorecardAttachmentsProps> = props => ( |
10 | | - <div className={props.className}> |
11 | | - attachments |
12 | | - </div> |
13 | | -) |
| 25 | +const ScorecardAttachments: FC<ScorecardAttachmentsProps> = (props: ScorecardAttachmentsProps) => { |
| 26 | + const className = props.className |
| 27 | + const { width: screenWidth }: WindowSize = useWindowSize() |
| 28 | + const isTablet = useMemo(() => screenWidth <= 1000, [screenWidth]) |
| 29 | + const { workflowId, workflowRun }: ReviewsContextModel = useReviewsContext() |
| 30 | + const { artifacts }: AiWorkflowRunAttachmentsResponse |
| 31 | + = useFetchAiWorkflowsRunAttachments(workflowId, workflowRun?.id) |
| 32 | + const { download, isDownloading }: AiWorkflowRunArtifactDownloadResponse = useDownloadAiWorkflowsRunArtifact( |
| 33 | + workflowId, |
| 34 | + workflowRun?.id, |
| 35 | + ) |
| 36 | + |
| 37 | + const handleDownload = useCallback( |
| 38 | + async (artifactId: number): Promise<void> => { |
| 39 | + await download(artifactId) |
| 40 | + }, |
| 41 | + [download], |
| 42 | + ) |
| 43 | + |
| 44 | + const createDownloadHandler = useCallback( |
| 45 | + (id: number) => () => handleDownload(id), |
| 46 | + [handleDownload], |
| 47 | + ) |
| 48 | + |
| 49 | + const columns = useMemo<TableColumn<AiWorkflowRunArtifact>[]>( |
| 50 | + () => [ |
| 51 | + { |
| 52 | + className: classNames(styles.tableCell), |
| 53 | + label: 'Filename', |
| 54 | + propertyName: 'name', |
| 55 | + renderer: (attachment: AiWorkflowRunArtifact) => { |
| 56 | + const isExpired = attachment.expired |
| 57 | + |
| 58 | + return ( |
| 59 | + <div |
| 60 | + className={classNames( |
| 61 | + styles.filenameCell, |
| 62 | + { |
| 63 | + [styles.expired]: isExpired, |
| 64 | + [styles.downloading]: isDownloading && !isExpired, |
| 65 | + }, |
| 66 | + )} |
| 67 | + onClick={!isExpired ? createDownloadHandler(attachment.id) : undefined} |
| 68 | + > |
| 69 | + <span>{attachment.name}</span> |
| 70 | + {isExpired && <span>(Link Expired)</span>} |
| 71 | + </div> |
| 72 | + ) |
| 73 | + }, |
| 74 | + type: 'element', |
| 75 | + }, |
| 76 | + { |
| 77 | + className: classNames(styles.tableCell), |
| 78 | + label: 'Type', |
| 79 | + renderer: () => ( |
| 80 | + <div className={styles.artifactType}> |
| 81 | + <IconOutline.CubeIcon className={styles.artifactIcon} width={26} /> |
| 82 | + <span>Artifact</span> |
| 83 | + </div> |
| 84 | + ), |
| 85 | + type: 'element', |
| 86 | + }, |
| 87 | + { |
| 88 | + className: classNames(styles.tableCell), |
| 89 | + label: 'Size', |
| 90 | + propertyName: 'sizeInBytes', |
| 91 | + renderer: (attachment: AiWorkflowRunArtifact) => ( |
| 92 | + <div>{formatFileSize(attachment.size_in_bytes)}</div> |
| 93 | + ), |
| 94 | + type: 'element', |
| 95 | + }, |
| 96 | + { |
| 97 | + className: styles.tableCell, |
| 98 | + label: 'Attached Date', |
| 99 | + renderer: (attachment: AiWorkflowRunArtifact) => ( |
| 100 | + <span className='last-element'> |
| 101 | + {moment(attachment.created_at) |
| 102 | + .local() |
| 103 | + .format(TABLE_DATE_FORMAT)} |
| 104 | + </span> |
| 105 | + ), |
| 106 | + type: 'element', |
| 107 | + }, |
| 108 | + ], |
| 109 | + [createDownloadHandler, isDownloading], |
| 110 | + ) |
| 111 | + |
| 112 | + const [openRow, setOpenRow] = useState<number | undefined>(undefined) |
| 113 | + const toggleRow = useCallback( |
| 114 | + (id: number) => () => { |
| 115 | + setOpenRow(prev => (prev === id ? undefined : id)) |
| 116 | + }, |
| 117 | + [], |
| 118 | + ) |
| 119 | + const renderMobileRow = (attachment: AiWorkflowRunArtifact): JSX.Element => { |
| 120 | + const isExpired = attachment.expired |
| 121 | + const downloading = isDownloading |
| 122 | + const isOpen = openRow === attachment.id |
| 123 | + |
| 124 | + return ( |
| 125 | + <div key={attachment.id} className={styles.mobileRow}> |
| 126 | + {/* Top collapsed row */} |
| 127 | + <div className={styles.mobileHeader}> |
| 128 | + <IconOutline.ChevronDownIcon |
| 129 | + onClick={toggleRow(attachment.id)} |
| 130 | + className={classNames(styles.chevron, { |
| 131 | + [styles.open]: isOpen, |
| 132 | + })} |
| 133 | + width={20} |
| 134 | + /> |
| 135 | + <div |
| 136 | + className={classNames(styles.filenameCell, { |
| 137 | + [styles.expired]: isExpired, |
| 138 | + [styles.downloading]: downloading, |
| 139 | + })} |
| 140 | + onClick={!isExpired ? createDownloadHandler(attachment.id) : undefined} |
| 141 | + > |
| 142 | + {attachment.name} |
| 143 | + </div> |
| 144 | + |
| 145 | + </div> |
| 146 | + |
| 147 | + {/* Expanded content */} |
| 148 | + {isOpen && ( |
| 149 | + <div className={styles.mobileExpanded}> |
| 150 | + <div className={styles.rowItem}> |
| 151 | + <span className={styles.rowItemHeading}>Type:</span> |
| 152 | + <div className={styles.artifactType}> |
| 153 | + <IconOutline.CubeIcon className={styles.artifactIcon} width={20} /> |
| 154 | + Artifact |
| 155 | + </div> |
| 156 | + </div> |
| 157 | + |
| 158 | + <div className={styles.rowItem}> |
| 159 | + <span className={styles.rowItemHeading}>Size:</span> |
| 160 | + {formatFileSize(attachment.size_in_bytes)} |
| 161 | + </div> |
| 162 | + |
| 163 | + <div className={styles.rowItem}> |
| 164 | + <span className={styles.rowItemHeading}>Date:</span> |
| 165 | + {moment(attachment.created_at) |
| 166 | + .local() |
| 167 | + .format(TABLE_DATE_FORMAT)} |
| 168 | + </div> |
| 169 | + </div> |
| 170 | + )} |
| 171 | + </div> |
| 172 | + ) |
| 173 | + } |
| 174 | + |
| 175 | + return ( |
| 176 | + <TableWrapper |
| 177 | + className={classNames( |
| 178 | + styles.tableWrapper, |
| 179 | + className, |
| 180 | + 'enhanced-table', |
| 181 | + )} |
| 182 | + > |
| 183 | + {!artifacts || artifacts.length === 0 ? ( |
| 184 | + <div className={styles.noAttachmentText}>No attachments</div> |
| 185 | + ) : isTablet ? ( |
| 186 | + <div className={styles.mobileList}> |
| 187 | + {artifacts.map(renderMobileRow)} |
| 188 | + </div> |
| 189 | + ) : ( |
| 190 | + <Table |
| 191 | + columns={columns} |
| 192 | + data={artifacts} |
| 193 | + disableSorting |
| 194 | + onToggleSort={noop} |
| 195 | + removeDefaultSort |
| 196 | + /> |
| 197 | + )} |
| 198 | + |
| 199 | + </TableWrapper> |
| 200 | + ) |
| 201 | + |
| 202 | +} |
14 | 203 |
|
15 | 204 | export default ScorecardAttachments |
0 commit comments