fork/replay-manager-for-slippi

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

Nicoletnormalize tooltip arrow, a couple more placements1b12ff3

2.3.7
2.6 KiB102 linesraw
1import { Clear } from '@mui/icons-material';
2import {
3  Box,
4  IconButton,
5  InputAdornment,
6  TextField,
7  Tooltip,
8} from '@mui/material';
9import { useCallback, useEffect, useRef, useState } from 'react';
10import { Mode } from '../common/types';
11import { setWindowEventListener, WindowEvent } from './setWindowEventListener';
12
13export default function SearchBox({
14  mode,
15  searchSubstr,
16  setSearchSubstr,
17  vlerkMode,
18}: {
19  mode: Mode;
20  searchSubstr: string;
21  setSearchSubstr: (searchSubstr: string) => void;
22  vlerkMode: boolean;
23}) {
24  const searchInputRef = useRef<HTMLInputElement>();
25  const [showSearch, setShowSearch] = useState(false);
26
27  const clearSearch = useCallback(() => {
28    setSearchSubstr('');
29    setShowSearch(false);
30  }, [setSearchSubstr]);
31
32  const ctrlF = useCallback(() => {
33    setShowSearch(true);
34    searchInputRef.current?.select();
35  }, []);
36
37  useEffect(() => {
38    setWindowEventListener(WindowEvent.ESCAPE, clearSearch);
39  }, [clearSearch]);
40
41  useEffect(() => {
42    setWindowEventListener(WindowEvent.CTRLF, ctrlF);
43  }, [ctrlF]);
44
45  return (
46    (showSearch || (vlerkMode && mode === Mode.STARTGG)) && (
47      <>
48        <Box
49          style={{
50            background: '#F5A9B8',
51            padding: '8px',
52            position: 'fixed',
53            width: '300px',
54            right: 0,
55          }}
56          sx={{
57            zIndex: (theme) => theme.zIndex.drawer + 5,
58          }}
59        >
60          <TextField
61            autoFocus
62            fullWidth
63            label="Search"
64            onChange={(event) => {
65              setSearchSubstr(event.target.value);
66            }}
67            onKeyDown={(event) => {
68              if (event.key === 'Escape') {
69                clearSearch();
70              } else if (
71                ((window.electron.isMac && event.metaKey) ||
72                  (!window.electron.isMac && event.ctrlKey)) &&
73                event.key === 'f'
74              ) {
75                searchInputRef.current?.select();
76              }
77            }}
78            inputRef={searchInputRef}
79            size="small"
80            value={searchSubstr}
81            InputProps={{
82              endAdornment: (
83                <InputAdornment position="end">
84                  <Tooltip arrow title="Clear search">
85                    <IconButton
86                      onClick={() => {
87                        clearSearch();
88                      }}
89                    >
90                      <Clear />
91                    </IconButton>
92                  </Tooltip>
93                </InputAdornment>
94              ),
95            }}
96          />
97        </Box>
98        <Box marginTop="56px" />
99      </>
100    )
101  );
102}