Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/midden. Archive posts and media across platforms, with tenant accounts and Telegram access
Something went wrong. Try again.
1.6 kB · 54 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import { onUnmounted, shallowRef } from 'vue'import { api, errorText, type Collection, type Page } from '../api'export function useCollection() { const items = shallowRef<Collection[]>([]) const next = shallowRef('') const loading = shallowRef(false) const error = shallowRef('') let query = '' let controller: AbortController | undefined let generation = 0 async function load(params: string, append = false) { controller?.abort() controller = new AbortController() const version = ++generation loading.value = true error.value = '' if (!append) { items.value = [] next.value = '' } query = params const q = new URLSearchParams(params) q.set('q', q.get('q') || '') if (append && next.value) q.set('cursor', next.value) try { const p = await api<Page<Collection>>(`/collections?${q}`, { signal: controller.signal, }) if (version !== generation) return items.value = append ? [...items.value, ...p.items] : p.items next.value = p.next_cursor || '' } catch (e) { if ( version === generation && (!(e instanceof DOMException) || e.name !== 'AbortError') ) error.value = errorText(e) } finally { if (version === generation) loading.value = false } } onUnmounted(() => controller?.abort()) return { items, next, loading, error, load, more: () => load(query, true), remove: (id: string) => { items.value = items.value.filter((a) => a.id !== id) }, }}