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