-
-
Notifications
You must be signed in to change notification settings - Fork 535
Expand file tree
/
Copy pathSidebar.tsx
More file actions
169 lines (161 loc) · 4.72 KB
/
Copy pathSidebar.tsx
File metadata and controls
169 lines (161 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { useMemo, useState } from 'react';
import {
Drawer,
IconButton,
InputAdornment,
List,
TextField,
Typography,
useMediaQuery,
} from '@mui/material';
import SearchIcon from '@mui/icons-material/Search';
import CloseIcon from '@mui/icons-material/Close';
import { SideBarItems } from './SidebarItems';
import { RouteItem, routes } from './routes';
import { matchSorter } from 'match-sorter';
import { usePlausible } from 'next-plausible';
interface Props {
navOpen: boolean;
setNavOpen: (navOpen: boolean) => void;
}
export const SideBar = ({ navOpen, setNavOpen }: Props) => {
const plausible = usePlausible();
const isMobile = useMediaQuery('(max-width: 900px)');
const [search, setSearch] = useState('');
const filteredRoutes = useMemo(() => {
const filterRoutesRecursively = (routes: RouteItem[]): RouteItem[] => {
return routes.reduce((acc: RouteItem[], route) => {
const matchKeys = ['label'];
if (route.keywords) {
matchKeys.push('keywords');
}
const matchesSearch =
matchSorter([route], search, { keys: matchKeys }).length > 0;
if (matchesSearch) {
// If the parent route matches, include all its items and secondary items
acc.push({
...route,
items: route.items,
secondaryItems: route.secondaryItems,
});
} else {
// If parent doesn't match, check children
const filteredItems = route.items
? filterRoutesRecursively(route.items)
: undefined;
const filteredSecondaryItems = route.secondaryItems
? filterRoutesRecursively(route.secondaryItems)
: undefined;
if (filteredItems?.length || filteredSecondaryItems?.length) {
acc.push({
...route,
items: filteredItems,
secondaryItems: filteredSecondaryItems,
});
}
}
return acc;
}, []);
};
return filterRoutesRecursively(routes);
}, [routes, search]);
return (
<Drawer
slotProps={{ paper: { component: 'aside' } }}
open={navOpen}
onClose={() => setNavOpen(false)}
variant={isMobile ? 'temporary' : 'permanent'}
sx={{
zIndex: 4,
position: 'relative',
}}
>
<List
sx={{
overflow: 'visible',
overflowY: navOpen ? 'overlay' : 'hidden',
mt: '52px',
p: 0,
pb: '10rem',
scrollPaddingTop: '20%',
transition: 'all .2s',
width: navOpen ? '300px' : 0,
'@media (max-width: 900px)': {
mt: '50px',
},
}}
>
<TextField
onFocus={() => plausible('page-filter')}
placeholder="Find Page"
variant="outlined"
fullWidth
size="small"
slotProps={{
input: {
startAdornment: (
<InputAdornment
sx={{
opacity: search ? '1' : '0.5',
transition: 'all .2s',
}}
position="start"
>
<SearchIcon />
</InputAdornment>
),
endAdornment: (
<InputAdornment
sx={{
opacity: search ? '1' : '0.5',
transition: 'all .2s',
}}
position="end"
>
<IconButton
aria-label="Clear Search"
disabled={!search}
size="small"
onClick={() => setSearch('')}
>
<CloseIcon />
</IconButton>
</InputAdornment>
),
},
}}
sx={(theme) => ({
backgroundColor: theme.palette.background.paper,
mx: '2px',
my: '0',
p: 0,
position: 'sticky',
top: '1px',
width: 'calc(100% - 4px)',
zIndex: 2,
'&:hover': {
button: {
opacity: '1',
},
},
})}
onChange={(e) => setSearch(e.target.value)}
value={search}
/>
{!filteredRoutes.length ? (
<Typography sx={{ p: 2, textAlign: 'center' }} color="textSecondary">
No results found for "{search}"
</Typography>
) : (
<SideBarItems
expandAll={!!search}
routes={filteredRoutes}
setNavOpen={setNavOpen}
setSearch={setSearch}
search={search}
/>
)}
</List>
</Drawer>
);
};