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>
52 lines
1.5 KiB
JavaScript
52 lines
1.5 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { ref, computed } from 'vue'
|
|
import { useApi } from '../composables/useApi.js'
|
|
import { useAsyncAction } from '../composables/useAsyncAction.js'
|
|
|
|
const PAGE = 60
|
|
|
|
export const useArtistDirectoryStore = defineStore('artistDirectory', () => {
|
|
const api = useApi()
|
|
const cards = ref([])
|
|
const nextCursor = ref(null)
|
|
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
|
|
const q = ref('')
|
|
const platform = ref(null)
|
|
let started = false
|
|
|
|
async function loadMore() {
|
|
if (loading.value) return
|
|
if (started && nextCursor.value === null) return
|
|
await run(async () => {
|
|
const params = { limit: PAGE }
|
|
if (q.value) params.q = q.value
|
|
if (platform.value) params.platform = platform.value
|
|
if (nextCursor.value) params.cursor = nextCursor.value
|
|
const body = await api.get('/api/artists/directory', { params })
|
|
cards.value.push(...body.cards)
|
|
nextCursor.value = body.next_cursor
|
|
started = true
|
|
})
|
|
}
|
|
|
|
async function reset() {
|
|
cards.value = []
|
|
nextCursor.value = null
|
|
started = false
|
|
await loadMore()
|
|
}
|
|
|
|
function setQuery(text) { q.value = text; reset() }
|
|
function setPlatform(p) { platform.value = p; reset() }
|
|
|
|
const hasMore = computed(() => !started || nextCursor.value !== null)
|
|
const isEmpty = computed(
|
|
() => !loading.value && cards.value.length === 0 && error.value === null
|
|
)
|
|
|
|
return {
|
|
cards, loading, error, q, platform, hasMore, isEmpty,
|
|
loadMore, reset, setQuery, setPlatform,
|
|
}
|
|
})
|