Compare commits

...
2 changed files with 66 additions and 30 deletions
+63 -30
View File
@@ -297,6 +297,58 @@ function isActiveUpdateRequest(requestId: number) {
return activeUpdateRequestId.value === requestId return activeUpdateRequestId.value === requestId
} }
function sortVersionsByPublishedDate(versions: Labrinth.Versions.v2.Version[]) {
return [...versions].sort(
(a, b) => new Date(b.date_published).getTime() - new Date(a.date_published).getTime(),
)
}
function mergeVersionIntoList(
versions: Labrinth.Versions.v2.Version[],
version: Labrinth.Versions.v2.Version,
) {
const existingIndex = versions.findIndex((v) => v.id === version.id)
if (existingIndex === -1) {
return sortVersionsByPublishedDate([version, ...versions])
}
const mergedVersions = [...versions]
mergedVersions[existingIndex] = version
return sortVersionsByPublishedDate(mergedVersions)
}
async function getUpdaterProjectVersions(projectId: string, pinnedVersionId?: string) {
let fetchError: unknown = null
let versions = (await get_project_versions(projectId, 'bypass').catch((err) => {
fetchError = err
return null
})) as Labrinth.Versions.v2.Version[] | null
if (!versions) {
versions = (await get_project_versions(projectId).catch(() => null)) as
| Labrinth.Versions.v2.Version[]
| null
}
if (!versions && fetchError) {
handleError(fetchError as Error)
}
let mergedVersions = sortVersionsByPublishedDate(versions ?? [])
if (pinnedVersionId && !mergedVersions.some((version) => version.id === pinnedVersionId)) {
const pinnedVersion = (await get_version(pinnedVersionId, 'bypass').catch(
() => null,
)) as Labrinth.Versions.v2.Version | null
if (pinnedVersion) {
mergedVersions = mergeVersionIntoList(mergedVersions, pinnedVersion)
}
}
return mergedVersions
}
async function handleBrowseContent() { async function handleBrowseContent() {
if (!props.instance) return if (!props.instance) return
await router.push({ await router.push({
@@ -543,16 +595,14 @@ async function handleUpdate(id: string) {
}) })
contentUpdaterModal.value?.show(initialVersionId) contentUpdaterModal.value?.show(initialVersionId)
const versions = (await get_project_versions(item.project.id).catch((e) => { const versions = await getUpdaterProjectVersions(item.project.id, initialVersionId)
return handleError(e)
})) as Labrinth.Versions.v2.Version[] | null
if (!isActiveUpdateRequest(requestId) || getContentItemId(updatingProject.value) !== itemId) if (!isActiveUpdateRequest(requestId) || getContentItemId(updatingProject.value) !== itemId)
return return
loadingVersions.value = false loadingVersions.value = false
if (!versions) { if (versions.length === 0) {
debug('handleUpdate: no versions returned', { projectId: item.project.id }) debug('handleUpdate: no versions returned', { projectId: item.project.id })
return return
} }
@@ -571,9 +621,6 @@ async function handleUpdate(id: string) {
updateVersionInList: versions.some((v) => v.id === item.update_version_id), updateVersionInList: versions.some((v) => v.id === item.update_version_id),
}) })
versions.sort(
(a, b) => new Date(b.date_published).getTime() - new Date(a.date_published).getTime(),
)
const preselectedVersion = const preselectedVersion =
versions.find((version) => version.id === initialVersionId) ?? versions[0] ?? null versions.find((version) => version.id === initialVersionId) ?? versions[0] ?? null
debug('handleUpdate: resolved content updater preselection', { debug('handleUpdate: resolved content updater preselection', {
@@ -611,23 +658,16 @@ async function handleSwitchVersion(item: ContentItem) {
await nextTick() await nextTick()
contentUpdaterModal.value?.show(item.version.id, { switchMode: true }) const initialVersionId = item.version.id
contentUpdaterModal.value?.show(initialVersionId, { switchMode: true })
const versions = (await get_project_versions(item.project.id).catch((e) => { const versions = await getUpdaterProjectVersions(item.project.id, initialVersionId)
return handleError(e)
})) as Labrinth.Versions.v2.Version[] | null
if (!isActiveUpdateRequest(requestId) || getContentItemId(updatingProject.value) !== itemId) if (!isActiveUpdateRequest(requestId) || getContentItemId(updatingProject.value) !== itemId)
return return
loadingVersions.value = false loadingVersions.value = false
if (!versions) return
versions.sort(
(a, b) => new Date(b.date_published).getTime() - new Date(a.date_published).getTime(),
)
updatingProjectVersions.value = versions updatingProjectVersions.value = versions
} }
@@ -713,19 +753,17 @@ async function handleModpackUpdate() {
}) })
contentUpdaterModal.value?.show(initialVersionId) contentUpdaterModal.value?.show(initialVersionId)
const versions = (await get_project_versions(props.instance.linked_data.project_id).catch( const versions = await getUpdaterProjectVersions(
handleError, props.instance.linked_data.project_id,
)) as Labrinth.Versions.v2.Version[] | null initialVersionId,
)
if (!isActiveUpdateRequest(requestId) || !updatingModpack.value) return if (!isActiveUpdateRequest(requestId) || !updatingModpack.value) return
loadingVersions.value = false loadingVersions.value = false
if (!versions) return if (versions.length === 0) return
versions.sort(
(a, b) => new Date(b.date_published).getTime() - new Date(a.date_published).getTime(),
)
const preselectedVersion = const preselectedVersion =
versions.find((version) => version.id === initialVersionId) ?? versions[0] ?? null versions.find((version) => version.id === initialVersionId) ?? versions[0] ?? null
debug('handleModpackUpdate: resolved modpack updater preselection', { debug('handleModpackUpdate: resolved modpack updater preselection', {
@@ -760,12 +798,7 @@ async function fetchAndSpliceVersion(
)) as Labrinth.Versions.v2.Version | null )) as Labrinth.Versions.v2.Version | null
if (!isActiveUpdateRequest(requestId)) return if (!isActiveUpdateRequest(requestId)) return
if (!fullVersion) return if (!fullVersion) return
const index = updatingProjectVersions.value.findIndex((v) => v.id === versionId) updatingProjectVersions.value = mergeVersionIntoList(updatingProjectVersions.value, fullVersion)
if (index !== -1) {
const newVersions = [...updatingProjectVersions.value]
newVersions[index] = fullVersion
updatingProjectVersions.value = newVersions
}
} }
async function handleVersionSelect(version: Labrinth.Versions.v2.Version) { async function handleVersionSelect(version: Labrinth.Versions.v2.Version) {
@@ -426,6 +426,7 @@ const pendingIncompatibleUpdate = ref<{
} | null>(null) } | null>(null)
// Store the initial version ID to select when versions become available // Store the initial version ID to select when versions become available
const pendingInitialVersionId = ref<string | undefined>(undefined) const pendingInitialVersionId = ref<string | undefined>(undefined)
const pinnedInitialVersionId = ref<string | undefined>(undefined)
watch( watch(
() => props.versions, () => props.versions,
@@ -511,6 +512,7 @@ const filteredVersions = computed(() => {
(version) => (version) =>
version.id === props.currentVersionId || version.id === props.currentVersionId ||
version.id === selectedVersion.value?.id || version.id === selectedVersion.value?.id ||
version.id === pinnedInitialVersionId.value ||
isVersionCompatible(version), isVersionCompatible(version),
) )
} }
@@ -683,6 +685,7 @@ function show(initialVersionId?: string, options?: { switchMode?: boolean }) {
searchQuery.value = '' searchQuery.value = ''
hideIncompatibleState.value = !isModpack.value hideIncompatibleState.value = !isModpack.value
pendingIncompatibleUpdate.value = null pendingIncompatibleUpdate.value = null
pinnedInitialVersionId.value = initialVersionId
switchMode.value = options?.switchMode ?? false switchMode.value = options?.switchMode ?? false
debug('show() called', { debug('show() called', {