import { ref, type Ref } from "vue"; import type { Note } from "../stores/notes"; // The load-a-list-of-notes scaffold every recall view (Search, Timeline, Reminders) // hand-rolled: an items ref + loading/error flags + a load() that fills them and never // leaves a half-state. The view supplies just the fetcher; wire the returned refs into // and pass `items` to /cards. // // `fetcher` returns the notes to show (or [] — e.g. an empty search box). A thrown // ApiError's `.error` becomes the message; otherwise `fallbackError` is shown. export function useNoteList( fetcher: () => Promise, fallbackError = "Couldn't load your notes.", ): { items: Ref; loading: Ref; error: Ref; load: () => Promise; } { const items = ref([]); const loading = ref(false); const error = ref(""); async function load(): Promise { loading.value = true; error.value = ""; try { items.value = await fetcher(); } catch (e) { error.value = (e as { error?: string }).error ?? fallbackError; items.value = []; } finally { loading.value = false; } } return { items, loading, error, load }; }