Skip to content

Commit

Permalink
Merge pull request #2132 from blockscout/fix-marketplace-search-query
Browse files Browse the repository at this point in the history
Fix marketplace search query
  • Loading branch information
maxaleks authored Aug 6, 2024
2 parents c582d91 + a5a2b75 commit 5d1edc5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
27 changes: 20 additions & 7 deletions ui/marketplace/useMarketplace.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _pickBy from 'lodash/pickBy';
import { useRouter } from 'next/router';
import React from 'react';

Expand Down Expand Up @@ -109,24 +108,38 @@ export default function useMarketplace() {
}, [ isPlaceholderData ]);

React.useEffect(() => {
const query = _pickBy({
category: selectedCategoryId === MarketplaceCategory.ALL ? undefined : selectedCategoryId,
filter: debouncedFilterQuery,
}, Boolean);
if (isPlaceholderData) {
return;
}

const { query } = router;
const newQuery = { ...query };

if (selectedCategoryId !== MarketplaceCategory.ALL) {
newQuery.category = selectedCategoryId;
} else {
delete newQuery.category;
}

if (debouncedFilterQuery) {
newQuery.filter = debouncedFilterQuery;
} else {
delete newQuery.filter;
}

if (debouncedFilterQuery.length > 0) {
mixpanel.logEvent(mixpanel.EventTypes.LOCAL_SEARCH, { Source: 'Marketplace', 'Search query': debouncedFilterQuery });
}

router.replace(
{ pathname: '/apps', query },
{ pathname: '/apps', query: newQuery },
undefined,
{ shallow: true },
);
// omit router in the deps because router.push() somehow modifies it
// and we get infinite re-renders then
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ debouncedFilterQuery, selectedCategoryId ]);
}, [ debouncedFilterQuery, selectedCategoryId, isPlaceholderData ]);

return React.useMemo(() => ({
selectedCategoryId,
Expand Down
16 changes: 11 additions & 5 deletions ui/marketplace/useMarketplaceApps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default function useMarketplaceApps(

const { data: securityReports, isPlaceholderData: isSecurityReportsPlaceholderData } = useSecurityReports();

const [ sorting, setSorting ] = React.useState<SortValue>();
// Set the value only 1 time to avoid unnecessary useQuery calls and re-rendering of all applications
const [ snapshotFavoriteApps, setSnapshotFavoriteApps ] = React.useState<Array<string> | undefined>();
const isInitialSetup = React.useRef(true);
Expand All @@ -73,7 +74,9 @@ export default function useMarketplaceApps(
}
}, [ isFavoriteAppsLoaded, favoriteApps ]);

const { isPlaceholderData, isError, error, data } = useQuery<unknown, ResourceError<unknown>, Array<MarketplaceAppWithSecurityReport>>({
const {
isPlaceholderData: isAppsPlaceholderData, isError, error, data,
} = useQuery<unknown, ResourceError<unknown>, Array<MarketplaceAppWithSecurityReport>>({
queryKey: [ 'marketplace-dapps', snapshotFavoriteApps ],
queryFn: async() => {
if (!feature.isEnabled) {
Expand All @@ -90,7 +93,7 @@ export default function useMarketplaceApps(
enabled: feature.isEnabled && Boolean(snapshotFavoriteApps),
});

const [ sorting, setSorting ] = React.useState<SortValue>();
const isPlaceholderData = isAppsPlaceholderData || isSecurityReportsPlaceholderData;

const appsWithSecurityReportsAndRating = React.useMemo(() =>
data?.map((app) => ({
Expand All @@ -101,6 +104,10 @@ export default function useMarketplaceApps(
[ data, securityReports, ratings ]);

const displayedApps = React.useMemo(() => {
if (isPlaceholderData) {
return appsWithSecurityReportsAndRating || [];
}

return appsWithSecurityReportsAndRating
?.filter(app => isAppNameMatches(filter, app) && isAppCategoryMatches(selectedCategoryId, app, favoriteApps))
.sort((a, b) => {
Expand All @@ -112,22 +119,21 @@ export default function useMarketplaceApps(
}
return 0;
}) || [];
}, [ selectedCategoryId, appsWithSecurityReportsAndRating, filter, favoriteApps, sorting ]);
}, [ selectedCategoryId, appsWithSecurityReportsAndRating, filter, favoriteApps, sorting, isPlaceholderData ]);

return React.useMemo(() => ({
data,
displayedApps,
error,
isError,
isPlaceholderData: isPlaceholderData || isSecurityReportsPlaceholderData,
isPlaceholderData,
setSorting,
}), [
data,
displayedApps,
error,
isError,
isPlaceholderData,
isSecurityReportsPlaceholderData,
setSorting,
]);
}
6 changes: 3 additions & 3 deletions ui/shared/EmptySearchResult.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box, Heading, Text, Icon } from '@chakra-ui/react';
import { Box, Heading, Icon } from '@chakra-ui/react';
import React from 'react';

// This icon doesn't work properly when it is in the sprite
Expand Down Expand Up @@ -30,9 +30,9 @@ const EmptySearchResult = ({ text }: Props) => {
No results
</Heading>

<Text fontSize={{ base: 'sm', sm: 'md' }} align="center">
<Box fontSize={{ base: 'sm', sm: 'md' }} textAlign="center">
{ text }
</Text>
</Box>
</Box>
);
};
Expand Down

0 comments on commit 5d1edc5

Please sign in to comment.