|
| 1 | +import { ChangeEvent, FC, useMemo, useState } from "react"; |
| 2 | +import QRCode from "react-qr-code"; |
| 3 | +import { IGroup, Session } from "@/types/sessions"; |
| 4 | +import { format, isWithinInterval, parseISO } from "date-fns"; |
| 5 | +import { SectionWrapper } from "@components/SectionWrapper/SectionWrapper"; |
| 6 | +import { Color } from "@styles/colors"; |
| 7 | + |
| 8 | +interface QrCodeProps { |
| 9 | + currentDateTime: Date; |
| 10 | + openFeedbackId: string; |
| 11 | + tracks?: Array<IGroup>; |
| 12 | +} |
| 13 | + |
| 14 | +interface Track { |
| 15 | + name: string; |
| 16 | + id: number; |
| 17 | +} |
| 18 | + |
| 19 | +const areTracksAvailable = (availableTracks: Set<Track>) => |
| 20 | + availableTracks.size > 0; |
| 21 | + |
| 22 | +const QrCode: FC<React.PropsWithChildren<QrCodeProps>> = ({ |
| 23 | + currentDateTime, |
| 24 | + openFeedbackId, |
| 25 | + tracks, |
| 26 | +}) => { |
| 27 | + const availableTracks: Set<Track> = useMemo<Set<Track>>(() => { |
| 28 | + const list: Track[] = |
| 29 | + tracks |
| 30 | + ?.flatMap((group) => group.sessions) |
| 31 | + .filter((session) => |
| 32 | + isWithinInterval(currentDateTime, { |
| 33 | + start: session.startsAt, |
| 34 | + end: session.endsAt, |
| 35 | + }), |
| 36 | + ) |
| 37 | + ?.map((session) => ({ |
| 38 | + name: session.room, |
| 39 | + id: session.roomId, |
| 40 | + })) |
| 41 | + .sort((a, b) => a.name.localeCompare(b.name)) ?? []; |
| 42 | + |
| 43 | + return new Set(list); |
| 44 | + }, [tracks, currentDateTime]); |
| 45 | + |
| 46 | + const [selectedTrack, setSelectedTrack] = useState<number>( |
| 47 | + Array.from(availableTracks)?.at(0)?.id ?? 0, |
| 48 | + ); |
| 49 | + const availableTalks: Array<Session> | undefined = tracks |
| 50 | + ?.map((track: IGroup) => track.sessions) |
| 51 | + .flat(1) |
| 52 | + .filter((talk) => |
| 53 | + isWithinInterval(currentDateTime, { |
| 54 | + start: talk.startsAt, |
| 55 | + end: talk.endsAt, |
| 56 | + }), |
| 57 | + ); |
| 58 | + |
| 59 | + const [session, setSession] = useState<Session | undefined>( |
| 60 | + availableTalks?.at(0), |
| 61 | + ); |
| 62 | + |
| 63 | + const [qrValue, setQrValue] = useState( |
| 64 | + "https://openfeedback.io/TG4hBcL7iPtV2LecVdHu/2025-07-09/945091", |
| 65 | + ); |
| 66 | + |
| 67 | + const setQrCodeHandler = (e: ChangeEvent<HTMLSelectElement>) => { |
| 68 | + const qrCode: number = parseInt(e.target.value); |
| 69 | + e.preventDefault(); |
| 70 | + setSelectedTrack(qrCode); |
| 71 | + setSession(availableTalks?.filter((talk) => talk.roomId == qrCode)[0]); |
| 72 | + if (session != undefined) |
| 73 | + setQrValue( |
| 74 | + `https://openfeedback.io/${openFeedbackId}/${format(parseISO(session.startsAt), "yyyy-MM-dd")}/${session.id}`, |
| 75 | + ); |
| 76 | + }; |
| 77 | + |
| 78 | + return ( |
| 79 | + <SectionWrapper color={Color.WHITE} marginTop={8}> |
| 80 | + <h1>OpenFeedback QR Code</h1> |
| 81 | + <div |
| 82 | + style={{ |
| 83 | + width: "85vh", |
| 84 | + background: "white", |
| 85 | + display: "flex", |
| 86 | + flexDirection: "column", |
| 87 | + margin: "0 auto 200px", |
| 88 | + alignItems: "center", |
| 89 | + }} |
| 90 | + > |
| 91 | + <p>{currentDateTime.toLocaleString()}</p> |
| 92 | + {areTracksAvailable(availableTracks) && ( |
| 93 | + <> |
| 94 | + <h2>Choose track</h2> |
| 95 | + <select onChange={setQrCodeHandler} value={selectedTrack}> |
| 96 | + {Array.from(availableTracks).map((option) => ( |
| 97 | + <option key={option.id} value={option.id}> |
| 98 | + {option.name} |
| 99 | + </option> |
| 100 | + ))} |
| 101 | + </select> |
| 102 | + </> |
| 103 | + )} |
| 104 | + <br /> |
| 105 | + {session && ( |
| 106 | + <p> |
| 107 | + <strong> |
| 108 | + {session.speakers.map((speaker) => speaker.name).join(", ")} |
| 109 | + </strong>{" "} |
| 110 | + — {session.title} |
| 111 | + </p> |
| 112 | + )} |
| 113 | + <QRCode value={qrValue} size={512} /> |
| 114 | + </div> |
| 115 | + </SectionWrapper> |
| 116 | + ); |
| 117 | +}; |
| 118 | +export default QrCode; |
0 commit comments