a37dad33c7
New composables/useAsyncAction.js owns the loading/error/try-finally lifecycle. Migrated 11 stores: credentials, downloads, sources, posts (error=raw) + ml, artistDirectory, tagDirectory, showcase, suggestions, seriesReader, modal (error=message). The errorAs option preserves each store's existing error shape so store.error keeps the same type for components (~50 consumption sites unchanged). Stores whose catch also reset data (suggestions/seriesReader/modal) clear it upfront instead. Deliberately NOT migrated (special control flow, would change behavior): artist (conditional 404 catch + dual loading states), migration (rethrows), gallery (inflight-id stale-response guard), and the Shape-B no-catch / loaded-guard / keyed-cache stores. Net -77 lines. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
51 lines
1.5 KiB
JavaScript
51 lines
1.5 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import { useApi } from '../composables/useApi.js'
|
|
import { useAsyncAction } from '../composables/useAsyncAction.js'
|
|
|
|
// IR reader.js "viewport-center-in-third" rule, pure for unit tests.
|
|
// metrics: [{page_number, top, height}] in page order.
|
|
export function currentPageFromScroll(metrics, scrollTop, viewportH) {
|
|
if (!metrics.length) return 1
|
|
const target = scrollTop + viewportH / 3
|
|
let cur = metrics[0].page_number
|
|
for (const m of metrics) {
|
|
if (target >= m.top) cur = m.page_number
|
|
}
|
|
return cur
|
|
}
|
|
|
|
export function progressPct(scrollTop, scrollHeight, clientHeight) {
|
|
const denom = scrollHeight - clientHeight
|
|
if (denom <= 0) return 0
|
|
const pct = (scrollTop / denom) * 100
|
|
return Math.min(100, Math.max(0, pct))
|
|
}
|
|
|
|
export function clampPage(n, total) {
|
|
if (!total || total < 1) return 1
|
|
if (!Number.isFinite(n) || n < 1) return 1
|
|
if (n > total) return total
|
|
return Math.floor(n)
|
|
}
|
|
|
|
export const useSeriesReaderStore = defineStore('seriesReader', () => {
|
|
const api = useApi()
|
|
|
|
const series = ref(null)
|
|
const pages = ref([])
|
|
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
|
|
|
|
async function load(tagId) {
|
|
series.value = null // cleared upfront so they stay reset on error
|
|
pages.value = []
|
|
await run(async () => {
|
|
const body = await api.get(`/api/series/${tagId}/pages`)
|
|
series.value = body.series
|
|
pages.value = body.pages
|
|
})
|
|
}
|
|
|
|
return { series, pages, loading, error, load }
|
|
})
|