|
| 1 | +import { useRequest } from 'ahooks' |
| 2 | +import { Table } from 'antd' |
| 3 | +import type { ColumnsType } from 'antd/es/table' |
| 4 | +import Head from 'components/Head' |
| 5 | +import RecordCaseStatus from 'components/RecordCaseStatus' |
| 6 | +import RecordStatus from 'components/RecordStatus' |
| 7 | +import ShadowCard from 'components/ShadowCard' |
| 8 | +import SidePage from 'components/SidePage' |
| 9 | +import UserBadge from 'components/UserBadge' |
| 10 | +import dayjs from 'dayjs' |
| 11 | +import { useMessage } from 'hooks' |
| 12 | +import { isNumber } from 'lodash-es' |
| 13 | +import type React from 'react' |
| 14 | +import { useMemo } from 'react' |
| 15 | +import { useTranslation } from 'react-i18next' |
| 16 | +import { Link, useParams } from 'react-router-dom' |
| 17 | +import { memoryKb2String, timeMs2String } from 'utils' |
| 18 | +import { NoDomainUrlError, NoRecordIdError } from 'utils/exception' |
| 19 | +import type { RecordCase } from 'utils/service' |
| 20 | +import Horse from 'utils/service' |
| 21 | + |
| 22 | +const Index: React.FC = () => { |
| 23 | + const { t } = useTranslation() |
| 24 | + const msg = useMessage() |
| 25 | + const { domainUrl, recordId } = |
| 26 | + useParams<{ domainUrl: string; recordId: string }>() |
| 27 | + |
| 28 | + if (!domainUrl) { |
| 29 | + throw new NoDomainUrlError() |
| 30 | + } |
| 31 | + |
| 32 | + if (!recordId) { |
| 33 | + throw new NoRecordIdError() |
| 34 | + } |
| 35 | + |
| 36 | + const columns: ColumnsType<RecordCase> = [ |
| 37 | + { |
| 38 | + title: '#', |
| 39 | + render: (_text, _record, index) => <span>#{index + 1}</span>, |
| 40 | + width: 70 |
| 41 | + }, |
| 42 | + { |
| 43 | + title: t('RecordDetail.state'), |
| 44 | + dataIndex: 'state', |
| 45 | + key: 'state', |
| 46 | + render: (_value, record) => <RecordCaseStatus caseResult={record.state} /> |
| 47 | + }, |
| 48 | + { |
| 49 | + title: t('RecordDetail.time'), |
| 50 | + dataIndex: 'timeMs', |
| 51 | + key: 'timeMs', |
| 52 | + width: 100, |
| 53 | + render: value => (isNumber(value) ? timeMs2String(value) : 'N/A') |
| 54 | + }, |
| 55 | + { |
| 56 | + title: t('RecordDetail.memory'), |
| 57 | + dataIndex: 'memoryKb', |
| 58 | + key: 'memoryKb', |
| 59 | + width: 100, |
| 60 | + render: value => (isNumber(value) ? memoryKb2String(value) : 'N/A') |
| 61 | + } |
| 62 | + ] |
| 63 | + |
| 64 | + const { data: record, loading } = useRequest( |
| 65 | + async () => { |
| 66 | + const response = await Horse.record.v1GetRecord(recordId, domainUrl) |
| 67 | + return response.data.data |
| 68 | + }, |
| 69 | + { |
| 70 | + onError: () => { |
| 71 | + msg.error.fetch(t('RecordDetail.record')) |
| 72 | + } |
| 73 | + } |
| 74 | + ) |
| 75 | + |
| 76 | + const problemLink = useMemo(() => { |
| 77 | + if (record?.problemId && record.problemSetId) |
| 78 | + return ( |
| 79 | + <Link |
| 80 | + to={`/domain/${domainUrl}/problem-set/${record.problemSetId}/problem/${record.problemId}`} |
| 81 | + > |
| 82 | + {record.problemId} |
| 83 | + </Link> |
| 84 | + ) |
| 85 | + if (record?.problemId) |
| 86 | + return ( |
| 87 | + <Link to={`/domain/${domainUrl}/problem/${record.problemId}`}> |
| 88 | + {record.problemId} |
| 89 | + </Link> |
| 90 | + ) |
| 91 | + return 'N/A' |
| 92 | + }, [domainUrl, record?.problemId, record?.problemSetId]) |
| 93 | + |
| 94 | + const sideInfo = ( |
| 95 | + <ShadowCard |
| 96 | + loading={loading} |
| 97 | + title={ |
| 98 | + <h3 className='m-0 text-lg font-medium leading-6 text-gray-900'> |
| 99 | + {t('RecordDetail.title')} |
| 100 | + </h3> |
| 101 | + } |
| 102 | + > |
| 103 | + <dl> |
| 104 | + <div className='py-1 sm:grid sm:grid-cols-3 sm:gap-4'> |
| 105 | + <dt className='text-sm font-medium text-gray-500'>Submit By</dt> |
| 106 | + <dd className='mt-1 text-sm text-gray-900 sm:col-span-2 sm:mt-0'> |
| 107 | + {record?.committerId ? ( |
| 108 | + <UserBadge userId={record.committerId} /> |
| 109 | + ) : ( |
| 110 | + 'N/A' |
| 111 | + )} |
| 112 | + </dd> |
| 113 | + </div> |
| 114 | + <div className='py-1 sm:grid sm:grid-cols-3 sm:gap-4'> |
| 115 | + <dt className='text-sm font-medium text-gray-500'>Problem</dt> |
| 116 | + <dd className='mt-1 text-sm text-gray-900 sm:col-span-2 sm:mt-0'> |
| 117 | + {problemLink} |
| 118 | + </dd> |
| 119 | + </div> |
| 120 | + {record?.problemSetId ? ( |
| 121 | + <div className='py-1 sm:grid sm:grid-cols-3 sm:gap-4'> |
| 122 | + <dt className='text-sm font-medium text-gray-500'>Assignment</dt> |
| 123 | + <dd className='mt-1 text-sm text-gray-900 sm:col-span-2 sm:mt-0'> |
| 124 | + <Link |
| 125 | + to={`/domain/${domainUrl}/problem-set/${record.problemSetId}`} |
| 126 | + > |
| 127 | + {record.problemSetId} |
| 128 | + </Link> |
| 129 | + </dd> |
| 130 | + </div> |
| 131 | + ) : null} |
| 132 | + <div className='py-1 sm:grid sm:grid-cols-3 sm:gap-4'> |
| 133 | + <dt className='text-sm font-medium text-gray-500'>Language</dt> |
| 134 | + <dd className='mt-1 text-sm text-gray-900 sm:col-span-2 sm:mt-0'> |
| 135 | + {record?.language} |
| 136 | + </dd> |
| 137 | + </div> |
| 138 | + <div className='py-1 sm:grid sm:grid-cols-3 sm:gap-4'> |
| 139 | + <dt className='text-sm font-medium text-gray-500'>Submit At</dt> |
| 140 | + <dd className='mt-1 text-sm text-gray-900 sm:col-span-2 sm:mt-0'> |
| 141 | + {record?.createdAt |
| 142 | + ? dayjs(record.createdAt).format('YYYY-MM-DD HH:mm:ss') |
| 143 | + : 'N/A'} |
| 144 | + </dd> |
| 145 | + </div> |
| 146 | + <div className='py-1 sm:grid sm:grid-cols-3 sm:gap-4'> |
| 147 | + <dt className='text-sm font-medium text-gray-500'>Judged At</dt> |
| 148 | + <dd className='mt-1 text-sm text-gray-900 sm:col-span-2 sm:mt-0'> |
| 149 | + {record?.judgedAt |
| 150 | + ? dayjs(record.judgedAt).format('YYYY-MM-DD HH:mm:ss') |
| 151 | + : 'N/A'} |
| 152 | + </dd> |
| 153 | + </div> |
| 154 | + </dl> |
| 155 | + <dl className='m-0'> |
| 156 | + <div className='py-1 sm:grid sm:grid-cols-3 sm:gap-4'> |
| 157 | + <dt className='text-sm font-medium text-gray-500'>Score</dt> |
| 158 | + <dd className='mt-1 text-sm text-gray-900 sm:col-span-2 sm:mt-0'> |
| 159 | + {record?.score ?? 'N/A'} |
| 160 | + </dd> |
| 161 | + </div> |
| 162 | + <div className='py-1 sm:grid sm:grid-cols-3 sm:gap-4'> |
| 163 | + <dt className='text-sm font-medium text-gray-500'>Total Time</dt> |
| 164 | + <dd className='mt-1 text-sm text-gray-900 sm:col-span-2 sm:mt-0'> |
| 165 | + {record?.timeMs === undefined |
| 166 | + ? 'N/A' |
| 167 | + : timeMs2String(record.timeMs)} |
| 168 | + </dd> |
| 169 | + </div> |
| 170 | + <div className='py-1 sm:grid sm:grid-cols-3 sm:gap-4'> |
| 171 | + <dt className='text-sm font-medium text-gray-500'>Peak Memory</dt> |
| 172 | + <dd className='mt-1 text-sm text-gray-900 sm:col-span-2 sm:mt-0'> |
| 173 | + {record?.memoryKb === undefined |
| 174 | + ? 'N/A' |
| 175 | + : memoryKb2String(record.memoryKb)} |
| 176 | + </dd> |
| 177 | + </div> |
| 178 | + </dl> |
| 179 | + </ShadowCard> |
| 180 | + ) |
| 181 | + |
| 182 | + return ( |
| 183 | + <div> |
| 184 | + <Head title={t('RecordDetail.title')} /> |
| 185 | + <SidePage extra={sideInfo}> |
| 186 | + <ShadowCard |
| 187 | + noPadding={!loading} |
| 188 | + loading={loading} |
| 189 | + title={ |
| 190 | + loading ? ( |
| 191 | + t('RecordDetail.title') |
| 192 | + ) : ( |
| 193 | + <RecordStatus |
| 194 | + domainUrl={domainUrl} |
| 195 | + record={record} |
| 196 | + size='large' |
| 197 | + /> |
| 198 | + ) |
| 199 | + } |
| 200 | + > |
| 201 | + <Table |
| 202 | + columns={columns} |
| 203 | + dataSource={record?.cases} |
| 204 | + loading={loading} |
| 205 | + /> |
| 206 | + </ShadowCard> |
| 207 | + </SidePage> |
| 208 | + </div> |
| 209 | + ) |
| 210 | +} |
| 211 | + |
| 212 | +export default Index |
0 commit comments