refactor(nextui): type check sorts for series and books
Some checks failed
Dispatch events / dispatch (push) Has been cancelled
Test NextUI / Test unit (push) Has been cancelled
Validate NextUI i18n / Build with i18n (push) Has been cancelled
Test Komga / Test Komga - windows-latest (push) Has been cancelled
Chromatic / Run Chromatic (push) Has been cancelled
Test Komga / Test Komga - macos-latest (push) Has been cancelled
Test Komga / Test Komga - ubuntu-latest (push) Has been cancelled
Test NextUI / Test storybook (push) Has been cancelled
Test NextUI / Lint (push) Has been cancelled
Test NextUI / Prettier (push) Has been cancelled
Test NextUI / Type check (push) Has been cancelled
Test NextUI / Build (push) Has been cancelled
Test NextUI / Check for missing i18n (push) Has been cancelled

This commit is contained in:
Gauthier Roebroeck 2026-09-29 16:14:06 +08:00
parent 12568dbd33
commit 664037c44a
4 changed files with 32 additions and 14 deletions

View file

@ -6,6 +6,7 @@ import type { SearchConditionBook, SearchConditionSeries } from '@/generated/ope
import { seriesListQueryInfinite, seriesUpdatedQueryInfinite } from '@/colada/series'
import type { OverviewSection } from '@/types/OverviewSection'
import type { Sort } from '@/types/PageRequest'
import type { SortBook, SortSeries } from '@/types/sort'
export function useOverviewSection(
section: MaybeRefOrGetter<OverviewSection>,
@ -15,19 +16,19 @@ export function useOverviewSection(
const sort = computed<Sort[]>(() => {
switch (toValue(section)) {
case 'on_deck':
return []
return [] satisfies SortBook[]
case 'keep_reading':
return [{ key: 'readProgress.readDate', order: 'desc' }]
return [{ key: 'readProgress.readDate', order: 'desc' }] satisfies SortBook[]
case 'recently_released_books':
return [{ key: 'metadata.releaseDate', order: 'desc' }]
return [{ key: 'metadata.releaseDate', order: 'desc' }] satisfies SortBook[]
case 'recently_added_books':
return [{ key: 'createdDate', order: 'desc' }]
return [{ key: 'createdDate', order: 'desc' }] satisfies SortBook[]
case 'recently_read_books':
return [{ key: 'readProgress.readDate', order: 'desc' }]
return [{ key: 'readProgress.readDate', order: 'desc' }] satisfies SortBook[]
case 'recently_added_series':
return [{ key: 'createdDate', order: 'desc' }]
return [{ key: 'createdDate', order: 'desc' }] satisfies SortSeries[]
case 'recently_updated_series':
return [{ key: 'lastModifiedDate', order: 'desc' }]
return [{ key: 'lastModifiedDate', order: 'desc' }] satisfies SortSeries[]
}
})

View file

@ -1,6 +1,6 @@
import { useRouteQuerySchema } from '@/composables/useRouteQuerySchema'
import { type Sort } from '@/types/PageRequest'
import { MultiSortSchema, sortOptionToSorts, stringifySort } from '@/functions/sort'
import { MultiSortSchema, sortDefinitionToSorts, stringifySort } from '@/functions/sort'
import { deepEqual } from 'fast-equals'
import type { SortOption } from '@/types/sort'
@ -17,7 +17,7 @@ export function useSort(defaultSort: Sort[], options: SortOption[], multiSort: b
defaultSort,
).data
const validSorts = options.flatMap((it) => sortOptionToSorts(it))
const validSorts = options.flatMap((it) => sortDefinitionToSorts(it))
// filter out invalid values
watch(

View file

@ -1,6 +1,6 @@
import type { Sort } from '@/types/PageRequest'
import * as v from 'valibot'
import type { SortOption } from '@/types/sort'
import type { SortDefinition } from '@/types/sort'
/**
* Creates a compare function for Array.prototype.sort()
@ -53,7 +53,7 @@ export function stringifySort(sort: Sort): string {
return `${sort.key},${sort.order || 'asc'}`
}
export function sortOptionToSorts(option: SortOption): Sort[] {
export function sortDefinitionToSorts(option: SortDefinition): Sort[] {
const sorts: Sort[] = [
{
key: option.key,

View file

@ -1,8 +1,6 @@
import { defineMessage, type MessageDescriptor } from 'vue-intl'
export type SortOption = {
// for display
label: string
export type SortDefinition = {
// sorting key sent to API
key: string
// initial order
@ -11,10 +9,27 @@ export type SortOption = {
invertible: boolean
}
export type SortOption = {
// for display
label: string
} & SortDefinition
export type SortOrder = 'asc' | 'desc'
export type SortOptionDescriptor = Omit<SortOption, 'label'> & { message: MessageDescriptor }
type ResolveOrder<T extends readonly SortDefinition[]> = {
[K in keyof T]: T[K] extends {
key: infer KKey extends string
initialOrder: infer KOrder
invertible: infer KInvert
}
? KInvert extends true
? { key: KKey; order: SortOrder }
: { key: KKey; order: KOrder }
: never
}[number]
const messages = {
createdDate: defineMessage({
description: 'Sort label: createdDate',
@ -105,6 +120,7 @@ export const sortSeries = [
] as const satisfies readonly SortOptionDescriptor[]
export type SortKeysSeries = (typeof sortSeries)[number]['key']
export type SortSeries = ResolveOrder<typeof sortSeries>
export const sortBooks = [
{
@ -198,3 +214,4 @@ export const sortBooks = [
] as const satisfies readonly SortOptionDescriptor[]
export type SortKeysBook = (typeof sortBooks)[number]['key']
export type SortBook = ResolveOrder<typeof sortBooks>