fork/replay-manager-for-slippi

git clone https://git.julia.blue/fork/replay-manager-for-slippi

Nicoletsgg phase group entrants -> seedsac16564

2.3.7
12.3 KiB465 linesraw
1import styled from '@emotion/styled';
2import {
3  Box,
4  CircularProgress,
5  Collapse,
6  IconButton,
7  ListItemButton,
8  Tooltip,
9  Typography,
10} from '@mui/material';
11import {
12  KeyboardArrowDown,
13  KeyboardArrowRight,
14  KeyboardArrowUp,
15  Refresh,
16} from '@mui/icons-material';
17import { useState } from 'react';
18import {
19  Event,
20  NameWithHighlight,
21  Phase,
22  PhaseGroup,
23  SelectedEvent,
24  SelectedPhase,
25  SelectedPhaseGroup,
26  Set,
27  State,
28  Tournament,
29} from '../common/types';
30import SetViewInner from './SetView';
31import filterSets from './filterSets';
32import TiebreakerDialog from './TiebreakerDialog';
33
34const Block = styled.div`
35  padding-left: 8px;
36`;
37
38const Name = styled.div`
39  overflow-x: hidden;
40  text-overflow: ellipsis;
41  white-space: nowrap;
42`;
43
44function SetView({
45  set,
46  entrant1Names,
47  entrant2Names,
48  vlerkMode,
49  selectSet,
50}: {
51  set: Set;
52  entrant1Names: NameWithHighlight[];
53  entrant2Names: NameWithHighlight[];
54  vlerkMode: boolean;
55  selectSet: () => Promise<void>;
56}) {
57  return (
58    <ListItemButton
59      dense
60      disableGutters
61      onClick={() => {
62        selectSet();
63      }}
64    >
65      <SetViewInner
66        entrant1Names={entrant1Names}
67        entrant1Score={set.entrant1Score}
68        entrant1Win={set.entrant1Id === set.winnerId}
69        entrant2Names={entrant2Names}
70        entrant2Score={set.entrant2Score}
71        fullRoundText={set.fullRoundText}
72        showScores={set.state === State.COMPLETED}
73        state={set.state}
74        stream={set.stream}
75        station={set.station}
76        wasReported={vlerkMode && set.wasReported}
77        updatedAtMs={set.updatedAtMs}
78      />
79    </ListItemButton>
80  );
81}
82
83function PhaseGroupView({
84  phaseGroup,
85  initiallyOpen,
86  ancestorsOpen,
87  searchSubstr,
88  vlerkMode,
89  getPhaseGroup,
90  selectSet,
91}: {
92  phaseGroup: PhaseGroup;
93  initiallyOpen: boolean;
94  ancestorsOpen: boolean;
95  searchSubstr: string;
96  vlerkMode: boolean;
97  getPhaseGroup: (id: number) => Promise<void>;
98  selectSet: (set: Set) => Promise<void>;
99}) {
100  const [getting, setGetting] = useState(false);
101  const [completedOpen, setCompletedOpen] = useState(vlerkMode);
102  const [open, setOpen] = useState(initiallyOpen);
103
104  const get = async () => {
105    setGetting(true);
106    await getPhaseGroup(phaseGroup.id);
107    setGetting(false);
108  };
109
110  const pendingSetsToShow = filterSets(
111    phaseGroup.sets.pendingSets,
112    searchSubstr,
113  );
114  const completedSetsToShow = filterSets(
115    phaseGroup.sets.completedSets,
116    searchSubstr,
117  );
118
119  return (
120    (!searchSubstr ||
121      pendingSetsToShow.length > 0 ||
122      completedSetsToShow.length > 0) && (
123      <>
124        <ListItemButton
125          dense
126          disableGutters
127          onClick={() => {
128            if (
129              !open &&
130              phaseGroup.sets.pendingSets.length === 0 &&
131              phaseGroup.sets.completedSets.length === 0
132            ) {
133              get();
134            }
135            setOpen((oldOpen) => !oldOpen);
136          }}
137          sx={{ typography: 'caption' }}
138        >
139          {open ? <KeyboardArrowDown /> : <KeyboardArrowRight />}
140          <Name>{phaseGroup.name}</Name>
141          {'\u00A0'}({phaseGroup.id})
142          <Tooltip arrow title="Refresh pool">
143            <IconButton
144              disabled={getting}
145              onClick={(event) => {
146                event.stopPropagation();
147                get();
148              }}
149              size="small"
150            >
151              {getting ? <CircularProgress size="24px" /> : <Refresh />}
152            </IconButton>
153          </Tooltip>
154        </ListItemButton>
155        <Collapse in={open && ancestorsOpen} unmountOnExit>
156          <div style={{ marginLeft: '-16px' }}>
157            {pendingSetsToShow.map((setWithNames) => (
158              <SetView
159                key={setWithNames.set.id}
160                set={setWithNames.set}
161                entrant1Names={setWithNames.entrant1Names}
162                entrant2Names={setWithNames.entrant2Names}
163                vlerkMode={vlerkMode}
164                selectSet={() => selectSet(setWithNames.set)}
165              />
166            ))}
167            {completedSetsToShow.length > 0 && (
168              <>
169                <ListItemButton
170                  dense
171                  onClick={() =>
172                    setCompletedOpen((oldCompletedOpen) => !oldCompletedOpen)
173                  }
174                >
175                  <Typography
176                    alignItems="center"
177                    display="flex"
178                    justifyContent="right"
179                    variant="subtitle2"
180                    width="100%"
181                  >
182                    completed
183                    {completedOpen ? (
184                      <KeyboardArrowUp />
185                    ) : (
186                      <KeyboardArrowDown />
187                    )}
188                  </Typography>
189                </ListItemButton>
190                <Collapse in={completedOpen && ancestorsOpen} unmountOnExit>
191                  {completedSetsToShow.map((setWithNames) => (
192                    <SetView
193                      key={setWithNames.set.id}
194                      set={setWithNames.set}
195                      entrant1Names={setWithNames.entrant1Names}
196                      entrant2Names={setWithNames.entrant2Names}
197                      vlerkMode={vlerkMode}
198                      selectSet={() => selectSet(setWithNames.set)}
199                    />
200                  ))}
201                </Collapse>
202              </>
203            )}
204            {(phaseGroup.bracketType === 3 || phaseGroup.bracketType === 4) &&
205              phaseGroup.sets.completedSets.length > 0 &&
206              phaseGroup.sets.pendingSets.length === 0 && (
207                <TiebreakerDialog
208                  entrants={phaseGroup.seeds
209                    .filter((seed) => seed.entrant)
210                    .map((seed) => seed.entrant!)}
211                  selectSet={(set: Set) => selectSet(set)}
212                />
213              )}
214          </div>
215        </Collapse>
216      </>
217    )
218  );
219}
220
221function PhaseView({
222  phase,
223  initiallyOpen,
224  ancestorsOpen,
225  searchSubstr,
226  vlerkMode,
227  selectedPhaseGroupId,
228  getPhase,
229  getPhaseGroup,
230  selectSet,
231}: {
232  phase: Phase;
233  initiallyOpen: boolean;
234  ancestorsOpen: boolean;
235  searchSubstr: string;
236  vlerkMode: boolean;
237  selectedPhaseGroupId: number | undefined;
238  getPhase: (id: number) => Promise<void>;
239  getPhaseGroup: (id: number) => Promise<void>;
240  selectSet: (set: Set, phaseGroup: SelectedPhaseGroup) => Promise<void>;
241}) {
242  const [getting, setGetting] = useState(false);
243  const [open, setOpen] = useState(initiallyOpen);
244
245  const get = async () => {
246    setGetting(true);
247    await getPhase(phase.id);
248    setGetting(false);
249  };
250  return (
251    <>
252      <ListItemButton
253        dense
254        disableGutters
255        onClick={() => {
256          if (!open && phase.phaseGroups.length === 0) {
257            get();
258          }
259          setOpen((oldOpen) => !oldOpen);
260        }}
261        sx={{ typography: 'caption' }}
262      >
263        {open ? <KeyboardArrowDown /> : <KeyboardArrowRight />}
264        <Name>{phase.name}</Name>
265        {'\u00A0'}({phase.id})
266        <Tooltip arrow title="Refresh phase">
267          <IconButton
268            onClick={(event) => {
269              event.stopPropagation();
270              get();
271            }}
272            size="small"
273          >
274            {getting ? <CircularProgress size="24px" /> : <Refresh />}
275          </IconButton>
276        </Tooltip>
277      </ListItemButton>
278      <Collapse in={open}>
279        <Block>
280          {phase.phaseGroups.map((phaseGroup) => (
281            <PhaseGroupView
282              key={phaseGroup.id}
283              phaseGroup={phaseGroup}
284              initiallyOpen={
285                phase.phaseGroups.length === 1 ||
286                phaseGroup.id === selectedPhaseGroupId
287              }
288              ancestorsOpen={ancestorsOpen && open}
289              searchSubstr={searchSubstr}
290              vlerkMode={vlerkMode}
291              getPhaseGroup={getPhaseGroup}
292              selectSet={(set: Set) =>
293                selectSet(set, {
294                  id: phaseGroup.id,
295                  name: phaseGroup.name,
296                  bracketType: phaseGroup.bracketType,
297                  hasSiblings: phase.phaseGroups.length > 1,
298                  waveId: phaseGroup.waveId,
299                  winnersTargetPhaseId: phaseGroup.winnersTargetPhaseId,
300                })
301              }
302            />
303          ))}
304        </Block>
305      </Collapse>
306    </>
307  );
308}
309
310function EventView({
311  event,
312  initiallyOpen,
313  searchSubstr,
314  vlerkMode,
315  selectedPhaseId,
316  selectedPhaseGroupId,
317  getEvent,
318  getPhase,
319  getPhaseGroup,
320  selectSet,
321}: {
322  event: Event;
323  initiallyOpen: boolean;
324  searchSubstr: string;
325  vlerkMode: boolean;
326  selectedPhaseId: number | undefined;
327  selectedPhaseGroupId: number | undefined;
328  getEvent: (id: number) => Promise<void>;
329  getPhase: (id: number) => Promise<void>;
330  getPhaseGroup: (id: number) => Promise<void>;
331  selectSet: (
332    set: Set,
333    phaseGroup: SelectedPhaseGroup,
334    phase: SelectedPhase,
335  ) => Promise<void>;
336}) {
337  const [getting, setGetting] = useState(false);
338  const [open, setOpen] = useState(initiallyOpen);
339
340  const get = async () => {
341    setGetting(true);
342    await getEvent(event.id);
343    setGetting(false);
344  };
345  return (
346    <>
347      <ListItemButton
348        dense
349        disableGutters
350        onClick={() => {
351          if (!open && event.phases.length === 0) {
352            get();
353          }
354          setOpen((oldOpen) => !oldOpen);
355        }}
356        sx={{ typography: 'caption' }}
357      >
358        {open ? <KeyboardArrowDown /> : <KeyboardArrowRight />}
359        <Name>{event.name}</Name>
360        {'\u00A0'}({event.id})
361        <Box>
362          <Tooltip arrow title="Refresh event">
363            <IconButton
364              onClick={(ev) => {
365                ev.stopPropagation();
366                get();
367              }}
368              size="small"
369            >
370              {getting ? <CircularProgress size="24px" /> : <Refresh />}
371            </IconButton>
372          </Tooltip>
373        </Box>
374      </ListItemButton>
375      <Collapse in={open}>
376        <Block>
377          {event.phases.map((phase) => (
378            <PhaseView
379              key={phase.id}
380              phase={phase}
381              initiallyOpen={
382                event.phases.length === 1 || phase.id === selectedPhaseId
383              }
384              ancestorsOpen={open}
385              searchSubstr={searchSubstr}
386              vlerkMode={vlerkMode}
387              selectedPhaseGroupId={selectedPhaseGroupId}
388              getPhase={getPhase}
389              getPhaseGroup={getPhaseGroup}
390              selectSet={(set: Set, phaseGroup: SelectedPhaseGroup) =>
391                selectSet(set, phaseGroup, {
392                  id: phase.id,
393                  name: phase.name,
394                  hasSiblings: event.phases.length > 1,
395                })
396              }
397            />
398          ))}
399        </Block>
400      </Collapse>
401    </>
402  );
403}
404
405export default function StartggView({
406  searchSubstr,
407  tournament,
408  vlerkMode,
409  selectedEventId,
410  selectedPhaseId,
411  selectedPhaseGroupId,
412  getEvent,
413  getPhase,
414  getPhaseGroup,
415  selectSet,
416}: {
417  searchSubstr: string;
418  tournament: Tournament;
419  vlerkMode: boolean;
420  selectedEventId: number | undefined;
421  selectedPhaseId: number | undefined;
422  selectedPhaseGroupId: number | undefined;
423  getEvent: (id: number) => Promise<void>;
424  getPhase: (id: number) => Promise<void>;
425  getPhaseGroup: (id: number) => Promise<void>;
426  selectSet: (
427    set: Set,
428    phaseGroup: SelectedPhaseGroup,
429    phase: SelectedPhase,
430    event: SelectedEvent,
431  ) => Promise<void>;
432}) {
433  return (
434    <Box bgcolor="white">
435      {tournament.events.map((event) => (
436        <EventView
437          key={event.id}
438          event={event}
439          initiallyOpen={
440            tournament.events.length === 1 || event.id === selectedEventId
441          }
442          searchSubstr={searchSubstr}
443          vlerkMode={vlerkMode}
444          selectedPhaseId={selectedPhaseId}
445          selectedPhaseGroupId={selectedPhaseGroupId}
446          getEvent={getEvent}
447          getPhase={getPhase}
448          getPhaseGroup={getPhaseGroup}
449          selectSet={(
450            set: Set,
451            phaseGroup: SelectedPhaseGroup,
452            phase: SelectedPhase,
453          ) =>
454            selectSet(set, phaseGroup, phase, {
455              id: event.id,
456              name: event.name,
457              slug: event.slug,
458              hasSiblings: tournament.events.length > 1,
459            })
460          }
461        />
462      ))}
463    </Box>
464  );
465}