-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathDocSearch.tsx
More file actions
289 lines (251 loc) · 8.69 KB
/
DocSearch.tsx
File metadata and controls
289 lines (251 loc) · 8.69 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import type { JSX } from 'react';
import React from 'react';
import { useDocSearchKeyboardEvents } from './useDocSearchKeyboardEvents';
import { useIsMobile } from './useIsMobile';
import { useKeyboardShortcuts } from './useKeyboardShortcuts';
import type { KeyboardShortcuts } from './useKeyboardShortcuts.ts';
import type { DocSearchTheme } from './useTheme';
import { useTheme } from './useTheme';
export type DocSearchState = 'modal-askai' | 'modal-search' | 'ready' | 'sidepanel';
export type View = 'modal' | 'sidepanel' | (Record<string, unknown> & string);
export type InitialAskAiMessage = {
query: string;
messageId?: string;
suggestedQuestionId?: string;
};
export type OnAskAiToggle = (active: boolean, initialMessage?: InitialAskAiMessage) => void;
/**
* Imperative handle exposed by the DocSearch provider for programmatic control.
*/
export interface DocSearchRef {
/** Opens the search modal. */
open: () => void;
/** Closes the search modal. */
close: () => void;
/** Opens Ask AI mode (sidepanel if available, otherwise modal). */
openAskAi: (initialMessage?: InitialAskAiMessage) => void;
/** Opens the sidepanel directly (no-op if sidepanel view not registered). */
openSidepanel: (initialMessage?: InitialAskAiMessage) => void;
/** Returns true once the component is mounted and ready. */
readonly isReady: boolean;
/** Returns true if the modal is currently open. */
readonly isOpen: boolean;
/** Returns true if the sidepanel is currently open. */
readonly isSidepanelOpen: boolean;
/** Returns true if sidepanel view is registered (hybrid mode). */
readonly isSidepanelSupported: boolean;
}
export interface DocSearchContext {
docsearchState: DocSearchState;
setDocsearchState: (newState: DocSearchState) => void;
searchButtonRef: React.RefObject<HTMLButtonElement | null>;
initialQuery: string;
keyboardShortcuts: Required<KeyboardShortcuts>;
openModal: () => void;
closeModal: () => void;
isAskAiActive: boolean;
isModalActive: boolean;
onAskAiToggle: OnAskAiToggle;
initialAskAiMessage: InitialAskAiMessage | undefined;
clearInitialAskAiMessage: () => void;
registerView: (view: View) => void;
isHybridModeSupported: boolean;
}
/**
* Lifecycle callbacks for DocSearch.
*/
export interface DocSearchCallbacks {
/** Called once DocSearch is mounted and ready for interaction. */
onReady?: () => void;
/** Called when the modal opens. */
onOpen?: () => void;
/** Called when the modal closes. */
onClose?: () => void;
/** Called when the sidepanel opens. */
onSidepanelOpen?: () => void;
/** Called when the sidepanel closes. */
onSidepanelClose?: () => void;
}
export interface DocSearchProps extends DocSearchCallbacks {
children: Array<JSX.Element | null> | JSX.Element | React.ReactNode | null;
theme?: DocSearchTheme;
initialQuery?: string;
keyboardShortcuts?: KeyboardShortcuts;
}
const Context = React.createContext<DocSearchContext | undefined>(undefined);
Context.displayName = 'DocSearchContext';
function DocSearchInner(
{ children, theme, onReady, onOpen, onClose, onSidepanelOpen, onSidepanelClose, ...props }: DocSearchProps,
ref: React.ForwardedRef<DocSearchRef>,
): JSX.Element {
const [docsearchState, setDocsearchState] = React.useState<DocSearchState>('ready');
const [initialQuery, setInitialQuery] = React.useState<string>(props.initialQuery || '');
const searchButtonRef = React.useRef<HTMLButtonElement>(null);
const keyboardShortcuts = useKeyboardShortcuts(props.keyboardShortcuts);
const [initialAskAiMessage, setInitialAskAiMessage] = React.useState<InitialAskAiMessage>();
const [registeredViews, setRegisteredViews] = React.useState(() => new Set<View>());
const isMobile = useIsMobile();
const prevStateRef = React.useRef<DocSearchState>('ready');
const isModalActive = ['modal-search', 'modal-askai'].includes(docsearchState);
const isAskAiActive = docsearchState === 'modal-askai';
const isHybridModeSupported = !isMobile && registeredViews.has('sidepanel');
const isSidepanelOpen = docsearchState === 'sidepanel';
// Call onReady on mount
React.useEffect(() => {
onReady?.();
}, [onReady]);
// Track state changes for lifecycle callbacks
React.useEffect(() => {
const prevState = prevStateRef.current;
const currentState = docsearchState;
// Modal opened
if (
(currentState === 'modal-search' || currentState === 'modal-askai') &&
prevState !== 'modal-search' &&
prevState !== 'modal-askai'
) {
onOpen?.();
}
// Modal closed
if (currentState === 'ready' && (prevState === 'modal-search' || prevState === 'modal-askai')) {
onClose?.();
}
// Sidepanel opened
if (currentState === 'sidepanel' && prevState !== 'sidepanel') {
onSidepanelOpen?.();
}
// Sidepanel closed
if (currentState !== 'sidepanel' && prevState === 'sidepanel') {
onSidepanelClose?.();
}
prevStateRef.current = currentState;
}, [docsearchState, onOpen, onClose, onSidepanelOpen, onSidepanelClose]);
const openModal = React.useCallback((): void => {
setDocsearchState('modal-search');
}, []);
const closeModal = React.useCallback((): void => {
setDocsearchState('ready');
// Refocus the Modal trigger on close
searchButtonRef.current?.focus();
setInitialQuery(props.initialQuery ?? '');
}, [setDocsearchState, props.initialQuery]);
const onAskAiToggle: OnAskAiToggle = React.useCallback(
(active, initialMessage) => {
// Don't use hybrid mode on mobile
if (!isMobile && active && isHybridModeSupported) {
setInitialAskAiMessage(initialMessage);
setDocsearchState('sidepanel');
return;
}
setDocsearchState(active ? 'modal-askai' : 'modal-search');
},
[setDocsearchState, isMobile, isHybridModeSupported],
);
const openSidepanel = React.useCallback(
(initialMessage?: InitialAskAiMessage): void => {
// Guard: no-op if sidepanel view hasn't been registered
if (!registeredViews.has('sidepanel')) return;
setInitialAskAiMessage(initialMessage);
setDocsearchState('sidepanel');
},
[setDocsearchState, registeredViews],
);
const onInput = React.useCallback(
(event: KeyboardEvent): void => {
setDocsearchState('modal-search');
setInitialQuery(event.key);
},
[setDocsearchState, setInitialQuery],
);
const clearInitialAskAiMessage = React.useCallback((): void => {
setInitialAskAiMessage(undefined);
}, []);
const registerView = React.useCallback(
(view: View): void => {
if (registeredViews.has(view)) return;
setRegisteredViews((prev) => {
const newViews = new Set(prev);
newViews.add(view);
return newViews;
});
},
[registeredViews],
);
// Expose imperative handle for programmatic control
React.useImperativeHandle(
ref,
() => ({
open: openModal,
close: closeModal,
openAskAi: (initialMessage?: InitialAskAiMessage): void => onAskAiToggle(true, initialMessage),
openSidepanel,
get isReady(): boolean {
return true;
},
get isOpen(): boolean {
return isModalActive;
},
get isSidepanelOpen(): boolean {
return isSidepanelOpen;
},
get isSidepanelSupported(): boolean {
return isHybridModeSupported;
},
}),
[openModal, closeModal, onAskAiToggle, openSidepanel, isModalActive, isSidepanelOpen, isHybridModeSupported],
);
useTheme({ theme });
useDocSearchKeyboardEvents({
isOpen: isModalActive,
onOpen: openModal,
onClose: closeModal,
onAskAiToggle,
onInput,
isAskAiActive,
searchButtonRef,
keyboardShortcuts,
});
const value: DocSearchContext = React.useMemo(
() => ({
docsearchState,
setDocsearchState,
searchButtonRef,
initialQuery,
keyboardShortcuts,
openModal,
closeModal,
isAskAiActive,
isModalActive,
onAskAiToggle,
initialAskAiMessage,
clearInitialAskAiMessage,
registerView,
isHybridModeSupported,
}),
[
docsearchState,
searchButtonRef,
initialQuery,
keyboardShortcuts,
openModal,
closeModal,
isAskAiActive,
isModalActive,
onAskAiToggle,
initialAskAiMessage,
clearInitialAskAiMessage,
registerView,
isHybridModeSupported,
],
);
return <Context.Provider value={value}>{children}</Context.Provider>;
}
export const DocSearch = React.forwardRef(DocSearchInner);
DocSearch.displayName = 'DocSearch';
export function useDocSearch(): DocSearchContext {
const ctx = React.useContext(Context);
if (ctx === undefined) {
throw new Error('`useDocSearch` must be used within the `DocSearch` provider');
}
return ctx;
}