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() { const res = await client.api.me.get(); if (res.status !== 200 || !res.data) return; isAuthenticated.value = true; isAuthenticating.value = false; user.value = res.data.user; const resActors = res.data.actors; actors.value = resActors.reduce( (acc, actor) => { acc[actor.guild!.discordId!] = actor; return acc; }, {} as Record, ); guild.value = res.data.actors[0]?.guild; } async function waitForAuth() { while (isAuthenticating.value) { await getUser(); } } return { isAuthenticated, isAuthenticating, waitForAuth, user, actors, getUser, }; });