({ skipModelPick = false } = {})
| 483 | } |
| 484 | |
| 485 | async function updateAuthUI({ skipModelPick = false } = {}) { |
| 486 | const loggedOutEl = $("authLoggedOut"); |
| 487 | const loggedInEl = $("authLoggedIn"); |
| 488 | if (!loggedOutEl || !loggedInEl) return; |
| 489 | |
| 490 | $("authLoginBtn").onclick = () => { |
| 491 | window.location.href = getAuthorizeUrl(); |
| 492 | }; |
| 493 | $("authLogoutBtn").onclick = () => { |
| 494 | clearApiKey(); |
| 495 | updateAuthUI(); |
| 496 | notify("Logged out. Log in to use CatGPT."); |
| 497 | }; |
| 498 | |
| 499 | const generatorSection = document.querySelector(".generator-section"); |
| 500 | const heroHeader = document.querySelector("header"); |
| 501 | |
| 502 | const apiKey = getStoredApiKey(); |
| 503 | if (!apiKey) { |
| 504 | show(loggedOutEl); |
| 505 | hide(loggedInEl); |
| 506 | hide(generatorSection); |
| 507 | show(heroHeader); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | hide(loggedOutEl); |
| 512 | show(loggedInEl); |
| 513 | show(generatorSection); |
| 514 | hide(heroHeader); |
| 515 | $("authApiKey").textContent = `${apiKey.substring(0, 4)}••••••••`; |
| 516 | |
| 517 | // Pick best available model (skip if URL param already set one) |
| 518 | if (!skipModelPick) { |
| 519 | pickModel(apiKey).then(({ model, isPremium }) => { |
| 520 | setActiveModel(model); |
| 521 | const upsellEl = $("modelUpsell"); |
| 522 | isPremium ? hide(upsellEl) : show(upsellEl); |
| 523 | }); |
| 524 | } |
| 525 | |
| 526 | try { |
| 527 | const [profile, balance] = await Promise.all([ |
| 528 | fetchProfile(apiKey).catch(() => null), |
| 529 | fetchBalance(apiKey).catch(() => null), |
| 530 | ]); |
| 531 | |
| 532 | if (profile) { |
| 533 | const name = profile.githubUsername || "User"; |
| 534 | $("authUserName").textContent = name; |
| 535 | |
| 536 | const avatarImg = $("authAvatar"); |
| 537 | const avatarFallback = $("authAvatarFallback"); |
| 538 | if (profile.image) { |
| 539 | avatarImg.src = profile.image; |
| 540 | show(avatarImg); |
| 541 | hide(avatarFallback); |
| 542 | } else { |
no test coverage detected