import type { App, SanitisedActorWithGuild, SanitisedUser } from "@stealth-developers/api"; import { treaty } from "@elysia/eden"; import { defineStore } from "pinia"; import { ref } from "vue"; export const client = treaty(window.location.host); export const useApiStore = defineStore("api", () => { const isAuthenticated = ref(false); const isAuthenticating = ref(true); const user = ref(null); const actors = ref>({}); const guild = ref(null); async function getUser() { try { const res = await client.api.me.get(); if (res.status === 200 && res.data) { isAuthenticated.value = true; user.value = res.data.user; const resActors = res.data.actors; actors.value = resActors.reduce( (acc, actor) => { if (actor.guild?.discordId) { acc[actor.guild.discordId] = actor; } return acc; }, {} as Record, ); guild.value = res.data.actors[0]?.guild; } } catch (error) { console.error(error); } finally { isAuthenticating.value = false; } } async function waitForAuth() { while (isAuthenticating.value) { await getUser(); } } return { isAuthenticated, isAuthenticating, waitForAuth, user, actors, getUser, }; });