Files
LinkDesk/frontend/src/stores/episodes.ts
T
indigo 4f9deeb57a Phase 4: Architecture & tooling investment
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>
2026-07-18 04:03:58 +08:00

132 lines
3.8 KiB
TypeScript

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { episodeService, type Episode, type EpisodeCreate, type EpisodeUpdate } from '@/services/episode'
import { useAsyncAction } from '@/composables/useAsyncAction'
export const useEpisodesStore = defineStore('episodes', () => {
// State
const episodes = ref<Episode[]>([])
const currentEpisode = ref<Episode | null>(null)
const isLoading = ref(false)
const error = ref<string | null>(null)
const { run } = useAsyncAction({ isLoading, error })
// Getters
const getEpisodeById = computed(() => {
return (id: number) => episodes.value.find(episode => episode.id === id)
})
const getEpisodesByProject = computed(() => {
return (projectId: number) => episodes.value.filter(episode => episode.project_id === projectId)
})
const episodesByStatus = computed(() => {
return (status: string) => episodes.value.filter(episode => episode.status === status)
})
// Actions
const fetchEpisodes = async (projectId?: number) => {
return run(async () => {
episodes.value = projectId
? await episodeService.getProjectEpisodes(projectId)
: await episodeService.getEpisodes()
}, { errorMessage: 'Failed to fetch episodes' })
}
const fetchEpisode = async (episodeId: number) => {
return run(async () => {
const episode = await episodeService.getEpisode(episodeId)
// Update the episode in the list if it exists
const index = episodes.value.findIndex(e => e.id === episodeId)
if (index !== -1) {
episodes.value[index] = episode
} else {
episodes.value.push(episode)
}
currentEpisode.value = episode
return episode
}, { errorMessage: 'Failed to fetch episode' })
}
const createEpisode = async (projectId: number, episodeData: EpisodeCreate) => {
return run(async () => {
const newEpisode = await episodeService.createEpisode(projectId, episodeData)
episodes.value.push(newEpisode)
return newEpisode
}, { errorMessage: 'Failed to create episode' })
}
const updateEpisode = async (episodeId: number, episodeData: EpisodeUpdate) => {
return run(async () => {
const updatedEpisode = await episodeService.updateEpisode(episodeId, episodeData)
// Update the episode in the list
const index = episodes.value.findIndex(e => e.id === episodeId)
if (index !== -1) {
episodes.value[index] = updatedEpisode
}
// Update current episode if it's the same
if (currentEpisode.value?.id === episodeId) {
currentEpisode.value = updatedEpisode
}
return updatedEpisode
}, { errorMessage: 'Failed to update episode' })
}
const deleteEpisode = async (episodeId: number) => {
return run(async () => {
await episodeService.deleteEpisode(episodeId)
// Remove the episode from the list
episodes.value = episodes.value.filter(e => e.id !== episodeId)
// Clear current episode if it's the deleted one
if (currentEpisode.value?.id === episodeId) {
currentEpisode.value = null
}
}, { errorMessage: 'Failed to delete episode' })
}
const setCurrentEpisode = (episode: Episode | null) => {
currentEpisode.value = episode
}
const clearError = () => {
error.value = null
}
const clearEpisodes = () => {
episodes.value = []
currentEpisode.value = null
error.value = null
}
return {
// State
episodes,
currentEpisode,
isLoading,
error,
// Getters
getEpisodeById,
getEpisodesByProject,
episodesByStatus,
// Actions
fetchEpisodes,
fetchEpisode,
createEpisode,
updateEpisode,
deleteEpisode,
setCurrentEpisode,
clearError,
clearEpisodes
}
})
export type { Episode, EpisodeCreate, EpisodeUpdate }