Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
interface
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
LuckySwap
interface
Commits
397b9d42
Unverified
Commit
397b9d42
authored
Sep 22, 2022
by
Jordan Frankfurt
Committed by
GitHub
Sep 22, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(explore): rewrite top tokens hook (#4697)
rewrite top tokens hook
parent
d9113fb6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
65 deletions
+53
-65
TokenTable.tsx
src/components/Tokens/TokenTable/TokenTable.tsx
+9
-22
Token.ts
src/graphql/data/Token.ts
+0
-1
TopTokens.ts
src/graphql/data/TopTokens.ts
+44
-42
No files found.
src/components/Tokens/TokenTable/TokenTable.tsx
View file @
397b9d42
...
...
@@ -116,28 +116,15 @@ export default function TokenTable() {
<
GridContainer
>
<
HeaderRow
/>
<
TokenDataContainer
>
{
tokens
.
map
((
token
,
index
)
=>
{
if
(
tokens
.
length
===
index
+
1
)
{
return
(
<
LoadedRow
key=
{
token
?.
name
}
tokenListIndex=
{
index
}
tokenListLength=
{
tokens
?.
length
??
0
}
token=
{
token
}
ref=
{
lastTokenRef
}
/>
)
}
else
{
return
(
<
LoadedRow
key=
{
token
?.
name
}
tokenListIndex=
{
index
}
tokenListLength=
{
tokens
?.
length
??
0
}
token=
{
token
}
/>
)
}
})
}
{
tokens
.
map
((
token
,
index
)
=>
(
<
LoadedRow
key=
{
token
?.
name
}
tokenListIndex=
{
index
}
tokenListLength=
{
tokens
?.
length
??
0
}
token=
{
token
}
ref=
{
tokens
.
length
===
index
+
1
?
lastTokenRef
:
undefined
}
/>
))
}
{
loading
&&
LoadingMoreRows
}
</
TokenDataContainer
>
</
GridContainer
>
...
...
src/graphql/data/Token.ts
View file @
397b9d42
...
...
@@ -113,7 +113,6 @@ const tokenQuery = graphql`
`
export
function
useTokenQuery
(
address
:
string
,
chain
:
Chain
,
timePeriod
:
TimePeriod
)
{
//const cachedTopToken = cachedTopTokens[address]
const
data
=
useLazyLoadQuery
<
TokenQuery
>
(
tokenQuery
,
{
contract
:
{
address
,
chain
},
duration
:
toHistoryDuration
(
timePeriod
),
...
...
src/graphql/data/TopTokens.ts
View file @
397b9d42
...
...
@@ -18,9 +18,8 @@ import { toHistoryDuration, useCurrentChainName } from './util'
export
function
usePrefetchTopTokens
()
{
const
duration
=
toHistoryDuration
(
useAtomValue
(
filterTimeAtom
))
const
chain
=
useCurrentChainName
()
const
top100
=
useLazyLoadQuery
<
TopTokens100Query
>
(
topTokens100Query
,
{
duration
,
chain
})
return
top100
const
args
=
useMemo
(()
=>
({
chain
,
duration
}),
[
chain
,
duration
])
return
useLazyLoadQuery
<
TopTokens100Query
>
(
topTokens100Query
,
args
)
}
const
topTokens100Query
=
graphql
`
...
...
@@ -131,59 +130,62 @@ function toContractInput(token: PrefetchedTopToken) {
}
export
type
TopToken
=
NonNullable
<
TopTokens_TokensQuery
[
'
response
'
][
'
tokens
'
]
>
[
number
]
export
function
useTopTokens
(
prefetchedData
:
TopTokens100Query
[
'
response
'
])
{
interface
UseTopTokensReturnValue
{
loading
:
boolean
tokens
:
TopToken
[]
loadMoreTokens
:
()
=>
void
}
export
function
useTopTokens
(
prefetchedData
:
TopTokens100Query
[
'
response
'
]):
UseTopTokensReturnValue
{
const
duration
=
toHistoryDuration
(
useAtomValue
(
filterTimeAtom
))
const
environment
=
useRelayEnvironment
()
const
[
tokens
,
setTokens
]
=
useState
<
TopToken
[]
>
()
const
[
tokens
,
setTokens
]
=
useState
<
TopToken
[]
>
(
[]
)
const
[
page
,
setPage
]
=
useState
(
1
)
const
prefetchedSelectedTokens
=
useFilteredTokens
(
useSortedTokens
(
prefetchedData
.
topTokens
))
const
[
page
,
setPage
]
=
useState
(
0
)
const
[
loading
,
setLoading
]
=
useState
(
true
)
// TopTokens should ideally be fetched with usePaginationFragment. The backend does not current support graphql cursors;
// in the meantime, fetchQuery is used, as other relay hooks do not allow the refreshing and lazy loading we need
const
loadTokens
=
useCallback
(
(
contracts
:
ContractInput
[],
onSuccess
:
(
data
:
TopTokens_TokensQuery
[
'
response
'
]
|
undefined
)
=>
void
)
=>
{
fetchQuery
<
TopTokens_TokensQuery
>
(
environment
,
tokensQuery
,
{
contracts
,
duration
},
{
fetchPolicy
:
'
store-or-network
'
}
const
appendTokens
=
useCallback
(
(
newTokens
:
TopToken
[])
=>
{
setTokens
(
Object
.
values
(
tokens
.
concat
(
newTokens
)
.
reduce
((
acc
,
token
)
=>
(
token
?.
address
?
{
...
acc
,
[
token
.
address
]:
token
}
:
acc
),
{})
)
)
.
toPromise
()
.
then
(
onSuccess
)
},
[
duration
,
environment
]
[
tokens
]
)
const
loadMoreTokens
=
useCallback
(()
=>
setPage
(
page
+
1
),
[
page
])
const
loadMoreTokens
=
useCallback
(()
=>
{
setLoading
(
true
)
const
contracts
=
prefetchedSelectedTokens
.
slice
(
page
*
PAGE_SIZE
,
(
page
+
1
)
*
PAGE_SIZE
).
map
(
toContractInput
)
loadTokens
(
contracts
,
(
data
)
=>
{
if
(
data
?.
tokens
)
{
setTokens
([...(
tokens
??
[]),
...
data
.
tokens
])
setLoading
(
false
)
setPage
(
page
+
1
)
}
})
},
[
loadTokens
,
page
,
prefetchedSelectedTokens
,
tokens
])
// TopTokens should ideally be fetched with usePaginationFragment. The backend does not current support graphql cursors;
// in the meantime, fetchQuery is used, as other relay hooks do not allow the refreshing and lazy loading we need
const
prefetchedSelectedTokens
=
useFilteredTokens
(
useSortedTokens
(
prefetchedData
.
topTokens
))
const
contracts
:
ContractInput
[]
=
useMemo
(
()
=>
prefetchedSelectedTokens
.
slice
(
page
*
PAGE_SIZE
,
(
page
+
1
)
*
PAGE_SIZE
).
map
(
toContractInput
),
[
page
,
prefetchedSelectedTokens
]
)
// Reset count when filters are changed
useEffect
(()
=>
{
setLoading
(
true
)
setTokens
([])
const
contracts
=
prefetchedSelectedTokens
.
slice
(
0
,
PAGE_SIZE
).
map
(
toContractInput
)
loadTokens
(
contracts
,
(
data
)
=>
{
if
(
data
?.
tokens
)
{
// @ts-ignore prevent typescript from complaining about readonly data
setTokens
(
data
.
tokens
)
const
subscription
=
fetchQuery
<
TopTokens_TokensQuery
>
(
environment
,
tokensQuery
,
{
contracts
,
duration
},
{
fetchPolicy
:
'
store-or-network
'
}
).
subscribe
({
start
()
{
setLoading
(
true
)
},
complete
()
{
setLoading
(
false
)
setPage
(
1
)
}
},
next
(
data
)
{
appendTokens
(
data
.
tokens
as
TopToken
[])
},
})
},
[
loadTokens
,
prefetchedSelectedTokens
])
return
subscription
.
unsubscribe
},
[
appendTokens
,
contracts
,
duration
,
environment
])
return
{
loading
,
tokens
,
loadMoreTokens
}
return
{
loading
,
tokens
:
useFilteredTokens
(
useSortedTokens
(
tokens
))
as
TopToken
[]
,
loadMoreTokens
}
}
export
const
tokensQuery
=
graphql
`
...
...
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