fork/replay-manager-for-slippi

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

Nicoletbetter admined tournamentscbf5f06

main
20.4 KiB613 linesraw
1import {
2  CloudDownload,
3  ContentCopy,
4  Settings as SettingsIcon,
5} from '@mui/icons-material';
6import {
7  Alert,
8  Button,
9  CircularProgress,
10  Dialog,
11  DialogContent,
12  DialogContentText,
13  DialogTitle,
14  Fab,
15  FormControl,
16  FormControlLabel,
17  FormLabel,
18  InputLabel,
19  Link,
20  MenuItem,
21  Radio,
22  RadioGroup,
23  Select,
24  Stack,
25  TextField,
26  Tooltip,
27  Typography,
28} from '@mui/material';
29import { ChangeEvent, useEffect, useMemo, useState } from 'react';
30import { lt, valid } from 'semver';
31import LabeledCheckbox from './LabeledCheckbox';
32import {
33  AdminedTournament,
34  CopyHostFormat,
35  EnforcerSetting,
36  Mode,
37} from '../common/types';
38
39function LabeledRadioButton({ label, value }: { label: string; value: Mode }) {
40  return (
41    <FormControlLabel
42      disableTypography
43      label={label}
44      labelPlacement="end"
45      value={value}
46      control={<Radio />}
47      sx={{ typography: 'caption' }}
48    />
49  );
50}
51
52export default function Settings({
53  appVersion,
54  latestAppVersion,
55  mode,
56  setMode,
57  enforcerSetting,
58  setEnforcerSetting,
59  vlerkMode,
60  setVlerkMode,
61  guidedMode,
62  setGuidedMode,
63  smuggleCostumeIndex,
64  setSmuggleCostumeIndex,
65  useLAN,
66  setUseLAN,
67  fileNameFormat,
68  setFileNameFormat,
69  folderNameFormat,
70  setFolderNameFormat,
71  hideCopyButton,
72  setHideCopyButton,
73  setAdminedTournaments,
74  setGettingAdminedTournaments,
75  showErrorDialog,
76  enforcerVersion,
77  hostFormat,
78}: {
79  appVersion: string;
80  latestAppVersion: string;
81  mode: Mode;
82  setMode: (mode: Mode) => void;
83  enforcerSetting: EnforcerSetting;
84  setEnforcerSetting: (enforcerSetting: EnforcerSetting) => void;
85  vlerkMode: boolean;
86  setVlerkMode: (vlerkMode: boolean) => void;
87  guidedMode: boolean;
88  setGuidedMode: (guidedMode: boolean) => void;
89  smuggleCostumeIndex: boolean;
90  setSmuggleCostumeIndex: (smuggleCostumeIndex: boolean) => void;
91  useLAN: boolean;
92  setUseLAN: (useLAN: boolean) => void;
93  fileNameFormat: string;
94  setFileNameFormat: (fileNameFormat: string) => void;
95  folderNameFormat: string;
96  setFolderNameFormat: (folderNameFormat: string) => void;
97  hideCopyButton: boolean;
98  setHideCopyButton: (hideCopyButton: boolean) => void;
99  setAdminedTournaments: (tournaments: AdminedTournament[]) => void;
100  setGettingAdminedTournaments: (gettingAdminedTournaments: boolean) => void;
101  showErrorDialog: (errors: string[]) => void;
102  enforcerVersion: string;
103  hostFormat: CopyHostFormat;
104}) {
105  const [gotSettings, setGotSettings] = useState(false);
106  const [startggApiKey, setStartggApiKey] = useState('');
107  const [challongeApiKey, setChallongeApiKey] = useState('');
108  const [parryggApiKey, setParryggApiKey] = useState('');
109  useEffect(() => {
110    (async () => {
111      const startggKeyPromise = window.electron.getStartggKey();
112      const challongeKeyPromise = window.electron.getChallongeKey();
113      const parryggKeyPromise = window.electron.getParryggKey();
114      setStartggApiKey(await startggKeyPromise);
115      setChallongeApiKey(await challongeKeyPromise);
116      setParryggApiKey(await parryggKeyPromise);
117      setGotSettings(true);
118    })();
119  }, []);
120
121  const [open, setOpen] = useState(false);
122  const [copied, setCopied] = useState(false);
123  const [hasAutoOpened, setHasAutoOpened] = useState(false);
124  const [shouldGetTournaments, setShouldGetTournaments] = useState(false);
125
126  const needUpdate = useMemo(
127    () =>
128      valid(appVersion) &&
129      valid(latestAppVersion) &&
130      lt(appVersion, latestAppVersion) &&
131      mode !== Mode.CHALLONGE,
132    [appVersion, latestAppVersion, mode],
133  );
134  if (
135    gotSettings &&
136    !hasAutoOpened &&
137    ((mode === Mode.STARTGG && !startggApiKey) ||
138      (mode === Mode.CHALLONGE && !challongeApiKey) ||
139      (mode === Mode.PARRYGG && !parryggApiKey) ||
140      needUpdate)
141  ) {
142    setOpen(true);
143    setHasAutoOpened(true);
144  }
145
146  const [trashDir, setTrashDir] = useState('');
147  useEffect(() => {
148    (async () => {
149      const trashDirPromise = window.electron.getTrashDir();
150      setTrashDir(await trashDirPromise);
151    })();
152  }, []);
153
154  const [choosingTrashDir, setChoosingTrashDir] = useState(false);
155
156  return (
157    <>
158      <Tooltip arrow title="Settings">
159        <Fab
160          onClick={() => setOpen(true)}
161          size="small"
162          style={{ position: 'absolute', bottom: 8, left: 8 }}
163          sx={{
164            zIndex: (theme) => theme.zIndex.modal + 1,
165          }}
166        >
167          <SettingsIcon />
168        </Fab>
169      </Tooltip>
170      <Dialog
171        fullWidth
172        open={open}
173        onClose={async () => {
174          try {
175            await Promise.all([
176              window.electron.setChallongeKey(challongeApiKey),
177              window.electron.setStartggKey(startggApiKey),
178              window.electron.setParryggKey(parryggApiKey),
179              window.electron.setFileNameFormat(fileNameFormat),
180              window.electron.setFolderNameFormat(folderNameFormat),
181            ]);
182          } catch (e: any) {
183            showErrorDialog([e instanceof Error ? e.message : e]);
184          }
185          setOpen(false);
186          if (shouldGetTournaments) {
187            setAdminedTournaments([]);
188            if (
189              (mode === Mode.STARTGG && startggApiKey) ||
190              (mode === Mode.CHALLONGE && challongeApiKey) ||
191              (mode === Mode.PARRYGG && parryggApiKey)
192            ) {
193              try {
194                setGettingAdminedTournaments(true);
195                setAdminedTournaments(await window.electron.getTournaments());
196              } catch (e: any) {
197                showErrorDialog([e instanceof Error ? e.message : e]);
198              } finally {
199                setGettingAdminedTournaments(false);
200                setShouldGetTournaments(false);
201              }
202            } else {
203              setShouldGetTournaments(false);
204            }
205          }
206        }}
207      >
208        <Stack
209          alignItems="center"
210          direction="row"
211          justifyContent="space-between"
212          marginRight="24px"
213        >
214          <DialogTitle>Settings</DialogTitle>
215          <Typography variant="caption">
216            Replay Reporter for Slippi version {appVersion}
217          </Typography>
218        </Stack>
219        <DialogContent sx={{ pt: 0 }}>
220          <Stack>
221            <FormControl>
222              <FormLabel id="mode-radio-group-label">Mode</FormLabel>
223              <RadioGroup
224                aria-labelledby="mode-radio-group-label"
225                name="mode-radio-group"
226                row
227                value={mode}
228                onChange={async (event: ChangeEvent<HTMLInputElement>) => {
229                  const newMode = event.target.value as Mode;
230                  await window.electron.setMode(newMode);
231                  setMode(newMode);
232                  setShouldGetTournaments(true);
233                }}
234              >
235                <LabeledRadioButton label="start.gg" value={Mode.STARTGG} />
236                <LabeledRadioButton label="Challonge" value={Mode.CHALLONGE} />
237                <LabeledRadioButton label="parry.gg" value={Mode.PARRYGG} />
238                <LabeledRadioButton
239                  label="Offline Mode"
240                  value={Mode.OFFLINE_MODE}
241                />
242                <LabeledRadioButton label="Manual" value={Mode.MANUAL} />
243              </RadioGroup>
244            </FormControl>
245          </Stack>
246          {mode === Mode.STARTGG && (
247            <>
248              <DialogContentText>
249                Get your start.gg API key by clicking “Create new token” in the
250                <br />
251                “Personal Access Tokens” tab of{' '}
252                <Link
253                  href="https://start.gg/admin/profile/developer"
254                  target="_blank"
255                  rel="noreferrer"
256                >
257                  this page
258                </Link>
259                . Keep it private!
260              </DialogContentText>
261              <Stack alignItems="center" direction="row" gap="8px">
262                <TextField
263                  fullWidth
264                  label="start.gg API key (Keep it private!)"
265                  onChange={(event) => {
266                    setStartggApiKey(event.target.value);
267                    setShouldGetTournaments(true);
268                  }}
269                  size="small"
270                  type="password"
271                  value={startggApiKey}
272                  variant="standard"
273                />
274                <Button
275                  disabled={copied}
276                  endIcon={copied ? undefined : <ContentCopy />}
277                  onClick={async () => {
278                    await window.electron.copyToClipboard(startggApiKey);
279                    setCopied(true);
280                    setTimeout(() => setCopied(false), 5000);
281                  }}
282                  variant="contained"
283                >
284                  {copied ? 'Copied!' : 'Copy'}
285                </Button>
286              </Stack>
287            </>
288          )}
289          {mode === Mode.CHALLONGE && (
290            <>
291              <DialogContentText>
292                Get your Challonge v1 API key on{' '}
293                <Link
294                  href="https://challonge.com/settings/developer"
295                  target="_blank"
296                  rel="noreferrer"
297                >
298                  this page
299                </Link>{' '}
300                (you may need to click “Generate a new API key”). Keep it
301                private!
302              </DialogContentText>
303              <Stack alignItems="center" direction="row" gap="8px">
304                <TextField
305                  fullWidth
306                  label="Challonge v1 API key (Keep it private!)"
307                  onChange={(event) => {
308                    setChallongeApiKey(event.target.value);
309                    setShouldGetTournaments(true);
310                  }}
311                  size="small"
312                  type="password"
313                  value={challongeApiKey}
314                  variant="standard"
315                />
316                <Button
317                  disabled={copied}
318                  endIcon={copied ? undefined : <ContentCopy />}
319                  onClick={async () => {
320                    await window.electron.copyToClipboard(challongeApiKey);
321                    setCopied(true);
322                    setTimeout(() => setCopied(false), 5000);
323                  }}
324                  variant="contained"
325                >
326                  {copied ? 'Copied!' : 'Copy'}
327                </Button>
328              </Stack>
329            </>
330          )}
331          {mode === Mode.PARRYGG && (
332            <>
333              <DialogContentText>
334                Get your parry.gg API key from{' '}
335                <Link href="https://parry.gg" target="_blank" rel="noreferrer">
336                  parry.gg
337                </Link>
338                . Keep it private!
339              </DialogContentText>
340              <Stack alignItems="center" direction="row" gap="8px">
341                <TextField
342                  fullWidth
343                  label="parry.gg API key (Keep it private!)"
344                  onChange={(event) => {
345                    setParryggApiKey(event.target.value);
346                    setShouldGetTournaments(true);
347                  }}
348                  size="small"
349                  type="password"
350                  value={parryggApiKey}
351                  variant="standard"
352                />
353                <Button
354                  disabled={copied}
355                  endIcon={copied ? undefined : <ContentCopy />}
356                  onClick={async () => {
357                    await window.electron.copyToClipboard(parryggApiKey);
358                    setCopied(true);
359                    setTimeout(() => setCopied(false), 5000);
360                  }}
361                  variant="contained"
362                >
363                  {copied ? 'Copied!' : 'Copy'}
364                </Button>
365              </Stack>
366            </>
367          )}
368          <Stack>
369            <LabeledCheckbox
370              checked={guidedMode}
371              label="Use walkthrough mode"
372              labelPlacement="end"
373              set={async (checked) => {
374                await window.electron.setGuidedMode(checked);
375                setGuidedMode(checked);
376              }}
377            />
378            <Stack
379              direction="row"
380              justifyContent="space-between"
381              alignItems="center"
382            >
383              <LabeledCheckbox
384                checked={trashDir.length > 0}
385                disabled={!trashDir}
386                label={
387                  trashDir
388                    ? 'Using trash folder:'
389                    : 'Deleting replays permanently'
390                }
391                labelPlacement="end"
392                set={async (checked) => {
393                  if (!checked) {
394                    await window.electron.clearTrashDir();
395                    setTrashDir('');
396                  }
397                }}
398                style={{ flexShrink: 0 }}
399              />
400              {trashDir ? (
401                <Typography
402                  overflow="hidden"
403                  textAlign="left"
404                  textOverflow="ellipsis"
405                  style={{ direction: 'rtl', opacity: '0.5' }}
406                  variant="caption"
407                  whiteSpace="nowrap"
408                >
409                  {trashDir}
410                </Typography>
411              ) : (
412                <Button
413                  disabled={choosingTrashDir}
414                  endIcon={
415                    choosingTrashDir ? (
416                      <CircularProgress size="24px" />
417                    ) : undefined
418                  }
419                  onClick={async () => {
420                    setChoosingTrashDir(true);
421                    try {
422                      setTrashDir(await window.electron.chooseTrashDir());
423                    } catch {
424                      // just catch
425                    } finally {
426                      setChoosingTrashDir(false);
427                    }
428                  }}
429                  variant="contained"
430                >
431                  Set trash folder
432                </Button>
433              )}
434            </Stack>
435            <LabeledCheckbox
436              checked={vlerkMode}
437              label={
438                <span>
439                  Use &quot;Vlerk mode&quot; for post tournament supplemental
440                  reporting,{' '}
441                  <Link
442                    href={`https://github.com/jmlee337/replay-manager-for-slippi/blob/${appVersion}/src/docs/vlerk.md`}
443                    target="_blank"
444                    rel="noreferrer"
445                  >
446                    see more details
447                  </Link>
448                </span>
449              }
450              labelPlacement="end"
451              set={async (checked) => {
452                await window.electron.setVlerkMode(checked);
453                setVlerkMode(checked);
454              }}
455            />
456            <LabeledCheckbox
457              checked={useLAN}
458              label="Use LAN"
459              labelPlacement="end"
460              set={async (checked) => {
461                await window.electron.setUseLAN(checked);
462                setUseLAN(checked);
463              }}
464            />
465            {(mode === Mode.STARTGG || mode === Mode.OFFLINE_MODE) && (
466              <LabeledCheckbox
467                disabled={hostFormat.smuggleCostumeIndex !== undefined}
468                checked={smuggleCostumeIndex}
469                label={
470                  <span>
471                    Report character color,{' '}
472                    <Link
473                      href={`https://github.com/jmlee337/replay-manager-for-slippi/blob/${appVersion}/src/docs/color.md`}
474                      target="_blank"
475                      rel="noreferrer"
476                    >
477                      see more details
478                    </Link>
479                  </span>
480                }
481                labelPlacement="end"
482                set={async (checked) => {
483                  await window.electron.setSmuggleCostumeIndex(checked);
484                  setSmuggleCostumeIndex(checked);
485                }}
486              />
487            )}
488            <LabeledCheckbox
489              checked={hideCopyButton}
490              label="Hide Copy Button"
491              labelPlacement="end"
492              set={async (checked) => {
493                await window.electron.setHideCopyButton(checked);
494                setHideCopyButton(checked);
495              }}
496            />
497            <FormControl variant="standard" style={{ marginBottom: '8px' }}>
498              <InputLabel id="enforcer-setting-select-label">
499                SLP Enforcer {enforcerVersion} (logged to copy folder
500                enforcer.csv)
501              </InputLabel>
502              <Select
503                labelId="enforcer-setting-select-label"
504                disabled={hostFormat.enforcerSetting !== undefined}
505                value={enforcerSetting}
506                onChange={async (event) => {
507                  const newEnforcerSetting = event.target
508                    .value as EnforcerSetting;
509                  await window.electron.setEnforcerSetting(newEnforcerSetting);
510                  setEnforcerSetting(newEnforcerSetting);
511                }}
512              >
513                <MenuItem value={EnforcerSetting.NONE}>Off</MenuItem>
514                <MenuItem value={EnforcerSetting.LOG_ONLY}>Log Only</MenuItem>
515                <MenuItem value={EnforcerSetting.POP_UP_GOOMWAVE}>
516                  Alert on Goomwave
517                </MenuItem>
518                <MenuItem value={EnforcerSetting.POP_UP_ALL}>
519                  Alert on All Violations
520                </MenuItem>
521              </Select>
522            </FormControl>
523            <DialogContentText>
524              File/Folder name format placeholders documented{' '}
525              <Link
526                href={`https://github.com/jmlee337/replay-manager-for-slippi/blob/${appVersion}/src/docs/format.md`}
527                target="_blank"
528                rel="noreferrer"
529              >
530                here
531              </Link>
532              .
533            </DialogContentText>
534            <Stack alignItems="end" direction="row" gap="8px">
535              <TextField
536                fullWidth
537                disabled={Boolean(hostFormat.fileNameFormat)}
538                label="File name format"
539                onChange={(event) => {
540                  const newFileNameFormat = event.target.value;
541                  if (newFileNameFormat) {
542                    setFileNameFormat(newFileNameFormat.slice(9));
543                  }
544                }}
545                size="small"
546                value={`{ordinal}${fileNameFormat}`}
547                variant="standard"
548              />
549              <DialogContentText paddingBottom="5px">.slp</DialogContentText>
550              <Button
551                disabled={Boolean(hostFormat.fileNameFormat)}
552                onClick={async () => {
553                  setFileNameFormat(
554                    await window.electron.resetFileNameFormat(),
555                  );
556                }}
557                variant="contained"
558              >
559                Reset
560              </Button>
561            </Stack>
562            <Stack alignItems="end" direction="row" gap="8px">
563              <TextField
564                fullWidth
565                disabled={Boolean(hostFormat.folderNameFormat)}
566                label="Folder name format"
567                onChange={(event) => {
568                  const newFolderNameFormat = event.target.value;
569                  if (newFolderNameFormat) {
570                    setFolderNameFormat(newFolderNameFormat);
571                  }
572                }}
573                size="small"
574                value={folderNameFormat}
575                variant="standard"
576              />
577              <Button
578                disabled={Boolean(hostFormat.folderNameFormat)}
579                onClick={async () => {
580                  setFolderNameFormat(
581                    await window.electron.resetFolderNameFormat(),
582                  );
583                }}
584                variant="contained"
585              >
586                Reset
587              </Button>
588            </Stack>
589          </Stack>
590          {needUpdate && (
591            <Alert
592              severity="warning"
593              style={{ marginTop: '8px' }}
594              action={
595                <Button
596                  endIcon={<CloudDownload />}
597                  variant="contained"
598                  onClick={() => {
599                    window.electron.update();
600                  }}
601                >
602                  Quit and download
603                </Button>
604              }
605            >
606              Update available! Version {latestAppVersion}
607            </Alert>
608          )}
609        </DialogContent>
610      </Dialog>
611    </>
612  );
613}