Skip to content

Commit 1a47def

Browse files
authored
Merge pull request #11491 from kodadot/refactor--asyncdata-already-mounted-part-3
fix: asyncdata already mounted part 3
2 parents 0564f4a + 7ecb084 commit 1a47def

File tree

18 files changed

+215
-197
lines changed

18 files changed

+215
-197
lines changed
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
11
<template>
22
<div ref="carouselDrop">
33
<CarouselIndex
4+
v-if="data.value.length"
45
data-testid="generative-activity"
56
:title="$t('general.generativeArt')"
6-
:nfts="nfts.value"
7+
:nfts="data.value"
78
action-type="pagination"
89
/>
910
</div>
1011
</template>
1112

1213
<script lang="ts" setup>
1314
import { useElementVisibility } from '@vueuse/core'
14-
import { useCarouselGenerativeNftEvents } from './utils/useCarouselEvents'
15+
import { unionBy } from 'lodash'
16+
import { useEvents, sortNfts } from './utils/useCarouselEvents'
1517
import { useProfileOnboardingStore } from '@/stores/profileOnboarding'
1618
17-
const nfts = useCarouselGenerativeNftEvents()
19+
const props = defineProps<{
20+
collectionIds: string[]
21+
}>()
22+
1823
const carouselDrop = ref()
1924
2025
watch([useElementVisibility(carouselDrop)], ([isVisible]) => {
2126
if (isVisible) {
2227
useProfileOnboardingStore().setCarouselVisited()
2328
}
2429
})
30+
31+
const LIMIT = 12
32+
const { data: ahpEventsNewestList } = useEvents('ahp', 'newestList', LIMIT, props.collectionIds)
33+
const { data: ahpEventsLatestSales } = useEvents('ahp', 'latestSales', LIMIT, props.collectionIds)
34+
35+
const data = computed(() => {
36+
const ahpEvents = [...ahpEventsNewestList.value, ...ahpEventsLatestSales.value]
37+
38+
return sortNfts(unionBy(ahpEvents, 'id')).nfts
39+
})
2540
</script>

components/carousel/utils/useCarouselEvents.ts

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import type { Prefix } from '@kodadot1/static'
2-
import unionBy from 'lodash/unionBy'
2+
import type { ResultOf } from 'gql.tada'
33
import type { CarouselNFT } from '@/components/base/types'
44
import type { NFTWithMetadata } from '@/composables/useNft'
55
import { formatNFT } from '@/utils/carousel'
6-
import { AHK_GENERATIVE_DROPS } from '@/utils/drop'
7-
import { getDrops } from '@/services/fxart'
86
import latestEvents from '@/queries/subsquid/general/latestEvents'
97

