import { defineStore } from 'pinia'; import { groupService, type Group } from '@/services/groupService'; export const useGroupStore = defineStore('group', { state: () => ({ groups: [] as Group[], isLoading: false, error: null as Error | null, }), actions: { async fetchGroups() { // Small cache implemented to prevent re-fetching on every mount if (this.groups.length > 0) { return; } this.isLoading = true; this.error = null; try { this.groups = await groupService.getUserGroups(); } catch (error) { this.error = error as Error; console.error('Failed to fetch groups:', error); } finally { this.isLoading = false; } }, }, getters: { groupCount: (state) => state.groups.length, firstGroupId: (state): number | null => { if (state.groups.length === 1) { return state.groups[0].id; } return null; } } });