-
Notifications
You must be signed in to change notification settings - Fork 9
Add a display for current session/match in event page #1849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
CoolGame8
wants to merge
1
commit into
main
Choose a base branch
from
display-for-current-session/match
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
210 changes: 210 additions & 0 deletions
210
apps/portal/src/app/[locale]/event/[slug]/components/current-activity.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| 'use client'; | ||
|
|
||
| import Link from 'next/link'; | ||
| import { useParams } from 'next/navigation'; | ||
| import { useTranslations } from 'next-intl'; | ||
| import dayjs from 'dayjs'; | ||
| import { Paper, Typography, Stack, Box, Chip } from '@mui/material'; | ||
| import { PlayArrow, Schedule, Gavel } from '@mui/icons-material'; | ||
| import { useMatchTranslations } from '@lems/localization'; | ||
| import { CurrentActivity } from '@lems/types/api/portal'; | ||
| import { useRealtimeData } from '../../../hooks/use-realtime-data'; | ||
| import { useDivision } from './division-data-context'; | ||
|
|
||
| export const CurrentActivityDisplay = () => { | ||
| const t = useTranslations('pages.event'); | ||
| const { getStage } = useMatchTranslations(); | ||
| const division = useDivision(); | ||
| const params = useParams(); | ||
| const eventSlug = params.slug as string; | ||
|
|
||
| const { data } = useRealtimeData<CurrentActivity>( | ||
| `/portal/divisions/${division.id}/current-activity`, | ||
| { suspense: true } | ||
| ); | ||
|
|
||
| if (!data) { | ||
| return null; | ||
| } | ||
|
|
||
| const { activeMatch, loadedMatch, currentSessions } = data; | ||
|
|
||
| // Don't show anything if there's no current activity | ||
| if (!activeMatch && !loadedMatch && currentSessions.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <Paper | ||
| sx={{ | ||
| p: 2, | ||
| mb: 3, | ||
| border: '1px solid', | ||
| borderColor: 'primary.main', | ||
| bgcolor: 'primary.50' | ||
| }} | ||
| > | ||
| <Stack direction="row" alignItems="center" spacing={1} sx={{ mb: 1.5 }}> | ||
| <PlayArrow color="primary" /> | ||
| <Typography variant="h6" fontWeight="bold" color="primary.main"> | ||
| {t('current-activity.title')} | ||
| </Typography> | ||
| </Stack> | ||
|
|
||
| <Stack spacing={1.5}> | ||
| {/* Active Match */} | ||
| {activeMatch && ( | ||
| <Box | ||
| sx={{ | ||
| p: 1.5, | ||
| borderRadius: 1, | ||
| bgcolor: 'white', | ||
| border: '1px solid', | ||
| borderColor: 'error.main' | ||
| }} | ||
| > | ||
| <Stack direction="row" alignItems="center" spacing={1} sx={{ mb: 0.5 }}> | ||
| <Typography variant="subtitle2" fontWeight="bold" color="text.primary"> | ||
| {t('current-activity.active-match')} | ||
| </Typography> | ||
| <Chip | ||
| label={t('current-activity.live')} | ||
| size="small" | ||
| color="error" | ||
| sx={{ | ||
| fontWeight: 'bold', | ||
| fontSize: '0.7rem', | ||
| height: 20 | ||
| }} | ||
| /> | ||
| </Stack> | ||
| <Typography variant="body2" color="text.secondary" sx={{ mb: 0.5 }}> | ||
| {getStage(activeMatch.stage)} {t('current-activity.round')} {activeMatch.round},{' '} | ||
| {t('current-activity.match')} #{activeMatch.number} | ||
| </Typography> | ||
| <Stack direction="row" spacing={2} flexWrap="wrap"> | ||
| {activeMatch.participants.map((participant, idx) => ( | ||
| <Box key={idx}> | ||
| {participant.team ? ( | ||
| <Link | ||
| href={`/event/${eventSlug}/team/${participant.team.slug}`} | ||
| style={{ textDecoration: 'none' }} | ||
| > | ||
| <Typography | ||
| variant="body2" | ||
| color="primary.main" | ||
| sx={{ '&:hover': { textDecoration: 'underline' } }} | ||
| > | ||
| {participant.table.name}: #{participant.team.number} {participant.team.name} | ||
| </Typography> | ||
| </Link> | ||
| ) : ( | ||
| <Typography variant="body2" color="text.disabled"> | ||
| {participant.table.name}: {t('current-activity.no-team')} | ||
| </Typography> | ||
| )} | ||
| </Box> | ||
| ))} | ||
| </Stack> | ||
| </Box> | ||
| )} | ||
|
|
||
| {/* Loaded Match (Next Up) */} | ||
| {loadedMatch && !activeMatch && ( | ||
| <Box | ||
| sx={{ | ||
| p: 1.5, | ||
| borderRadius: 1, | ||
| bgcolor: 'white', | ||
| border: '1px solid', | ||
| borderColor: 'grey.300' | ||
| }} | ||
| > | ||
| <Stack direction="row" alignItems="center" spacing={1} sx={{ mb: 0.5 }}> | ||
| <Schedule fontSize="small" color="action" /> | ||
| <Typography variant="subtitle2" fontWeight="bold" color="text.primary"> | ||
| {t('current-activity.next-match')} | ||
| </Typography> | ||
| </Stack> | ||
| <Typography variant="body2" color="text.secondary" sx={{ mb: 0.5 }}> | ||
| {getStage(loadedMatch.stage)} {t('current-activity.round')} {loadedMatch.round},{' '} | ||
| {t('current-activity.match')} #{loadedMatch.number} | ||
| </Typography> | ||
| <Typography variant="caption" color="text.disabled" sx={{ mb: 0.5, display: 'block' }}> | ||
| {t('current-activity.scheduled-at')}:{' '} | ||
| {dayjs(loadedMatch.scheduledTime).format('HH:mm')} | ||
| </Typography> | ||
| <Stack direction="row" spacing={2} flexWrap="wrap"> | ||
| {loadedMatch.participants.map((participant, idx) => ( | ||
| <Box key={idx}> | ||
| {participant.team ? ( | ||
| <Link | ||
| href={`/event/${eventSlug}/team/${participant.team.slug}`} | ||
| style={{ textDecoration: 'none' }} | ||
| > | ||
| <Typography | ||
| variant="body2" | ||
| color="primary.main" | ||
| sx={{ '&:hover': { textDecoration: 'underline' } }} | ||
| > | ||
| {participant.table.name}: #{participant.team.number} {participant.team.name} | ||
| </Typography> | ||
| </Link> | ||
| ) : ( | ||
| <Typography variant="body2" color="text.disabled"> | ||
| {participant.table.name}: {t('current-activity.no-team')} | ||
| </Typography> | ||
| )} | ||
| </Box> | ||
| ))} | ||
| </Stack> | ||
| </Box> | ||
| )} | ||
|
|
||
| {/* Current Judging Sessions */} | ||
| {currentSessions.length > 0 && ( | ||
| <Box | ||
| sx={{ | ||
| p: 1.5, | ||
| borderRadius: 1, | ||
| bgcolor: 'white', | ||
| border: '1px solid', | ||
| borderColor: 'grey.300' | ||
| }} | ||
| > | ||
| <Stack direction="row" alignItems="center" spacing={1} sx={{ mb: 0.5 }}> | ||
| <Gavel fontSize="small" color="action" /> | ||
| <Typography variant="subtitle2" fontWeight="bold" color="text.primary"> | ||
| {t('current-activity.judging-sessions')} ({currentSessions.length}) | ||
| </Typography> | ||
| </Stack> | ||
| <Stack spacing={0.5}> | ||
| {currentSessions.map(session => ( | ||
| <Box key={session.id}> | ||
| {session.team ? ( | ||
| <Link | ||
| href={`/event/${eventSlug}/team/${session.team.slug}`} | ||
| style={{ textDecoration: 'none' }} | ||
| > | ||
| <Typography | ||
| variant="body2" | ||
| color="primary.main" | ||
| sx={{ '&:hover': { textDecoration: 'underline' } }} | ||
| > | ||
| {session.room.name}: #{session.team.number} {session.team.name} | ||
| </Typography> | ||
| </Link> | ||
| ) : ( | ||
| <Typography variant="body2" color="text.disabled"> | ||
| {session.room.name}: {t('current-activity.no-team')} | ||
| </Typography> | ||
| )} | ||
| </Box> | ||
| ))} | ||
| </Stack> | ||
| </Box> | ||
| )} | ||
| </Stack> | ||
| </Paper> | ||
| ); | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Missing rate limiting High
Copilot Autofix
AI 9 days ago
In general, the problem is solved by applying a rate-limiting middleware to the route (or router) so that each client can only call the expensive endpoint a limited number of times per time window. In Express, a common solution is the well-known
express-rate-limitpackage, which provides a simple middleware you can attach to specific routes without changing their existing behavior besides rejecting excessive requests.For this code, the least invasive and clearest fix is to import
express-rate-limit, define a limiter configured for the “current activity” endpoint, and apply it specifically to the/ :divisionId/current-activityroute. This avoids changing the logic of the handler and does not affect other endpoints in this router. Concretely, inapps/backend/src/routers/portal/divisions/index.tswe will: (1) add an import forexpress-rate-limit; (2) define acurrentActivityLimiterconstant near the top of the file, for example allowing a reasonable number of requests per IP per minute; and (3) update therouter.get('/:divisionId/current-activity', ...)call to insertcurrentActivityLimiteras middleware before the async handler. No other routes or behavior need to be changed.