-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathindex.tsx
More file actions
171 lines (160 loc) · 4.92 KB
/
Copy pathindex.tsx
File metadata and controls
171 lines (160 loc) · 4.92 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
import { AgentUI } from '@automattic/agenttic-ui';
import {
Button,
SearchControl,
__experimentalVStack as VStack,
__experimentalItemGroup as ItemGroup,
__experimentalItem as Item,
Spinner,
} from '@wordpress/components';
import { useDebouncedInput } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import clsx from 'clsx';
import { Link, useLocation } from 'react-router-dom';
import useFloatingPanelProps from '../../hooks/use-floating-panel-props';
import useHasAiChatEntryButton from '../../hooks/use-has-ai-chat-entry-button';
import useHelpSearchQuery from '../../hooks/use-help-search-query';
import ChatHeader, { type Options as ChatHeaderOptions } from '../chat-header';
import './style.scss';
interface SearchResultsProps {
searchInput: string;
}
function SearchResults( { searchInput }: SearchResultsProps ) {
const trimmedInput = searchInput.trim();
// An empty input returns recommended guides from the API.
const isRecommended = ! trimmedInput;
const { data, isFetching, isError, refetch } = useHelpSearchQuery( trimmedInput );
if ( isFetching ) {
return (
<div className="agent-manager-support-guides__status">
<Spinner />
</div>
);
}
if ( isError ) {
// If recommended guides fail to load, show the search prompt instead of an error.
if ( isRecommended ) {
return (
<div className="agent-manager-support-guides__status">
{ __( 'Search guides to find answers to your questions.', __i18n_text_domain__ ) }
</div>
);
}
return (
<div className="agent-manager-support-guides__status">
{ __( 'Something went wrong.', __i18n_text_domain__ ) }{ ' ' }
<Button
className="agent-manager-support-guides__retry"
variant="link"
onClick={ () => refetch() }
>
{ __( 'Try again', __i18n_text_domain__ ) }
</Button>
</div>
);
}
if ( ! data?.length ) {
return (
<div className="agent-manager-support-guides__status">
{ __( 'No results found.', __i18n_text_domain__ ) }
</div>
);
}
return (
<>
<h3 className="agent-manager-support-guides__title">
{ isRecommended
? __( 'Recommended Guides', __i18n_text_domain__ )
: __( 'Search Results', __i18n_text_domain__ ) }
</h3>
<ItemGroup isSeparated isBordered isRounded>
{ data?.map( ( item ) => (
// `post_id` is optional, so fall back to keep keys unique and defined.
<Item key={ item.post_id ?? item.link ?? item.title }>
<Link
to={ `/post?link=${ encodeURIComponent( item.link ) }` }
state={ { searchQuery: trimmedInput } }
>
{ item.title }
</Link>
</Item>
) ) }
</ItemGroup>
</>
);
}
interface SupportGuidesProps {
/** Chat header menu options. */
chatHeaderOptions: ChatHeaderOptions;
/** Indicates if the chat is docked in the sidebar. */
isDocked: boolean;
/** Indicates if the chat is expanded (floating mode). */
isOpen: boolean;
/** Called when the user aborts the current request. */
onAbort: () => void;
/** Called when the chat is closed. */
onClose: () => void;
/** Called when the chat is expanded (floating mode). */
onExpand: () => void;
}
export default function SupportGuides( {
isOpen,
chatHeaderOptions,
isDocked,
onAbort,
onClose,
onExpand,
}: SupportGuidesProps ) {
const { state } = useLocation();
const [ searchInput, setSearchInput, debouncedSearchInput ] = useDebouncedInput(
state?.searchQuery ?? ''
);
const floatingPanelProps = useFloatingPanelProps();
// Without the AI chat entry button, use `collapsed` (a FAB) instead of `minimized`.
const closedChatState = useHasAiChatEntryButton() ? 'minimized' : 'collapsed';
const title = __( 'Support Guides', __i18n_text_domain__ );
return (
<AgentUI.Container
// Remount on dock/undock so the mount-only seed props re-apply.
key={ isDocked ? 'embedded' : 'floating' }
{ ...floatingPanelProps }
className={ clsx( 'agenttic', { dark: isDocked } ) }
messages={ [] }
isProcessing={ false }
error={ null }
onSubmit={ () => {} }
variant={ isDocked ? 'embedded' : 'floating' }
freeDrag={ ! isDocked }
resizable={ ! isDocked }
floatingChatState={ isOpen ? 'expanded' : closedChatState }
triggerTitle={ title }
onClose={ onClose }
onExpand={ onExpand }
onStop={ onAbort }
expandOnHover={ false }
>
<AgentUI.ConversationView>
<ChatHeader
onClose={ onClose }
options={ chatHeaderOptions }
title={ title }
isDocked={ isDocked }
/>
<VStack
className="agent-manager-support-guides-wrapper"
alignment="stretch"
justify="stretch"
>
<SearchControl
placeholder={ __( 'Search guides…', __i18n_text_domain__ ) }
onChange={ setSearchInput }
// The click event is highjacked by the drag-handlers of the floating chat container.
onClick={ ( e ) => e.currentTarget.focus() }
value={ searchInput }
/>
<SearchResults searchInput={ debouncedSearchInput } />
</VStack>
</AgentUI.ConversationView>
</AgentUI.Container>
);
}