import { defineStore } from 'pinia' import { ref } from 'vue' import * as pointsApi from '@/services/points' export const usePointsStore = defineStore('points', () => { const account = ref(null) const checkinStatus = ref(null) const loading = ref(false) const lastFetched = ref(0) const CACHE_TTL = 2 * 60 * 1000 async function refresh() { if (Date.now() - lastFetched.value < CACHE_TTL && account.value) return loading.value = true try { const [acct, checkin] = await Promise.all([ pointsApi.getAccount(), pointsApi.getCheckinStatus(), ]) account.value = acct checkinStatus.value = checkin lastFetched.value = Date.now() } catch { /* ignore */ } loading.value = false } function invalidate() { lastFetched.value = 0 } async function doCheckin(): Promise { try { const result = await pointsApi.dailyCheckin() checkinStatus.value = result lastFetched.value = 0 account.value = await pointsApi.getAccount() return true } catch { return false } } return { account, checkinStatus, loading, lastFetched, refresh, invalidate, doCheckin } })