108
interface Types {
@@ -27,6 +25,7 @@ const fetchLatestEvents = async (chain, type, where = {}, limit = 20) => {
2725
...nftEventVariables[type],
2826
...where,
2927
},
28+
chain,
3029
},
3130
context: {
3231
endpoint: chain,
@@ -52,7 +51,7 @@ const createEventQuery = (
5251

5352
const LIMIT_PER_COLLECTION = 3
5453

55-
const useEvents = (chain, type, limit = 10, collectionIds = []) => {
54+
export const useEvents = (chain, type, limit = 10, collectionIds) => {
5655
const collections = reactive({})
5756
const items = ref<
5857
(NFTWithMetadata & {
@@ -79,7 +78,7 @@ const useEvents = (chain, type, limit = 10, collectionIds = []) => {
7978
collectionIds,
8079
)
8180

82-
const data = ref()
81+
const data = ref<ResultOf<typeof latestEvents>>()
8382
fetchLatestEvents(chain, type, where).then((result) => {
8483
data.value = result.data
8584
})
@@ -150,7 +149,7 @@ export const flattenNFT = (data, chain) => {
150149

151150
const sortNftByTime = data => data.sort((a, b) => b.unixTime - a.unixTime)
152151

153-
const sortNfts = (data) => {
152+
export const sortNfts = (data) => {
154153
const nfts = ref<CarouselNFT[]>([])
155154
nfts.value = sortNftByTime(data)
156155

@@ -180,56 +179,3 @@ export const useCarouselNftEvents = ({ type }: Types) => {
180179

181180
return computed(() => items.value.nfts)
182181
}
183-
184-
const GENERATIVE_CONFIG: Partial<
185-
Record<Prefix, { limit: number, collections: string[] }>
186-
> = {
187-
ahp: {
188-
limit: 12,
189-
collections: [],
190-
},
191-
ahk: {
192-
limit: 3,
193-
collections: AHK_GENERATIVE_DROPS,
194-
},
195-
}
196-
197-
export const useCarouselGenerativeNftEvents = () => {
198-
const nfts = ref<CarouselNFT[]>([])
199-
const eventType = ['newestList', 'latestSales']
200-
const dropsAhp = computedAsync(async () => {
201-
return await getDrops({
202-
limit: 12,
203-
active: [true],
204-
chain: ['ahp'],
205-
})
206-
})
207-
208-
const eventsDataRefs = Object.keys(GENERATIVE_CONFIG).map((chain) => {
209-
let collections = GENERATIVE_CONFIG[chain].collections
210-
211-
if (isProduction && chain === 'ahk') {
212-
return []
213-
}
214-
215-
if (chain === 'ahp' && dropsAhp.value?.length) {
216-
collections = dropsAhp.value.map(drop => drop.collection)
217-
}
218-
219-
return eventType.map((eventName) => {
220-
const { data } = useEvents(
221-
chain,
222-
eventName,
223-
GENERATIVE_CONFIG[chain].limit,
224-
collections,
225-
)
226-
return data
227-
})
228-
})
229-
230-
watchEffect(() => {
231-
nfts.value = eventsDataRefs.flat().flatMap(dataRef => dataRef.value)
232-
})
233-
234-
return computed(() => sortNfts(unionBy(nfts.value, 'id')).nfts)
235-
}

components/collection/utils/useCollectionDetails.ts

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { useQuery } from '@tanstack/vue-query'
2+
import type { ResultOf } from 'gql.tada'
23
import type { Stats } from './types'
34
import { getVolume } from '@/utils/math'
4-
import type { NFT, NFTMetadata } from '@/types'
5-
import type { NFTListSold } from '@/components/identity/utils/useIdentity'
5+
import type { NFTMetadata } from '@/types'
66
import { processSingleMetadata } from '@/utils/cachingStrategy'
77
import collectionBuyEventStatsById from '@/queries/subsquid/general/collectionBuyEventStatsById.query'
8+
import collectionStatsById from '@/queries/subsquid/general/collectionStatsById'
9+
import nftListSoldByCollection from '~/queries/subsquid/general/nftListSoldByCollection'
810

911
export const useCollectionDetails = ({
1012
collectionId,
@@ -15,13 +17,26 @@ export const useCollectionDetails = ({
1517
id: collectionId.value,
1618
}))
1719

18-
const { data, refetch } = useGraphql({
19-
queryPrefix: 'subsquid',
20-
queryName: 'collectionStatsById',
21-
variables: variables.value,
22-
})
20+
const { $apolloClient } = useNuxtApp()
21+
const { urlPrefix } = usePrefix()
22+
23+
const data = ref<ResultOf<typeof collectionStatsById>>()
2324
const stats = ref<Stats>({})
2425

26+
const fetchStats = () => {
27+
$apolloClient.query({
28+
query: collectionStatsById,
29+
variables: variables.value,
30+
context: {
31+
endpoint: urlPrefix.value,
32+
},
33+
}).then((res) => {
34+
data.value = res.data
35+
})
36+
}
37+
38+
fetchStats()
39+
2540
watch(data, () => {
2641
if (data.value?.stats) {
2742
const uniqueOwnerCount = [
@@ -33,12 +48,12 @@ export const useCollectionDetails = ({
3348
const listedNfts = data.value.stats.listed
3449

3550
stats.value = {
36-
maxSupply: data.value.stats.max,
51+
maxSupply: data.value.stats.max ?? undefined,
3752
listedCount: data.value.stats.listed.length,
3853
collectionLength,
3954
collectionFloorPrice:
4055
listedNfts.length > 0
41-
? Math.min(...listedNfts.map(item => parseInt(item.price)))
56+
? Math.min(...listedNfts.map(item => parseInt(item.price ?? '0')))
4257
: undefined,
4358
uniqueOwners: uniqueOwnerCount,
4459
uniqueOwnersPercent: `${(
@@ -52,11 +67,11 @@ export const useCollectionDetails = ({
5267
}
5368
})
5469

55-
watch(variables, () => refetch(variables.value))
70+
watch(variables, () => fetchStats())
5671

5772
return {
5873
stats,
59-
refetch,
74+
refetch: fetchStats,
6075
}
6176
}
6277

@@ -78,27 +93,27 @@ export const useBuyEvents = ({ collectionId }) => {
7893
}
7994

8095
export function useCollectionSoldData({ address, collectionId }) {
81-
const nftEntities = ref<NFT[]>([])
82-
const { data } = useGraphql({
83-
queryName: 'nftListSoldByCollection',
96+
const data = ref<ResultOf<typeof nftListSoldByCollection>>()
97+
98+
const { $apolloClient } = useNuxtApp()
99+
const { urlPrefix } = usePrefix()
100+
101+
$apolloClient.query({
102+
query: nftListSoldByCollection,
84103
variables: {
85104
account: address,
86105
limit: 3,
87106
orderBy: 'price_DESC',
88107
collectionId,
89-
where: {
90-
collection: { id_eq: collectionId },
91-
},
92108
},
109+
context: {
110+
endpoint: urlPrefix.value,
111+
},
112+
}).then((res) => {
113+
data.value = res.data
93114
})
94115

95-
watch(data as unknown as NFTListSold, (list) => {
96-
if (list?.nftEntities?.length) {
97-
nftEntities.value = list.nftEntities
98-
}
99-
})
100-
101-
return { nftEntities }
116+
return { nftEntities: computed(() => data.value?.nftEntities?.length ? data.value.nftEntities : []) }
102117
}
103118

104119
export const useCollectionMinimal = ({

components/identity/utils/useIdentity.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

components/identity/utils/useIdentityStats.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { isAfter, subHours } from 'date-fns'
2-
2+
import type { ResultOf } from 'gql.tada'
33
import { useIdentityMintStore } from '@/stores/identityMint'
44
import { formatToNow } from '@/utils/format/time'
55
import buyEventByProfile from '@/queries/subsquid/general/buyEventByProfile.query'
6+
import userStatsByAccount from '~/queries/subsquid/general/userStatsByAccount'
67

78
const useLastBought = ({ address }: { address: Ref<string> }) => {
89
const lastBoughtDate = ref(new Date())
@@ -66,11 +67,26 @@ export default function useIdentityStats({
6667
const firstMintDate = ref(new Date())
6768

6869
const { lastBoughtDate } = useLastBought({ address })
69-
const { data: stats, loading } = useGraphql({
70-
queryName: 'userStatsByAccount',
70+
71+
const { $apolloClient, $consola } = useNuxtApp()
72+
const { urlPrefix } = usePrefix()
73+
const stats = ref<ResultOf<typeof userStatsByAccount>>()
74+
const loading = ref(true)
75+
76+
$apolloClient.query({
77+
query: userStatsByAccount,
7178
variables: {
7279
account: address.value,
7380
},
81+
context: {
82+
endpoint: urlPrefix.value,
83+
},
84+
}).then((res) => {
85+
stats.value = res.data
86+
loading.value = false
87+
}).catch(() => {
88+
$consola.error('Error fetching identity stats')
89+
loading.value = false
7490
})
7591

7692
const startedMinting = computed(() => formatToNow(firstMintDate.value))

components/landing/LandingPage.vue

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
<section class="py-8 instance">
1919
<div class="container-fluid">
2020
<!-- generative -->
21-
<LazyCarouselTypeGenerative />
21+
<LazyCarouselTypeGenerative
22+
v-if="dropsAhp.length"
23+
:collection-ids="dropsAhp"
24+
/>
2225
</div>
2326
</section>
2427

@@ -39,6 +42,7 @@
3942
import type { Prefix } from '@kodadot1/static'
4043
import { openProfileCreateModal } from '@/components/profile/create/openProfileModal'
4144
import { useProfileOnboardingStore } from '@/stores/profileOnboarding'
45+
import { getDrops } from '@/services/fxart'
4246
4347
const hiddenCarrouselPrefixes: Prefix[] = ['dot']
4448
const forbiddenPrefixesForTopCollections: Prefix[] = ['ksm', 'dot']
@@ -65,4 +69,11 @@ watchEffect(() => {
6569
}, 2000)
6670
}
6771
})
72+
73+
const drops = await getDrops({
74+
limit: 12,
75+
active: [true],
76+
chain: ['ahp'],
77+
})
78+
const dropsAhp = drops.map(drop => drop.collection)
6879
</script>

components/profile/ProfileActivitySummery.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const statsRows = computed(() => [
7373
},
7474
{
7575
label: 'activity.estimatedValue',
76-
value: stats.value.listedValue,
76+
value: stats.value.listedValue.toString(),
7777
component: CommonTokenMoney,
7878
},
7979
{

0 commit comments

Comments
 (0)