Files
codex-proxy-rs/frontend/src/composables/useTableSort.ts
T
f0484c4e11 chore(frontend): upgrade ESLint config to v10 and migrate formatting (#522)
* chore(deps-dev): bump @antfu/eslint-config in /frontend

Bumps [@antfu/eslint-config](https://github.com/antfu/eslint-config) from 9.5.1 to 10.0.0.
- [Release notes](https://github.com/antfu/eslint-config/releases)
- [Commits](https://github.com/antfu/eslint-config/compare/v9.5.1...v10.0.0)

---
updated-dependencies:
- dependency-name: "@antfu/eslint-config"
  dependency-version: 10.0.0
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* chore(frontend): migrate formatting to ESLint v10

Co-authored-by: Codex <noreply@openai.com>

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Zyy <zyycn@users.noreply.github.com>
Co-authored-by: Codex <noreply@openai.com>
2026-10-26 11:34:32 +08:00

38 lines
1.3 KiB
TypeScript

import type { TableColumn, TableSort } from '@codex-proxy/ui'
import type { MaybeRefOrGetter } from 'vue'
import { useStorage } from '@vueuse/core'
import { toValue } from 'vue'
export function useTableSort<Row extends object>(
source: MaybeRefOrGetter<TableColumn<Row>[]>,
tableId: string,
) {
function readSort(value: string): TableSort | undefined {
try {
const parsed: unknown = JSON.parse(value)
if (!parsed || typeof parsed !== 'object' || !('key' in parsed) || !('direction' in parsed))
return undefined
const { key, direction } = parsed
if (typeof key === 'string' && (direction === 'asc' || direction === 'desc')
&& toValue(source).some(column => column.sortable && (column.sortable === true
? column.key
: column.sortable) === key)) {
return { key, direction }
}
}
catch {
// 损坏的本地偏好回退到默认排序,不影响列表查询
}
return undefined
}
return useStorage<TableSort | undefined>(`codex-proxy:table-sort:${tableId}`, undefined, undefined, {
shallow: true,
writeDefaults: false,
// 其他标签页的偏好在下次进入时读取,避免当前排序与分页游标失配
listenToStorageChanges: false,
serializer: { read: readSort, write: JSON.stringify },
})
}