-
Notifications
You must be signed in to change notification settings - Fork 5k
/
ProfilePage.tsx
350 lines (331 loc) · 11.1 KB
/
ProfilePage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import { useNftBalance } from 'graphql/data/nft/NftBalance'
import { AnimatedBox, Box } from 'nft/components/Box'
import { LoadingAssets } from 'nft/components/collection/CollectionAssetLoading'
import { assetList } from 'nft/components/collection/CollectionNfts.css'
import { FilterButton } from 'nft/components/collection/FilterButton'
import { ClearAllButton } from 'nft/components/collection/shared'
import { Column, Row } from 'nft/components/Flex'
import { CrossIcon } from 'nft/components/icons'
import { FilterSidebar } from 'nft/components/profile/view/FilterSidebar'
import { subhead } from 'nft/css/common.css'
import {
useBag,
useFiltersExpanded,
useIsMobile,
useSellAsset,
useWalletBalance,
useWalletCollections,
} from 'nft/hooks'
import { ScreenBreakpointsPaddings } from 'nft/pages/collection/index.css'
import { OSCollectionsFetcher } from 'nft/queries'
import { WalletCollection } from 'nft/types'
import { Dispatch, SetStateAction, Suspense, useCallback, useEffect, useMemo, useState } from 'react'
import InfiniteScroll from 'react-infinite-scroll-component'
import { useInfiniteQuery } from 'react-query'
import { easings, useSpring } from 'react-spring'
import styled from 'styled-components'
import { shallow } from 'zustand/shallow'
import { EmptyWalletModule } from './EmptyWalletContent'
import * as styles from './ProfilePage.css'
import { ProfileBodyLoadingSkeleton } from './ProfilePageLoadingSkeleton'
import { ViewMyNftsAsset } from './ViewMyNftsAsset'
const ProfilePageColumn = styled(Column)`
${ScreenBreakpointsPaddings}
`
const ProfileHeader = styled.div`
font-size: 28px;
font-weight: 500;
line-height: 38px;
padding-bottom: 16px;
margin-bottom: 8px;
border-bottom: 1px solid ${({ theme }) => theme.backgroundOutline};
@media only screen and (max-width: ${({ theme }) => `${theme.breakpoint.sm}px`}) {
font-size: 20px;
line-height: 28px;
margin-bottom: 0px;
}
`
const EmptyStateContainer = styled.div`
margin-top: 164px;
`
export const DEFAULT_WALLET_ASSET_QUERY_AMOUNT = 25
export const WALLET_COLLECTIONS_PAGINATION_LIMIT = 300
const FILTER_SIDEBAR_WIDTH = 300
const PADDING = 16
export const ProfilePage = () => {
const { address } = useWalletBalance()
const walletCollections = useWalletCollections((state) => state.walletCollections)
const setWalletCollections = useWalletCollections((state) => state.setWalletCollections)
const { resetSellAssets } = useSellAsset(
({ reset }) => ({
resetSellAssets: reset,
}),
shallow
)
const sellAssets = useSellAsset((state) => state.sellAssets)
const toggleBag = useBag((state) => state.toggleBag)
const [isFiltersExpanded, setFiltersExpanded] = useFiltersExpanded()
const isMobile = useIsMobile()
const getOwnerCollections = async ({ pageParam = 0 }) => {
const res = await OSCollectionsFetcher({
params: {
asset_owner: address,
offset: `${pageParam * WALLET_COLLECTIONS_PAGINATION_LIMIT}`,
limit: `${WALLET_COLLECTIONS_PAGINATION_LIMIT}`,
},
})
return {
data: res,
nextPage: pageParam + 1,
}
}
const {
data: ownerCollectionsData,
fetchNextPage,
hasNextPage,
isFetchingNextPage,
isSuccess,
} = useInfiniteQuery(['ownerCollections', { address }], getOwnerCollections, {
getNextPageParam: (lastGroup) => (lastGroup.data.length === 0 ? undefined : lastGroup.nextPage),
refetchInterval: 15000,
refetchIntervalInBackground: false,
refetchOnWindowFocus: false,
refetchOnMount: false,
})
const ownerCollections = useMemo(
() => (isSuccess ? ownerCollectionsData?.pages.map((page) => page.data).flat() : null),
[isSuccess, ownerCollectionsData]
)
useEffect(() => {
ownerCollections && setWalletCollections(ownerCollections)
}, [ownerCollections, setWalletCollections])
return (
<ProfilePageColumn width="full" paddingTop={{ sm: `${PADDING}`, md: '40' }}>
<>
<ProfileHeader>My NFTs</ProfileHeader>
<Row alignItems="flex-start" position="relative">
<FilterSidebar
fetchNextPage={fetchNextPage}
hasNextPage={hasNextPage}
isFetchingNextPage={isFetchingNextPage}
walletCollections={walletCollections}
/>
{(!isMobile || !isFiltersExpanded) && (
<Suspense fallback={<ProfileBodyLoadingSkeleton />}>
<ProfilePageNfts
walletCollections={walletCollections}
isFiltersExpanded={isFiltersExpanded}
setFiltersExpanded={setFiltersExpanded}
/>
</Suspense>
)}
</Row>
</>
{sellAssets.length > 0 && (
<Row
display={{ sm: 'flex', md: 'none' }}
position="fixed"
left="16"
height="56"
borderRadius="12"
paddingX="16"
paddingY="12"
background="backgroundSurface"
borderStyle="solid"
borderColor="backgroundOutline"
borderWidth="1px"
style={{ bottom: '68px', width: 'calc(100% - 32px)', lineHeight: '24px' }}
className={subhead}
>
{sellAssets.length} NFT{sellAssets.length === 1 ? '' : 's'}
<Box
fontWeight="semibold"
fontSize="14"
cursor="pointer"
color="textSecondary"
marginRight="20"
marginLeft="auto"
onClick={resetSellAssets}
lineHeight="16"
>
Clear
</Box>
<Box
color="white"
marginRight="0"
fontWeight="medium"
fontSize="14"
cursor="pointer"
backgroundColor="accentAction"
onClick={toggleBag}
lineHeight="16"
borderRadius="12"
paddingY="8"
paddingX="28"
>
List for sale
</Box>
</Row>
)}
</ProfilePageColumn>
)
}
const ProfilePageNfts = ({
walletCollections,
isFiltersExpanded,
setFiltersExpanded,
}: {
walletCollections: WalletCollection[]
isFiltersExpanded: boolean
setFiltersExpanded: (filtersExpanded: boolean) => void
}) => {
const { address } = useWalletBalance()
const setCollectionFilters = useWalletCollections((state) => state.setCollectionFilters)
const collectionFilters = useWalletCollections((state) => state.collectionFilters)
const clearCollectionFilters = useWalletCollections((state) => state.clearCollectionFilters)
const isBagExpanded = useBag((state) => state.bagExpanded)
const [currentTokenPlayingMedia, setCurrentTokenPlayingMedia] = useState<string | undefined>()
const isMobile = useIsMobile()
const sellAssets = useSellAsset((state) => state.sellAssets)
const {
walletAssets: ownerAssets,
loading,
hasNext,
loadMore,
} = useNftBalance(address, collectionFilters, [], DEFAULT_WALLET_ASSET_QUERY_AMOUNT)
const { gridX } = useSpring({
gridX: isFiltersExpanded ? FILTER_SIDEBAR_WIDTH : -PADDING,
config: {
duration: 250,
easing: easings.easeOutSine,
},
})
if (loading) return <ProfileBodyLoadingSkeleton />
return (
<Column width="full">
{ownerAssets?.length === 0 ? (
<EmptyStateContainer>
<EmptyWalletModule />
</EmptyStateContainer>
) : (
<AnimatedBox
flexShrink="0"
position={isMobile && isBagExpanded ? 'fixed' : 'static'}
style={{
transform: gridX.to(
(x) => `translate(${Number(x) - (!isMobile && isFiltersExpanded ? FILTER_SIDEBAR_WIDTH : -PADDING)}px)`
),
}}
paddingY="20"
>
<Row gap="8" flexWrap="nowrap" justifyContent="space-between">
<FilterButton
isMobile={isMobile}
isFiltersExpanded={isFiltersExpanded}
onClick={() => setFiltersExpanded(!isFiltersExpanded)}
/>
</Row>
<Row>
<CollectionFiltersRow
collections={walletCollections}
collectionFilters={collectionFilters}
setCollectionFilters={setCollectionFilters}
clearCollectionFilters={clearCollectionFilters}
/>
</Row>
<InfiniteScroll
next={loadMore}
hasMore={hasNext ?? false}
loader={
Boolean(hasNext && ownerAssets?.length) && <LoadingAssets count={DEFAULT_WALLET_ASSET_QUERY_AMOUNT} />
}
dataLength={ownerAssets?.length ?? 0}
className={ownerAssets?.length ? assetList : undefined}
style={{ overflow: 'unset' }}
>
{ownerAssets?.length
? ownerAssets.map((asset, index) => (
<div key={index}>
<ViewMyNftsAsset
asset={asset}
mediaShouldBePlaying={asset.tokenId === currentTokenPlayingMedia}
setCurrentTokenPlayingMedia={setCurrentTokenPlayingMedia}
hideDetails={sellAssets.length > 0}
/>
</div>
))
: null}
</InfiniteScroll>
</AnimatedBox>
)}
</Column>
)
}
const CollectionFiltersRow = ({
collections,
collectionFilters,
setCollectionFilters,
clearCollectionFilters,
}: {
collections: WalletCollection[]
collectionFilters: Array<string>
setCollectionFilters: (address: string) => void
clearCollectionFilters: Dispatch<SetStateAction<void>>
}) => {
const getCollection = (collectionAddress: string) => {
return collections?.find((collection) => collection.address === collectionAddress)
}
const handleClearAllClick = useCallback(() => clearCollectionFilters(), [clearCollectionFilters])
return (
<Row paddingY="18" gap="8" flexWrap="wrap">
{Boolean(collectionFilters?.length) &&
collectionFilters.map((collectionAddress, index) => (
<CollectionFilterItem
collection={getCollection(collectionAddress)}
key={`CollectionFilterItem-${collectionAddress}-${index}`}
setCollectionFilters={setCollectionFilters}
/>
))}
{Boolean(collectionFilters?.length) && <ClearAllButton onClick={handleClearAllClick}>Clear all</ClearAllButton>}
</Row>
)
}
const CollectionFilterItem = ({
collection,
setCollectionFilters,
}: {
collection?: WalletCollection
setCollectionFilters: (address: string) => void
}) => {
if (!collection) return null
return (
<Row
justifyContent="center"
paddingTop="6"
paddingRight="6"
paddingBottom="6"
paddingLeft="12"
borderRadius="8"
background="backgroundOutline"
fontSize="14"
>
<Box as="img" borderRadius="round" width="20" height="20" src={collection.image} />
<Box marginLeft="6" className={styles.collectionFilterBubbleText}>
{collection?.name}
</Box>
<Box
color="textSecondary"
background="none"
height="28"
width="28"
padding="0"
as="button"
border="none"
cursor="pointer"
onClick={() => setCollectionFilters(collection.address)}
>
<CrossIcon />
</Box>
</Row>
)
}