Files
TelegramMemeVault/miniapp/src/pages/SendPage.svelte
T

254 lines
8.2 KiB
Svelte

<script>
import { User, Users, Megaphone } from 'lucide-svelte'
import { onMount } from 'svelte'
import { api } from '../lib/api.js'
import {
getQueryId, getChatName, getChatType,
isInlineMode, switchInlineQuery,
close as tgClose, showAlert,
} from '../lib/telegram.js'
const inlineMode = isInlineMode()
const queryId = getQueryId()
const chatName = getChatName()
const chatType = getChatType()
const PREVIEWABLE = new Set(['Photo', 'Video', 'Animation', 'Sticker'])
let items = $state([])
let query = $state('')
let loading = $state(true)
let error = $state(null)
let sending = $state(null) // id медиа, которое сейчас отправляется
let previews = $state({})
async function load() {
loading = true; error = null; previews = {}
try {
items = await api.getMedia(query)
for (const item of items) {
if (PREVIEWABLE.has(item.mediaType)) loadPreview(item.id)
}
} catch (e) {
error = e.message
} finally {
loading = false
}
}
async function loadPreview(id) {
previews = { ...previews, [id]: { url: null, loading: true, error: false } }
try {
const url = await api.getDownloadUrl(id)
previews = { ...previews, [id]: { url, loading: false, error: false } }
} catch {
previews = { ...previews, [id]: { url: null, loading: false, error: true } }
}
}
async function send(id) {
if (sending) return
sending = id
if (inlineMode) {
// Inline-режим: передаём управление боту через switchInlineQuery.
// Telegram вставляет «@bot send:<id>» в поле ввода — пользователь тапает результат.
switchInlineQuery(`send:${id}`)
// switchInlineQuery закрывает Mini App сам, но на случай если нет — подстрахуемся
tgClose()
return
}
// Attachment-режим (query_id): отправляем через answerWebAppQuery
try {
await api.sendToChat(id, queryId)
tgClose()
} catch (e) {
showAlert(e.message)
sending = null
}
}
let searchTimer
function onSearch() {
clearTimeout(searchTimer)
searchTimer = setTimeout(load, 400)
}
onMount(load)
</script>
<div class="page">
<div class="header">
<div class="title">{inlineMode ? 'Выберите медиа' : 'Отправить в чат'}</div>
{#if chatName && !inlineMode}
<div class="chat-name">
{#if chatType === 'private'}
<User size={14} strokeWidth={2} />
{:else if chatType === 'channel'}
<Megaphone size={14} strokeWidth={2} />
{:else if chatType === 'group' || chatType === 'supergroup'}
<Users size={14} strokeWidth={2} />
{/if}
{chatName}
</div>
{/if}
</div>
<input
class="search"
placeholder="Поиск: #мем -#кот слово"
bind:value={query}
oninput={onSearch}
/>
{#if loading}
<div class="hint">Загрузка…</div>
{:else if error}
<div class="hint error">{error}</div>
{:else if items.length === 0}
<div class="hint">Нет медиа{query ? ' по запросу' : ''}</div>
{:else}
<ul class="list">
{#each items as item (item.id)}
{@const preview = previews[item.id]}
{@const isSending = sending === item.id}
<!-- svelte-ignore a11y-click-events-have-key-events a11y-no-noninteractive-element-interactions -->
<li
class="item"
class:item--sending={isSending}
onclick={() => send(item.id)}
role="button"
>
{#if PREVIEWABLE.has(item.mediaType)}
<div class="thumb-wrap">
{#if preview?.loading || !preview}
<div class="thumb-skeleton"></div>
{:else if preview.url}
{#if item.mediaType === 'Video'}
<video class="thumb" src={preview.url} muted preload="metadata" />
{:else}
<img class="thumb" src={preview.url} alt={item.caption ?? item.mediaType} loading="lazy" />
{/if}
{:else}
<div class="thumb-error">🖼</div>
{/if}
</div>
{:else}
<div class="thumb-wrap thumb-text">
<span class="text-icon">T</span>
</div>
{/if}
<div class="item-info">
<span class="item-type">{item.mediaType}</span>
{#if item.caption}
<span class="item-caption">{item.caption}</span>
{/if}
{#if item.tags?.length}
<div class="tags">
{#each item.tags as tag}
<span class="tag">#{tag}</span>
{/each}
</div>
{/if}
</div>
<span class="send-label">
{#if isSending}…{:else}→{/if}
</span>
</li>
{/each}
</ul>
{/if}
</div>
<style>
.page { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
.header {
padding: 12px 12px 4px;
}
.title {
font-size: 16px; font-weight: 600; color: var(--tg-text, #000);
}
.chat-name {
font-size: 13px; color: var(--tg-hint, #888); margin-top: 2px;
white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
display: flex; align-items: center; gap: 4px;
}
.chat-name :global(svg) { flex-shrink: 0; color: var(--tg-hint, #888); }
.search {
margin: 4px 8px 6px; width: calc(100% - 16px);
padding: 8px 12px; border-radius: 8px; border: none;
background: var(--tg-secondary-bg, #f0f0f0);
color: var(--tg-text, #000); font-size: 14px;
}
.hint { text-align: center; padding: 32px; color: var(--tg-hint, #888); }
.error { color: #e53935; }
.list {
list-style: none; margin: 0; padding: 0 8px 8px;
display: flex; flex-direction: column; gap: 6px;
overflow-y: auto; flex: 1;
}
.item {
display: flex; align-items: center; gap: 10px;
background: var(--tg-secondary-bg, #f5f5f5);
border-radius: 10px; padding: 10px 12px;
cursor: pointer; transition: opacity .15s;
-webkit-tap-highlight-color: transparent;
}
.item:active, .item--sending { opacity: 0.6; }
/* ── Thumbnail ─────────────────────────────────────────────────────── */
.thumb-wrap {
flex-shrink: 0; width: 52px; height: 52px;
border-radius: 6px; overflow: hidden;
}
.thumb { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb-skeleton {
width: 100%; height: 100%;
background: linear-gradient(90deg,
var(--tg-secondary-bg, #e0e0e0) 25%,
var(--tg-bg, #f5f5f5) 50%,
var(--tg-secondary-bg, #e0e0e0) 75%);
background-size: 200% 100%;
animation: shimmer 1.4s infinite;
}
.thumb-error {
width: 100%; height: 100%;
display: flex; align-items: center; justify-content: center; font-size: 20px;
background: var(--tg-secondary-bg, #eee);
}
.thumb-text {
background: var(--tg-secondary-bg, #e8e8e8);
display: flex; align-items: center; justify-content: center;
}
.text-icon { font-size: 20px; font-weight: 700; color: var(--tg-hint, #888); }
@keyframes shimmer {
0% { background-position: 200% 0; }
100% { background-position: -200% 0; }
}
/* ── Text info ─────────────────────────────────────────────────────── */
.item-info { flex: 1; min-width: 0; }
.item-type { font-size: 11px; color: var(--tg-hint, #888); text-transform: uppercase; }
.item-caption { display: block; font-size: 14px; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tags { display: flex; flex-wrap: wrap; gap: 3px; margin-top: 4px; }
.tag {
font-size: 11px; background: var(--tg-button, #2196f3);
color: var(--tg-btn-text, #fff); border-radius: 4px; padding: 1px 5px;
}
.send-label {
font-size: 18px; color: var(--tg-button, #2196f3);
flex-shrink: 0; font-weight: 500;
}
</style>