fork/replay-manager-for-slippi
git clone https://git.julia.blue/fork/replay-manager-for-slippi
276de8b
2.4.0
1import { useState } from 'react' ; 2import { 3Button , 4CircularProgress , 5Dialog , 6DialogActions , 7DialogContent , 8DialogContentText , 9DialogTitle , 10IconButton , 11List , 12ListItemButton , 13ListItemText , 14Stack , 15Tooltip , 16} from '@mui/material' ; 17import { Refresh , Tv } from '@mui/icons-material' ; 18import { Mode , Set , State , Station , Stream } from '../common/types' ; 19import SetView from './SetView' ; 20import { assertInteger } from '../common/asserts' ; 21 22function getCombinedStreamName ( stream :Stream ) { 23let prefix = '' ; 24if ( stream . domain === 'twitch' ) { 25prefix = 'ttv/' ; 26} else if ( stream . domain === 'youtube' ) { 27prefix = 'yt/' ; 28} 29return prefix + stream . path ; 30} 31 32export default function AssignStream ({ 33 mode, 34 selectedSet, 35 streams, 36 stations, 37 refreshStartggTournament, 38} :{ 39mode :Mode ; 40selectedSet :Set ; 41streams :Stream []; 42stations :Station []; 43refreshStartggTournament :() => Promise < void >; 44}) { 45const [ chooseOpen , setChooseOpen ] = useState ( false ); 46const [ errorOpen , setErrorOpen ] = useState ( false ); 47const [ error , setError ] = useState ( '' ); 48 49const [ assigning , setAssigning ] = useState ( false ); 50const assignStream = async ( streamId :number ) => { 51setAssigning ( true ); 52try { 53if ( mode === Mode . STARTGG ) { 54await window . electron . assignStream ( selectedSet , streamId ); 55} else if ( mode === Mode . OFFLINE_MODE ) { 56await window . electron . assignOfflineModeSetStream ( 57assertInteger ( selectedSet . id ), 58streamId , 59); 60} 61setChooseOpen ( false ); 62} catch ( e :any ) { 63setError ( e . toString ()); 64setErrorOpen ( true ); 65} finally { 66setAssigning ( false ); 67} 68}; 69const assignStation = async ( stationId :number ) => { 70setAssigning ( true ); 71try { 72if ( mode === Mode . STARTGG ) { 73await window . electron . assignStation ( selectedSet , stationId ); 74} else if ( mode === Mode . OFFLINE_MODE ) { 75await window . electron . assignOfflineModeSetStation ( 76assertInteger ( selectedSet . id ), 77stationId , 78); 79} 80setChooseOpen ( false ); 81} catch ( e :any ) { 82setError ( e . toString ()); 83setErrorOpen ( true ); 84} finally { 85setAssigning ( false ); 86} 87}; 88 89const [ refreshingStartggTournament , setRefreshingStartggTournament ] = 90useState ( false ); 91 92return ( 93<> 94< Tooltip arrow title = "Assign set to stream or station" > 95< div > 96< IconButton 97color = "primary" 98disabled = { 99! ( 100typeof selectedSet . id === 'string' || 101( Number . isInteger ( selectedSet . id ) && selectedSet . id > 0 ) 102) || 103( mode !== Mode . STARTGG && mode !== Mode . OFFLINE_MODE ) 104} 105size = "small" 106onClick = { async () => { 107setChooseOpen ( true ); 108}} 109> 110< Tv /> 111</ IconButton > 112</ div > 113</ Tooltip > 114< Dialog open = { chooseOpen } onClose = {() => setChooseOpen ( false )}> 115< Stack 116alignItems = "center" 117direction = "row" 118justifyContent = "space-between" 119marginRight = "24px" 120> 121< DialogTitle > Assign Set to Stream or Station </ DialogTitle > 122{ mode === Mode . STARTGG && ( 123< Tooltip title = "Refresh" > 124< IconButton 125disabled = { refreshingStartggTournament } 126onClick = { async () => { 127try { 128setRefreshingStartggTournament ( true ); 129await refreshStartggTournament (); 130} finally { 131setRefreshingStartggTournament ( false ); 132} 133}} 134> 135{ refreshingStartggTournament ?( 136< CircularProgress size = "24px" /> 137) :( 138< Refresh /> 139)} 140</ IconButton > 141</ Tooltip > 142)} 143</ Stack > 144< DialogContent style = {{ maxWidth :'500px' }}> 145< SetView 146entrant1Names = { selectedSet . entrant1Participants . map ( 147( participant ) => ({ name :participant . displayName }), 148)} 149entrant1Score = { selectedSet . entrant1Score } 150entrant1Win = { selectedSet . winnerId === selectedSet . entrant1Id } 151entrant2Names = { selectedSet . entrant2Participants . map ( 152( participant ) => ({ name :participant . displayName }), 153)} 154entrant2Score = { selectedSet . entrant2Score } 155fullRoundText = { selectedSet . fullRoundText } 156showScores = { selectedSet . state === State . COMPLETED } 157state = { selectedSet . state } 158stream = { selectedSet . stream } 159station = { selectedSet . station } 160wasReported = { false } 161updatedAtMs = { selectedSet . updatedAtMs } 162/> 163{ streams . length > 0 && ( 164<> 165{ selectedSet . stream && ( 166< ListItemButton 167disabled = { assigning } 168disableGutters 169style = {{ marginTop :'8px' }} 170onClick = {() => assignStream ( 0 )} 171> 172< ListItemText > 173Remove from { getCombinedStreamName ( selectedSet . stream )} 174</ ListItemText > 175</ ListItemButton > 176)} 177< List disablePadding > 178{ streams 179. filter (( stream ) => stream . id !== selectedSet . stream ?. id ) 180. map (( stream ) => ( 181< ListItemButton 182disabled = { assigning } 183key = { stream . id } 184disableGutters 185onClick = {() => assignStream ( stream . id )} 186> 187< ListItemText > 188{ getCombinedStreamName ( stream )} 189</ ListItemText > 190</ ListItemButton > 191))} 192</ List > 193</> 194)} 195{ stations . length > 0 && ( 196<> 197{ selectedSet . station && ( 198< ListItemText style = {{ padding :'12px 0' , margin :'8px 0 0' }}> 199Assigned to station { selectedSet . station . number } 200</ ListItemText > 201)} 202< List 203disablePadding 204style = {{ 205display :'flex' , 206flexDirection :'row' , 207flexWrap :'wrap' , 208}} 209> 210{ stations 211. filter (( station ) => station . id !== selectedSet . station ?. id ) 212. map (( station ) => ( 213< ListItemButton 214disabled = { assigning } 215key = { station . id } 216style = {{ flexGrow :0 }} 217onClick = {() => assignStation ( station . id )} 218> 219< ListItemText >{ station . number }</ ListItemText > 220</ ListItemButton > 221))} 222</ List > 223</> 224)} 225</ DialogContent > 226</ Dialog > 227< Dialog 228open = { errorOpen } 229onClose = {() => { 230setErrorOpen ( false ); 231setError ( '' ); 232}} 233> 234< DialogTitle > Assign Set to Stream error! </ DialogTitle > 235< DialogContent > 236< DialogContentText >{ error }</ DialogContentText > 237</ DialogContent > 238< DialogActions > 239< Button 240onClick = {() => { 241setErrorOpen ( false ); 242setError ( '' ); 243}} 244> 245Close 246</ Button > 247</ DialogActions > 248</ Dialog > 249</> 250); 251}