-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSolutionFiles.js
More file actions
233 lines (220 loc) · 9.74 KB
/
SolutionFiles.js
File metadata and controls
233 lines (220 loc) · 9.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import React from 'react';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { FormattedMessage } from 'react-intl';
import { Table, OverlayTrigger, Tooltip } from 'react-bootstrap';
import { lruMemoize } from 'reselect';
import DownloadSolutionArchiveContainer from '../../../containers/DownloadSolutionArchiveContainer';
import ResourceRenderer from '../../helpers/ResourceRenderer';
import Callout from '../../widgets/Callout';
import Button, { TheButtonGroup } from '../../widgets/TheButton';
import Box from '../../widgets/Box';
import Icon, { CodeFileIcon, DownloadIcon, WarningIcon, ZipIcon } from '../../icons';
import { prettyPrintBytes } from '../../helpers/stringFormatters.js';
const nameComparator = (a, b) => a.name.localeCompare(b.name, 'en');
const preprocessZipEntries = ({ zipEntries, ...file }) => {
if (zipEntries) {
file.zipEntriesBadNames = zipEntries.reduce((bad, { name }) => bad || name.includes('\\'), false);
file.zipEntries = zipEntries
.filter(({ name, size }) => !(name.endsWith('/') || name.endsWith('\\')) || size !== 0)
.map(({ name, size }) => ({ name, size, id: `${file.id}/${name}`, parentId: file.id }))
.sort(nameComparator);
}
return file;
};
const preprocessFiles = lruMemoize(files =>
files
.sort(nameComparator)
.map(preprocessZipEntries)
.reduce((acc, file) => [...acc, file, ...(file.zipEntries || [])], [])
);
const SolutionFiles = ({
solutionId,
attemptIndex,
files,
authorId,
isReference = false,
solutionFilesLimit = null,
solutionSizeLimit = null,
openFile = null,
download = null,
}) =>
files && (
<ResourceRenderer resource={files}>
{files => {
const filesSize = files.reduce((acc, { size }) => acc + size, 0);
return (
<>
{isReference && solutionFilesLimit !== null && files.length > solutionFilesLimit && (
<Callout variant="warning">
<FormattedMessage
id="app.solutionFiles.countLimitExceeded"
defaultMessage="The total number of submitted files exceeds the default solution files limit ({limit})."
values={{ limit: solutionFilesLimit }}
/>
</Callout>
)}
{isReference && solutionSizeLimit !== null && filesSize > solutionSizeLimit && (
<Callout variant="warning">
<FormattedMessage
id="app.solutionFiles.sizeLimitExceeded"
defaultMessage="The total size of all submitted files exceeds the default solution size limit ({limit} KiB)."
values={{ limit: Math.ceil(solutionSizeLimit / 1024) }}
/>
</Callout>
)}
<Box
collapsable
noPadding
unlimitedHeight
title={<FormattedMessage id="app.solutionFiles.title" defaultMessage="Submitted Files" />}>
{files.length === 0 ? (
<div className="text-center text-body-secondary small">
<FormattedMessage id="app.solutionFiles.noFiles" defaultMessage="No files were submitted." />
</div>
) : (
<Table hover className="card-table">
<tbody>
{preprocessFiles(files).map(file => (
<tr key={file.id}>
{file.parentId && (
<td className="text-nowrap shrink-col text-body-secondary pe-0">
<Icon icon="level-up-alt" className="fa-rotate-90" gapLeft={2} />
</td>
)}
<td className="text-nowrap shrink-col text-body-secondary">
{file.isEntryPoint ? (
<Icon
icon="sign-in-alt"
className="text-success"
tooltipId={`entrypoint-${file.id}`}
tooltipPlacement="bottom"
tooltip={
<FormattedMessage
id="app.solutionFiles.entryPoint"
defaultMessage="Execution entry point (bootstrap)"
/>
}
/>
) : file.name.toLowerCase().endsWith('.zip') ? (
<ZipIcon />
) : (
<CodeFileIcon />
)}
</td>
<td className="w-100" colSpan={file.parentId ? 1 : 2}>
<code
className={
file.parentId
? 'text-body-secondary small'
: file.isEntryPoint
? 'text-success fw-bold'
: ''
}>
{file.name}
</code>
{file.zipEntriesBadNames && (
<WarningIcon
gapLeft
className="text-danger"
tooltipId={`${file.id}-badEntries`}
tooltip={
<FormattedMessage
id="app.solutionFiles.badZipEntryNames"
defaultMessage="Some of the ZIP entry names are invalid."
/>
}
/>
)}
</td>
<td className="small text-nowrap">{prettyPrintBytes(file.size)}</td>
<td className="text-nowrap shrink-col text-end">
<TheButtonGroup>
{Boolean(openFile) && !file.name.toLowerCase().endsWith('.zip') && (
<OverlayTrigger
placement="bottom"
overlay={
<Tooltip id={`open-file-${file.id}`}>
<FormattedMessage
id="app.solutionFiles.openButton"
defaultMessage="Open preview in dialog"
/>
</Tooltip>
}>
<Button
onClick={() =>
openFile(file.parentId || file.id, file.name, file.parentId ? file.name : null)
}
size="xs"
variant="secondary">
<Icon icon="book-open" fixedWidth />
</Button>
</OverlayTrigger>
)}
{Boolean(download) && (
<OverlayTrigger
placement="bottom"
overlay={
<Tooltip id={`download-${file.id}`}>
<FormattedMessage
id="app.solutionFiles.downloadButton"
defaultMessage="Download file"
/>
</Tooltip>
}>
<Button
onClick={() => download(file.parentId || file.id, file.parentId ? file.name : null)}
size="xs"
variant="primary">
<DownloadIcon fixedWidth />
</Button>
</OverlayTrigger>
)}
</TheButtonGroup>
</td>
</tr>
))}
</tbody>
<tfoot>
<tr>
<td colSpan={2} />
<td className="small text-body-secondary">
<em>
<FormattedMessage id="app.solutionFiles.total" defaultMessage="Total:" />
</em>
</td>
<td className="small text-body-secondary text-nowrap">
<em>{prettyPrintBytes(filesSize)}</em>
</td>
<td className="text-nowrap shrink-col text-end">
<DownloadSolutionArchiveContainer
solutionId={solutionId}
attemptIndex={attemptIndex}
authorId={authorId}
isReference={isReference}
size="xs"
iconOnly
/>
</td>
</tr>
</tfoot>
</Table>
)}
</Box>
</>
);
}}
</ResourceRenderer>
);
SolutionFiles.propTypes = {
solutionId: PropTypes.string.isRequired,
attemptIndex: PropTypes.number,
files: ImmutablePropTypes.map,
authorId: PropTypes.string.isRequired,
isReference: PropTypes.bool,
solutionFilesLimit: PropTypes.number,
solutionSizeLimit: PropTypes.number,
openFile: PropTypes.func,
download: PropTypes.func,
};
export default SolutionFiles;