Skip to content

Commit 7e1400d

Browse files
authored
feat: use tanstack query for changelog tab (#5175)
* use tanstack query for changelog tab * fix query key
1 parent 7595e77 commit 7e1400d

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

apps/frontend/src/pages/[type]/[id]/changelog.vue

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/>
1616
</div>
1717
<div class="card changelog-wrapper">
18-
<template v-if="paginatedVersions">
18+
<template v-if="paginatedVersions && !isLoadingVersions">
1919
<div v-for="version in paginatedVersions" :key="version.id" class="changelog-item">
2020
<div
2121
:class="`changelog-bar ${version.version_type} ${version.duplicate ? 'duplicate' : ''}`"
@@ -78,6 +78,7 @@ import { DownloadIcon, SpinnerIcon } from '@modrinth/assets'
7878
import { injectModrinthClient, Pagination } from '@modrinth/ui'
7979
import VersionFilterControl from '@modrinth/ui/src/components/version/VersionFilterControl.vue'
8080
import { renderHighlightedString } from '@modrinth/utils'
81+
import { useQuery } from '@tanstack/vue-query'
8182
8283
const props = defineProps({
8384
project: {
@@ -135,32 +136,44 @@ const filteredVersions = computed(() => {
135136
136137
const { labrinth } = injectModrinthClient()
137138
138-
const paginatedVersions = ref(null)
139-
140-
watch(
141-
[filteredVersions, currentPage],
142-
async ([filtered, page]) => {
143-
const paginated = filtered.slice((page - 1) * 20, page * 20)
139+
const paginatedVersionIds = computed(() => {
140+
const page = currentPage.value
141+
const paginated = filteredVersions.value.slice((page - 1) * 20, page * 20)
142+
return paginated.map((v) => v.id)
143+
})
144144
145-
const ids = paginated.map((v) => v.id)
145+
const { data: fetchedVersions, isLoading: isLoadingVersions } = useQuery({
146+
queryKey: computed(() => ['versions', { ids: paginatedVersionIds.value.toSorted() }]),
147+
queryFn: async () => {
148+
const ids = paginatedVersionIds.value
149+
if (ids.length === 0) return []
146150
const versions = await labrinth.versions_v3.getVersions(toRaw(ids))
147151
versions.sort((a, b) => ids.indexOf(a.id) - ids.indexOf(b.id))
148-
149-
paginatedVersions.value = paginated.map((version, index) => {
150-
const fullVersion = versions.find((v) => v.id === version.id)
151-
if (fullVersion)
152-
return {
153-
...version,
154-
duplicate:
155-
!!fullVersion.changelog &&
156-
versions.slice(index + 1).some((v) => v.changelog === fullVersion.changelog),
157-
changelog: fullVersion.changelog,
158-
}
159-
else return version
160-
})
152+
return versions
161153
},
162-
{ immediate: true },
163-
)
154+
enabled: computed(() => paginatedVersionIds.value.length > 0),
155+
})
156+
157+
const paginatedVersions = computed(() => {
158+
if (!fetchedVersions.value) return null
159+
160+
const page = currentPage.value
161+
const paginated = filteredVersions.value.slice((page - 1) * 20, page * 20)
162+
const versions = fetchedVersions.value
163+
164+
return paginated.map((version, index) => {
165+
const fullVersion = versions.find((v) => v.id === version.id)
166+
if (fullVersion)
167+
return {
168+
...version,
169+
duplicate:
170+
!!fullVersion.changelog &&
171+
versions.slice(index + 1).some((v) => v.changelog === fullVersion.changelog),
172+
changelog: fullVersion.changelog,
173+
}
174+
else return version
175+
})
176+
})
164177
165178
function switchPage(page) {
166179
currentPage.value = page

0 commit comments

Comments
 (0)