fork/replay-manager-for-slippi

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

Nicoletofflinemode password to connectc6a7fa9

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