-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNavbar.tsx
120 lines (112 loc) · 3.27 KB
/
Navbar.tsx
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
// import Bookmarks from '../icons/Bookmarks';
import Filter from '../icons/Filter';
import Left from '../icons/Left';
import House from '../icons/House';
import { Link, useLocation } from '@tanstack/react-location';
import { useEffect, useState } from 'react';
import { useSearch } from '../context/searchContext';
import FilterModal from './FilterModal';
import Search from '../icons/Search';
import Clear from '../icons/Clear';
import { detectColorScheme, switchTheme } from '../util/colorScheme';
import ColorSchemeToggle from './ColorSchemeToggle';
export default function Navbar(): JSX.Element {
const location = useLocation();
const [from, setFrom] = useState<'home' | 'list'>();
const { search, setSearch, filter, setFilter } = useSearch();
const [lastListView, setLastListView] = useState<string>();
const [filterOpen, setFilterOpen] = useState<boolean>(false);
useEffect(() => {
const { pathname } = location.current;
if (pathname.match(/^\/s\/\d+/)) {
switch (location.current.search.from) {
case 'home':
setFrom('home');
location.navigate({ ...location.current, search: {}, searchStr: '' }, true);
break;
case 'list':
setFrom('list');
location.navigate({ ...location.current, search: {}, searchStr: '' }, true);
break;
default: {
setFrom(undefined);
}
}
} else {
setFrom(undefined);
}
if (pathname !== lastListView && (pathname === '/' || pathname.match(/^\/l\/.+/))) {
setLastListView(pathname);
if (lastListView) setSearch('');
window.scrollTo(0, 0);
}
}, [location.current.pathname]);
useEffect(() => {
if (location.current.pathname === lastListView) return;
if (!lastListView && (!search || !filter.length)) return;
if (from === 'list') return location.history.back();
location.history.push(lastListView || '/');
}, [search, filter]);
return (
<>
<nav>
<div className="menu">
<div className="flex-row items-center">
<Link to="/" disabled={location.current.pathname === '/' || from === 'home'}>
<button
disabled={location.current.pathname === '/'}
onClick={from === 'home' ? location.history.back : undefined}
>
<House />
</button>
</Link>
{from === 'list' && (
<button onClick={location.history.back}>
<Left />
</button>
)}
<h1>Songbook</h1>
</div>
<div className="flex-row">
<ColorSchemeToggle />
<button onClick={() => setFilterOpen(true)}>
<Filter />
</button>
{/* <Link to="/bookmarks">
<button>
<Bookmarks />
</button>
</Link> */}
</div>
</div>
<div className="search">
<button onClick={() => document.getElementById('searchbar')?.focus()}>
<Search size="sm" />
</button>
<input
id="searchbar"
value={search}
onChange={(e) => setSearch(e.currentTarget.value)}
placeholder="Search for title or text"
/>
{!!search && (
<button
onClick={() => {
setSearch('');
document.getElementById('searchbar')?.focus();
}}
>
<Clear size="sm" />
</button>
)}
</div>
</nav>
<FilterModal
isOpen={filterOpen}
onClose={() => setFilterOpen(false)}
onConfirm={setFilter}
startValue={filter}
/>
</>
);
}