You've already forked AstralRinth
forked from didirus/AstralRinth
feat: use tanstack query for changelog tab (#5175)
* use tanstack query for changelog tab * fix query key
This commit is contained in:
@@ -15,7 +15,7 @@
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div class="card changelog-wrapper">
|
<div class="card changelog-wrapper">
|
||||||
<template v-if="paginatedVersions">
|
<template v-if="paginatedVersions && !isLoadingVersions">
|
||||||
<div v-for="version in paginatedVersions" :key="version.id" class="changelog-item">
|
<div v-for="version in paginatedVersions" :key="version.id" class="changelog-item">
|
||||||
<div
|
<div
|
||||||
:class="`changelog-bar ${version.version_type} ${version.duplicate ? 'duplicate' : ''}`"
|
:class="`changelog-bar ${version.version_type} ${version.duplicate ? 'duplicate' : ''}`"
|
||||||
@@ -78,6 +78,7 @@ import { DownloadIcon, SpinnerIcon } from '@modrinth/assets'
|
|||||||
import { injectModrinthClient, Pagination } from '@modrinth/ui'
|
import { injectModrinthClient, Pagination } from '@modrinth/ui'
|
||||||
import VersionFilterControl from '@modrinth/ui/src/components/version/VersionFilterControl.vue'
|
import VersionFilterControl from '@modrinth/ui/src/components/version/VersionFilterControl.vue'
|
||||||
import { renderHighlightedString } from '@modrinth/utils'
|
import { renderHighlightedString } from '@modrinth/utils'
|
||||||
|
import { useQuery } from '@tanstack/vue-query'
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
project: {
|
project: {
|
||||||
@@ -135,32 +136,44 @@ const filteredVersions = computed(() => {
|
|||||||
|
|
||||||
const { labrinth } = injectModrinthClient()
|
const { labrinth } = injectModrinthClient()
|
||||||
|
|
||||||
const paginatedVersions = ref(null)
|
const paginatedVersionIds = computed(() => {
|
||||||
|
const page = currentPage.value
|
||||||
|
const paginated = filteredVersions.value.slice((page - 1) * 20, page * 20)
|
||||||
|
return paginated.map((v) => v.id)
|
||||||
|
})
|
||||||
|
|
||||||
watch(
|
const { data: fetchedVersions, isLoading: isLoadingVersions } = useQuery({
|
||||||
[filteredVersions, currentPage],
|
queryKey: computed(() => ['versions', { ids: paginatedVersionIds.value.toSorted() }]),
|
||||||
async ([filtered, page]) => {
|
queryFn: async () => {
|
||||||
const paginated = filtered.slice((page - 1) * 20, page * 20)
|
const ids = paginatedVersionIds.value
|
||||||
|
if (ids.length === 0) return []
|
||||||
const ids = paginated.map((v) => v.id)
|
|
||||||
const versions = await labrinth.versions_v3.getVersions(toRaw(ids))
|
const versions = await labrinth.versions_v3.getVersions(toRaw(ids))
|
||||||
versions.sort((a, b) => ids.indexOf(a.id) - ids.indexOf(b.id))
|
versions.sort((a, b) => ids.indexOf(a.id) - ids.indexOf(b.id))
|
||||||
|
return versions
|
||||||
paginatedVersions.value = paginated.map((version, index) => {
|
|
||||||
const fullVersion = versions.find((v) => v.id === version.id)
|
|
||||||
if (fullVersion)
|
|
||||||
return {
|
|
||||||
...version,
|
|
||||||
duplicate:
|
|
||||||
!!fullVersion.changelog &&
|
|
||||||
versions.slice(index + 1).some((v) => v.changelog === fullVersion.changelog),
|
|
||||||
changelog: fullVersion.changelog,
|
|
||||||
}
|
|
||||||
else return version
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
{ immediate: true },
|
enabled: computed(() => paginatedVersionIds.value.length > 0),
|
||||||
)
|
})
|
||||||
|
|
||||||
|
const paginatedVersions = computed(() => {
|
||||||
|
if (!fetchedVersions.value) return null
|
||||||
|
|
||||||
|
const page = currentPage.value
|
||||||
|
const paginated = filteredVersions.value.slice((page - 1) * 20, page * 20)
|
||||||
|
const versions = fetchedVersions.value
|
||||||
|
|
||||||
|
return paginated.map((version, index) => {
|
||||||
|
const fullVersion = versions.find((v) => v.id === version.id)
|
||||||
|
if (fullVersion)
|
||||||
|
return {
|
||||||
|
...version,
|
||||||
|
duplicate:
|
||||||
|
!!fullVersion.changelog &&
|
||||||
|
versions.slice(index + 1).some((v) => v.changelog === fullVersion.changelog),
|
||||||
|
changelog: fullVersion.changelog,
|
||||||
|
}
|
||||||
|
else return version
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
function switchPage(page) {
|
function switchPage(page) {
|
||||||
currentPage.value = page
|
currentPage.value = page
|
||||||
|
|||||||
Reference in New Issue
Block a user