fork/replay-manager-for-slippi

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

NicoletMerge pull request #167 from HannahConnolly/mainddc617a

2.3.5
5.5 KiB192 linesraw
1import {
2  Box,
3  CircularProgress,
4  Collapse,
5  IconButton,
6  ListItemButton,
7  Tooltip,
8  Typography,
9} from '@mui/material';
10import {
11  KeyboardArrowDown,
12  KeyboardArrowRight,
13  KeyboardArrowUp,
14  PlayArrow,
15  Refresh,
16} from '@mui/icons-material';
17import { useState } from 'react';
18import { ChallongeTournament, Set, SetWithNames, State } from '../common/types';
19import SetViewInner from './SetView';
20import filterSets from './filterSets';
21import TiebreakerDialog from './TiebreakerDialog';
22
23function SetView({
24  setWithNames,
25  selectSet,
26}: {
27  setWithNames: SetWithNames;
28  selectSet: (set: Set) => void;
29}) {
30  return (
31    <ListItemButton
32      dense
33      disableGutters
34      onClick={() => {
35        selectSet(setWithNames.set);
36      }}
37    >
38      <SetViewInner
39        entrant1Names={setWithNames.entrant1Names}
40        entrant1Score={setWithNames.set.entrant1Score}
41        entrant1Win={setWithNames.set.entrant1Id === setWithNames.set.winnerId}
42        entrant2Names={setWithNames.entrant2Names}
43        entrant2Score={setWithNames.set.entrant2Score}
44        fullRoundText={setWithNames.set.fullRoundText}
45        showScores={setWithNames.set.state === State.COMPLETED}
46        state={setWithNames.set.state}
47        stream={setWithNames.set.stream}
48        station={setWithNames.set.station}
49        wasReported={false}
50        updatedAtMs={setWithNames.set.updatedAtMs}
51      />
52    </ListItemButton>
53  );
54}
55
56export default function ChallongeView({
57  searchSubstr,
58  tournament,
59  getChallongeTournament,
60  selectSet,
61}: {
62  searchSubstr: string;
63  tournament: ChallongeTournament;
64  getChallongeTournament: () => Promise<void>;
65  selectSet: (set: Set) => void;
66}) {
67  const [open, setOpen] = useState(true);
68  const [completedOpen, setCompletedOpen] = useState(false);
69  const [getting, setGetting] = useState(false);
70  const get = async () => {
71    setGetting(true);
72    await getChallongeTournament();
73    setGetting(false);
74  };
75  const pendingSetsToShow = filterSets(
76    tournament.sets.pendingSets,
77    searchSubstr,
78  );
79  const completedSetsToShow = filterSets(
80    tournament.sets.completedSets,
81    searchSubstr,
82  );
83  return (
84    (!searchSubstr ||
85      pendingSetsToShow.length > 0 ||
86      completedSetsToShow.length > 0) && (
87      <Box bgcolor="white">
88        <ListItemButton
89          dense
90          disableGutters
91          onClick={() => {
92            setOpen(!open);
93          }}
94          sx={{ typography: 'caption' }}
95        >
96          {open ? <KeyboardArrowDown /> : <KeyboardArrowRight />}
97          <div
98            style={{
99              overflowX: 'hidden',
100              textOverflow: 'ellipsis',
101              whiteSpace: 'nowrap',
102            }}
103          >
104            {tournament.name}
105          </div>
106          <Box>
107            <Tooltip arrow title="Refresh tournament">
108              <IconButton
109                onClick={(ev) => {
110                  ev.stopPropagation();
111                  get();
112                }}
113                size="small"
114              >
115                {getting ? <CircularProgress size="24px" /> : <Refresh />}
116              </IconButton>
117            </Tooltip>
118          </Box>
119          {tournament.state === State.PENDING && (
120            <Box>
121              <Tooltip arrow title="Start tournament on website">
122                <IconButton
123                  onClick={(ev) => {
124                    ev.stopPropagation();
125                    window.open(`//challonge.com/${tournament.slug}`);
126                  }}
127                  size="small"
128                >
129                  <PlayArrow />
130                </IconButton>
131              </Tooltip>
132            </Box>
133          )}
134        </ListItemButton>
135        <Collapse in={open}>
136          <div>
137            {pendingSetsToShow.map((setWithNames) => (
138              <SetView
139                key={setWithNames.set.id}
140                setWithNames={setWithNames}
141                selectSet={selectSet}
142              />
143            ))}
144            {completedSetsToShow.length > 0 && (
145              <>
146                <ListItemButton
147                  dense
148                  onClick={() => setCompletedOpen(!completedOpen)}
149                >
150                  <Typography
151                    alignItems="center"
152                    display="flex"
153                    justifyContent="right"
154                    variant="subtitle2"
155                    width="100%"
156                  >
157                    completed
158                    {completedOpen ? (
159                      <KeyboardArrowUp />
160                    ) : (
161                      <KeyboardArrowDown />
162                    )}
163                  </Typography>
164                </ListItemButton>
165                <Collapse in={completedOpen}>
166                  {completedSetsToShow.map((setWithNames) => (
167                    <SetView
168                      key={setWithNames.set.id}
169                      setWithNames={setWithNames}
170                      selectSet={selectSet}
171                    />
172                  ))}
173                </Collapse>
174              </>
175            )}
176            {(tournament.tournamentType === 'swiss' ||
177              tournament.tournamentType === 'round robin') &&
178              tournament.sets.completedSets.length > 0 &&
179              tournament.sets.pendingSets.length === 0 && (
180                <TiebreakerDialog
181                  entrants={tournament.entrants}
182                  selectSet={async (set: Set) => {
183                    selectSet(set);
184                  }}
185                />
186              )}
187          </div>
188        </Collapse>
189      </Box>
190    )
191  );
192}