|
| 1 | +/* |
| 2 | + * This file is part of NER's FinishLine and licensed under GNU AGPLv3. |
| 3 | + * See the LICENSE file in the repository root folder for details. |
| 4 | + */ |
| 5 | + |
| 6 | +import React, { useState } from 'react'; |
| 7 | +import { Box, useTheme, Collapse, Tabs, Tab, Typography } from '@mui/material'; |
| 8 | +import { ChangeRequest } from 'shared'; |
| 9 | +import ChangeRequestDetailCard from '../../components/ChangeRequestDetailCard'; |
| 10 | +import { useAllChangeRequests } from '../../hooks/change-requests.hooks'; |
| 11 | +import { ExpandLess, ExpandMore } from '@mui/icons-material'; |
| 12 | +import LoadingIndicator from '../../components/LoadingIndicator'; |
| 13 | +import ErrorPage from '../ErrorPage'; |
| 14 | +import { fullNamePipe } from '../../utils/pipes'; |
| 15 | + |
| 16 | +interface OtherChangeRequestsPopupTabsProps { |
| 17 | + changeRequest: ChangeRequest; |
| 18 | +} |
| 19 | + |
| 20 | +const OtherChangeRequestsPopupTabs: React.FC<OtherChangeRequestsPopupTabsProps> = ({ |
| 21 | + changeRequest |
| 22 | +}: OtherChangeRequestsPopupTabsProps) => { |
| 23 | + const theme = useTheme(); |
| 24 | + const [tab, setTab] = useState(0); |
| 25 | + const { data: changeRequests, isError, isLoading, error } = useAllChangeRequests(); |
| 26 | + |
| 27 | + if (isLoading) return <LoadingIndicator />; |
| 28 | + if (isError) return <ErrorPage error={error} message={error.message} />; |
| 29 | + |
| 30 | + // the CRs submitted or reviewed by the submitter of this CR |
| 31 | + const crsFromSubmitter = changeRequests |
| 32 | + ?.filter( |
| 33 | + (cr) => |
| 34 | + (cr.submitter.userId === changeRequest.submitter.userId || cr.reviewer?.userId === changeRequest.submitter.userId) && |
| 35 | + cr.crId !== changeRequest.crId |
| 36 | + ) |
| 37 | + .sort((a: ChangeRequest, b: ChangeRequest) => { |
| 38 | + return b.dateSubmitted.getTime() - a.dateSubmitted.getTime(); |
| 39 | + }); |
| 40 | + |
| 41 | + const displayTab = (value: number, title: string) => ( |
| 42 | + <Tab |
| 43 | + value={value} |
| 44 | + sx={{ borderRadius: '16px 16px 0 0' }} |
| 45 | + label={ |
| 46 | + <Typography sx={{ display: 'flex' }}> |
| 47 | + {title} |
| 48 | + {tab === value ? <ExpandMore sx={{ pl: 0.5 }} /> : <ExpandLess sx={{ pl: 0.5 }} />} |
| 49 | + </Typography> |
| 50 | + } |
| 51 | + onClick={() => tab === value && setTab(0)} |
| 52 | + /> |
| 53 | + ); |
| 54 | + |
| 55 | + const displayCRCards = (crList: ChangeRequest[]) => ( |
| 56 | + <Box |
| 57 | + sx={{ |
| 58 | + display: 'flex', |
| 59 | + justifyContent: 'flex-start', |
| 60 | + padding: '20px', |
| 61 | + background: theme.palette.background.paper, |
| 62 | + borderTop: `solid 1px ${theme.palette.divider}`, |
| 63 | + borderLeft: `solid 1px ${theme.palette.divider}`, |
| 64 | + '&::-webkit-scrollbar': { |
| 65 | + height: '20px' |
| 66 | + }, |
| 67 | + '&::-webkit-scrollbar-track': { |
| 68 | + backgroundColor: 'transparent' |
| 69 | + }, |
| 70 | + '&::-webkit-scrollbar-thumb': { |
| 71 | + backgroundColor: theme.palette.divider, |
| 72 | + borderRadius: '20px', |
| 73 | + border: '6px solid transparent', |
| 74 | + backgroundClip: 'content-box' |
| 75 | + }, |
| 76 | + overflowX: 'scroll' |
| 77 | + }} |
| 78 | + > |
| 79 | + {crList.length !== 0 ? ( |
| 80 | + crList.map((cr: ChangeRequest) => <ChangeRequestDetailCard changeRequest={cr} />) |
| 81 | + ) : ( |
| 82 | + <Typography>No related change requests.</Typography> |
| 83 | + )} |
| 84 | + </Box> |
| 85 | + ); |
| 86 | + |
| 87 | + return ( |
| 88 | + <Box |
| 89 | + sx={{ |
| 90 | + position: 'fixed', |
| 91 | + bottom: '0px', |
| 92 | + left: '65px', |
| 93 | + width: 'calc(100% - 65px)' |
| 94 | + }} |
| 95 | + > |
| 96 | + <Tabs |
| 97 | + value={tab} |
| 98 | + onChange={(e, newValue: number) => setTab(newValue)} |
| 99 | + sx={{ |
| 100 | + '.MuiTabs-indicator': { |
| 101 | + background: 'transparent' |
| 102 | + }, |
| 103 | + '& button': { |
| 104 | + background: theme.palette.background.paper, |
| 105 | + border: `solid 1px ${theme.palette.divider}`, |
| 106 | + color: theme.palette.text.primary, |
| 107 | + textTransform: 'none' |
| 108 | + }, |
| 109 | + '& button.Mui-selected': { |
| 110 | + color: theme.palette.text.primary, |
| 111 | + borderBottom: 'transparent' |
| 112 | + }, |
| 113 | + mb: '-1px' |
| 114 | + }} |
| 115 | + > |
| 116 | + {displayTab(1, `Other CR's from ${fullNamePipe(changeRequest.submitter)}`)} |
| 117 | + </Tabs> |
| 118 | + <Collapse in={tab !== 0}>{tab === 1 && displayCRCards(crsFromSubmitter || [])}</Collapse> |
| 119 | + </Box> |
| 120 | + ); |
| 121 | +}; |
| 122 | + |
| 123 | +export default OtherChangeRequestsPopupTabs; |
0 commit comments