fork/replay-manager-for-slippi

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

NicoletMerge pull request #156 from parry-gg/parrygg43048f4

2.5.1
6.9 KiB298 linesraw
1import {
2  Chip,
3  ThemeOptions,
4  ThemeProvider,
5  Tooltip,
6  createTheme,
7} from '@mui/material';
8import { CSSProperties, DragEvent, ReactElement } from 'react';
9import { Id, NameWithHighlight, PlayerOverrides } from '../common/types';
10import { highlightColor } from '../common/constants';
11
12function parseId(id: string): Id {
13  try {
14    const parsed = Number(id);
15    return Number.isInteger(parsed) ? parsed : id;
16  } catch {
17    return id;
18  }
19}
20
21function Name({ nameWithHighlight }: { nameWithHighlight: NameWithHighlight }) {
22  if (!nameWithHighlight.highlight) {
23    return nameWithHighlight.name;
24  }
25
26  return (
27    <>
28      <span>
29        {nameWithHighlight.name.substring(0, nameWithHighlight.highlight.start)}
30      </span>
31      <span style={{ backgroundColor: highlightColor }}>
32        {nameWithHighlight.name.substring(
33          nameWithHighlight.highlight.start,
34          nameWithHighlight.highlight.end,
35        )}
36      </span>
37      <span>
38        {nameWithHighlight.name.substring(nameWithHighlight.highlight.end)}
39      </span>
40    </>
41  );
42}
43
44export function DraggableChip({
45  entrantId,
46  nameWithHighlight,
47  participantId,
48  prefix,
49  pronouns,
50  selectedChipData,
51  setSelectedChipData,
52  elevate,
53}: {
54  entrantId: Id;
55  nameWithHighlight: NameWithHighlight;
56  participantId: Id;
57  prefix: string;
58  pronouns: string;
59  selectedChipData: PlayerOverrides;
60  setSelectedChipData: ({
61    displayName,
62    entrantId,
63    participantId,
64    prefix,
65    pronouns,
66  }: {
67    displayName: string;
68    entrantId: Id;
69    participantId: Id;
70    prefix: string;
71    pronouns: string;
72  }) => void;
73  elevate?: boolean;
74}) {
75  const dragStart = (event: DragEvent<HTMLDivElement>) => {
76    event.dataTransfer.setData(
77      'text/plain',
78      JSON.stringify({
79        displayName: event.currentTarget.dataset.displayName,
80        entrantId: event.currentTarget.dataset.entrantId,
81        participantId: event.currentTarget.dataset.participantId,
82        prefix: event.currentTarget.dataset.prefix,
83        pronouns: event.currentTarget.dataset.pronouns,
84      }),
85    );
86  };
87  const selected =
88    selectedChipData.entrantId === entrantId &&
89    selectedChipData.participantId === participantId;
90
91  return (
92    <ThemeProvider
93      theme={createTheme({
94        components: {
95          MuiButtonBase: {
96            defaultProps: {
97              disableRipple: true,
98            },
99          },
100        },
101        palette: {
102          primary: {
103            contrastText: '#FFF',
104            light: '#5BCEFA',
105            main: '#5BCEFA',
106            dark: '#5BCEFA',
107          },
108          action: {
109            hover: elevate ? 'rgba(255, 255, 255, 0.9)' : 'rgba(0, 0, 0, 0.04)',
110          },
111        },
112      })}
113    >
114      <Chip
115        color={selected ? 'primary' : undefined}
116        data-display-name={nameWithHighlight.name}
117        data-entrant-id={entrantId.toString(10)}
118        data-participant-id={participantId.toString(10)}
119        data-prefix={prefix}
120        data-pronouns={pronouns}
121        draggable
122        onClick={() => {
123          if (selected) {
124            setSelectedChipData({
125              displayName: '',
126              entrantId: 0,
127              participantId: 0,
128              prefix: '',
129              pronouns: '',
130            });
131          } else {
132            setSelectedChipData({
133              displayName: nameWithHighlight.name,
134              entrantId,
135              participantId,
136              prefix,
137              pronouns,
138            });
139          }
140        }}
141        onDragStart={dragStart}
142        label={<Name nameWithHighlight={nameWithHighlight} />}
143        sx={{
144          backgroundColor: !selected && elevate ? 'white' : undefined,
145          zIndex: (theme) => {
146            if (selected) {
147              return theme.zIndex.drawer + 4;
148            }
149            if (elevate) {
150              return theme.zIndex.drawer + 2;
151            }
152            return undefined;
153          },
154        }}
155        variant={selected ? 'filled' : 'outlined'}
156      />
157    </ThemeProvider>
158  );
159}
160
161DraggableChip.defaultProps = {
162  elevate: false,
163};
164
165export function DroppableChip({
166  active,
167  avatar,
168  label,
169  outlined,
170  selectedChipData,
171  style,
172  onClickOrDrop,
173  elevate,
174}: {
175  active: boolean;
176  avatar?: ReactElement | undefined;
177  label: string;
178  outlined: boolean;
179  selectedChipData: PlayerOverrides;
180  style: CSSProperties;
181  onClickOrDrop: (
182    displayName: string,
183    entrantId: Id,
184    participantId: Id,
185    prefix: string,
186    pronouns: string,
187  ) => void;
188  elevate?: boolean;
189}) {
190  const drop = (event: DragEvent<HTMLDivElement>) => {
191    const json = JSON.parse(event.dataTransfer.getData('text/plain'));
192    onClickOrDrop(
193      json.displayName,
194      parseId(json.entrantId),
195      parseId(json.participantId),
196      json.prefix,
197      json.pronouns,
198    );
199  };
200
201  const dragEnterOver = (event: DragEvent<HTMLDivElement>) => {
202    if (active) {
203      event.preventDefault();
204    }
205  };
206
207  const selectedChip = Boolean(
208    selectedChipData.entrantId && selectedChipData.participantId,
209  );
210
211  const themeOptions: ThemeOptions = {
212    palette: {
213      secondary: {
214        contrastText: '#FFF',
215        light: '#F5A9B8',
216        main: '#F5A9B8',
217        dark: '#F5A9B8',
218      },
219    },
220  };
221  if (avatar) {
222    themeOptions.components = {
223      MuiChip: {
224        styleOverrides: {
225          label: {
226            alignItems: 'center',
227            display: 'flex',
228            flexGrow: 1,
229          },
230        },
231      },
232    };
233  }
234
235  const chip = (
236    <Chip
237      avatar={avatar}
238      color={active && selectedChip ? 'secondary' : undefined}
239      onClick={
240        active && selectedChip
241          ? (event) => {
242              onClickOrDrop(
243                selectedChipData.displayName,
244                selectedChipData.entrantId,
245                selectedChipData.participantId,
246                selectedChipData.prefix,
247                selectedChipData.pronouns,
248              );
249              event.stopPropagation();
250            }
251          : undefined
252      }
253      onDrop={drop}
254      onDragEnter={dragEnterOver}
255      onDragOver={dragEnterOver}
256      label={label}
257      style={style}
258      sx={{
259        backgroundColor:
260          active && elevate && !selectedChip ? 'white' : undefined,
261        zIndex: (theme) => {
262          if (active && selectedChip) {
263            return theme.zIndex.drawer + 4;
264          }
265          if (active && elevate) {
266            return theme.zIndex.drawer + 2;
267          }
268          return undefined;
269        },
270      }}
271      variant={outlined && !selectedChip ? 'outlined' : 'filled'}
272    />
273  );
274
275  return (
276    <ThemeProvider theme={createTheme(themeOptions)}>
277      {active ? (
278        <Tooltip
279          arrow
280          title={
281            selectedChip
282              ? `Click to assign ${selectedChipData.displayName}`
283              : 'Drop here!'
284          }
285        >
286          {chip}
287        </Tooltip>
288      ) : (
289        chip
290      )}
291    </ThemeProvider>
292  );
293}
294
295DroppableChip.defaultProps = {
296  avatar: undefined,
297  elevate: false,
298};