Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
72744c92
Commit
72744c92
authored
Apr 19, 2024
by
Max Alekseenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix apps re-rendering
parent
1391915e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
10 deletions
+12
-10
useMarketplaceApps.tsx
ui/marketplace/useMarketplaceApps.tsx
+12
-10
No files found.
ui/marketplace/useMarketplaceApps.tsx
View file @
72744c92
...
@@ -18,13 +18,13 @@ function isAppNameMatches(q: string, app: MarketplaceAppWithSecurityReport) {
...
@@ -18,13 +18,13 @@ function isAppNameMatches(q: string, app: MarketplaceAppWithSecurityReport) {
return
app
.
title
.
toLowerCase
().
includes
(
q
.
toLowerCase
());
return
app
.
title
.
toLowerCase
().
includes
(
q
.
toLowerCase
());
}
}
function
isAppCategoryMatches
(
category
:
string
,
app
:
MarketplaceAppWithSecurityReport
,
favoriteApps
:
Array
<
string
>
)
{
function
isAppCategoryMatches
(
category
:
string
,
app
:
MarketplaceAppWithSecurityReport
,
favoriteApps
:
Array
<
string
>
=
[]
)
{
return
category
===
MarketplaceCategory
.
ALL
||
return
category
===
MarketplaceCategory
.
ALL
||
(
category
===
MarketplaceCategory
.
FAVORITES
&&
favoriteApps
.
includes
(
app
.
id
))
||
(
category
===
MarketplaceCategory
.
FAVORITES
&&
favoriteApps
.
includes
(
app
.
id
))
||
app
.
categories
.
includes
(
category
);
app
.
categories
.
includes
(
category
);
}
}
function
sortApps
(
apps
:
Array
<
MarketplaceAppWithSecurityReport
>
,
favoriteApps
:
Array
<
string
>
)
{
function
sortApps
(
apps
:
Array
<
MarketplaceAppWithSecurityReport
>
,
favoriteApps
:
Array
<
string
>
=
[]
)
{
return
apps
.
sort
((
a
,
b
)
=>
{
return
apps
.
sort
((
a
,
b
)
=>
{
const
priorityA
=
a
.
priority
||
0
;
const
priorityA
=
a
.
priority
||
0
;
const
priorityB
=
b
.
priority
||
0
;
const
priorityB
=
b
.
priority
||
0
;
...
@@ -60,17 +60,19 @@ export default function useMarketplaceApps(
...
@@ -60,17 +60,19 @@ export default function useMarketplaceApps(
const
{
data
:
securityReports
,
isPlaceholderData
:
isSecurityReportsPlaceholderData
}
=
useSecurityReports
();
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
<
Array
<
string
>
|
undefined
>
();
const
[
snapshotFavoriteApps
,
setSnapshotFavoriteApps
]
=
React
.
useState
<
Array
<
string
>
|
undefined
>
();
const
isInitialSetup
=
React
.
useRef
(
true
);
React
.
useEffect
(()
=>
{
React
.
useEffect
(()
=>
{
if
(
isFavoriteAppsLoaded
)
{
if
(
isInitialSetup
.
current
&&
(
isFavoriteAppsLoaded
||
favoriteApps
===
undefined
))
{
setSnapshotFavoriteApps
(
favoriteApps
);
setSnapshotFavoriteApps
(
favoriteApps
||
[]);
isInitialSetup
.
current
=
false
;
}
}
},
[
selectedCategoryId
,
isFavoriteAppsLoaded
]);
// eslint-disable-line react-hooks/exhaustive-deps
},
[
isFavoriteAppsLoaded
,
favoriteApps
]);
const
{
isPlaceholderData
,
isError
,
error
,
data
}
=
useQuery
<
unknown
,
ResourceError
<
unknown
>
,
Array
<
MarketplaceAppWithSecurityReport
>>
({
const
{
isPlaceholderData
,
isError
,
error
,
data
}
=
useQuery
<
unknown
,
ResourceError
<
unknown
>
,
Array
<
MarketplaceAppWithSecurityReport
>>
({
queryKey
:
[
'
marketplace-dapps
'
,
snapshotFavoriteApps
,
favoriteApps
],
queryKey
:
[
'
marketplace-dapps
'
,
snapshotFavoriteApps
],
queryFn
:
async
()
=>
{
queryFn
:
async
()
=>
{
if
(
!
feature
.
isEnabled
)
{
if
(
!
feature
.
isEnabled
)
{
return
[];
return
[];
...
@@ -80,10 +82,10 @@ export default function useMarketplaceApps(
...
@@ -80,10 +82,10 @@ export default function useMarketplaceApps(
return
apiFetch
(
'
marketplace_dapps
'
,
{
pathParams
:
{
chainId
:
config
.
chain
.
id
}
});
return
apiFetch
(
'
marketplace_dapps
'
,
{
pathParams
:
{
chainId
:
config
.
chain
.
id
}
});
}
}
},
},
select
:
(
data
)
=>
sortApps
(
data
as
Array
<
MarketplaceAppWithSecurityReport
>
,
snapshotFavoriteApps
||
[]
),
select
:
(
data
)
=>
sortApps
(
data
as
Array
<
MarketplaceAppWithSecurityReport
>
,
snapshotFavoriteApps
),
placeholderData
:
feature
.
isEnabled
?
Array
(
9
).
fill
(
MARKETPLACE_APP
)
:
undefined
,
placeholderData
:
feature
.
isEnabled
?
Array
(
9
).
fill
(
MARKETPLACE_APP
)
:
undefined
,
staleTime
:
Infinity
,
staleTime
:
Infinity
,
enabled
:
feature
.
isEnabled
&&
(
!
favoriteApps
||
Boolean
(
snapshotFavoriteApps
)
),
enabled
:
feature
.
isEnabled
&&
Boolean
(
snapshotFavoriteApps
),
});
});
const
appsWithSecurityReports
=
React
.
useMemo
(()
=>
const
appsWithSecurityReports
=
React
.
useMemo
(()
=>
...
@@ -91,7 +93,7 @@ export default function useMarketplaceApps(
...
@@ -91,7 +93,7 @@ export default function useMarketplaceApps(
[
data
,
securityReports
]);
[
data
,
securityReports
]);
const
displayedApps
=
React
.
useMemo
(()
=>
{
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
]);
},
[
selectedCategoryId
,
appsWithSecurityReports
,
filter
,
favoriteApps
]);
return
React
.
useMemo
(()
=>
({
return
React
.
useMemo
(()
=>
({
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment