Files
FabledCurator/frontend/src/stores/sources.js
T
bvandeusen b7d07324ee
CI / lint (push) Successful in 2s
CI / frontend-build (push) Successful in 22s
CI / backend-lint-and-test (push) Successful in 24s
CI / integration (push) Successful in 3m2s
fix(subs): stop the lock/reload on source actions + regroup the row buttons
Lock/reload: every inline source action (check / backfill / recover / toggle /
remove) ended by refetching the WHOLE subscription list (store.loadAll /
refresh), which blocked the UI and re-rendered the table — collapsing the
expanded row, which read as "locks then resets." The action APIs already return
the updated source, so the store now patches that one row in place
(_patchSource / _dropSource); the post-action loadAll/refresh calls are gone.
Toggling enabled, starting/stopping a backfill, recovering, and removing are now
instant and leave the expansion intact.

Button regroup (operator-flagged: tiny, mis-clickable, not grouped by function):
- New shared SourceActions.vue used by desktop SourceRow + mobile SourceCard.
- Frequent actions stay as size="small" buttons: Check, Backfill/Stop.
- Low-frequency / destructive actions move into a labelled overflow (⋮) menu —
  Preview backfill, Recover dropped near-duplicates, Remove source — so they
  can't be fat-fingered, and the labels spell out recover-vs-backfill.
- Edit moves next to the source URL (its identity), out of the action cluster
  where it sat beside Remove.
- Single-source Remove now confirms (it had no guard before).

Tests: store patches/drops in place without dropping the cache.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-06 20:48:01 -04:00

176 lines
5.9 KiB
JavaScript

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { useApi } from '../composables/useApi.js'
import { useAsyncAction } from '../composables/useAsyncAction.js'
export const useSourcesStore = defineStore('sources', () => {
const api = useApi()
// keyed cache: null => all sources, number => artist_id filtered
const byArtist = ref(new Map())
const { loading, error, run } = useAsyncAction()
const scheduleStatus = ref(null)
const allSources = computed(() => byArtist.value.get(null) ?? [])
async function loadAll() {
await run(async () => {
const body = await api.get('/api/sources')
byArtist.value.set(null, body)
})
}
async function loadForArtist(artistId) {
await run(async () => {
const body = await api.get('/api/sources', { params: { artist_id: artistId } })
byArtist.value.set(artistId, body)
})
}
function _invalidate(artistId) {
byArtist.value.delete(null)
if (artistId != null) byArtist.value.delete(artistId)
}
// Patch a single updated source into every cached array IN PLACE, rather than
// invalidating + refetching the whole list. The full refetch was what locked
// the Subscriptions UI and collapsed the expanded row after every inline
// action (check / backfill / recover / toggle) — operator-flagged 2026-06-07.
// Map.set on a Vue-reactive ref(Map) triggers re-render; we hand each cache a
// NEW array so the table diffs just the one changed row.
function _patchSource(updated) {
if (!updated || updated.id == null) return
for (const [key, arr] of byArtist.value) {
const i = arr.findIndex((s) => s.id === updated.id)
if (i === -1) continue
const next = arr.slice()
next[i] = { ...arr[i], ...updated }
byArtist.value.set(key, next)
}
}
function _dropSource(id) {
for (const [key, arr] of byArtist.value) {
if (arr.some((s) => s.id === id)) {
byArtist.value.set(key, arr.filter((s) => s.id !== id))
}
}
}
async function create(payload) {
const body = await api.post('/api/sources', { body: payload })
_invalidate(payload.artist_id)
return body
}
async function update(id, patch, artistIdHint = null) {
const body = await api.patch(`/api/sources/${id}`, { body: patch })
_patchSource(body)
return body
}
async function remove(id, artistIdHint = null) {
await api.delete(`/api/sources/${id}`)
_dropSource(id)
}
async function findOrCreateArtist(name) {
const body = await api.post('/api/artists', { body: { name } })
return { artist: { id: body.id, name: body.name, slug: body.slug }, created: body.created }
}
async function autocompleteArtist(query, limit = 20) {
if (!query || !query.trim()) return []
return await api.get('/api/artists/autocomplete', { params: { q: query, limit } })
}
async function loadScheduleStatus() {
scheduleStatus.value = await api.get('/api/sources/schedule-status')
return scheduleStatus.value
}
// FC-3c: trigger a download for one source. Returns {download_event_id,status}.
const checkingIds = ref(new Set())
// force=true bypasses the platform-rate-limit cooldown gate. Single-
// source RETRY clicks pass it (operator-explicit override, useful for
// rapid auth-fix testing); bulk RETRY ALL / MaintenanceMenu retries
// leave it off so the cooldown does its preventive job.
async function checkNow(id, { force = false } = {}) {
checkingIds.value = new Set(checkingIds.value).add(id)
try {
const url = force
? `/api/sources/${id}/check?force=true`
: `/api/sources/${id}/check`
return await api.post(url)
} finally {
const next = new Set(checkingIds.value)
next.delete(id)
checkingIds.value = next
}
}
// Plan #693: start/stop a run-until-done backfill. 'start' walks the full
// post history in time-boxed chunks until it reaches the bottom (then the
// source shows backfill_state 'complete'); 'stop' cancels back to tick mode.
async function startBackfill(id, artistIdHint = null) {
const body = await api.post(`/api/sources/${id}/backfill`, { body: { action: 'start' } })
_patchSource(body)
return body
}
async function stopBackfill(id, artistIdHint = null) {
const body = await api.post(`/api/sources/${id}/backfill`, { body: { action: 'stop' } })
_patchSource(body)
return body
}
// Plan #697: arm a recovery walk — a backfill that bypasses the Patreon
// seen-ledger to re-fetch dropped-and-deleted near-dups and re-evaluate them
// under the current pHash threshold. Stop with stopBackfill (shared lifecycle).
async function recoverSource(id, artistIdHint = null) {
const body = await api.post(`/api/sources/${id}/backfill`, { body: { action: 'recover' } })
_patchSource(body)
return body
}
// Plan #708 B4: dry-run preview — count what a backfill WOULD download
// (native platforms), without downloading. Read-only, so no cache invalidate.
async function previewSource(id) {
return await api.post(`/api/sources/${id}/preview`)
}
function sourcesByArtistGrouped() {
// returns [{artist: {id,name,slug}, sources: [...]}, ...]
const arr = byArtist.value.get(null) ?? []
const groups = new Map()
for (const s of arr) {
const key = s.artist_id
if (!groups.has(key)) {
groups.set(key, {
artist: { id: s.artist_id, name: s.artist_name, slug: s.artist_slug },
sources: [],
})
}
groups.get(key).sources.push(s)
}
return Array.from(groups.values()).sort(
(a, b) => a.artist.name.localeCompare(b.artist.name)
)
}
return {
byArtist, loading, error, scheduleStatus,
allSources,
checkingIds,
loadAll, loadForArtist,
create, update, remove,
checkNow,
startBackfill,
stopBackfill,
recoverSource,
previewSource,
findOrCreateArtist, autocompleteArtist,
loadScheduleStatus,
sourcesByArtistGrouped,
}
})