Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ContentSearch to use react query #326

Merged
merged 15 commits into from
Jun 11, 2024
Merged
Prev Previous commit
Next Next commit
fix linting
fabiankaegy committed Jun 11, 2024

Verified

This commit was signed with the committer’s verified signature.
fabiankaegy Fabian Kägy
commit 3a95ba68840f28609758761254b703959df7f7a2
32 changes: 14 additions & 18 deletions components/content-search/SearchItem.tsx
Original file line number Diff line number Diff line change
@@ -58,6 +58,19 @@ const StyledTextHighlight = styled(TextHighlight)`
padding: 0 !important;
`;

export function defaultRenderItemType(suggestion: NormalizedSuggestion): string {
// Rename 'post_tag' to 'tag'. Ideally, the API would return the localized CPT or taxonomy label.
if (suggestion.type === 'post_tag') {
return 'tag';
}

if (suggestion.subtype) {
return suggestion.subtype;
}

return suggestion.type;
}

const SearchItem: React.FC<RenderItemComponentProps> = ({
item: suggestion,
onSelect: onClick,
@@ -85,27 +98,10 @@ const SearchItem: React.FC<RenderItemComponentProps> = ({
</Truncate>
</SearchItemInfo>
</SearchItemHeader>
{showType && (
<SearchItemType>
{renderType(suggestion)}
</SearchItemType>
)}
{showType && <SearchItemType>{renderType(suggestion)}</SearchItemType>}
</SearchItemWrapper>
</Tooltip>
);
};

export function defaultRenderItemType(suggestion: NormalizedSuggestion): string {
// Rename 'post_tag' to 'tag'. Ideally, the API would return the localized CPT or taxonomy label.
if (suggestion.type === 'post_tag') {
return 'tag';
}

if (suggestion.subtype) {
return suggestion.subtype;
}

return suggestion.type;
}

export default SearchItem;
71 changes: 30 additions & 41 deletions components/content-search/index.tsx
Original file line number Diff line number Diff line change
@@ -3,14 +3,15 @@ import { useState, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import styled from '@emotion/styled';
import { useMergeRefs } from '@wordpress/compose';
import { QueryClient, QueryClientProvider, useInfiniteQuery } from '@tanstack/react-query';
import SearchItem from './SearchItem';
import { StyledComponentContext } from '../styled-components-context';
import type { ContentSearchMode, IdentifiableObject, QueryFilter, RenderItemComponentProps } from './types';
import {
QueryClient,
QueryClientProvider,
useInfiniteQuery,
} from '@tanstack/react-query';
import type {
ContentSearchMode,
IdentifiableObject,
QueryFilter,
RenderItemComponentProps,
} from './types';
import { useOnClickOutside } from '../../hooks/use-on-click-outside';
import { NormalizedSuggestion, fetchSearchResults } from './utils';

@@ -117,38 +118,23 @@ const ContentSearch: React.FC<ContentSearchProps> = ({

const mergedRef = useMergeRefs([searchContainer, clickOutsideRef]);

const {
status,
data,
error,
isFetching,
isFetchingNextPage,
fetchNextPage,
hasNextPage,
} = useInfiniteQuery(
{
queryKey: [
'search',
searchString,
contentTypes.join(','),
mode,
perPage,
queryFilter,
],
queryFn: async ({ pageParam = 1 }) => fetchSearchResults({
keyword: searchString,
page: pageParam,
mode,
perPage,
contentTypes,
queryFilter,
excludeItems,
}),
const { status, data, error, isFetching, isFetchingNextPage, fetchNextPage, hasNextPage } =
useInfiniteQuery({
queryKey: ['search', searchString, contentTypes.join(','), mode, perPage, queryFilter],
queryFn: async ({ pageParam = 1 }) =>
fetchSearchResults({
keyword: searchString,
page: pageParam,
mode,
perPage,
contentTypes,
queryFilter,
excludeItems,
}),
getNextPageParam: (lastPage) => lastPage.nextPage,
getPreviousPageParam: (firstPage) => firstPage.previousPage,
initialPageParam: 1
}
);
initialPageParam: 1,
});

const searchResults = data?.pages.map((page) => page?.results).flat() || undefined;

@@ -162,7 +148,7 @@ const ContentSearch: React.FC<ContentSearchProps> = ({
<StyledNavigableMenu ref={mergedRef} orientation="vertical">
<StyledSearchControl
value={searchString}
onChange={(newSearchString) => {
onChange={(newSearchString: string) => {
setSearchString(newSearchString);
}}
label={label}
@@ -176,16 +162,19 @@ const ContentSearch: React.FC<ContentSearchProps> = ({

{hasSearchString || hasInitialResults ? (
<>
<List className={`tenup-content-search-list`}>
{isPending && <StyledSpinner onPointerEnterCapture={null} onPointerLeaveCapture={null} />}
<List className="tenup-content-search-list">
{isPending && <StyledSpinner />}
{hasNoResults && <ContentSearchNoResults />}
{hasSearchResults &&
searchResults.map((item) => {
const selectItem = () => {
handleItemSelection(item);
};
return (
<ListItem key={item.id} className="tenup-content-search-list-item">
<ListItem
key={item.id}
className="tenup-content-search-list-item"
>
<SearchResultItem
item={item}
onSelect={selectItem}
@@ -206,7 +195,7 @@ const ContentSearch: React.FC<ContentSearchProps> = ({
</LoadingContainer>
)}

{isFetchingNextPage && <StyledSpinner onPointerEnterCapture={null} onPointerLeaveCapture={null} />}
{isFetchingNextPage && <StyledSpinner />}
</>
) : null}
</StyledNavigableMenu>

Unchanged files with check annotations Beta

*
* @param {object} settings block settings
* @param {string} name block name
* @returns {Array}

Check failure on line 56 in api/register-block-extension/index.js

GitHub Actions / Cypress

Generic type 'Array<T>' requires 1 type argument(s).

Check failure on line 56 in api/register-block-extension/index.js

GitHub Actions / build

Generic type 'Array<T>' requires 1 type argument(s).
*/
const addAttributesToBlock = (settings, name) => {
// return early from the block modification
if (!shouldApplyBlockExtension(name)) {
return settings;

Check failure on line 61 in api/register-block-extension/index.js

GitHub Actions / Cypress

Type '{}' is missing the following properties from type 'any[]': length, pop, push, concat, and 35 more.

Check failure on line 61 in api/register-block-extension/index.js

GitHub Actions / build

Type '{}' is missing the following properties from type 'any[]': length, pop, push, concat, and 35 more.
}
// modify block registration object
return {
...settings,
attributes: {

Check failure on line 67 in api/register-block-extension/index.js

GitHub Actions / Cypress

Object literal may only specify known properties, and 'attributes' does not exist in type 'any[]'.

Check failure on line 67 in api/register-block-extension/index.js

GitHub Actions / build

Object literal may only specify known properties, and 'attributes' does not exist in type 'any[]'.
...settings.attributes,

Check failure on line 68 in api/register-block-extension/index.js

GitHub Actions / Cypress

Property 'attributes' does not exist on type 'object'.

Check failure on line 68 in api/register-block-extension/index.js

GitHub Actions / build

Property 'attributes' does not exist on type 'object'.
...attributes,
},
};
return (
<>
{shouldRenderBefore && <Edit {...props} />}

Check failure on line 99 in api/register-block-extension/index.js

GitHub Actions / Cypress

'Edit' cannot be used as a JSX component.

Check failure on line 99 in api/register-block-extension/index.js

GitHub Actions / build

'Edit' cannot be used as a JSX component.
<BlockEdit {...props} />
{shouldRenderAfter && <Edit {...props} />}

Check failure on line 101 in api/register-block-extension/index.js

GitHub Actions / Cypress

'Edit' cannot be used as a JSX component.

Check failure on line 101 in api/register-block-extension/index.js

GitHub Actions / build

'Edit' cannot be used as a JSX component.
{shouldRenderFallback && <Edit {...props} />}

Check failure on line 102 in api/register-block-extension/index.js

GitHub Actions / Cypress

'Edit' cannot be used as a JSX component.

Check failure on line 102 in api/register-block-extension/index.js

GitHub Actions / build

'Edit' cannot be used as a JSX component.
</>
);
};
* @returns {object}
*/
const addAdditionalPropertiesToSavedMarkup = (props, block, attributes) => {
const { className, style } = props;

Check failure on line 165 in api/register-block-extension/index.js

GitHub Actions / Cypress

Property 'className' does not exist on type '{}'.

Check failure on line 165 in api/register-block-extension/index.js

GitHub Actions / Cypress

Property 'style' does not exist on type '{}'.

Check failure on line 165 in api/register-block-extension/index.js

GitHub Actions / build

Property 'className' does not exist on type '{}'.

Check failure on line 165 in api/register-block-extension/index.js

GitHub Actions / build

Property 'style' does not exist on type '{}'.
// return early from the block modification
if (!shouldApplyBlockExtension(block.name)) {

Check failure on line 168 in api/register-block-extension/index.js

GitHub Actions / Cypress

Property 'name' does not exist on type 'object'.

Check failure on line 168 in api/register-block-extension/index.js

GitHub Actions / build

Property 'name' does not exist on type 'object'.
return props;
}