import { defineStore } from "pinia"; import { UserResponseDTO } from "~/netcode/events/dto/userresponse.dto"; export const useUserStore = defineStore("user", { state: () => ({ users: {} as Record, self: null as UserResponseDTO | null, has_contacted: false }), actions: { async fetchUser(id: string) { if (id in this.users) { return this.users[id] } const config = useRuntimeConfig(); const data = await useFetch(config.public.endpoint + `/users/` + id, { credentials: "include", immediate: true }) if (data.data.value) { this.users[data.data.value.id] = data.data.value } return data.data.value }, async whoami() { const config = useRuntimeConfig(); const req = await useFetch(config.public.endpoint + "/whoami", { credentials: "include", server: false, immediate: true, onResponse: (data) => { this.self = data.response._data this.has_contacted = true }, lazy: true }) return req; } }, });