5666fd5ca5
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
134 lines
4.1 KiB
JavaScript
134 lines
4.1 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { ref, computed } from 'vue'
|
|
import { useApi } from '../composables/useApi.js'
|
|
|
|
export const useImportStore = defineStore('import', () => {
|
|
const api = useApi()
|
|
|
|
const settings = ref(null)
|
|
const settingsLoading = ref(false)
|
|
const settingsError = ref(null)
|
|
|
|
const activeBatch = ref(null)
|
|
const statusLoading = ref(false)
|
|
|
|
const tasks = ref([])
|
|
const tasksNextCursor = ref(null)
|
|
const tasksLoading = ref(false)
|
|
const tasksFilter = ref({ status: null, limit: 50 })
|
|
|
|
const triggerError = ref(null)
|
|
|
|
async function loadSettings() {
|
|
settingsLoading.value = true
|
|
settingsError.value = null
|
|
try {
|
|
settings.value = await api.get('/api/settings/import')
|
|
} catch (e) {
|
|
settingsError.value = e.message
|
|
} finally {
|
|
settingsLoading.value = false
|
|
}
|
|
}
|
|
|
|
async function patchSettings(patch) {
|
|
settingsError.value = null
|
|
try {
|
|
settings.value = await api.patch('/api/settings/import', { body: patch })
|
|
} catch (e) {
|
|
settingsError.value = e.message
|
|
window.__fcToast?.({ text: `Settings save failed: ${e.message}`, type: 'error' })
|
|
throw e
|
|
}
|
|
}
|
|
|
|
async function refreshStatus() {
|
|
statusLoading.value = true
|
|
try {
|
|
const body = await api.get('/api/import/status')
|
|
activeBatch.value = body.active_batch
|
|
} finally {
|
|
statusLoading.value = false
|
|
}
|
|
}
|
|
|
|
async function triggerScan() {
|
|
triggerError.value = null
|
|
try {
|
|
await api.post('/api/import/trigger', { body: { mode: 'quick' } })
|
|
// Acknowledge immediately so the click isn't invisible. scan_directory
|
|
// can finalize the batch synchronously when every file in /import is
|
|
// already on a non-failed ImportTask (operator-flagged 2026-05-25:
|
|
// 233k existing tasks → all paths in skip-set → files_seen=0 →
|
|
// batch flashes 'running' for <100ms then 'complete' before the
|
|
// first refreshStatus() lands; UI never sees the active state).
|
|
window.__fcToast?.({ text: 'Scan triggered', type: 'success' })
|
|
await refreshStatus()
|
|
// Re-poll twice over ~5s to catch quick-finalize transitions and
|
|
// surface a result toast either way.
|
|
setTimeout(async () => {
|
|
await refreshStatus()
|
|
if (!activeBatch.value) {
|
|
// Either scan completed with zero new files, or it never visibly
|
|
// started. Fetch the freshest task to differentiate.
|
|
window.__fcToast?.({
|
|
text: 'Scan complete — no new files (everything already on an ImportTask row)',
|
|
type: 'info',
|
|
})
|
|
}
|
|
}, 2000)
|
|
} catch (e) {
|
|
triggerError.value = e.message
|
|
window.__fcToast?.({ text: `Scan failed: ${e.message}`, type: 'error' })
|
|
throw e
|
|
}
|
|
}
|
|
|
|
async function loadTasks(reset = true) {
|
|
tasksLoading.value = true
|
|
try {
|
|
const params = { limit: tasksFilter.value.limit }
|
|
if (tasksFilter.value.status) params.status = tasksFilter.value.status
|
|
if (!reset && tasksNextCursor.value) params.cursor = tasksNextCursor.value
|
|
const body = await api.get('/api/import/tasks', { params })
|
|
tasks.value = reset ? body.tasks : [...tasks.value, ...body.tasks]
|
|
tasksNextCursor.value = body.next_cursor
|
|
} finally {
|
|
tasksLoading.value = false
|
|
}
|
|
}
|
|
|
|
function setStatusFilter(status) {
|
|
tasksFilter.value.status = status
|
|
}
|
|
|
|
async function retryFailed() {
|
|
await api.post('/api/import/retry-failed')
|
|
await loadTasks(true)
|
|
}
|
|
|
|
async function clearCompleted(ageDays = 0) {
|
|
await api.post('/api/import/clear-completed', { body: { age_days: ageDays } })
|
|
await loadTasks(true)
|
|
}
|
|
|
|
async function clearStuck() {
|
|
const body = await api.post('/api/import/clear-stuck')
|
|
await loadTasks(true)
|
|
await refreshStatus()
|
|
return body
|
|
}
|
|
|
|
const hasMore = computed(() => tasksNextCursor.value !== null)
|
|
|
|
return {
|
|
settings, settingsLoading, settingsError,
|
|
activeBatch, statusLoading,
|
|
tasks, tasksLoading, tasksFilter, hasMore,
|
|
triggerError,
|
|
loadSettings, patchSettings,
|
|
refreshStatus, triggerScan,
|
|
loadTasks, setStatusFilter, retryFailed, clearCompleted, clearStuck
|
|
}
|
|
})
|