fork/replay-manager-for-slippi

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

Nicoletset section ellipsis and called icon and colorc9ee152

2.5.0
5.8 KiB225 linesraw
1import { formatDistanceToNowStrict } from 'date-fns';
2import styled from '@emotion/styled';
3import {
4  Backup,
5  CheckBox,
6  HourglassTop,
7  NotificationsActive,
8  Tv,
9} from '@mui/icons-material';
10import { Box, Stack, Tooltip, Typography } from '@mui/material';
11import { useState } from 'react';
12import { NameWithHighlight, State, Station, Stream } from '../common/types';
13import { calledColor, highlightColor, startedColor } from '../common/constants';
14
15const EntrantNames = styled(Stack)`
16  width: calc(100% - 22px);
17`;
18
19const EntrantSection = styled(Stack)`
20  align-items: center;
21  box-sizing: border-box;
22  width: 50%;
23`;
24
25const EntrantScore = styled(Box)`
26  overflow-x: hidden;
27  margin: 0 4px;
28  text-overflow: ellipsis;
29  width: 14px;
30`;
31
32const Name = styled.div`
33  overflow-x: hidden;
34  text-overflow: ellipsis;
35  white-space: nowrap;
36`;
37
38function EntrantName({ entrantName }: { entrantName: NameWithHighlight }) {
39  if (!entrantName.highlight) {
40    return <Name>{entrantName.name}</Name>;
41  }
42
43  return (
44    <Name>
45      <span>{entrantName.name.substring(0, entrantName.highlight.start)}</span>
46      <span style={{ backgroundColor: highlightColor }}>
47        {entrantName.name.substring(
48          entrantName.highlight.start,
49          entrantName.highlight.end,
50        )}
51      </span>
52      <span>{entrantName.name.substring(entrantName.highlight.end)}</span>
53    </Name>
54  );
55}
56
57function CallTimer({
58  updatedAtMs,
59  matchState,
60}: {
61  updatedAtMs: number;
62  matchState: number;
63}) {
64  const [timeAgo, setTimeAgo] = useState(
65    formatDistanceToNowStrict(updatedAtMs, { addSuffix: true }),
66  );
67  const handleOpen = () => {
68    setTimeAgo(formatDistanceToNowStrict(updatedAtMs, { addSuffix: true }));
69  };
70
71  if (matchState === State.STARTED) {
72    return (
73      <Tooltip
74        arrow
75        placement="left-start"
76        onOpen={handleOpen}
77        title={`Started ${timeAgo}`}
78      >
79        <HourglassTop
80          fontSize="small"
81          style={{ color: startedColor, padding: '9px 0' }}
82        />
83      </Tooltip>
84    );
85  }
86
87  if (matchState === State.CALLED) {
88    return (
89      <Tooltip
90        arrow
91        placement="left-start"
92        onOpen={handleOpen}
93        title={`Called ${timeAgo}`}
94      >
95        <NotificationsActive
96          fontSize="small"
97          style={{ color: calledColor, padding: '9px 0' }}
98        />
99      </Tooltip>
100    );
101  }
102
103  return null;
104}
105
106export default function SetView({
107  entrant1Names,
108  entrant1Score,
109  entrant1Win,
110  entrant2Names,
111  entrant2Score,
112  fullRoundText,
113  showScores,
114  state,
115  stream,
116  station,
117  wasReported,
118  updatedAtMs,
119}: {
120  entrant1Names: NameWithHighlight[];
121  entrant1Score: number | null;
122  entrant1Win: boolean;
123  entrant2Names: NameWithHighlight[];
124  entrant2Score: number | null;
125  fullRoundText: string;
126  showScores: boolean;
127  state: number;
128  stream: Stream | null;
129  station: Station | null;
130  wasReported: boolean;
131  updatedAtMs: number;
132}) {
133  let leftScore = '\u00A0';
134  let rightScore = '\u00A0';
135  if (showScores) {
136    if (entrant1Score || entrant2Score) {
137      leftScore = entrant1Score ? entrant1Score.toString(10) : '0';
138      rightScore = entrant2Score ? entrant2Score.toString(10) : '0';
139    } else if (entrant1Win) {
140      leftScore = 'W';
141      rightScore = 'L';
142    } else {
143      leftScore = 'L';
144      rightScore = 'W';
145    }
146  }
147
148  let streamVerb = 'Queued';
149  if (state === State.STARTED) {
150    streamVerb = 'Streaming';
151  } else if (state === State.COMPLETED) {
152    streamVerb = 'Streamed';
153  }
154
155  let stationVerb = 'Queued';
156  if (state === State.STARTED) {
157    stationVerb = 'Playing';
158  } else if (state === State.COMPLETED) {
159    stationVerb = 'Played';
160  }
161
162  return (
163    <Stack direction="row" width="100%">
164      <Stack alignItems="center" direction="row" width="20px">
165        {wasReported && (
166          <Tooltip arrow title="Reported">
167            <CheckBox />
168          </Tooltip>
169        )}
170        {!wasReported && stream && (
171          <Tooltip arrow title={`${streamVerb} on ${stream.path}`}>
172            <Tv fontSize="small" />
173          </Tooltip>
174        )}
175        {!wasReported && !stream && station && (
176          <Tooltip arrow title={`${stationVerb} at station ${station.number}`}>
177            <Typography variant="body1" textAlign="center" width="20px">
178              {station.number}
179            </Typography>
180          </Tooltip>
181        )}
182      </Stack>
183      <Stack width="calc(100% - 40px)">
184        <Box sx={{ typography: 'caption' }} textAlign="center">
185          {fullRoundText}
186        </Box>
187        <Stack alignItems="center" direction="row" sx={{ typography: 'body2' }}>
188          <EntrantSection borderRight={1} direction="row-reverse">
189            <EntrantScore textAlign="right">{leftScore}</EntrantScore>
190            <EntrantNames textAlign="right">
191              <EntrantName entrantName={entrant1Names[0]} />
192              {entrant1Names.length > 1 && (
193                <EntrantName entrantName={entrant1Names[1]} />
194              )}
195            </EntrantNames>
196          </EntrantSection>
197          <EntrantSection borderLeft={1} direction="row">
198            <EntrantScore>{rightScore}</EntrantScore>
199            <EntrantNames>
200              <EntrantName entrantName={entrant2Names[0]} />
201              {entrant2Names.length > 1 && (
202                <EntrantName entrantName={entrant2Names[1]} />
203              )}
204            </EntrantNames>
205          </EntrantSection>
206        </Stack>
207      </Stack>
208      <Stack
209        alignItems="center"
210        direction="row"
211        justifyContent="flex-end"
212        width="20px"
213      >
214        {(state === State.CALLED || state === State.STARTED) && (
215          <CallTimer updatedAtMs={updatedAtMs} matchState={state} />
216        )}
217        {state === State.COMPLETED && (
218          <Tooltip arrow title="Completed">
219            <Backup fontSize="small" />
220          </Tooltip>
221        )}
222      </Stack>
223    </Stack>
224  );
225}