11/**
22 * Tab content for submissions during the submission phase.
33 */
4- import {
5- FC ,
6- MouseEvent ,
7- useCallback ,
8- useContext ,
9- useMemo ,
10- useState ,
11- } from 'react'
4+ import { FC , MouseEvent , useContext , useMemo } from 'react'
125import { toast } from 'react-toastify'
136import _ from 'lodash'
147import classNames from 'classnames'
@@ -21,7 +14,6 @@ import { MobileTableColumn } from '~/apps/admin/src/lib/models/MobileTableColumn
2114import { copyTextToClipboard , useWindowSize , WindowSize } from '~/libs/shared'
2215import { IconOutline , Table , TableColumn , Tooltip } from '~/libs/ui'
2316
24- import { SubmissionHistoryModal } from '../SubmissionHistoryModal'
2517import {
2618 BackendSubmission ,
2719 ChallengeDetailContextModel ,
@@ -35,7 +27,6 @@ import type { UseSubmissionDownloadAccessResult } from '../../hooks/useSubmissio
3527import { ChallengeDetailContext } from '../../contexts'
3628import {
3729 challengeHasSubmissionLimit ,
38- getSubmissionHistoryKey ,
3930 hasIsLatestFlag ,
4031 partitionSubmissionHistory ,
4132} from '../../utils'
@@ -66,26 +57,7 @@ export const TabContentSubmissions: FC<Props> = props => {
6657 getRestrictionMessageForMember,
6758 } : UseSubmissionDownloadAccessResult = useSubmissionDownloadAccess ( )
6859
69- const {
70- challengeInfo,
71- myResources,
72- } : ChallengeDetailContextModel = useContext ( ChallengeDetailContext )
73-
74- const ownedMemberIds = useMemo < Set < string > > (
75- ( ) => {
76- const ids = new Set < string > ( )
77- myResources . forEach ( resource => {
78- const memberId = resource ?. memberId
79- if ( memberId === undefined || memberId === null ) {
80- return
81- }
82-
83- ids . add ( String ( memberId ) )
84- } )
85- return ids
86- } ,
87- [ myResources ] ,
88- )
60+ const { challengeInfo } : ChallengeDetailContextModel = useContext ( ChallengeDetailContext )
8961
9062 const submissionMetaById = useMemo (
9163 ( ) => {
@@ -109,11 +81,7 @@ export const TabContentSubmissions: FC<Props> = props => {
10981 ( ) => partitionSubmissionHistory ( submissionInfos , submissionInfos ) ,
11082 [ submissionInfos ] ,
11183 )
112- const {
113- historyByMember,
114- latestSubmissionIds,
115- latestSubmissions,
116- } : SubmissionHistoryPartition = submissionHistory
84+ const { latestSubmissions } : SubmissionHistoryPartition = submissionHistory
11785
11886 const latestBackendSubmissions = useMemo < BackendSubmission [ ] > (
11987 ( ) => latestSubmissions
@@ -127,14 +95,14 @@ export const TabContentSubmissions: FC<Props> = props => {
12795 [ challengeInfo ] ,
12896 )
12997
130- const shouldShowHistoryActions = useMemo (
98+ const hasLatestFlag = useMemo (
13199 ( ) => hasIsLatestFlag ( props . submissions ) ,
132100 [ props . submissions ] ,
133101 )
134102
135103 const filteredSubmissions = useMemo < BackendSubmission [ ] > (
136104 ( ) => {
137- if ( restrictToLatest && shouldShowHistoryActions ) {
105+ if ( restrictToLatest && hasLatestFlag ) {
138106 return latestBackendSubmissions . length
139107 ? latestBackendSubmissions
140108 : props . submissions
@@ -146,106 +114,10 @@ export const TabContentSubmissions: FC<Props> = props => {
146114 latestBackendSubmissions ,
147115 props . submissions ,
148116 restrictToLatest ,
149- shouldShowHistoryActions ,
117+ hasLatestFlag ,
150118 ] ,
151119 )
152120
153- const [ historyKey , setHistoryKey ] = useState < string | undefined > ( undefined )
154-
155- const openHistoryModal = useCallback (
156- ( memberId : string | undefined , submissionId : string ) : void => {
157- const key = getSubmissionHistoryKey ( memberId , submissionId )
158- const entries = historyByMember . get ( key )
159- if ( ! entries || entries . length === 0 ) {
160- return
161- }
162-
163- setHistoryKey ( key )
164- } ,
165- [ historyByMember ] ,
166- )
167-
168- const closeHistoryModal = useCallback ( ( ) : void => {
169- setHistoryKey ( undefined )
170- } , [ ] )
171-
172- const historyEntriesForModal = useMemo < SubmissionInfo [ ] > (
173- ( ) => {
174- if ( ! historyKey ) {
175- return [ ]
176- }
177-
178- const entries = historyByMember . get ( historyKey ) ?? [ ]
179- if ( ! entries . length ) {
180- return [ ]
181- }
182-
183- return entries . map ( entry => {
184- if ( ! entry ?. id ) {
185- return entry
186- }
187-
188- const meta = submissionMetaById . get ( entry . id )
189- if ( ! meta ) {
190- return entry
191- }
192-
193- const metaInfo = convertBackendSubmissionToSubmissionInfo ( meta )
194- return {
195- ...metaInfo ,
196- ...entry ,
197- userInfo : entry . userInfo ?? metaInfo . userInfo ,
198- }
199- } )
200- } ,
201- [ historyByMember , historyKey , submissionMetaById ] ,
202- )
203-
204- const getSubmissionMeta = useCallback (
205- ( submissionId : string ) : SubmissionInfo | undefined => {
206- const submission = submissionMetaById . get ( submissionId )
207- return submission
208- ? convertBackendSubmissionToSubmissionInfo ( submission )
209- : undefined
210- } ,
211- [ submissionMetaById ] ,
212- )
213-
214- const getHistoryRestriction = useCallback (
215- ( submission : SubmissionInfo ) : { restricted : boolean ; message ?: string } => {
216- const restrictedForMember = isSubmissionDownloadRestrictedForMember ( submission . memberId )
217- const message = restrictedForMember
218- ? getRestrictionMessageForMember ( submission . memberId ) ?? restrictionMessage
219- : undefined
220-
221- return {
222- message,
223- restricted : restrictedForMember ,
224- }
225- } ,
226- [
227- getRestrictionMessageForMember ,
228- isSubmissionDownloadRestrictedForMember ,
229- restrictionMessage ,
230- ] ,
231- )
232-
233- const handleHistoryButtonClick = useCallback (
234- ( event : MouseEvent < HTMLButtonElement > ) : void => {
235- const submissionId = event . currentTarget . dataset . submissionId
236- if ( ! submissionId ) {
237- return
238- }
239-
240- const memberIdValue = event . currentTarget . dataset . memberId
241- const normalizedMemberId = memberIdValue && memberIdValue . length ? memberIdValue : undefined
242- openHistoryModal ( normalizedMemberId , submissionId )
243- } ,
244- [ openHistoryModal ] ,
245- )
246-
247- const isHistoryModalOpen = Boolean ( historyKey )
248-
249121 const columns = useMemo < TableColumn < BackendSubmission > [ ] > (
250122 ( ) => {
251123 const baseColumns : TableColumn < BackendSubmission > [ ] = [
@@ -369,62 +241,12 @@ export const TabContentSubmissions: FC<Props> = props => {
369241 } ,
370242 ]
371243
372- if ( shouldShowHistoryActions ) {
373- baseColumns . push ( {
374- label : 'Actions' ,
375- propertyName : 'actions' ,
376- renderer : ( submission : BackendSubmission ) => {
377- if ( ! submission . id ) {
378- return < span > --</ span >
379- }
380-
381- const isLatestSubmission = latestSubmissionIds . has ( submission . id )
382- if ( ! isLatestSubmission ) {
383- return < span > --</ span >
384- }
385-
386- const ownsSubmission = ownedMemberIds . has ( String ( submission . memberId ) )
387- if ( ! ownsSubmission ) {
388- return < span > --</ span >
389- }
390-
391- const historyKeyValue = getSubmissionHistoryKey ( submission . memberId , submission . id )
392- const entries = historyByMember . get ( historyKeyValue ) ?? [ ]
393- const relevantHistory = entries . filter (
394- historyEntry => historyEntry . id && historyEntry . id !== submission . id ,
395- )
396-
397- if ( ! relevantHistory . length ) {
398- return < span > --</ span >
399- }
400-
401- return (
402- < button
403- type = 'button'
404- className = { styles . textBlue }
405- data-member-id = { submission . memberId }
406- data-submission-id = { submission . id }
407- onClick = { handleHistoryButtonClick }
408- >
409- View Submission History
410- </ button >
411- )
412- } ,
413- type : 'element' ,
414- } )
415- }
416-
417244 return baseColumns
418245 } ,
419246 [
420247 getRestrictionMessageForMember ,
421- handleHistoryButtonClick ,
422- historyByMember ,
423248 isSubmissionDownloadRestrictedForMember ,
424- latestSubmissionIds ,
425- ownedMemberIds ,
426249 restrictionMessage ,
427- shouldShowHistoryActions ,
428250 props . downloadSubmission ,
429251 props . isDownloading ,
430252 ] ,
@@ -464,30 +286,19 @@ export const TabContentSubmissions: FC<Props> = props => {
464286 }
465287
466288 return (
467- < >
468- < TableWrapper className = { classNames ( styles . container , 'enhanced-table' ) } >
469- { isTablet ? (
470- < TableMobile columns = { columnsMobile } data = { filteredSubmissions } />
471- ) : (
472- < Table
473- columns = { columns }
474- data = { filteredSubmissions }
475- disableSorting
476- onToggleSort = { _ . noop }
477- removeDefaultSort
478- />
479- ) }
480- </ TableWrapper >
481- < SubmissionHistoryModal
482- open = { isHistoryModalOpen }
483- onClose = { closeHistoryModal }
484- submissions = { historyEntriesForModal }
485- downloadSubmission = { props . downloadSubmission }
486- isDownloading = { props . isDownloading }
487- getRestriction = { getHistoryRestriction }
488- getSubmissionMeta = { getSubmissionMeta }
489- />
490- </ >
289+ < TableWrapper className = { classNames ( styles . container , 'enhanced-table' ) } >
290+ { isTablet ? (
291+ < TableMobile columns = { columnsMobile } data = { filteredSubmissions } />
292+ ) : (
293+ < Table
294+ columns = { columns }
295+ data = { filteredSubmissions }
296+ disableSorting
297+ onToggleSort = { _ . noop }
298+ removeDefaultSort
299+ />
300+ ) }
301+ </ TableWrapper >
491302 )
492303}
493304
0 commit comments