Repository navigation
Expand file tree
/
Copy pathNavSearch.tsx
More file actions
233 lines (217 loc) · 7.79 KB
/
Copy pathNavSearch.tsx
File metadata and controls
233 lines (217 loc) · 7.79 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'use client'
import { useOperatingSystem } from '@/src/hooks/useOperatingSystem'
import * as VisuallyHidden from '@radix-ui/react-visually-hidden'
import cn from 'classnames'
import { Command } from 'cmdk'
import { debounce } from 'lodash'
import { FileText, LoaderCircle, Search, X } from 'lucide-react'
import { useRouter } from 'next/navigation'
import { useCallback, useEffect, useRef, useState } from 'react'
import { Button } from '../ui/button'
import { Dialog, DialogContent, DialogDescription, DialogTitle, DialogTrigger } from '../ui/dialog'
import { buttonClass } from './Nav'
type PagefindResult = {
url: string
meta?: { title?: string }
excerpt?: string
}
let pagefindInstance: any = null
async function getPagefind() {
if (pagefindInstance) return pagefindInstance
try {
// @ts-expect-error — pagefind is generated at build time, not a real module
pagefindInstance = await import(/* webpackIgnore: true */ '/pagefind/pagefind.js')
await pagefindInstance.init()
return pagefindInstance
} catch {
console.warn('Pagefind not available — search disabled')
return null
}
}
export default function NavSearch() {
const router = useRouter()
const inputRef = useRef<HTMLInputElement>(null)
const [isOpen, setIsOpen] = useState(false)
const [query, setQuery] = useState('')
const [results, setResults] = useState<PagefindResult[]>([])
const [isLoading, setIsLoading] = useState(false)
const [hasSearched, setHasSearched] = useState(false)
const os = useOperatingSystem()
const doSearch = useCallback(
debounce(async (value: string) => {
if (!value) {
setResults([])
setHasSearched(false)
setIsLoading(false)
return
}
setIsLoading(true)
try {
const pagefind = await getPagefind()
if (!pagefind) {
setResults([])
setHasSearched(true)
setIsLoading(false)
return
}
const search = await pagefind.search(value)
const loaded = await Promise.all(search.results.slice(0, 20).map((r: any) => r.data()))
setResults(loaded)
} catch (err) {
console.error('Search error:', err)
setResults([])
}
setHasSearched(true)
setIsLoading(false)
}, 300),
[]
)
useEffect(() => {
return () => doSearch.cancel()
}, [doSearch])
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
e.preventDefault()
setIsOpen(true)
}
}
document.addEventListener('keydown', handleKeyDown)
return () => document.removeEventListener('keydown', handleKeyDown)
}, [setIsOpen])
const handleItemClick = (url: string) => {
setIsOpen(false)
setQuery('')
setResults([])
setHasSearched(false)
setTimeout(() => {
router.push(url)
}, 50)
}
const handleOpenChange = (open: boolean) => {
setIsOpen(open)
if (!open) {
setQuery('')
setResults([])
setHasSearched(false)
}
}
return (
<Dialog open={isOpen} onOpenChange={handleOpenChange}>
<DialogTrigger asChild>
<button
type="button"
className={cn(
'group flex h-full items-center justify-center rounded-md p-1.5 hover:cursor-pointer focus:outline-none',
buttonClass
)}
onClick={() => setIsOpen(true)}
aria-label={`Search for notes (${os === 'mac' ? '⌘' : 'Ctrl'}+K)`}
>
<Search size={18} />
</button>
</DialogTrigger>
<DialogContent
className="'flex md:min-w-[680px]' max-h-[80vh] max-w-[calc(100%-2rem)] flex-col !p-0 md:max-w-[680px]"
hideCloseBtn={true}
>
<VisuallyHidden.Root>
<DialogTitle>Search notes</DialogTitle>
<DialogDescription>Search through your notes</DialogDescription>
</VisuallyHidden.Root>
<Command
className="divide-border-muted flex h-full min-h-0 flex-1 flex-col divide-y"
shouldFilter={false}
loop
>
<div className="flex h-14 items-center justify-between gap-3 border-none p-3">
<div className={cn('text-muted grid place-items-center')}>
{!isLoading && <Search size={24} />}
{isLoading && (
<div className="animate-spin">
<LoaderCircle size={25} />
</div>
)}
</div>
<Command.Input
ref={inputRef}
className="placeholder:text-muted w-full bg-transparent focus:border-transparent focus:ring-0 focus:outline-none"
placeholder="Search for notes..."
onValueChange={value => {
setQuery(value)
doSearch(value)
}}
/>
<Button
onClick={() => setIsOpen(false)}
variant="ghost"
size="iconBig"
className="group rounded-full"
tooltip="Close search (ESC)"
>
<X className="text-text opacity-50 group-hover:opacity-100" />
</Button>
</div>
{query && !isLoading && hasSearched && (
<Command.List className="thi-scrollbar thi-scrollbar-small max-h-[60vh] overflow-y-auto p-1.5">
<Command.Empty className="text-muted flex h-full w-full items-center justify-center px-4 pt-8 pb-10 text-sm">
No note found!
</Command.Empty>
{results.length > 0 && (
<>
{results.map((item, index) => {
const url = item.url
.replace(/\/server\/app/, '')
.replace(/\.html$/, '')
.replace(/index$/, '')
const title = item.meta?.title || 'Untitled'
return (
<Command.Item
key={index}
value={url}
className="group hover:bg-bg-hover hover:text-text aria-selected:bg-bg-hover aria-selected:text-text mb-1 flex cursor-pointer items-start gap-3 rounded-lg px-3 py-2 text-sm outline-none"
onSelect={() => handleItemClick(url)}
onClick={() => handleItemClick(url)}
>
<div>
<FileText size={20} className="opacity-80" />
</div>
<div className={cn('text-text flex w-full flex-col gap-1')}>
<div
className={cn(
{
'border-border-muted border-b border-dashed pr-4 pb-1': item.excerpt
},
'text-text text-[0.9rem] font-medium'
)}
>
<span>{title}</span>
</div>
{item.excerpt && (
<div
className="text-muted text-sm opacity-90"
dangerouslySetInnerHTML={{ __html: item.excerpt }}
></div>
)}
</div>
</Command.Item>
)
})}
</>
)}
</Command.List>
)}
{query && hasSearched && results.length > 0 && (
<div className="text-muted p-3 pl-4 text-xs font-normal">
Found{' '}
<span className="font-semibold text-green-800 dark:text-green-300">
{results.length}
</span>{' '}
results
</div>
)}
</Command>
</DialogContent>
</Dialog>
)
}