| 1 |
import { __ } from '@wordpress/i18n' |
| 2 |
import React from 'react' |
| 3 |
import { Spinner } from '@wordpress/components' |
| 4 |
import { TablePagination } from '../../common/ListTable/TablePagination' |
| 5 |
import { SearchResult } from './SearchResult' |
| 6 |
import { useCloudSearch } from './WithCloudSearchContext' |
| 7 |
import { SearchFilters } from './SearchFilters' |
| 8 |
import type { TablePaginationProps } from '../../common/ListTable/TablePagination' |
| 9 |
import type { FormEventHandler } from 'react' |
| 10 |
|
| 11 |
const SearchBox = () => { |
| 12 |
const { searchParams, updateSearchParams, isSearching, doSearch } = useCloudSearch() |
| 13 |
|
| 14 |
const handleSubmit: FormEventHandler<HTMLFormElement> = event => { |
| 15 |
event.preventDefault() |
| 16 |
doSearch() |
| 17 |
} |
| 18 |
|
| 19 |
return ( |
| 20 |
<form className="cloud-search-form" onSubmit={handleSubmit}> |
| 21 |
<label className="screen-reader-text" htmlFor="cloud-search-method"> |
| 22 |
{__('Search method', 'code-snippets')} |
| 23 |
</label> |
| 24 |
<select |
| 25 |
id="cloud-search-method" |
| 26 |
value={searchParams.method} |
| 27 |
onChange={event => |
| 28 |
updateSearchParams({ method: 'codevault' === event.target.value ? 'codevault' : 'term' })} |
| 29 |
> |
| 30 |
<option value="term">{__('Search by keyword', 'code-snippets')}</option> |
| 31 |
<option value="codevault">{__('Name of codevault', 'code-snippets')}</option> |
| 32 |
</select> |
| 33 |
|
| 34 |
<div className="cloud-search-query"> |
| 35 |
<input |
| 36 |
id="cloud-search-query" |
| 37 |
type="search" |
| 38 |
value={searchParams.query} |
| 39 |
aria-label={__('Search query', 'code-snippets')} |
| 40 |
onChange={event => updateSearchParams({ query: event.target.value })} |
| 41 |
placeholder={__('e.g. Remove unused JavaScript…', 'code-snippets')} |
| 42 |
/> |
| 43 |
<span role="status" aria-live="polite"> |
| 44 |
{isSearching && |
| 45 |
<span className="screen-reader-text">{__('Searching…', 'code-snippets')}</span>} |
| 46 |
</span> |
| 47 |
</div> |
| 48 |
|
| 49 |
<button |
| 50 |
type="submit" |
| 51 |
className="button button-primary cloud-search-submit" |
| 52 |
disabled={isSearching} |
| 53 |
> |
| 54 |
{isSearching ? <Spinner /> : __('Search Cloud Library', 'code-snippets')} |
| 55 |
</button> |
| 56 |
</form> |
| 57 |
) |
| 58 |
} |
| 59 |
|
| 60 |
const SearchResultsTable = () => { |
| 61 |
const { searchResults, isSearching, doSearch } = useCloudSearch() |
| 62 |
|
| 63 |
if (!searchResults) { |
| 64 |
return null |
| 65 |
} |
| 66 |
|
| 67 |
const { totalItems, totalPages, page } = searchResults |
| 68 |
|
| 69 |
const paginationProps: Omit<TablePaginationProps, 'which'> = { |
| 70 |
totalItems, |
| 71 |
totalPages, |
| 72 |
disabled: isSearching, |
| 73 |
currentPage: page, |
| 74 |
setCurrentPage: newPage => doSearch({ page: newPage }) |
| 75 |
} |
| 76 |
|
| 77 |
return ( |
| 78 |
<> |
| 79 |
<div className="tablenav top"> |
| 80 |
<SearchFilters /> |
| 81 |
|
| 82 |
<TablePagination which="top" {...paginationProps} /> |
| 83 |
</div> |
| 84 |
|
| 85 |
<ul className="cloud-search-results"> |
| 86 |
{searchResults.snippets.map(result => |
| 87 |
<SearchResult key={result.id} snippet={result} />)} |
| 88 |
</ul> |
| 89 |
|
| 90 |
<div className="tablenav bottom"> |
| 91 |
<TablePagination which="bottom" {...paginationProps} /> |
| 92 |
</div> |
| 93 |
</> |
| 94 |
) |
| 95 |
} |
| 96 |
|
| 97 |
const SearchResults = () => { |
| 98 |
const { searchResults, searchParams, isErrored } = useCloudSearch() |
| 99 |
|
| 100 |
if (isErrored) { |
| 101 |
return ( |
| 102 |
<div className="banner banner-error"> |
| 103 |
<p>{__('An error occurred while fetching search results. Please try again.', 'code-snippets')}</p> |
| 104 |
</div> |
| 105 |
) |
| 106 |
} |
| 107 |
|
| 108 |
if (!searchResults) { |
| 109 |
return null |
| 110 |
} |
| 111 |
|
| 112 |
if (0 < searchResults.page && 0 === searchResults.snippets.length) { |
| 113 |
return ( |
| 114 |
<div className="banner banner-neutral no-results"> |
| 115 |
<p>{'codevault' === searchParams.method |
| 116 |
? __('Could not find a codevault with that name. Please try again.', 'code-snippets') |
| 117 |
: __('No snippets could be found with that search term. Please try again.', 'code-snippets') |
| 118 |
}</p> |
| 119 |
</div> |
| 120 |
) |
| 121 |
} |
| 122 |
|
| 123 |
return searchResults.snippets.length |
| 124 |
? <> |
| 125 |
{searchResults.isFeatured |
| 126 |
? <h3 className="cloud-featured-heading">{__('Featured Snippets', 'code-snippets')}</h3> |
| 127 |
: <h3 className="cloud-snippets-heading">{__('Search Results', 'code-snippets')}</h3>} |
| 128 |
<SearchResultsTable /> |
| 129 |
</> |
| 130 |
: null |
| 131 |
} |
| 132 |
|
| 133 |
export const CloudSearch = () => |
| 134 |
<div className="cloud-search"> |
| 135 |
<SearchBox /> |
| 136 |
<SearchResults /> |
| 137 |
</div> |
| 138 |
|