From 1391915eb2c1da7bbcf97cc1e7d4bf27ea931af8 Mon Sep 17 00:00:00 2001 From: Max Alekseenko Date: Fri, 19 Apr 2024 15:00:10 +0200 Subject: [PATCH 1/2] fix sorting between tabs --- ui/marketplace/MarketplaceListWithScores.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/marketplace/MarketplaceListWithScores.tsx b/ui/marketplace/MarketplaceListWithScores.tsx index 6d86eb63ba..2ed58dafec 100644 --- a/ui/marketplace/MarketplaceListWithScores.tsx +++ b/ui/marketplace/MarketplaceListWithScores.tsx @@ -32,7 +32,7 @@ const MarketplaceListWithScores = ({ showContractList, }: Props) => { - const displayedApps = React.useMemo(() => apps.sort((a, b) => { + const displayedApps = React.useMemo(() => [ ...apps ].sort((a, b) => { if (!a.securityReport) { return 1; } else if (!b.securityReport) { From 72744c92866dc6f8865ab5cfd41c2bfe6fc53f89 Mon Sep 17 00:00:00 2001 From: Max Alekseenko Date: Fri, 19 Apr 2024 19:44:23 +0200 Subject: [PATCH 2/2] fix apps re-rendering --- ui/marketplace/useMarketplaceApps.tsx | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/ui/marketplace/useMarketplaceApps.tsx b/ui/marketplace/useMarketplaceApps.tsx index 8aca496b9c..96f09f379e 100644 --- a/ui/marketplace/useMarketplaceApps.tsx +++ b/ui/marketplace/useMarketplaceApps.tsx @@ -18,13 +18,13 @@ function isAppNameMatches(q: string, app: MarketplaceAppWithSecurityReport) { return app.title.toLowerCase().includes(q.toLowerCase()); } -function isAppCategoryMatches(category: string, app: MarketplaceAppWithSecurityReport, favoriteApps: Array) { +function isAppCategoryMatches(category: string, app: MarketplaceAppWithSecurityReport, favoriteApps: Array = []) { return category === MarketplaceCategory.ALL || (category === MarketplaceCategory.FAVORITES && favoriteApps.includes(app.id)) || app.categories.includes(category); } -function sortApps(apps: Array, favoriteApps: Array) { +function sortApps(apps: Array, favoriteApps: Array = []) { return apps.sort((a, b) => { const priorityA = a.priority || 0; const priorityB = b.priority || 0; @@ -60,17 +60,19 @@ export default function useMarketplaceApps( const { data: securityReports, isPlaceholderData: isSecurityReportsPlaceholderData } = useSecurityReports(); - // Update favorite apps only when selectedCategoryId changes to avoid sortApps to be called on each favorite app click + // Set the value only 1 time to avoid unnecessary useQuery calls and re-rendering of all applications const [ snapshotFavoriteApps, setSnapshotFavoriteApps ] = React.useState | undefined>(); + const isInitialSetup = React.useRef(true); React.useEffect(() => { - if (isFavoriteAppsLoaded) { - setSnapshotFavoriteApps(favoriteApps); + if (isInitialSetup.current && (isFavoriteAppsLoaded || favoriteApps === undefined)) { + setSnapshotFavoriteApps(favoriteApps || []); + isInitialSetup.current = false; } - }, [ selectedCategoryId, isFavoriteAppsLoaded ]); // eslint-disable-line react-hooks/exhaustive-deps + }, [ isFavoriteAppsLoaded, favoriteApps ]); const { isPlaceholderData, isError, error, data } = useQuery, Array>({ - queryKey: [ 'marketplace-dapps', snapshotFavoriteApps, favoriteApps ], + queryKey: [ 'marketplace-dapps', snapshotFavoriteApps ], queryFn: async() => { if (!feature.isEnabled) { return []; @@ -80,10 +82,10 @@ export default function useMarketplaceApps( return apiFetch('marketplace_dapps', { pathParams: { chainId: config.chain.id } }); } }, - select: (data) => sortApps(data as Array, snapshotFavoriteApps || []), + select: (data) => sortApps(data as Array, snapshotFavoriteApps), placeholderData: feature.isEnabled ? Array(9).fill(MARKETPLACE_APP) : undefined, staleTime: Infinity, - enabled: feature.isEnabled && (!favoriteApps || Boolean(snapshotFavoriteApps)), + enabled: feature.isEnabled && Boolean(snapshotFavoriteApps), }); const appsWithSecurityReports = React.useMemo(() => @@ -91,7 +93,7 @@ export default function useMarketplaceApps( [ data, securityReports ]); const displayedApps = React.useMemo(() => { - return appsWithSecurityReports?.filter(app => isAppNameMatches(filter, app) && isAppCategoryMatches(selectedCategoryId, app, favoriteApps || [])) || []; + return appsWithSecurityReports?.filter(app => isAppNameMatches(filter, app) && isAppCategoryMatches(selectedCategoryId, app, favoriteApps)) || []; }, [ selectedCategoryId, appsWithSecurityReports, filter, favoriteApps ]); return React.useMemo(() => ({