File size: 4,381 Bytes
59cd560
d607a39
59cd560
58ae063
59cd560
16c1c4c
b200338
 
59cd560
b200338
 
 
58ae063
59cd560
 
b200338
 
 
 
 
c433511
b200338
 
 
 
 
 
 
 
 
 
2fc767f
59cd560
b200338
59cd560
 
 
 
b200338
 
 
 
 
 
 
59cd560
 
 
 
b200338
 
59cd560
 
b200338
 
 
59cd560
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c433511
59cd560
b200338
 
59cd560
c433511
b200338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59cd560
b200338
 
59cd560
 
b200338
 
 
 
 
c433511
b200338
59cd560
b200338
 
2fc767f
b200338
 
 
 
84f514d
b200338
 
 
 
 
 
 
ee999fe
b200338
 
58ae063
b200338
 
 
 
 
 
 
58ae063
 
 
59cd560
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { tokenManager } from "./token";
import { Client } from "./client";
import { WhoAmIUser } from "@huggingface/hub";

const baseUrl = "https://huggingface.co/api";

const showAuthenticatedUI = async () => {
  const accessToken = tokenManager.getAccessToken();

  if (!accessToken) {
    throw new Error("Access token not found");
  }

  const client = new Client(baseUrl, accessToken);

  // Hide token form
  const tokenForm = document.getElementById("token-form");
  if (tokenForm) {
    tokenForm.style.display = "none";
  }

  // Show repo form and signout button
  const repoForm = document.getElementById("repo-form");
  const signoutButton = document.getElementById("signout");
  if (repoForm) {
    repoForm.style.removeProperty("display");
  }
  if (signoutButton) {
    signoutButton.style.removeProperty("display");
    signoutButton.onclick = () => tokenManager.logout();
  }

  // Add create repo ui
  const createRepoButton = document.getElementById("create-repo");
  const repoNameInput = <HTMLInputElement>document.getElementById("repo-name");
  const resourceGroupInput = <HTMLInputElement>(
    document.getElementById("resource-group-name")
  );
  const resourceGroupContainer = document.getElementById(
    "resource-group-container"
  );

  if (createRepoButton && repoNameInput) {
    createRepoButton.onclick = async () => {
      const repoName = repoNameInput.value.trim();
      const orgSelect = <HTMLSelectElement>(
        document.getElementById("org-select")
      );

      if (repoName) {
        try {
          const organizationName = orgSelect?.value || undefined;
          const resourceGroupName = organizationName
            ? resourceGroupInput?.value.trim()
            : undefined;

          // create repo
          const repo = await client.post<void>("repos/create", {
            type: "model",
            name: repoName,
            organization: organizationName,
            private: true,
          });

          console.log({ repo });
          // create RG
          if (organizationName && resourceGroupName) {
            await client.post<void>(
              `organizations/${organizationName}/resource-groups`,
              {
                name: resourceGroupName,
                description: `Resource group for repository ${name}`,
                autoJoin: {
                  enabled: true,
                  role: "admin",
                },
              }
            );
          }

          alert("Repository and resource group created successfully!");
        } catch (error) {
          console.error(error);
        }
      }
    };
  }

  // Handle org select change to show/hide resource group input
  const orgSelect = document.getElementById("org-select") as HTMLSelectElement;
  if (orgSelect && resourceGroupContainer) {
    orgSelect.onchange = () => {
      if (orgSelect.value) {
        resourceGroupContainer.style.removeProperty("display");
      } else {
        resourceGroupContainer.style.display = "none";
      }
    };
  }

  try {
    const userInfo = await client.get<WhoAmIUser>("whoami-v2");

    // Populate org select
    if (orgSelect && userInfo.orgs) {
      userInfo.orgs.forEach((org) => {
        const option = document.createElement("option");
        option.value = org.name;
        option.textContent = org.name;
        orgSelect.appendChild(option);
      });
    }
  } catch (error) {
    console.error(error);
  }
};

const showUnauthenticatedUI = () => {
  const tokenForm = document.getElementById("token-form");
  const tokenInput = document.getElementById("token-input") as HTMLInputElement;
  const tokenSubmit = document.getElementById("token-submit");

  if (tokenForm && tokenInput && tokenSubmit) {
    tokenForm.style.removeProperty("display");
    tokenSubmit.onclick = () => {
      const token = tokenInput.value.trim();
      if (token) {
        tokenManager.setToken(token);
        showAuthenticatedUI();
      }
    };
  }

  const repoForm = document.getElementById("repo-form");
  const signoutButton = document.getElementById("signout");
  if (repoForm) {
    repoForm.style.display = "none";
  }
  if (signoutButton) {
    signoutButton.style.display = "none";
  }
};

const init = async (): Promise<void> => {
  if (tokenManager.isAuthenticated()) {
    showAuthenticatedUI();
  } else {
    showUnauthenticatedUI();
  }
};

init();