-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathSidepanel.tsx
More file actions
170 lines (154 loc) · 4.84 KB
/
Sidepanel.tsx
File metadata and controls
170 lines (154 loc) · 4.84 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
import { DocSearch, useDocSearch } from '@docsearch/core';
import type { DocSearchCallbacks, DocSearchRef, DocSearchTheme, SidepanelShortcuts } from '@docsearch/core';
import type { JSX } from 'react';
import React from 'react';
import { createPortal } from 'react-dom';
import type { AgentStudioSearchParameters, AskAiSearchParameters } from './DocSearch';
import type { SidepanelButtonProps, SidepanelProps as SidepanelPanelProps } from './Sidepanel/index';
import { SidepanelButton, Sidepanel } from './Sidepanel/index';
export type { DocSearchRef, DocSearchCallbacks } from '@docsearch/core';
export type SidepanelSearchParameters =
| {
/**
* **Experimental:** Whether to use Agent Studio as the chat backend.
*
* This is an experimental feature and its API may change without notice in future releases.
* Use with caution in production environments.
*
* @default false
*/
agentStudio?: never;
/**
* The search parameters to use for the ask AI feature.
*
* **NOTE**: If using `agentStudio = true`, the `searchParameters` object is
* keyed by the index name.
*/
searchParameters?: AskAiSearchParameters;
}
| {
agentStudio: false;
searchParameters?: AskAiSearchParameters;
}
| {
agentStudio: true;
/**
* The search parameters to use for the ask AI feature.
* Keyed by the index name.
*
* @example
* {
* "INDEX_NAME": { distinct: false }
* }
*/
searchParameters?: AgentStudioSearchParameters;
};
export type DocSearchSidepanelProps = DocSearchCallbacks & {
/**
* The assistant ID to use for the ask AI feature.
*/
assistantId: string;
/**
* Public api key with search permissions for the index.
*/
apiKey: string;
/**
* Algolia application id used by the search client.
*/
appId: string;
/**
* The index name to use for the ask AI feature. Your assistant will search this index for relevant documents.
*/
indexName: string;
/**
* Configuration for keyboard shortcuts. Allows enabling/disabling specific shortcuts.
*
* @default `{ 'Ctrl/Cmd+I': true }`
*/
keyboardShortcuts?: SidepanelShortcuts;
/**
* Theme overrides applied to the Sidepanel button and panel.
*
* @default 'light'
*/
theme?: DocSearchTheme;
/**
* Props specific to the Sidepanel button.
*/
button?: Omit<SidepanelButtonProps, 'keyboardShortcuts'>;
/**
* Props specific to the Sidepanel panel.
*/
panel?: Omit<SidepanelPanelProps, 'keyboardShortcuts'>;
};
type SidepanelProps = DocSearchSidepanelProps & SidepanelSearchParameters;
function DocSearchSidepanelComponent(
{ keyboardShortcuts, theme, onReady, onOpen, onClose, onSidepanelOpen, onSidepanelClose, ...props }: SidepanelProps,
ref: React.ForwardedRef<DocSearchRef>,
): JSX.Element {
return (
<DocSearch
keyboardShortcuts={keyboardShortcuts}
theme={theme}
ref={ref}
onReady={onReady}
onOpen={onOpen}
onClose={onClose}
onSidepanelOpen={onSidepanelOpen}
onSidepanelClose={onSidepanelClose}
>
<DocSearchSidepanelComp {...props} />
</DocSearch>
);
}
export const DocSearchSidepanel = React.forwardRef(DocSearchSidepanelComponent);
function DocSearchSidepanelComp({
button: buttonProps = {},
panel: { portalContainer, ...panelProps } = {},
...rootProps
}: DocSearchSidepanelProps): JSX.Element {
const {
docsearchState,
setDocsearchState,
keyboardShortcuts,
registerView,
initialAskAiMessage,
clearInitialAskAiMessage,
} = useDocSearch();
const toggleSidepanelState = React.useCallback(() => {
setDocsearchState(docsearchState === 'sidepanel' ? 'ready' : 'sidepanel');
}, [docsearchState, setDocsearchState]);
const handleClose = (): void => {
clearInitialAskAiMessage();
setDocsearchState('ready');
};
const handleOpen = (): void => {
setDocsearchState('sidepanel');
};
const containerElement = React.useMemo(() => portalContainer ?? document.body, [portalContainer]);
React.useEffect(() => {
registerView('sidepanel');
}, [registerView]);
const ButtonComp = React.useMemo(
() => <SidepanelButton keyboardShortcuts={keyboardShortcuts} onClick={toggleSidepanelState} {...buttonProps} />,
[keyboardShortcuts, toggleSidepanelState, buttonProps],
);
return (
<>
{buttonProps.variant === 'inline' ? ButtonComp : createPortal(ButtonComp, containerElement)}
{createPortal(
<Sidepanel
initialMessage={initialAskAiMessage}
isOpen={docsearchState === 'sidepanel'}
onClose={handleClose}
onOpen={handleOpen}
{...rootProps}
{...panelProps}
keyboardShortcuts={keyboardShortcuts}
/>,
containerElement,
)}
</>
);
}
export * from './Sidepanel/index';