Skip to content

Commit

Permalink
fix: Adjust search logic to avoid the limit is negative
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Jan 10, 2024
1 parent ea261bf commit 2a3d706
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 39 deletions.
20 changes: 7 additions & 13 deletions xmcl-keystone-ui/src/composables/curseforgeSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export function useCurseforgeSearch<T extends ProjectEntry<any>>(classId: number
if (keyword.value || curseforgeCategory.value) {
try {
curseforgeError.value = undefined
const remain = append && curseforge.value ? curseforge.value.pagination.totalCount - offset : Number.MAX_SAFE_INTEGER
const modLoaderTypes = getCursforgeModLoadersFromString(modLoaderFilters.value)
let modLoaderType = undefined as FileModLoaderType | undefined
if (modLoaderTypes.length === 1) {
Expand All @@ -45,7 +44,7 @@ export function useCurseforgeSearch<T extends ProjectEntry<any>>(classId: number
gameVersion: runtime.value.minecraft,
searchFilter: keyword.value,
categoryId: curseforgeCategory.value,
pageSize: append ? Math.min(20, remain) : 20,
pageSize: 20,
index: offset,
})

Expand Down Expand Up @@ -75,15 +74,15 @@ export function useCurseforgeSearch<T extends ProjectEntry<any>>(classId: number
})

const loadMoreCurseforge = async () => {
if (/* isActive.value && */canCurseforgeLoadMore.value) {
curseforgePage.value += 1
loadingCurseforge.value = true
await processCurseforge(curseforgePage.value * 20, true)
}
if (!curseforge.value) return
const hasMore = curseforge.value.pagination.totalCount > (curseforge.value.pagination.index + curseforge.value.pagination.resultCount)
if (!hasMore) return
curseforgePage.value += 1
loadingCurseforge.value = true
await processCurseforge(curseforgePage.value * 20, true)
}

const onSearch = async () => {
// if (!isActive.value) return
loadingCurseforge.value = true
curseforgePage.value = 0
processCurseforge(curseforgePage.value * 20, false)
Expand All @@ -93,11 +92,6 @@ export function useCurseforgeSearch<T extends ProjectEntry<any>>(classId: number
watch(modLoaderFilters, onSearch, { deep: true })
watch(curseforgeCategory, onSearch, { deep: true })
watch(sort, onSearch)
// watch(isActive, (v) => {
// if (v) {
// onSearch()
// }
// })

const mods = computed(() => {
const cf = curseforge.value
Expand Down
6 changes: 2 additions & 4 deletions xmcl-keystone-ui/src/composables/modSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ export function useModsSearch(runtime: Ref<InstanceData['runtime']>, instanceMod
modLoaderFilters.value = items
}, { immediate: true, deep: true })

const { loadMoreModrinth, loadingModrinth, canModrinthLoadMore, modrinth, modrinthError } = useModrinthSearch('mod', keyword, modLoaderFilters, modrinthCategories, modrinthSort, runtime)
const { loadMoreCurseforge, loadingCurseforge, canCurseforgeLoadMore, curseforge, curseforgeError } = useCurseforgeSearch<ProjectEntry<ModFile>>(CurseforgeBuiltinClassId.mod, keyword, modLoaderFilters, curseforgeCategory, curseforgeSort, runtime)
const { loadMoreModrinth, loadingModrinth, modrinth, modrinthError } = useModrinthSearch('mod', keyword, modLoaderFilters, modrinthCategories, modrinthSort, runtime)
const { loadMoreCurseforge, loadingCurseforge, curseforge, curseforgeError } = useCurseforgeSearch<ProjectEntry<ModFile>>(CurseforgeBuiltinClassId.mod, keyword, modLoaderFilters, curseforgeCategory, curseforgeSort, runtime)
const { cached: cachedMods, instances, loadingCached } = useLocalModsSearch(keyword, modLoaderFilters, runtime, instanceMods)
const loading = computed(() => loadingModrinth.value || loadingCurseforge.value || loadingCached.value)

Expand All @@ -228,8 +228,6 @@ export function useModsSearch(runtime: Ref<InstanceData['runtime']>, instanceMod
modrinthCategories,
loadMoreCurseforge,
loadMoreModrinth,
canCurseforgeLoadMore,
canModrinthLoadMore,
modrinthError,
loadingModrinth,
curseforgeError,
Expand Down
23 changes: 7 additions & 16 deletions xmcl-keystone-ui/src/composables/modrinthSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ export function useModrinthSearch<T extends ProjectEntry<any>>(projectType: stri
facets.push('[' + activeCategories.value.map(m => `"categories:${m}"`).join(', ') + ']')
}
const index = sort.value
const remain = append && modrinth.value ? modrinth.value.total_hits - offset : Number.MAX_SAFE_INTEGER
const result = await clientModrinthV2.searchProjects({
query: keyword.value,
facets: '[' + facets.join(',') + ']',
index,
offset,
limit: append ? Math.min(remain, 20) : 20,
limit: 20,
})
if (!append || !modrinth.value) {
modrinth.value = result
Expand All @@ -56,15 +55,13 @@ export function useModrinthSearch<T extends ProjectEntry<any>>(projectType: stri
}
}, 1000)

const canModrinthLoadMore = computed(() => {
return modrinth.value && modrinth.value.total_hits > (modrinth.value.offset + modrinth.value.limit)
})
const loadMoreModrinth = async () => {
if (canModrinthLoadMore.value) {
modrinthPage.value += 1
loadingModrinth.value = true
await doSearch(modrinthPage.value * 20, true)
}
if (!modrinth.value) return
const hasMore = modrinth.value.total_hits >= (modrinth.value.offset + modrinth.value.limit)
if (!hasMore) return
modrinthPage.value += 1
loadingModrinth.value = true
await doSearch(modrinthPage.value * 20, true)
}

const onSearch = async () => {
Expand All @@ -77,11 +74,6 @@ export function useModrinthSearch<T extends ProjectEntry<any>>(projectType: stri
watch(keyword, onSearch)
watch(activeCategories, onSearch, { deep: true })
watch(sort, onSearch)
// watch(isActive, (v) => {
// if (v) {
// onSearch()
// }
// })

const result = computed(() => {
const modr = modrinth.value
Expand All @@ -105,6 +97,5 @@ export function useModrinthSearch<T extends ProjectEntry<any>>(projectType: stri
modrinthError,
loadMoreModrinth,
loadingModrinth,
canModrinthLoadMore,
}
}
6 changes: 2 additions & 4 deletions xmcl-keystone-ui/src/composables/resourcePackSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ export function useResourcePackSearch(runtime: Ref<InstanceData['runtime']>, _en
const isModrinthActive = ref(true)
const { sort, modrinthSort, curseforgeSort } = useMarketSort(0)

const { loadMoreModrinth, loadingModrinth, canModrinthLoadMore, modrinth, modrinthError } = useModrinthSearch<ResourcePackProject>('resourcepack', keyword, ref([]), modrinthCategories,
const { loadMoreModrinth, loadingModrinth, modrinth, modrinthError } = useModrinthSearch<ResourcePackProject>('resourcepack', keyword, ref([]), modrinthCategories,
modrinthSort, runtime)
const { loadMoreCurseforge, loadingCurseforge, canCurseforgeLoadMore, curseforge, curseforgeError } = useCurseforgeSearch(12, keyword, ref([]), curseforgeCategory,
const { loadMoreCurseforge, loadingCurseforge, curseforge, curseforgeError } = useCurseforgeSearch(12, keyword, ref([]), curseforgeCategory,
curseforgeSort, runtime)
const { enabled, disabled, all: filtered, loadingCached } = useLocalSearch(keyword, _enabled, _disabled)
const loading = computed(() => loadingModrinth.value || loadingCached.value || loadingCurseforge.value)
Expand Down Expand Up @@ -147,15 +147,13 @@ export function useResourcePackSearch(runtime: Ref<InstanceData['runtime']>, _en
modrinthCategories,

loadMoreModrinth,
canModrinthLoadMore,
modrinthError,
modrinth,
loadingModrinth,

curseforgeCategory,
loadMoreCurseforge,
loadingCurseforge,
canCurseforgeLoadMore,
curseforge,
curseforgeError,

Expand Down
3 changes: 1 addition & 2 deletions xmcl-keystone-ui/src/composables/shaderPackSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function useShaderPackSearch(runtime: Ref<InstanceData['runtime']>, shade
const isModrinthActive = ref(true)
const { sort, modrinthSort } = useMarketSort(0)

const { loadMoreModrinth, loadingModrinth, canModrinthLoadMore, modrinth, modrinthError } = useModrinthSearch<ShaderPackProject>('shader', keyword, shaderLoaderFilters, modrinthCategories, modrinthSort, runtime)
const { loadMoreModrinth, loadingModrinth, modrinth, modrinthError } = useModrinthSearch<ShaderPackProject>('shader', keyword, shaderLoaderFilters, modrinthCategories, modrinthSort, runtime)
const { cached, loadingCached, shaderProjectFiles } = useLocalSearch(shaderPack)
const loading = computed(() => loadingModrinth.value || loadingCached.value)

Expand Down Expand Up @@ -148,7 +148,6 @@ export function useShaderPackSearch(runtime: Ref<InstanceData['runtime']>, shade
shaderLoaderFilters,
items,
loadMoreModrinth,
canModrinthLoadMore,
sort,
isModrinthActive,
modrinthError,
Expand Down

0 comments on commit 2a3d706

Please sign in to comment.