Something went wrong. Try again.
A streaming bot for Streamplace forked from bot.timtinkers.online/streamplace-bot
Something went wrong. Try again.
4.8 kB · 253 lines
Svelte
at frontend
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254<script lang="ts"> import { useAuth } from "$lib/auth/oauth.svelte"; import Login from "./Login.svelte"; import { atprotoCache } from "$lib/classes/atprotoCache"; import type { Blob } from "@atcute/lexicons";
// `fullWidth` renders the button stretched to fill its container, used // when this component sits inside the mobile dropdown instead of the // desktop header bar. // `onAction` fires after a click that should close the mobile menu // (opening the modal, or completing a logout). let { fullWidth = false, onAction, }: { fullWidth?: boolean; onAction?: () => void; } = $props();
const auth = useAuth();
let modalOpen = $state(false); let signingOut = $state(false); let closeBtnEl: HTMLButtonElement | undefined = $state(); let avatarUrl = $state<string | undefined>();
// Re-resolve the avatar whenever the logged-in DID changes (login, // logout, or account switch). Guards against a slow lookup resolving // after the DID has already moved on. $effect(() => { const did = auth.did;
if (!did) { avatarUrl = undefined; return; }
let cancelled = false;
(async () => { try { const profile = await atprotoCache.resolve(did); const avatarRef = (profile?.actorProfile?.avatar as Blob | undefined) ?.ref?.$link; const url = avatarRef ? `https://cdn.bsky.app/img/feed_thumbnail/plain/${did}/${avatarRef}@jpeg` : undefined;
if (!cancelled) avatarUrl = url; } catch (err) { console.error("failed to load avatar", err); if (!cancelled) avatarUrl = undefined; } })();
return () => { cancelled = true; }; });
function openModal() { modalOpen = true; onAction?.(); }
function closeModal() { modalOpen = false; }
async function onLogoutClick() { signingOut = true; try { await auth.logout(); } finally { signingOut = false; } onAction?.(); }
function onBackdropKeydown(e: KeyboardEvent) { if (e.key === "Escape") closeModal(); }
$effect(() => { if (modalOpen) closeBtnEl?.focus(); });</script>
{#if auth.ready} {#if auth.did} <div class="account" class:full-width={fullWidth}> <a class="avatar-link" href={`/${auth.did}`} aria-label={auth.handle ? `@${auth.handle}'s profile` : "View profile"} > {#if avatarUrl} <img src={avatarUrl} alt="" class="avatar" /> {:else} <div class="avatar avatar-placeholder" aria-hidden="true"></div> {/if} </a> <button class="auth-btn logout" class:full-width={fullWidth} onclick={onLogoutClick} disabled={signingOut} > {signingOut ? "Signing out…" : "Log out"} </button> </div> {:else} <button class="auth-btn login" class:full-width={fullWidth} onclick={openModal} > Log in </button> {/if}{/if}
{#if modalOpen} <div class="modal-backdrop" role="presentation" onclick={closeModal} onkeydown={onBackdropKeydown} > <div class="modal-content" role="dialog" aria-modal="true" aria-label="Log in" onclick={(e) => e.stopPropagation()} onkeydown={(e) => e.stopPropagation()} > <button class="modal-close" bind:this={closeBtnEl} onclick={closeModal} aria-label="Close" > × </button> <Login /> </div> </div>{/if}
<style> .account { display: flex; align-items: center; gap: 0.6rem; }
.account.full-width { width: 100%; }
.account.full-width .auth-btn { flex: 1; }
.avatar-link { display: flex; flex-shrink: 0; }
.avatar { width: 2rem; height: 2rem; border-radius: 50%; object-fit: cover; display: block; }
.avatar-placeholder { background: var(--mountain-near); }
.auth-btn { font-family: var(--font-sans); background: transparent; color: var(--text); border: 1px solid var(--hill-front); border-radius: 999px; padding: 0.4rem 1rem; font-size: 0.8rem; font-weight: 600; cursor: pointer; white-space: nowrap; transition: opacity 0.15s ease, background 0.15s ease; }
.auth-btn.login { background: var(--hill-front); color: var(--bg); border-color: var(--hill-front); }
.auth-btn:hover:not(:disabled) { opacity: 0.85; }
.auth-btn:disabled { opacity: 0.6; cursor: default; }
.auth-btn.full-width { width: 100%; text-align: center; border-radius: 8px; }
.modal-backdrop { position: fixed; inset: 0; background: rgba(10, 10, 13, 0.72); display: flex; align-items: center; justify-content: center; padding: 1rem; z-index: 100; }
.modal-content { position: relative; max-width: 26rem; width: 100%; }
.modal-close { position: absolute; top: -2.5rem; right: 0; background: none; border: none; color: var(--text); font-size: 1.75rem; line-height: 1; cursor: pointer; padding: 0.25rem 0.5rem; }
.modal-close:hover { color: var(--hill-front); }</style>