fork/replay-manager-for-slippi
git clone https://git.julia.blue/fork/replay-manager-for-slippi
2ca916a
2.3.5
1import { 2BrowserUpdated , 3Close , 4ContentCopy , 5FolderOpen , 6Router , 7} from '@mui/icons-material' ; 8import { 9Alert , 10Button , 11CircularProgress , 12Dialog , 13DialogActions , 14DialogContent , 15DialogContentText , 16DialogTitle , 17Divider , 18IconButton , 19InputBase , 20List , 21ListItem , 22ListItemButton , 23ListItemText , 24MenuItem , 25Snackbar , 26Stack , 27TextField , 28Tooltip , 29Typography , 30} from '@mui/material' ; 31import { useCallback , useEffect , useMemo , useState } from 'react' ; 32import { 33CopySettings , 34Output , 35WebSocketServerStatus , 36CopyHostOrClient , 37CopyHostFormat , 38GuideState , 39} from '../common/types' ; 40import ErrorDialog from './ErrorDialog' ; 41import LabeledCheckbox from './LabeledCheckbox' ; 42import { setWindowEventListener , WindowEvent } from './setWindowEventListener' ; 43 44function ClientsDialog ({ 45 disabled, 46 setHosting, 47} :{ 48disabled :boolean ; 49setHosting :( hosting :boolean ) => void ; 50}) { 51const [ open , setOpen ] = useState ( false ); 52const [ status , setStatus ] = useState ( WebSocketServerStatus . STOPPED ); 53const [ clients , setClients ] = useState < CopyHostOrClient []>([]); 54const [ v4Address , setV4Address ] = useState ( '' ); 55const [ v6Address , setV6Address ] = useState ( '' ); 56const [ port , setPort ] = useState ( 0 ); 57const [ hostname , setHostname ] = useState ( '' ); 58 59const [ v4Copied , setV4Copied ] = useState ( false ); 60const [ v6Copied , setV6Copied ] = useState ( false ); 61 62const v4AddressAndPort = useMemo ( 63() => ` ${ v4Address } : ${ port } ` , 64[ v4Address , port ], 65); 66const v6AddressAndPort = useMemo ( 67() => `[ ${ v6Address } ]: ${ port } ` , 68[ v6Address , port ], 69); 70 71useEffect (() => { 72( async () => { 73setClients ( await window . electron . getCopyClients ()); 74})(); 75}, []); 76 77useEffect (() => { 78window . electron . onHostServerStatus (( event , newStatus ) => { 79setStatus ( newStatus ); 80setHosting ( newStatus !== WebSocketServerStatus . STOPPED ); 81}); 82}, [ setHosting ]); 83useEffect (() => { 84window . electron . onCopyClients (( event , newClients ) => { 85setClients ( newClients ); 86}); 87}, []); 88 89const [ stopOpen , setStopOpen ] = useState ( false ); 90 91return ( 92<> 93< Button 94disabled = { disabled } 95endIcon = {< BrowserUpdated />} 96onClick = { async () => { 97setHostname ( await window . electron . startHostServer ()); 98const startBroadcastingHostRet = 99await window . electron . startBroadcastingHost (); 100setV4Address ( startBroadcastingHostRet . v4Address ); 101setV6Address ( startBroadcastingHostRet . v6Address ); 102setPort ( startBroadcastingHostRet . port ); 103setOpen ( true ); 104}} 105> 106{ status === WebSocketServerStatus . STOPPED 107 ?'Host on LAN' 108 :`Add Clients ( ${ clients . length } )` } 109</ Button > 110< Dialog 111open = { open } 112onClose = { async () => { 113if ( clients . length === 0 ) { 114await window . electron . stopHostServer (); 115} 116await window . electron . stopBroadcastingHost (); 117setOpen ( false ); 118}} 119fullWidth 120> 121< DialogTitle > Hosting on LAN as { hostname }</ DialogTitle > 122< DialogContent > 123< Stack alignItems = "center" direction = "row" gap = "8px" > 124< TextField 125disabled = { ! v6Address } 126fullWidth 127label = "Websocket Address (IPv6)" 128size = "small" 129value = { v6AddressAndPort } 130variant = "standard" 131/> 132< Button 133disabled = { v6Copied || ! v6Address } 134endIcon = { v6Copied ?undefined :< ContentCopy />} 135onClick = { async () => { 136await window . electron . copyToClipboard ( v6AddressAndPort ); 137setV6Copied ( true ); 138setTimeout (() => setV6Copied ( false ), 5000 ); 139}} 140variant = "contained" 141> 142{ v6Copied ?'Copied!' :'Copy' } 143</ Button > 144</ Stack > 145< Stack alignItems = "center" direction = "row" gap = "8px" > 146< TextField 147disabled = { ! v4Address } 148fullWidth 149label = "Websocket Address (IPv4)" 150size = "small" 151value = { v4AddressAndPort } 152variant = "standard" 153/> 154< Button 155disabled = { v4Copied || ! v4Address } 156endIcon = { v4Copied ?undefined :< ContentCopy />} 157onClick = { async () => { 158await window . electron . copyToClipboard ( v4AddressAndPort ); 159setV4Copied ( true ); 160setTimeout (() => setV4Copied ( false ), 5000 ); 161}} 162variant = "contained" 163> 164{ v4Copied ?'Copied!' :'Copy' } 165</ Button > 166</ Stack > 167< List > 168{ clients . map (({ address, name}) => ( 169< ListItem 170key = { address } 171disablePadding 172style = {{ marginRight :'-16px' , width :'calc(100% + 16px)' }} 173secondaryAction = { 174< Tooltip arrow title = "Disconnect" > 175< IconButton 176edge = "end" 177onClick = { async () => { 178await window . electron . kickCopyClient ( address ); 179}} 180> 181< Close /> 182</ IconButton > 183</ Tooltip > 184} 185> 186< ListItemText > 187{ address } - { name } 188</ ListItemText > 189</ ListItem > 190))} 191</ List > 192</ DialogContent > 193< DialogActions > 194< Button 195color = "error" 196variant = "contained" 197onClick = {() => { 198setStopOpen ( true ); 199}} 200> 201Stop Hosting 202</ Button > 203</ DialogActions > 204</ Dialog > 205< Dialog 206open = { stopOpen } 207onClose = {() => { 208setStopOpen ( false ); 209}} 210> 211< DialogTitle > Stop hosting on LAN? </ DialogTitle > 212< DialogActions > 213< Button 214variant = "contained" 215onClick = {() => { 216setStopOpen ( false ); 217}} 218> 219Cancel 220</ Button > 221< Button 222color = "error" 223variant = "contained" 224onClick = { async () => { 225await window . electron . stopBroadcastingHost (); 226await window . electron . stopHostServer (); 227setStopOpen ( false ); 228setOpen ( false ); 229}} 230> 231Stop 232</ Button > 233</ DialogActions > 234</ Dialog > 235</> 236); 237} 238 239function HostsDialog ({ host} :{ host :CopyHostOrClient }) { 240const [ open , setOpen ] = useState ( false ); 241const [ copyHosts , setCopyHosts ] = useState < string []>([]); 242const [ selfName , setSelfName ] = useState ( '' ); 243useEffect (() => { 244window . electron . onCopyHosts (( event , newCopyHosts ) => { 245setCopyHosts ( newCopyHosts ); 246}); 247}, []); 248 249const [ connecting , setConnecting ] = useState ( false ); 250const [ manualAddress , setManualAddress ] = useState ( '' ); 251const [ error , setError ] = useState ( '' ); 252 253return ( 254<> 255< Button 256onClick = { async () => { 257setCopyHosts ([]); 258setError ( '' ); 259setSelfName ( await window . electron . startListeningForHosts ()); 260setOpen ( true ); 261}} 262endIcon = {< Router />} 263> 264{ host . address ?'Connected' :'Search LAN' } 265</ Button > 266< Dialog 267open = { open } 268onClose = { async () => { 269await window . electron . stopListeningForHosts (); 270setOpen ( false ); 271}} 272fullWidth 273> 274< DialogTitle > Searching LAN for host Replay Reporter... </ DialogTitle > 275< DialogContent > 276< form 277style = {{ 278alignItems :'center' , 279display :'flex' , 280marginTop :'8px' , 281marginBottom :'8px' , 282gap :'8px' , 283}} 284onSubmit = { async ( event ) => { 285event . preventDefault (); 286event . stopPropagation (); 287 288setConnecting ( true ); 289try { 290await window . electron . connectToHost ( manualAddress ); 291await window . electron . stopListeningForHosts (); 292setOpen ( false ); 293} catch ( e :unknown ) { 294if ( e instanceof Error ) { 295setError ( e . message ); 296} 297} finally { 298setConnecting ( false ); 299} 300}} 301> 302< TextField 303autoFocus 304label = "IP address and port" 305placeholder = "192.168.0.106:54673" 306size = "small" 307variant = "outlined" 308value = { manualAddress } 309onChange = {( event ) => { 310setManualAddress ( event . target . value ); 311}} 312/> 313< Button 314disabled = { connecting } 315endIcon = { connecting && < CircularProgress size = "24px" />} 316type = "submit" 317variant = "contained" 318> 319Connect 320</ Button > 321</ form > 322{ error && < Alert severity = "error" >{ error }</ Alert >} 323< DialogContentText > Searching as { selfName }</ DialogContentText > 324< List > 325{ host . address && ( 326< ListItem 327disablePadding 328style = {{ marginRight :'-16px' , width :'calc(100% + 16px)' }} 329secondaryAction = { 330< Tooltip arrow title = "Disconnect" > 331< IconButton 332edge = "end" 333onClick = { async () => { 334await window . electron . disconnectFromHost (); 335}} 336> 337< Close /> 338</ IconButton > 339</ Tooltip > 340} 341> 342< ListItemText > 343{ host . address } - { host . name } 344</ ListItemText > 345</ ListItem > 346)} 347{ copyHosts . map (( copyHost ) => ( 348< ListItem 349key = { copyHost } 350disablePadding 351style = {{ margin :'0 -16px' , width :'calc(100% + 32px)' }} 352> 353< ListItemButton 354onClick = { async () => { 355setConnecting ( true ); 356try { 357await window . electron . connectToHost ( copyHost ); 358await window . electron . stopListeningForHosts (); 359setOpen ( false ); 360} catch ( e :unknown ) { 361if ( e instanceof Error ) { 362setError ( e . message ); 363} 364} finally { 365setConnecting ( false ); 366} 367}} 368> 369< ListItemText >{ copyHost }</ ListItemText > 370</ ListItemButton > 371</ ListItem > 372))} 373< ListItem disableGutters > 374< CircularProgress size = "24px" /> 375</ ListItem > 376</ List > 377</ DialogContent > 378</ Dialog > 379</> 380); 381} 382 383export default function CopyControls ({ 384 dir, 385 setDir, 386 useLAN, 387 host, 388 hostFormat, 389 error, 390 setError, 391 errorDialogOpen, 392 setErrorDialogOpen, 393 hasSelectedReplays, 394 isCopying, 395 onCopy, 396 success, 397 copySettings, 398 setCopySettings, 399 elevateSettings, 400 vlerkMode, 401 undoSubdir, 402 hideCopyButton, 403 setConfirmedCopySettings, 404 guideState, 405 setGuideBackdropOpen, 406} :{ 407dir :string ; 408setDir :( dir :string ) => void ; 409useLAN :boolean ; 410host :CopyHostOrClient ; 411hostFormat :CopyHostFormat ; 412error :string ; 413setError :( error :string ) => void ; 414errorDialogOpen :boolean ; 415setErrorDialogOpen :( errorDialogOpen :boolean ) => void ; 416hasSelectedReplays :boolean ; 417isCopying :boolean ; 418onCopy :() => Promise < void >; 419success :string ; 420copySettings :CopySettings ; 421setCopySettings :( newCopySettings :CopySettings ) => void ; 422elevateSettings :boolean ; 423vlerkMode :boolean ; 424undoSubdir :string ; 425hideCopyButton :boolean ; 426setConfirmedCopySettings :( confirmedCopySettings :boolean ) => void ; 427guideState :GuideState ; 428setGuideBackdropOpen :( backdropOpen :boolean ) => void ; 429}) { 430const [ hosting , setHosting ] = useState ( false ); 431const [ copySettingsOpen , setCopySettingsOpen ] = useState ( elevateSettings ); 432const [ copyToastOpen , setCopyToastOpen ] = useState ( false ); 433 434useEffect (() => { 435if ( elevateSettings ) { 436setCopySettingsOpen ( true ); 437} 438}, [ elevateSettings ]); 439 440const chooseDir = async () => { 441const newDir = await window . electron . chooseCopyDir (); 442if ( newDir ) { 443setDir ( newDir ); 444} 445}; 446 447const ctrlS = useCallback ( async () => { 448if ( 449vlerkMode && 450! isCopying && 451( dir || host . address ) && 452hasSelectedReplays 453) { 454try { 455await onCopy (); 456setCopyToastOpen ( true ); 457} catch ( e :any ) { 458const message = e instanceof Error ?e . message :e ; 459setError ( message ); 460setErrorDialogOpen ( true ); 461} 462} 463}, [ 464dir , 465hasSelectedReplays , 466host . address , 467isCopying , 468onCopy , 469setError , 470setErrorDialogOpen , 471vlerkMode , 472]); 473 474useEffect (() => { 475setWindowEventListener ( WindowEvent . CTRLS , ctrlS ); 476}, [ ctrlS ]); 477 478return ( 479<> 480< Divider /> 481< Stack paddingLeft = "42px" > 482< Stack direction = "row" > 483< InputBase 484disabled 485size = "small" 486value = { 487host . address 488 ?` ${ host . address } - ${ host . name } ` 489 :dir || 'Set copy folder...' 490} 491style = {{ flexGrow :1 }} 492/> 493{ useLAN && ( 494<> 495{ ! host . address && ( 496< ClientsDialog 497disabled = { ! dir } 498setHosting = { async ( newHosting :boolean ) => { 499if ( newHosting ) { 500setCopySettings ({ 501 ...copySettings , 502output :Output . ZIP , 503}); 504} else { 505setCopySettings ( await window . electron . getCopySettings ()); 506} 507setHosting ( newHosting ); 508}} 509/> 510)} 511{ ! hosting && < HostsDialog host = { host } />} 512</> 513)} 514< Tooltip arrow title = "Set copy folder" > 515< IconButton onClick = { chooseDir }> 516< FolderOpen /> 517</ IconButton > 518</ Tooltip > 519</ Stack > 520< Stack 521alignItems = "center" 522direction = "row" 523justifyContent = "right" 524spacing = "8px" 525> 526< ErrorDialog 527messages = {[ error ]} 528onClose = {() => { 529setError ( '' ); 530setErrorDialogOpen ( false ); 531}} 532open = { errorDialogOpen } 533/> 534< Button 535variant = "text" 536onClick = {() => { 537setCopySettingsOpen ( true ); 538}} 539sx = { 540elevateSettings 541 ?{ 542backgroundColor :'white' , 543zIndex :( theme ) => theme . zIndex . modal + 1 , 544} 545 :undefined 546} 547> 548Copy Settings 549</ Button > 550< Dialog 551open = { copySettingsOpen } 552onClose = {() => { 553setCopySettingsOpen ( false ); 554setConfirmedCopySettings ( true ); 555setGuideBackdropOpen ( guideState !== GuideState . NONE ); 556}} 557> 558< DialogTitle > Copy Settings </ DialogTitle > 559< DialogContent > 560< Tooltip 561arrow 562placement = "right" 563title = "A file containing set metadata will be included in the copy folder/zip" 564> 565< div > 566< LabeledCheckbox 567checked = { copySettings . writeContext } 568disabled = { 569hostFormat . copySettings !== undefined || 570copySettings . output === Output . FILES 571} 572label = "Write context.json" 573set = { async ( checked :boolean ) => { 574const newCopySettings = { ...copySettings }; 575newCopySettings . writeContext = checked ; 576await window . electron . setCopySettings ( newCopySettings ); 577setCopySettings ( newCopySettings ); 578}} 579/> 580</ div > 581</ Tooltip > 582< Tooltip 583arrow 584placement = "right" 585title = "Player tags will appear in the in-game HUD (like Slippi Broadcast/Online) depending on playback settings" 586> 587< div > 588< LabeledCheckbox 589checked = { copySettings . writeDisplayNames } 590disabled = { hostFormat . copySettings !== undefined } 591label = "Overwrite Display Names" 592set = { async ( checked :boolean ) => { 593const newCopySettings = { ...copySettings }; 594newCopySettings . writeDisplayNames = checked ; 595await window . electron . setCopySettings ( newCopySettings ); 596setCopySettings ( newCopySettings ); 597}} 598/> 599</ div > 600</ Tooltip > 601< Tooltip 602arrow 603placement = "right" 604title = "New file names will indicate game start time, player tags, and characters" 605> 606< div > 607< LabeledCheckbox 608checked = { copySettings . writeFileNames } 609disabled = { hostFormat . copySettings !== undefined } 610label = "Overwrite File Names" 611set = { async ( checked :boolean ) => { 612const newCopySettings = { ...copySettings }; 613newCopySettings . writeFileNames = checked ; 614await window . electron . setCopySettings ( newCopySettings ); 615setCopySettings ( newCopySettings ); 616}} 617/> 618</ div > 619</ Tooltip > 620< Tooltip 621arrow 622placement = "right" 623title = "Game start times will be shifted to have the set end exactly now. Useful if your Wii clocks aren't accurate" 624> 625< div > 626< LabeledCheckbox 627checked = { 628copySettings . writeStartTimes && undoSubdir . length === 0 629} 630disabled = { 631hostFormat . copySettings !== undefined || 632undoSubdir . length > 0 633} 634label = "Overwrite Start Times" 635set = { async ( checked :boolean ) => { 636const newCopySettings = { ...copySettings }; 637newCopySettings . writeStartTimes = checked ; 638await window . electron . setCopySettings ( newCopySettings ); 639setCopySettings ( newCopySettings ); 640}} 641/> 642</ div > 643</ Tooltip > 644< TextField 645disabled = { hostFormat . copySettings !== undefined || hosting } 646label = "Output" 647onChange = { async ( event ) => { 648const newCopySettings = { ...copySettings }; 649newCopySettings . output = parseInt ( event . target . value , 10 ); 650await window . electron . setCopySettings ( newCopySettings ); 651setCopySettings ( newCopySettings ); 652}} 653select 654size = "small" 655style = {{ marginTop :'8px' }} 656value = { copySettings . output } 657> 658< MenuItem value = { Output . FILES }> Separate Files </ MenuItem > 659< MenuItem value = { Output . FOLDER }> Make Subfolder </ MenuItem > 660< MenuItem value = { Output . ZIP }> Make ZIP folder </ MenuItem > 661</ TextField > 662</ DialogContent > 663</ Dialog > 664{( vlerkMode || ! hideCopyButton ) && ( 665<> 666{ success && < Typography variant = "caption" >{ success }</ Typography >} 667< Button 668disabled = { 669isCopying || ( ! dir && ! host . address ) || ! hasSelectedReplays 670} 671onClick = { async () => { 672try { 673await onCopy (); 674} catch ( e :any ) { 675const message = e instanceof Error ?e . message :e ; 676setError ( message ); 677setErrorDialogOpen ( true ); 678} 679}} 680variant = "contained" 681> 682{ isCopying ?'Copying...' :'Copy' } 683</ Button > 684</> 685)} 686</ Stack > 687</ Stack > 688{ vlerkMode && ( 689< Snackbar 690open = { copyToastOpen } 691autoHideDuration = { 5000 } 692onClose = {( ev , reason ) => { 693if ( reason === 'clickaway' ) { 694return ; 695} 696setCopyToastOpen ( false ); 697}} 698message = "Copied!" 699/> 700)} 701</> 702); 703}