Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 1,815 Bytes
79e869e 8816e9f 79e869e 8816e9f 79e869e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
import { token } from "./token.svelte.js";
interface Org {
type: "org";
id: string;
name: string;
fullname: string;
avatarUrl: string;
roleInOrg: string;
isEnterprise: boolean;
plan?: string;
}
interface WhoamiResponse {
type: "user";
id: string;
name: string;
fullname: string;
email: string;
avatarUrl: string;
orgs: Org[];
}
class User {
#info = $state<WhoamiResponse | null>(null);
#loading = $state(false);
#error = $state<string | null>(null);
constructor() {
$effect.root(() => {
$effect(() => {
if (token.value) {
this.fetchUserInfo();
} else {
this.#info = null;
}
});
});
}
get info() {
return this.#info;
}
get loading() {
return this.#loading;
}
get error() {
return this.#error;
}
get orgs() {
return this.#info?.orgs ?? [];
}
get paidOrgs() {
return this.orgs.filter(org => org.plan === "team" || org.plan === "enterprise");
}
get name() {
return this.#info?.name ?? "";
}
get fullname() {
return this.#info?.fullname ?? "";
}
get avatarUrl() {
return this.#info?.avatarUrl ?? "";
}
async fetchUserInfo() {
if (!token.value) {
this.#info = null;
return;
}
this.#loading = true;
this.#error = null;
try {
const response = await fetch("https://huggingface.co/api/whoami-v2", {
headers: {
Authorization: `Bearer ${token.value}`,
},
});
if (!response.ok) {
throw new Error(`Failed to fetch user info: ${response.statusText}`);
}
this.#info = await response.json();
} catch (error) {
this.#error = error instanceof Error ? error.message : "Unknown error";
console.error("Failed to fetch user info:", error);
} finally {
this.#loading = false;
}
}
reset() {
this.#info = null;
this.#error = null;
}
}
export const user = new User();
|