fork/replay-manager-for-slippi

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

Nicoletrefactor sgg station/stream276de8b

2.3.4
7.5 KiB251 linesraw
1import { useState } from 'react';
2import {
3  Button,
4  CircularProgress,
5  Dialog,
6  DialogActions,
7  DialogContent,
8  DialogContentText,
9  DialogTitle,
10  IconButton,
11  List,
12  ListItemButton,
13  ListItemText,
14  Stack,
15  Tooltip,
16} from '@mui/material';
17import { Refresh, Tv } from '@mui/icons-material';
18import { Mode, Set, State, Station, Stream } from '../common/types';
19import SetView from './SetView';
20import { assertInteger } from '../common/asserts';
21
22function getCombinedStreamName(stream: Stream) {
23  let prefix = '';
24  if (stream.domain === 'twitch') {
25    prefix = 'ttv/';
26  } else if (stream.domain === 'youtube') {
27    prefix = 'yt/';
28  }
29  return prefix + stream.path;
30}
31
32export default function AssignStream({
33  mode,
34  selectedSet,
35  streams,
36  stations,
37  refreshStartggTournament,
38}: {
39  mode: Mode;
40  selectedSet: Set;
41  streams: Stream[];
42  stations: Station[];
43  refreshStartggTournament: () => Promise<void>;
44}) {
45  const [chooseOpen, setChooseOpen] = useState(false);
46  const [errorOpen, setErrorOpen] = useState(false);
47  const [error, setError] = useState('');
48
49  const [assigning, setAssigning] = useState(false);
50  const assignStream = async (streamId: number) => {
51    setAssigning(true);
52    try {
53      if (mode === Mode.STARTGG) {
54        await window.electron.assignStream(selectedSet, streamId);
55      } else if (mode === Mode.OFFLINE_MODE) {
56        await window.electron.assignOfflineModeSetStream(
57          assertInteger(selectedSet.id),
58          streamId,
59        );
60      }
61      setChooseOpen(false);
62    } catch (e: any) {
63      setError(e.toString());
64      setErrorOpen(true);
65    } finally {
66      setAssigning(false);
67    }
68  };
69  const assignStation = async (stationId: number) => {
70    setAssigning(true);
71    try {
72      if (mode === Mode.STARTGG) {
73        await window.electron.assignStation(selectedSet, stationId);
74      } else if (mode === Mode.OFFLINE_MODE) {
75        await window.electron.assignOfflineModeSetStation(
76          assertInteger(selectedSet.id),
77          stationId,
78        );
79      }
80      setChooseOpen(false);
81    } catch (e: any) {
82      setError(e.toString());
83      setErrorOpen(true);
84    } finally {
85      setAssigning(false);
86    }
87  };
88
89  const [refreshingStartggTournament, setRefreshingStartggTournament] =
90    useState(false);
91
92  return (
93    <>
94      <Tooltip arrow title="Assign set to stream or station">
95        <div>
96          <IconButton
97            color="primary"
98            disabled={
99              !(
100                typeof selectedSet.id === 'string' ||
101                (Number.isInteger(selectedSet.id) && selectedSet.id > 0)
102              ) ||
103              (mode !== Mode.STARTGG && mode !== Mode.OFFLINE_MODE)
104            }
105            size="small"
106            onClick={async () => {
107              setChooseOpen(true);
108            }}
109          >
110            <Tv />
111          </IconButton>
112        </div>
113      </Tooltip>
114      <Dialog open={chooseOpen} onClose={() => setChooseOpen(false)}>
115        <Stack
116          alignItems="center"
117          direction="row"
118          justifyContent="space-between"
119          marginRight="24px"
120        >
121          <DialogTitle>Assign Set to Stream or Station</DialogTitle>
122          {mode === Mode.STARTGG && (
123            <Tooltip title="Refresh">
124              <IconButton
125                disabled={refreshingStartggTournament}
126                onClick={async () => {
127                  try {
128                    setRefreshingStartggTournament(true);
129                    await refreshStartggTournament();
130                  } finally {
131                    setRefreshingStartggTournament(false);
132                  }
133                }}
134              >
135                {refreshingStartggTournament ? (
136                  <CircularProgress size="24px" />
137                ) : (
138                  <Refresh />
139                )}
140              </IconButton>
141            </Tooltip>
142          )}
143        </Stack>
144        <DialogContent style={{ maxWidth: '500px' }}>
145          <SetView
146            entrant1Names={selectedSet.entrant1Participants.map(
147              (participant) => ({ name: participant.displayName }),
148            )}
149            entrant1Score={selectedSet.entrant1Score}
150            entrant1Win={selectedSet.winnerId === selectedSet.entrant1Id}
151            entrant2Names={selectedSet.entrant2Participants.map(
152              (participant) => ({ name: participant.displayName }),
153            )}
154            entrant2Score={selectedSet.entrant2Score}
155            fullRoundText={selectedSet.fullRoundText}
156            showScores={selectedSet.state === State.COMPLETED}
157            state={selectedSet.state}
158            stream={selectedSet.stream}
159            station={selectedSet.station}
160            wasReported={false}
161            updatedAtMs={selectedSet.updatedAtMs}
162          />
163          {streams.length > 0 && (
164            <>
165              {selectedSet.stream && (
166                <ListItemButton
167                  disabled={assigning}
168                  disableGutters
169                  style={{ marginTop: '8px' }}
170                  onClick={() => assignStream(0)}
171                >
172                  <ListItemText>
173                    Remove from {getCombinedStreamName(selectedSet.stream)}
174                  </ListItemText>
175                </ListItemButton>
176              )}
177              <List disablePadding>
178                {streams
179                  .filter((stream) => stream.id !== selectedSet.stream?.id)
180                  .map((stream) => (
181                    <ListItemButton
182                      disabled={assigning}
183                      key={stream.id}
184                      disableGutters
185                      onClick={() => assignStream(stream.id)}
186                    >
187                      <ListItemText>
188                        {getCombinedStreamName(stream)}
189                      </ListItemText>
190                    </ListItemButton>
191                  ))}
192              </List>
193            </>
194          )}
195          {stations.length > 0 && (
196            <>
197              {selectedSet.station && (
198                <ListItemText style={{ padding: '12px 0', margin: '8px 0 0' }}>
199                  Assigned to station {selectedSet.station.number}
200                </ListItemText>
201              )}
202              <List
203                disablePadding
204                style={{
205                  display: 'flex',
206                  flexDirection: 'row',
207                  flexWrap: 'wrap',
208                }}
209              >
210                {stations
211                  .filter((station) => station.id !== selectedSet.station?.id)
212                  .map((station) => (
213                    <ListItemButton
214                      disabled={assigning}
215                      key={station.id}
216                      style={{ flexGrow: 0 }}
217                      onClick={() => assignStation(station.id)}
218                    >
219                      <ListItemText>{station.number}</ListItemText>
220                    </ListItemButton>
221                  ))}
222              </List>
223            </>
224          )}
225        </DialogContent>
226      </Dialog>
227      <Dialog
228        open={errorOpen}
229        onClose={() => {
230          setErrorOpen(false);
231          setError('');
232        }}
233      >
234        <DialogTitle>Assign Set to Stream error!</DialogTitle>
235        <DialogContent>
236          <DialogContentText>{error}</DialogContentText>
237        </DialogContent>
238        <DialogActions>
239          <Button
240            onClick={() => {
241              setErrorOpen(false);
242              setError('');
243            }}
244          >
245            Close
246          </Button>
247        </DialogActions>
248      </Dialog>
249    </>
250  );
251}