File size: 7,895 Bytes
1e92f2d |
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 |
import page from '@automattic/calypso-router';
import { Gridicon } from '@automattic/components';
import { getContextResults } from '@automattic/data-stores';
import { useHelpSearchQuery } from '@automattic/help-center';
import { localizeUrl } from '@automattic/i18n-utils';
import { speak } from '@wordpress/a11y';
import { useDispatch as useDataStoreDispatch } from '@wordpress/data';
import { Icon, page as pageIcon, arrowRight } from '@wordpress/icons';
import { getLocaleSlug, useTranslate } from 'i18n-calypso';
import { debounce } from 'lodash';
import PropTypes from 'prop-types';
import { Fragment, useEffect, useMemo } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import QueryUserPurchases from 'calypso/components/data/query-user-purchases';
import { decodeEntities } from 'calypso/lib/formatting';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import getAdminHelpResults from 'calypso/state/selectors/get-admin-help-results';
import hasCancelableUserPurchases from 'calypso/state/selectors/has-cancelable-user-purchases';
import { useSiteOption } from 'calypso/state/sites/hooks';
import { getSectionName } from 'calypso/state/ui/selectors';
import {
SUPPORT_TYPE_ADMIN_SECTION,
SUPPORT_TYPE_API_HELP,
SUPPORT_TYPE_CONTEXTUAL_HELP,
} from './constants';
import PlaceholderLines from './placeholder-lines';
import './style.scss';
const noop = () => {};
function debounceSpeak( { message = '', priority = 'polite', timeout = 800 } ) {
return debounce( () => {
speak( message, priority );
}, timeout );
}
const loadingSpeak = debounceSpeak( { message: 'Loading search results.', timeout: 1500 } );
const resultsSpeak = debounceSpeak( { message: 'Search results loaded.' } );
const errorSpeak = debounceSpeak( { message: 'No search results found.' } );
const filterManagePurchaseLink = ( hasPurchases, isPurchasesSection ) => {
if ( hasPurchases || isPurchasesSection ) {
return () => true;
}
return ( { post_id } ) => post_id !== 111349;
};
function HelpSearchResults( {
externalLinks = false,
onSelect,
onAdminSectionSelect = noop,
searchQuery = '',
placeholderLines,
openAdminInNewTab = false,
location = 'inline-help-popover',
} ) {
const translate = useTranslate();
const dispatch = useDispatch();
const hasPurchases = useSelector( hasCancelableUserPurchases );
const sectionName = useSelector( getSectionName );
const isPurchasesSection = [ 'purchases', 'site-purchases' ].includes( sectionName );
const siteIntent = useSiteOption( 'site_intent' );
const rawContextualResults = useMemo(
() => getContextResults( sectionName, siteIntent ),
[ sectionName, siteIntent ]
);
const adminResults = useSelector( ( state ) => getAdminHelpResults( state, searchQuery, 3 ) );
const contextualResults = rawContextualResults.filter(
// Unless searching with Inline Help or on the Purchases section, hide the
// "Managing Purchases" documentation link for users who have not made a purchase.
filterManagePurchaseLink( hasPurchases, isPurchasesSection )
);
const { data: searchData, isInitialLoading: isSearching } = useHelpSearchQuery(
searchQuery,
getLocaleSlug(),
sectionName
);
const searchResults = searchData ?? [];
const hasAPIResults = searchResults.length > 0;
useEffect( () => {
// Cancel all queued speak messages.
loadingSpeak.cancel();
resultsSpeak.cancel();
errorSpeak.cancel();
// If there's no query, then we don't need to announce anything.
if ( ! searchQuery ) {
return;
}
if ( isSearching ) {
loadingSpeak();
} else if ( ! hasAPIResults ) {
errorSpeak();
} else if ( hasAPIResults ) {
resultsSpeak();
}
}, [ isSearching, hasAPIResults, searchQuery ] );
const { setShowSupportDoc } = useDataStoreDispatch( 'automattic/help-center' );
const onLinkClickHandler = ( event, result, type ) => {
const { link, post_id: postId, blog_id: blogId } = result;
if ( ! link ) {
onSelect( event, result );
return;
}
if ( type !== SUPPORT_TYPE_ADMIN_SECTION ) {
if ( type === SUPPORT_TYPE_API_HELP ) {
event.preventDefault();
setShowSupportDoc( link, postId, blogId );
}
onSelect( event, result );
return;
}
dispatch(
recordTracksEvent( 'calypso_inlinehelp_admin_section_visit', {
link: link,
search_term: searchQuery,
location,
section: sectionName,
} )
);
if ( ! /^http/.test( link ) ) {
event.preventDefault();
openAdminInNewTab ? window.open( 'https://wordpress.com' + link, '_blank' ) : page( link );
onAdminSectionSelect( event );
}
};
const renderHelpLink = ( result, type ) => {
const { link, title, icon } = result;
const external = externalLinks && type !== SUPPORT_TYPE_ADMIN_SECTION;
const LinkIcon = () => {
if ( type === 'admin_section' ) {
return <Icon icon={ arrowRight } />;
}
if ( icon ) {
return <Gridicon icon={ icon } />;
}
return <Icon icon={ pageIcon } />;
};
return (
<Fragment key={ title ?? link }>
<li className="inline-help__results-item">
<div className="inline-help__results-cell">
<a
href={ localizeUrl( link ) }
onClick={ ( event ) => {
if ( ! external ) {
event.preventDefault();
}
onLinkClickHandler( event, result, type );
} }
{ ...( external && {
target: '_blank',
rel: 'noreferrer',
} ) }
>
{ /* Old stuff - leaving this incase we need to quick revert
{ icon && <Gridicon icon={ icon } size={ 18 } /> } */ }
<LinkIcon />
<span>{ decodeEntities( title ) }</span>
</a>
</div>
</li>
</Fragment>
);
};
const renderSearchResultsSection = ( { type, title, results, condition } ) => {
const id = `inline-search--${ type }`;
return condition ? (
<Fragment key={ id }>
{ title ? (
<h3 id={ id } className="inline-help__results-title">
{ title }
</h3>
) : null }
<ul className="inline-help__results-list" aria-labelledby={ title ? id : undefined }>
{ results.map( ( result ) => renderHelpLink( result, type ) ) }
</ul>
</Fragment>
) : null;
};
const renderSearchSections = () => {
const sections = [
{
type: SUPPORT_TYPE_API_HELP,
title: translate( 'Recommended Resources' ),
results: searchResults.slice( 0, 5 ),
condition: ! isSearching && searchResults.length > 0,
},
{
type: SUPPORT_TYPE_CONTEXTUAL_HELP,
title: ! searchQuery.length ? translate( 'Recommended Resources' ) : '',
results: contextualResults.slice( 0, 6 ),
condition: ! isSearching && ! searchResults.length && contextualResults.length > 0,
},
{
type: SUPPORT_TYPE_ADMIN_SECTION,
title: translate( 'Show me where to' ),
results: adminResults,
condition: !! searchQuery && adminResults.length > 0,
},
];
return sections.map( renderSearchResultsSection );
};
const resultsLabel = hasAPIResults
? translate( 'Search Results' )
: translate( 'Helpful resources for this section' );
const renderSearchResults = () => {
if ( isSearching && ! searchResults.length && ! adminResults.length ) {
return <PlaceholderLines lines={ placeholderLines } />;
}
return (
<>
{ searchQuery && ! ( hasAPIResults || isSearching ) ? (
<p className="inline-help__empty-results">
{ translate(
'Sorry, there were no matches. Here are some of the most searched for help pages for this section:'
) }
</p>
) : null }
<div className="inline-help__results" aria-label={ resultsLabel }>
{ renderSearchSections() }
</div>
</>
);
};
return (
<>
<QueryUserPurchases />
{ renderSearchResults() }
</>
);
}
HelpSearchResults.propTypes = {
searchQuery: PropTypes.string,
onSelect: PropTypes.func.isRequired,
onAdminSectionSelect: PropTypes.func,
};
export default HelpSearchResults;
|