refactor(dry-F1): shared useAsyncAction lifecycle helper for stores

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>
This commit is contained in:
2026-05-28 12:53:57 -04:00
parent cabd73287a
commit a37dad33c7
12 changed files with 80 additions and 129 deletions
+5 -11
View File
@@ -2,6 +2,7 @@ import { defineStore } from 'pinia'
import { toast } from '../utils/toast.js'
import { ref } from 'vue'
import { useApi } from '../composables/useApi.js'
import { useAsyncAction } from '../composables/useAsyncAction.js'
// Category display order: people/sources first, general last.
export const CATEGORY_ORDER = ['artist', 'character', 'copyright', 'general']
@@ -15,23 +16,16 @@ export const CATEGORY_LABELS = {
export const useSuggestionsStore = defineStore('suggestions', () => {
const api = useApi()
const byCategory = ref({}) // { category: [suggestion, ...] }
const loading = ref(false)
const error = ref(null)
const { loading, error, run } = useAsyncAction({ errorAs: 'message' })
let currentImageId = null
async function load(imageId) {
currentImageId = imageId
loading.value = true
error.value = null
try {
byCategory.value = {} // cleared upfront so it stays empty on error
await run(async () => {
const body = await api.get(`/api/images/${imageId}/suggestions`)
byCategory.value = body.by_category || {}
} catch (e) {
error.value = e.message
byCategory.value = {}
} finally {
loading.value = false
}
})
}
function _drop(category, predicate) {