7d84990f6d
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
74 lines
2.3 KiB
JavaScript
74 lines
2.3 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { ref, computed } from 'vue'
|
|
import { useApi } from '../composables/useApi.js'
|
|
import { usePostsStore } from './posts.js'
|
|
|
|
const PAGE = 60
|
|
|
|
export const useArtistStore = defineStore('artist', () => {
|
|
const api = useApi()
|
|
const overview = ref(null)
|
|
const images = ref([])
|
|
const nextCursor = ref(null)
|
|
const loading = ref(false)
|
|
const imagesLoading = ref(false)
|
|
const error = ref(null)
|
|
const notFound = ref(false)
|
|
let started = false
|
|
|
|
async function load (slug) {
|
|
// Cross-artist reset: clear this store AND the posts store so the new
|
|
// artist doesn't briefly render with the previous artist's content
|
|
// when the user is on the Posts tab. (Gallery tab uses this artist
|
|
// store's own images list — cleared above.)
|
|
overview.value = null
|
|
images.value = []
|
|
nextCursor.value = null
|
|
notFound.value = false
|
|
started = false
|
|
error.value = null
|
|
loading.value = true
|
|
usePostsStore().$reset?.()
|
|
try {
|
|
overview.value = await api.get(`/api/artist/${encodeURIComponent(slug)}`)
|
|
await loadMoreImages(slug)
|
|
} catch (e) {
|
|
if (e.status === 404) notFound.value = true
|
|
else error.value = e.message
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
async function loadMoreImages (slug) {
|
|
if (imagesLoading.value) return
|
|
if (started && nextCursor.value === null) return
|
|
imagesLoading.value = true
|
|
try {
|
|
const params = { limit: PAGE }
|
|
if (nextCursor.value) params.cursor = nextCursor.value
|
|
const body = await api.get(
|
|
`/api/artist/${encodeURIComponent(slug)}/images`, { params }
|
|
)
|
|
images.value.push(...body.images)
|
|
nextCursor.value = body.next_cursor
|
|
started = true
|
|
} catch (e) {
|
|
error.value = e.message
|
|
} finally {
|
|
imagesLoading.value = false
|
|
}
|
|
}
|
|
|
|
const hasMoreImages = computed(() => !started || nextCursor.value !== null)
|
|
const postCount = computed(() => overview.value?.post_count ?? null)
|
|
const imageCount = computed(() => overview.value?.image_count ?? null)
|
|
const lastAdded = computed(() => overview.value?.date_range?.max ?? null)
|
|
|
|
return {
|
|
overview, images, loading, imagesLoading, error, notFound,
|
|
hasMoreImages, postCount, imageCount, lastAdded,
|
|
load, loadMoreImages,
|
|
}
|
|
})
|