|
| 1 | +import { useSearchParams } from 'react-router-dom'; |
| 2 | +import { TeamMember } from '../../../data/data.ts'; |
| 3 | + |
| 4 | +import StandupTimer from '../StandupTimer'; |
| 5 | +import Page from '../Page'; |
| 6 | + |
| 7 | +import Card from '../Card'; |
| 8 | +import { useState } from 'react'; |
| 9 | + |
| 10 | +export default function StandupIntro() { |
| 11 | + const [standupStarted, setStandupStarted] = useState(false); |
| 12 | + const [searchParams] = useSearchParams(); |
| 13 | + const [members, setMembers] = useState<TeamMember[]>(parseMembersFromParams()); |
| 14 | + console.log(searchParams); |
| 15 | + |
| 16 | + function shuffle(array: TeamMember[]): TeamMember[] { |
| 17 | + let currentIndex = array.length, |
| 18 | + randomIndex; |
| 19 | + |
| 20 | + // While there remain elements to shuffle. |
| 21 | + while (currentIndex > 0) { |
| 22 | + // Pick a remaining element. |
| 23 | + randomIndex = Math.floor(Math.random() * currentIndex); |
| 24 | + currentIndex--; |
| 25 | + |
| 26 | + // And swap it with the current element. |
| 27 | + [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]]; |
| 28 | + } |
| 29 | + |
| 30 | + return array; |
| 31 | + } |
| 32 | + |
| 33 | + function calculateTotalTime(members: TeamMember[]): number { |
| 34 | + return members.reduce((total, member) => total + member.time, 0); |
| 35 | + } |
| 36 | + |
| 37 | + function removeMemberbyIndex(index: number): void { |
| 38 | + // Remove the member at the specified index and set the new members state |
| 39 | + const newMembers = members.filter((_, i) => i !== index); |
| 40 | + setMembers(newMembers); |
| 41 | + } |
| 42 | + |
| 43 | + function parseMembersFromParams(): TeamMember[] { |
| 44 | + // members in query params are in the format: &name1=time1&name2=time2,... |
| 45 | + const params = []; |
| 46 | + |
| 47 | + for (const entry of searchParams.entries()) { |
| 48 | + params.push(entry); |
| 49 | + } |
| 50 | + |
| 51 | + const m: TeamMember[] = params.map((param) => { |
| 52 | + const [name, time] = param; |
| 53 | + return { |
| 54 | + name: name[0].toUpperCase() + name.substring(1), |
| 55 | + role: 'Member', |
| 56 | + time: parseInt(time, 10) || 2, |
| 57 | + }; |
| 58 | + }); |
| 59 | + |
| 60 | + return shuffle(m); |
| 61 | + } |
| 62 | + |
| 63 | + return ( |
| 64 | + <> |
| 65 | + {standupStarted && ( |
| 66 | + <Page title="Daily Standup" onClose={() => setStandupStarted(false)}> |
| 67 | + <StandupTimer members={members} /> |
| 68 | + </Page> |
| 69 | + )} |
| 70 | + |
| 71 | + {!standupStarted && ( |
| 72 | + <Page title="Daily Standup"> |
| 73 | + <div className="mx-auto grid max-w-2xl py-4"> |
| 74 | + <Card className="mb-10"> |
| 75 | + <h1 className="text-center text-3xl font-semibold dark:text-text-dark">Total Time</h1> |
| 76 | + <p className="text-center text-xl text-gray-500 dark:text-text-dark"> |
| 77 | + {calculateTotalTime(members) + 'm'} |
| 78 | + </p> |
| 79 | + <h1 |
| 80 | + onClick={setStandupStarted.bind(null, true)} |
| 81 | + className="mx-auto mt-3 w-fit cursor-pointer rounded-md bg-orange-600 px-2 py-1 text-center text-2xl font-bold text-white dark:text-text-dark" |
| 82 | + > |
| 83 | + Start |
| 84 | + </h1> |
| 85 | + </Card> |
| 86 | + |
| 87 | + {members.map((member, index) => ( |
| 88 | + <div key={index}> |
| 89 | + <Card> |
| 90 | + <h1 className="text-center text-xl font-semibold dark:text-text-dark">{member.name}</h1> |
| 91 | + <p className="text-center text-gray-500 dark:text-text-dark">{member.time + 'm'}</p> |
| 92 | + <h1 |
| 93 | + onClick={() => removeMemberbyIndex(index)} |
| 94 | + className="absolute right-4 top-6 mx-2 w-fit cursor-pointer rounded-md bg-orange-600 px-2 py-1 text-center text-xl font-bold text-white dark:text-text-dark" |
| 95 | + > |
| 96 | + X |
| 97 | + </h1> |
| 98 | + </Card> |
| 99 | + {index != members.length - 1 && ( |
| 100 | + <div className="divider-container -mt-2 flex flex-col items-center"> |
| 101 | + <div className="relative z-10 h-4 w-4 rounded-full bg-orange-600 dark:brightness-75"></div> |
| 102 | + <div className="-mt-2 h-5 w-1 rounded-full bg-gray-200 dark:bg-gray-500"></div> |
| 103 | + </div> |
| 104 | + )} |
| 105 | + </div> |
| 106 | + ))} |
| 107 | + </div> |
| 108 | + </Page> |
| 109 | + )} |
| 110 | + </> |
| 111 | + ); |
| 112 | +} |
0 commit comments