Files
TelegramMemeVault/miniapp/src/App.svelte
T

96 lines
3.0 KiB
Svelte
Raw Normal View History

2026-10-07 02:17:30 +04:00
<script>
import { onMount } from 'svelte'
import { jwt, isAuthed } from './stores/auth.js'
import { api } from './lib/api.js'
2026-10-09 02:07:18 +04:00
import { initData, applyTheme, isAttachmentMode } from './lib/telegram.js'
2026-10-07 02:17:30 +04:00
import MediaPage from './pages/MediaPage.svelte'
import CollectionsPage from './pages/CollectionsPage.svelte'
2026-10-09 02:07:18 +04:00
import SendPage from './pages/SendPage.svelte'
// Режим: пользователь хочет отправить медиа в чат (Mini App открыта через «Прикрепить»)
const sendMode = isAttachmentMode()
2026-10-07 02:17:30 +04:00
let tab = 'media' // 'media' | 'collections'
let authError = null
let authing = true
function devTokenFromHash() {
if (!import.meta.env.DEV) return null
const params = new URLSearchParams(window.location.hash.slice(1))
return params.get('token') ?? null
}
onMount(async () => {
applyTheme()
// Dev-режим: #token=<jwt> в URL — пропускаем обмен initData
const devToken = devTokenFromHash()
if (devToken) {
jwt.set(devToken)
authing = false
return
}
try {
const { token } = await api.auth(initData)
jwt.set(token)
} catch (e) {
authError = e.message
} finally {
authing = false
}
})
</script>
<main>
{#if authing}
<div class="splash">Загрузка…</div>
{:else if authError}
<div class="splash error">Ошибка авторизации:<br/>{authError}</div>
{:else if $isAuthed}
2026-10-09 02:07:18 +04:00
{#if sendMode}
<!-- ── Режим отправки: только поиск и листинг ───────────────────────── -->
<SendPage />
2026-10-07 02:17:30 +04:00
{:else}
2026-10-09 02:07:18 +04:00
<!-- ── Режим управления: полный интерфейс с вкладками ──────────────── -->
<nav>
<button class:active={tab === 'media'} onclick={() => (tab = 'media')}>Медиа</button>
<button class:active={tab === 'collections'} onclick={() => (tab = 'collections')}>Коллекции</button>
</nav>
{#if tab === 'media'}
<MediaPage />
{:else}
<CollectionsPage />
{/if}
2026-10-07 02:17:30 +04:00
{/if}
2026-10-09 02:07:18 +04:00
2026-10-07 02:17:30 +04:00
{/if}
</main>
<style>
main { display: flex; flex-direction: column; height: 100vh; }
.splash {
display: flex; align-items: center; justify-content: center;
height: 100vh; color: var(--tg-hint, #888); font-size: 16px; text-align: center; padding: 24px;
}
.error { color: #e53935; }
nav {
display: flex; border-bottom: 1px solid var(--tg-hint, #ccc);
background: var(--tg-bg, #fff); position: sticky; top: 0; z-index: 10;
}
nav button {
flex: 1; padding: 12px; border: none; background: none;
color: var(--tg-hint, #888); font-size: 15px; cursor: pointer;
border-bottom: 2px solid transparent;
}
nav button.active {
color: var(--tg-button, #2196f3);
border-bottom-color: var(--tg-button, #2196f3);
font-weight: 600;
}
</style>