fork/replay-manager-for-slippi
git clone https://git.julia.blue/fork/replay-manager-for-slippi
bb31521
2.5.0
1import { Cable , ContentCopy } from '@mui/icons-material' ; 2import { 3Alert , 4Button , 5CircularProgress , 6Dialog , 7DialogContent , 8DialogTitle , 9IconButton , 10InputBase , 11List , 12ListItemButton , 13ListItemText , 14Stack , 15TextField , 16Tooltip , 17} from '@mui/material' ; 18import { useEffect , useState } from 'react' ; 19import { 20OfflineModeStatus , 21RendererOfflineModeTournament , 22} from '../common/types' ; 23 24function toLabel ( offlineModeStatus :OfflineModeStatus ) { 25return offlineModeStatus . addressOrHost 26 ?'Offline Mode connected' 27 :'Connect to Offline Mode' ; 28} 29 30export function OfflineModeConnectionDialogContent ({ 31 offlineModeStatus, 32} :{ 33offlineModeStatus :OfflineModeStatus ; 34}) { 35const [ offlineModePassword , setOfflineModePassword ] = useState ( '' ); 36const [ offlineModeHosts , setOfflineModeHosts ] = useState < string []>([]); 37useEffect (() => { 38window . electron . onOfflineModeHosts (( event , newOfflineModeHosts ) => { 39setOfflineModeHosts ( newOfflineModeHosts ); 40}); 41( async () => { 42const offlineModePasswordPromise = 43window . electron . getOfflineModePassword (); 44const offlineModeHostsPromise = window . electron . getOfflineModeHosts (); 45setOfflineModePassword ( await offlineModePasswordPromise ); 46setOfflineModeHosts ( await offlineModeHostsPromise ); 47})(); 48}, []); 49 50const [ addressOrHost , setAddressOrHost ] = useState ( '127.0.0.1' ); 51const [ passwordCopied , setPasswordCopied ] = useState ( false ); 52const [ addressCopied , setAddressCopied ] = useState ( false ); 53 54return ( 55<> 56< DialogTitle >{ toLabel ( offlineModeStatus )}</ DialogTitle > 57< DialogContent > 58{ offlineModeStatus . addressOrHost ?( 59<> 60< Stack 61alignItems = "center" 62direction = "row" 63gap = "8px" 64marginBottom = "8px" 65> 66< TextField 67fullWidth 68disabled 69label = "Offline Mode password" 70size = "small" 71type = "password" 72value = { offlineModePassword } 73variant = "standard" 74/> 75< Button 76disabled = { passwordCopied } 77endIcon = { passwordCopied ?undefined :< ContentCopy />} 78onClick = { async () => { 79await window . electron . copyToClipboard ( offlineModePassword ); 80setPasswordCopied ( true ); 81setTimeout (() => setPasswordCopied ( false ), 5000 ); 82}} 83variant = "contained" 84> 85{ passwordCopied ?'Copied!' :'Copy' } 86</ Button > 87</ Stack > 88< Stack direction = "row" alignItems = "center" spacing = "8px" > 89< TextField 90fullWidth 91label = "Host" 92size = "small" 93value = { offlineModeStatus . addressOrHost } 94variant = "standard" 95/> 96< Button 97disabled = { addressCopied } 98endIcon = { addressCopied ?undefined :< ContentCopy />} 99onClick = { async () => { 100await window . electron . copyToClipboard ( 101offlineModeStatus . addressOrHost , 102); 103setAddressCopied ( true ); 104setTimeout (() => setAddressCopied ( false ), 5000 ); 105}} 106variant = "contained" 107> 108{ addressCopied ?'Copied!' :'Copy' } 109</ Button > 110</ Stack > 111</> 112) :( 113<> 114< form 115style = {{ 116display :'flex' , 117flexDirection :'column' , 118gap :'8px' , 119}} 120onSubmit = { async ( event ) => { 121event . preventDefault (); 122event . stopPropagation (); 123await window . electron . setOfflineModePassword ( 124offlineModePassword , 125); 126window . electron . connectToOfflineMode ( addressOrHost ); 127}} 128> 129< Stack alignItems = "center" direction = "row" gap = "8px" > 130< TextField 131fullWidth 132label = "Offline Mode password" 133onChange = {( event ) => { 134setOfflineModePassword ( event . target . value ); 135}} 136size = "small" 137type = "password" 138value = { offlineModePassword } 139variant = "standard" 140/> 141< Button 142disabled = { passwordCopied } 143endIcon = { passwordCopied ?undefined :< ContentCopy />} 144onClick = { async () => { 145await window . electron . copyToClipboard ( offlineModePassword ); 146setPasswordCopied ( true ); 147setTimeout (() => setPasswordCopied ( false ), 5000 ); 148}} 149variant = "contained" 150> 151{ passwordCopied ?'Copied!' :'Copy' } 152</ Button > 153</ Stack > 154< Stack alignItems = "center" direction = "row" gap = "8px" > 155< TextField 156autoFocus 157fullWidth 158label = "Host" 159onChange = {( event ) => { 160setAddressOrHost ( event . target . value ); 161}} 162size = "small" 163style = {{ flexGrow :1 }} 164value = { addressOrHost } 165variant = "standard" 166/> 167< Button 168disabled = { ! addressOrHost } 169type = "submit" 170variant = "contained" 171> 172Connect 173</ Button > 174</ Stack > 175</ form > 176{ offlineModeHosts . length === 0 ?( 177< Stack direction = "row" justifyContent = "center" margin = "8px 0" > 178< CircularProgress /> 179</ Stack > 180) :( 181< List > 182{ offlineModeHosts . map (( offlineModeHost ) => ( 183< ListItemButton 184key = { offlineModeHost } 185onClick = { async () => { 186await window . electron . setOfflineModePassword ( 187offlineModePassword , 188); 189window . electron . connectToOfflineMode ( offlineModeHost ); 190}} 191> 192< ListItemText >{ offlineModeHost }</ ListItemText > 193</ ListItemButton > 194))} 195</ List > 196)} 197{ offlineModeStatus . error && ( 198< Alert severity = "error" >{ offlineModeStatus . error }</ Alert > 199)} 200</> 201)} 202</ DialogContent > 203</> 204); 205} 206 207export default function OfflineModeConnection ({ 208 offlineModeStatus, 209 offlineModeTournament, 210} :{ 211offlineModeStatus :OfflineModeStatus ; 212offlineModeTournament :RendererOfflineModeTournament ; 213}) { 214const [ open , setOpen ] = useState ( false ); 215 216useEffect (() => { 217if ( offlineModeStatus . addressOrHost ) { 218setOpen ( false ); 219} 220}, [ offlineModeStatus ]); 221 222return ( 223< Stack direction = "row" > 224< InputBase 225disabled 226size = "small" 227value = { 228offlineModeTournament . slug || 229offlineModeStatus . addressOrHost || 230( offlineModeStatus . error ?'Error!' :'Connect to Offline Mode...' ) 231} 232style = {{ flexGrow :1 }} 233/> 234< Tooltip arrow title = { toLabel ( offlineModeStatus )}> 235< IconButton 236onClick = {() => { 237setOpen ( true ); 238if ( ! offlineModeStatus . addressOrHost ) { 239window . electron . listenForOfflineMode (); 240} 241}} 242> 243< Cable /> 244</ IconButton > 245</ Tooltip > 246< Dialog 247fullWidth 248open = { open } 249onClose = {() => { 250setOpen ( false ); 251}} 252> 253< OfflineModeConnectionDialogContent 254offlineModeStatus = { offlineModeStatus } 255/> 256</ Dialog > 257</ Stack > 258); 259}