4f9deeb57a
Fixes a real N+1 API-call bug (per-cell project-member fetches in shot/asset task-status cells, now a shared cached store), replaces a polling-based in-flight request de-dup with promise memoization, adds markRaw() around icon components in reactive state, and fixes a deep watcher that re-scanned the whole projects array just to trigger thumbnail loading. Introduces useAsyncAction and usePermission composables to cut duplicated store boilerplate and duplicated role/admin checks, applied only where the existing code was a clean match rather than forced onto everything. Extracts assets.ts's shared per-asset optimistic-update/rollback helper without merging the single and bulk API paths, which hit genuinely different endpoints. Adds toast-on-error for previously-silent secondary loads and fixes a dead try/catch in the router's auth-init guard along the way. Also fixes a thumbnail-loading regression introduced earlier in this same pass: the new ID-keyed watcher never fired on a repeat visit to the Projects page when the store already held the same project list, so thumbnails (local component state, reset per mount) silently stopped loading after the first visit. Replaced the watcher with a direct call after each fetch. Table virtualization, splitting the two largest view files, broad store caching, and moving domain types into types/ were scoped out as separate, higher-risk follow-ups (documented in frontend_tasks.md). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
42 lines
1.5 KiB
TypeScript
42 lines
1.5 KiB
TypeScript
import type { Ref } from 'vue'
|
|
|
|
interface UseAsyncActionOptions {
|
|
isLoading: Ref<boolean>
|
|
error: Ref<string | null>
|
|
}
|
|
|
|
interface RunOptions {
|
|
/** Fallback message shown when the error has no server-provided detail */
|
|
errorMessage: string
|
|
/** Whether to re-throw after recording the error (default true) */
|
|
rethrow?: boolean
|
|
}
|
|
|
|
/**
|
|
* Wraps the isLoading/error/try-catch-finally shape shared by most store actions:
|
|
* set loading, clear error, run fn, extract a message from an axios-shaped or
|
|
* plain Error on failure, log it, optionally re-throw, and always clear loading.
|
|
*/
|
|
export function useAsyncAction({ isLoading, error }: UseAsyncActionOptions) {
|
|
// Overloads so callers that don't pass `rethrow: false` keep a non-optional return type
|
|
function run<T>(fn: () => Promise<T>, options: RunOptions & { rethrow?: true }): Promise<T>
|
|
function run<T>(fn: () => Promise<T>, options: RunOptions & { rethrow: false }): Promise<T | undefined>
|
|
async function run<T>(fn: () => Promise<T>, options: RunOptions): Promise<T | undefined> {
|
|
const { errorMessage, rethrow = true } = options
|
|
try {
|
|
isLoading.value = true
|
|
error.value = null
|
|
return await fn()
|
|
} catch (err: any) {
|
|
error.value = err?.response?.data?.detail || (err instanceof Error ? err.message : errorMessage)
|
|
console.error(errorMessage, err)
|
|
if (rethrow) throw err
|
|
return undefined
|
|
} finally {
|
|
isLoading.value = false
|
|
}
|
|
}
|
|
|
|
return { run }
|
|
}
|