fork/replay-manager-for-slippi

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

Nicoletno reset confirm if state === CALLED || STARTEDf83b198

2.3.5
4.1 KiB143 linesraw
1import { RestartAlt } from '@mui/icons-material';
2import {
3  Button,
4  CircularProgress,
5  Dialog,
6  DialogActions,
7  DialogContent,
8  DialogContentText,
9  DialogTitle,
10  IconButton,
11  Tooltip,
12} from '@mui/material';
13import { useCallback, useState } from 'react';
14import { Mode, Set, State } from '../common/types';
15import SetView from './SetView';
16import { assertInteger } from '../common/asserts';
17
18export default function ResetSet({
19  mode,
20  selectedSet,
21}: {
22  mode: Mode;
23  selectedSet: Set;
24}) {
25  const [confirmOpen, setConfirmOpen] = useState(false);
26  const [errorOpen, setErrorOpen] = useState(false);
27  const [error, setError] = useState('');
28  const [resetting, setResetting] = useState(false);
29
30  const reset = useCallback(async () => {
31    setResetting(true);
32    try {
33      if (mode === Mode.STARTGG) {
34        await window.electron.resetSet(assertInteger(selectedSet.id));
35      } else if (mode === Mode.OFFLINE_MODE) {
36        await window.electron.resetOfflineModeSet(
37          assertInteger(selectedSet.id),
38        );
39      }
40      setConfirmOpen(false);
41    } catch (e: any) {
42      setError(e.toString());
43      setErrorOpen(true);
44    } finally {
45      setResetting(false);
46    }
47  }, [mode, selectedSet.id]);
48
49  return (
50    <>
51      <Tooltip arrow title="Reset set">
52        <div>
53          <IconButton
54            color="primary"
55            disabled={
56              !Number.isInteger(selectedSet.id) ||
57              (selectedSet.id as number) <= 0 ||
58              selectedSet.state === State.PENDING ||
59              resetting ||
60              (mode !== Mode.STARTGG && mode !== Mode.OFFLINE_MODE)
61            }
62            size="small"
63            onClick={() => {
64              if (
65                selectedSet.state === State.CALLED ||
66                selectedSet.state === State.STARTED
67              ) {
68                reset();
69              } else {
70                setConfirmOpen(true);
71              }
72            }}
73          >
74            {resetting ? <CircularProgress size="24px" /> : <RestartAlt />}
75          </IconButton>
76        </div>
77      </Tooltip>
78      <Dialog open={confirmOpen} onClose={() => setConfirmOpen(false)}>
79        <DialogTitle>Reset Set?</DialogTitle>
80        <DialogContent style={{ width: '300px' }}>
81          <SetView
82            entrant1Names={selectedSet.entrant1Participants.map(
83              (participant) => ({ name: participant.displayName }),
84            )}
85            entrant1Score={selectedSet.entrant1Score}
86            entrant1Win={selectedSet.winnerId === selectedSet.entrant1Id}
87            entrant2Names={selectedSet.entrant2Participants.map(
88              (participant) => ({ name: participant.displayName }),
89            )}
90            entrant2Score={selectedSet.entrant2Score}
91            fullRoundText={selectedSet.fullRoundText}
92            showScores={selectedSet.state === State.COMPLETED}
93            state={selectedSet.state}
94            stream={selectedSet.stream}
95            station={selectedSet.station}
96            wasReported={false}
97            updatedAtMs={selectedSet.updatedAtMs}
98          />
99        </DialogContent>
100        <DialogActions>
101          <Button
102            color="error"
103            disabled={
104              !Number.isInteger(selectedSet.id) ||
105              (selectedSet.id as number) <= 0 ||
106              selectedSet.state === State.PENDING ||
107              resetting
108            }
109            endIcon={
110              resetting ? <CircularProgress size="24px" /> : <RestartAlt />
111            }
112            onClick={reset}
113            variant="contained"
114          >
115            Reset
116          </Button>
117        </DialogActions>
118      </Dialog>
119      <Dialog
120        open={errorOpen}
121        onClose={() => {
122          setErrorOpen(false);
123          setError('');
124        }}
125      >
126        <DialogTitle>Reset error!</DialogTitle>
127        <DialogContent>
128          <DialogContentText>{error}</DialogContentText>
129        </DialogContent>
130        <DialogActions>
131          <Button
132            onClick={() => {
133              setErrorOpen(false);
134              setError('');
135            }}
136          >
137            Close
138          </Button>
139        </DialogActions>
140      </Dialog>
141    </>
142  );
143}