-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.tsx
More file actions
273 lines (246 loc) · 9.22 KB
/
search.tsx
File metadata and controls
273 lines (246 loc) · 9.22 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"use client";
import React, { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import { Button } from "./ui/button";
import { Input } from "./ui/input";
import { Dialog, DialogContent, DialogTitle } from "./ui/dialog";
import type { SearchResult } from "./search-index";
import { AIChat, AIChatOption } from "./ai-chat";
import { ReactDocsConfig } from "./config";
import { useDocsColors } from "./theme-context";
// Singleton to cache search index
class SearchIndexCache {
private static instances: Map<string, SearchIndexCache> = new Map();
private searchIndex: SearchResult[] | null = null;
private loading = false;
private callbacks: ((index: SearchResult[]) => void)[] = [];
constructor(private config: ReactDocsConfig) { }
static getInstance(config: ReactDocsConfig): SearchIndexCache {
const key = config.searchApiPath;
if (!SearchIndexCache.instances.has(key)) {
SearchIndexCache.instances.set(key, new SearchIndexCache(config));
}
return SearchIndexCache.instances.get(key)!;
}
async getSearchIndex(): Promise<SearchResult[]> {
if (this.searchIndex) {
return this.searchIndex;
}
if (this.loading) {
return new Promise((resolve) => {
this.callbacks.push(resolve);
});
}
this.loading = true;
try {
const response = await fetch(this.config.searchApiPath);
const data = await response.json();
this.searchIndex = data;
this.callbacks.forEach(callback => callback(data));
this.callbacks = [];
return data;
} finally {
this.loading = false;
}
}
}
interface SearchResultWithScore extends SearchResult {
score: number;
snippet: string;
}
function highlightText(text: string, query: string) {
if (!query) return text;
const parts = text.split(new RegExp(`(${query})`, "gi"));
return parts.map((part, i) =>
part.toLowerCase() === query.toLowerCase() ?
<mark key={i} className="bg-yellow-500/20 text-yellow-200 rounded px-0.5">{part}</mark> :
part
);
}
export interface SearchProps {
config: ReactDocsConfig;
}
export function Search({ config }: SearchProps) {
const [open, setOpen] = useState(false);
const [isMac, setIsMac] = useState(false);
const [query, setQuery] = useState("");
const [results, setResults] = useState<SearchResultWithScore[]>([]);
const [searchIndex, setSearchIndex] = useState<SearchResult[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [showAIChat, setShowAIChat] = useState(false);
const router = useRouter();
const colors = useDocsColors();
useEffect(() => {
// Detect if user is on macOS
setIsMac(navigator.platform.toUpperCase().indexOf('MAC') >= 0);
// Load search index using cache
const cache = SearchIndexCache.getInstance(config);
cache.getSearchIndex().then((data) => setSearchIndex(data));
const down = (e: KeyboardEvent) => {
if (e.key === "k" && (e.metaKey || e.ctrlKey)) {
e.preventDefault();
setOpen(true);
}
if (e.key === "Escape") {
setOpen(false);
}
};
document.addEventListener("keydown", down);
return () => document.removeEventListener("keydown", down);
}, [config]);
useEffect(() => {
const search = async () => {
setIsLoading(true);
// Only search if query has 2 or more characters
if (!query.trim() || query.length < 2) {
setResults([]);
setIsLoading(false);
return;
}
const searchQuery = query.toLowerCase();
const filtered = searchIndex
.map(item => {
const titleMatch = item.title.toLowerCase().includes(searchQuery);
const contentMatch = item.content.toLowerCase().includes(searchQuery);
if (!titleMatch && !contentMatch) return null;
// Find the best content snippet
let snippet = item.content;
if (contentMatch) {
const index = item.content.toLowerCase().indexOf(searchQuery);
const start = Math.max(0, index - 100);
const end = Math.min(item.content.length, index + 100);
snippet = (start > 0 ? "..." : "") +
item.content.slice(start, end) +
(end < item.content.length ? "..." : "");
}
return {
...item,
snippet,
score: titleMatch ? 2 : 1,
};
})
.filter((item): item is SearchResultWithScore => item !== null)
.sort((a, b) => b.score - a.score);
setResults(filtered);
setIsLoading(false);
};
const debounce = setTimeout(search, 200);
return () => clearTimeout(debounce);
}, [query, searchIndex]);
const handleSelect = (result: SearchResultWithScore) => {
router.push(result.url);
setOpen(false);
};
const handleAIChat = () => {
setShowAIChat(true);
};
return (
<div className="relative w-full">
<Button
variant="outline"
className="relative w-full justify-start text-sm text-muted-foreground bg-gray-800/50 hover:bg-gray-800 border-0 hover:text-gray-100"
style={{
'--ring-color': colors.primary,
} as React.CSSProperties}
onClick={() => setOpen(true)}
>
<span className="inline-flex items-center">
<svg
className="mr-2 h-4 w-4"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
/>
</svg>
Search documentation or ask AI...
</span>
<kbd className="pointer-events-none absolute right-2 top-[50%] translate-y-[-50%] inline-flex h-5 select-none items-center gap-1 rounded border border-gray-700 bg-gray-800 px-1.5 font-mono text-[10px] font-medium text-gray-400">
{isMac ? (
<span className="text-xs">⌘</span>
) : (
<span className="text-xs">Ctrl</span>
)}
+ K
</kbd>
</Button>
<Dialog open={open} onOpenChange={(isOpen) => {
setOpen(isOpen);
if (!isOpen) {
setShowAIChat(false);
setQuery("");
}
}}>
<DialogContent
className="fixed top-20 left-1/2 -translate-x-1/2 w-full max-w-2xl shadow-2xl backdrop-blur-sm bg-gray-900/95 border border-gray-800 rounded-lg overflow-hidden"
style={{ maxHeight: 'calc(100vh - 6rem)' }}
>
<DialogTitle className="absolute w-px h-px p-0 -m-px overflow-hidden whitespace-nowrap border-0" style={{ clip: 'rect(0, 0, 0, 0)' }}>
Search Documentation
</DialogTitle>
{!showAIChat ? (
<div className="flex flex-col" style={{ maxHeight: 'calc(100vh - 6rem)' }}>
<div className="shrink-0 p-4 border-b border-gray-800">
<Input
placeholder="Search documentation or ask AI..."
className="w-full bg-gray-800/50 border-gray-700"
value={query}
onChange={(e) => setQuery(e.target.value)}
autoFocus
/>
</div>
<div className="min-h-0 overflow-y-auto">
<div className="p-4 space-y-4">
{isLoading ? (
<div className="text-sm text-gray-400">Searching...</div>
) : query.length < 2 ? (
<div className="text-sm text-gray-400">Enter at least 2 characters to search...</div>
) : (
<>
{query.length >= 2 && <AIChatOption query={query} onClick={handleAIChat} />}
{results.length > 0 ? (
<div className="space-y-4">
{results.map((result) => (
<button
key={result.url}
className="block w-full text-left p-4 rounded-lg hover:bg-gray-800/50 transition-colors"
onClick={() => handleSelect(result)}
>
<h3 className="text-sm font-medium text-white mb-1">
{highlightText(result.title, query)}
</h3>
<p className="text-sm text-gray-400 line-clamp-2">
{highlightText(result.snippet, query)}
</p>
</button>
))}
</div>
) : (
<div className="text-sm text-gray-400">No documentation results found</div>
)}
</>
)}
</div>
</div>
</div>
) : (
<div className="overflow-y-auto" style={{ maxHeight: 'calc(100vh - 6rem)' }}>
<div className="p-4">
<AIChat
query={query}
onBack={() => setShowAIChat(false)}
config={config}
/>
</div>
</div>
)}
</DialogContent>
</Dialog>
</div>
);
}