-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.tsx
213 lines (186 loc) · 6.55 KB
/
index.tsx
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
/**
* External dependencies
*/
import React, { FunctionComponent, useState, useEffect } from 'react';
import { times } from 'lodash';
import { Button, TextControl } from '@wordpress/components';
import { Icon, search } from '@wordpress/icons';
import { getNewRailcarId, recordTrainTracksRender } from '@automattic/calypso-analytics';
import { useI18n } from '@automattic/react-i18n';
import type { DomainSuggestions } from '@automattic/data-stores';
/**
* Internal dependencies
*/
import SuggestionItem from './suggestion-item';
import SuggestionItemPlaceholder from './suggestion-item-placeholder';
import { useDomainSuggestions } from '../hooks/use-domain-suggestions';
import DomainCategories from '../domain-categories';
import { PAID_DOMAINS_TO_SHOW, PAID_DOMAINS_TO_SHOW_EXPANDED } from '../constants';
import { DomainNameExplanationImage } from '../domain-name-explanation/';
/**
* Style dependencies
*/
import './style.scss';
type DomainSuggestion = DomainSuggestions.DomainSuggestion;
export interface Props {
header: React.ReactElement;
showDomainCategories?: boolean;
/**
* Callback that will be invoked when a domain is selected.
*
* @param domainSuggestion The selected domain.
*/
onDomainSelect: ( domainSuggestion: DomainSuggestion ) => void;
/** Paid omain suggestions to show when the picker isn't expanded */
quantity?: number;
/** Domain suggestions to show when the picker is expanded */
quantityExpanded?: number;
currentDomain?: DomainSuggestion;
/** The flow where the Domain Picker is used. Eg: Gutenboarding */
analyticsFlowId: string;
/** An identifier for the wrapping UI used for setting ui_algo. Eg: domain_popover */
analyticsUiAlgo: string;
domainSuggestionVendor: string;
/** The initial domain search query */
initialDomainSearch: string;
/** Called when the domain search query is changed */
onSetDomainSearch: ( value: string ) => void;
}
const DomainPicker: FunctionComponent< Props > = ( {
header,
showDomainCategories,
onDomainSelect,
quantity = PAID_DOMAINS_TO_SHOW,
quantityExpanded = PAID_DOMAINS_TO_SHOW_EXPANDED,
analyticsFlowId,
analyticsUiAlgo,
domainSuggestionVendor,
initialDomainSearch,
onSetDomainSearch,
} ) => {
const { __ } = useI18n();
const label = __( 'Search for a domain' );
// We're not keeping the current selection in local state at the moment
//const [ currentSelection, setCurrentSelection ] = useState( currentDomain );
const [ isExpanded, setIsExpanded ] = useState( false );
// keep domain query in local state to allow free editing of the input value while the modal is open
const [ domainSearch, setDomainSearch ] = useState< string >( initialDomainSearch );
const [ domainCategory, setDomainCategory ] = useState< string | undefined >();
const allDomainSuggestions = useDomainSuggestions(
domainSearch.trim(),
quantityExpanded,
domainCategory,
useI18n().i18nLocale
) as DomainSuggestion[] | undefined;
const domainSuggestions = allDomainSuggestions?.slice(
0,
isExpanded ? quantityExpanded : quantity
);
// Put the free domain at the second place and things will be sorted properly
if ( domainSuggestions && domainSuggestions.length > 1 ) {
const freeDomainIndex = domainSuggestions.findIndex( ( domain ) => domain.is_free );
if ( freeDomainIndex > -1 ) {
const swap = domainSuggestions[ 1 ];
domainSuggestions[ 1 ] = domainSuggestions[ freeDomainIndex ];
domainSuggestions[ freeDomainIndex ] = swap;
}
}
// Reset expansion state after every search
useEffect( () => {
setIsExpanded( false );
}, [ domainSearch ] );
/** The train track ID for analytics. See https://wp.me/PCYsg-bor */
const [ baseRailcarId, setBaseRailcarId ] = useState< string | undefined >();
useEffect( () => {
// Only generate a railcarId when the domain suggestions change and are not empty.
if ( allDomainSuggestions ) {
setBaseRailcarId( getNewRailcarId( 'suggestion' ) );
}
}, [ allDomainSuggestions, setBaseRailcarId ] );
const handleItemRender = (
suggestion: DomainSuggestion,
railcarId: string,
uiPosition: number,
isRecommended: boolean
) => {
const fetchAlgo = `/domains/search/${ domainSuggestionVendor }/${ analyticsFlowId }${
domainCategory ? '/' + domainCategory : ''
}`;
const domain = suggestion.domain_name;
recordTrainTracksRender( {
uiAlgo: `/${ analyticsFlowId }/${ analyticsUiAlgo }`,
fetchAlgo,
query: domainSearch,
railcarId,
result: isRecommended ? domain + '#recommended' : domain,
uiPosition,
} );
};
const handleInputChange = ( searchQuery: string ) => {
setDomainSearch( searchQuery );
onSetDomainSearch( searchQuery );
};
return (
<div className="domain-picker">
{ header }
<div className="domain-picker__search">
<div className="domain-picker__search-icon">
<Icon icon={ search } />
</div>
<TextControl
// Unable to remove this instance due to it being a HotJar term: https://github.com/Automattic/wp-calypso/pull/43348#discussion_r442015229
data-hj-whitelist
hideLabelFromVision
label={ label }
placeholder={ label }
onChange={ handleInputChange }
value={ domainSearch }
/>
</div>
{ domainSearch.trim() ? (
<div className="domain-picker__body">
{ showDomainCategories && (
<div className="domain-picker__aside">
<DomainCategories selected={ domainCategory } onSelect={ setDomainCategory } />
</div>
) }
<div className="domain-picker__suggestion-item-group">
{ domainSuggestions?.map( ( suggestion, i ) => (
<SuggestionItem
key={ suggestion.domain_name }
suggestion={ suggestion }
railcarId={ baseRailcarId ? `${ baseRailcarId }${ i }` : undefined }
isRecommended={ i === 0 }
onRender={ () =>
handleItemRender( suggestion, `${ baseRailcarId }${ i }`, i, i === 0 )
}
onSelect={ onDomainSelect }
/>
) ) ?? times( quantity, ( i ) => <SuggestionItemPlaceholder key={ i } /> ) }
{ ! isExpanded &&
allDomainSuggestions?.length &&
allDomainSuggestions?.length > quantity && (
<div className="domain-picker__show-more">
<Button onClick={ () => setIsExpanded( true ) }>
{ __( 'View more results' ) }
</Button>
</div>
) }
</div>
</div>
) : (
<div className="domain-picker__empty-state">
<p className="domain-picker__empty-state--text">
{ __(
'A domain name is the site address people type in their browser to visit your site.'
) }
</p>
<div>
<DomainNameExplanationImage />
</div>
</div>
) }
</div>
);
};
export default DomainPicker;