import { create } from 'zustand' import { sni } from './network' import { hasCookie } from 'cookies-next/client' interface SnAccount { id: number createdAt: Date updatedAt: Date deletedAt?: Date | null confirmedAt?: Date | null contacts?: SnAccountContact[] | null avatar: string banner: string description: string name: string nick: string permNodes: Record profile?: SnAccountProfile | null badges: SnAccountBadge[] suspendedAt?: Date | null affiliatedId?: number | null affiliatedTo?: number | null automatedBy?: number | null automatedId?: number | null } interface SnAccountContact { accountId: number content: string createdAt: Date deletedAt?: Date | null id: number isPrimary: boolean isPublic: boolean type: number updatedAt: Date verifiedAt?: Date | null } interface SnAccountProfile { id: number accountId: number birthday?: Date | null createdAt: Date deletedAt?: Date | null experience: number firstName: string lastName: string lastSeenAt?: Date | null updatedAt: Date } interface SnAccountBadge { id: number createdAt: Date updatedAt: Date deletedAt?: Date | null type: string accountId: number metadata: Record } export interface UserStore { account: SnAccount | undefined fetchUser: () => Promise } export const useUserStore = create((set) => ({ account: undefined, fetchUser: async (): Promise => { if (!hasCookie('nex_user_atk')) return try { const resp = await sni.get('/cgi/id/users/me') set({ account: resp.data }) console.log('[Authenticator] Logged in as @' + resp.data.name) return resp.data } catch (err) { console.error('[Authenticator] Unable to get user profile: ', err) return } }, }))