fork/replay-manager-for-slippi

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

Nicolethoist settings FAB so it's always clickablee1bb94c

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