too many changes

This commit is contained in:
2026-10-07 02:17:30 +04:00
parent 0c7f3bce90
commit bcf985c17d
74 changed files with 4048 additions and 78 deletions
+280
View File
@@ -0,0 +1,280 @@
<script>
import { onMount } from 'svelte'
import { Pencil, Trash2, Star, Check, X } from 'lucide-svelte'
import { api } from '../lib/api.js'
import { showAlert } from '../lib/telegram.js'
let collections = $state([])
let loading = $state(true)
let error = $state(null)
let creating = $state(false)
let newName = $state('')
// Inline-редактирование: { id, value }
let editing = $state(null)
// Подтверждение удаления: id коллекции, которую хотим удалить
let confirmDelete = $state(null)
// Подтверждение смены Inbox: id целевой коллекции
let confirmInbox = $state(null)
$derived: var deleteTarget = collections.find(c => c.id === confirmDelete)
$derived: var inboxTarget = collections.find(c => c.id === confirmInbox)
async function load() {
loading = true; error = null
try { collections = await api.getCollections() }
catch (e) { error = e.message }
finally { loading = false }
}
async function createCollection() {
if (!newName.trim()) return
creating = true
try {
await api.createCollection(newName.trim(), null)
newName = ''
await load()
} catch (e) {
showAlert(e.message)
} finally { creating = false }
}
// ── Редактирование ─────────────────────────────────────────────────────────
function startEdit(col) {
editing = { id: col.id, value: col.name }
}
async function commitEdit() {
if (!editing || !editing.value.trim()) { editing = null; return }
try {
await api.updateCollection(editing.id, editing.value.trim(), null)
const idx = collections.findIndex(c => c.id === editing.id)
if (idx !== -1) collections[idx] = { ...collections[idx], name: editing.value.trim() }
editing = null
} catch (e) {
showAlert(e.message)
}
}
function cancelEdit() { editing = null }
// ── Удаление ───────────────────────────────────────────────────────────────
async function commitDelete() {
if (!confirmDelete) return
try {
await api.deleteCollection(confirmDelete)
collections = collections.filter(c => c.id !== confirmDelete)
confirmDelete = null
} catch (e) {
showAlert(e.message)
confirmDelete = null
}
}
// ── Смена Inbox ────────────────────────────────────────────────────────────
async function commitSetInbox() {
if (!confirmInbox) return
try {
await api.setCollectionInbox(confirmInbox)
collections = collections.map(c => ({
...c,
isInbox: c.id === confirmInbox,
}))
confirmInbox = null
} catch (e) {
showAlert(e.message)
confirmInbox = null
}
}
onMount(load)
</script>
<div class="page">
<!-- Форма создания -->
<div class="new-row">
<input
class="new-input"
placeholder="Новая коллекция…"
bind:value={newName}
onkeydown={(e) => e.key === 'Enter' && createCollection()}
disabled={creating}
/>
<button class="btn-create" onclick={createCollection} disabled={creating || !newName.trim()}>+</button>
</div>
{#if loading}
<div class="hint">Загрузка…</div>
{:else if error}
<div class="hint error">{error}</div>
{:else if collections.length === 0}
<div class="hint">Нет коллекций</div>
{:else}
<ul class="list">
{#each collections as col (col.id)}
<li class="item">
<!-- Имя / inline-редактор -->
<div class="col-info">
{#if editing?.id === col.id}
<input
class="edit-input"
bind:value={editing.value}
onkeydown={(e) => {
if (e.key === 'Enter') commitEdit()
if (e.key === 'Escape') cancelEdit()
}}
autofocus
/>
{:else}
<div class="col-name">
{col.name}
{#if col.isInbox}
<span class="badge-inbox">основная</span>
{/if}
</div>
{#if col.itemCount > 0}
<div class="col-meta">{col.itemCount} эл.</div>
{/if}
{/if}
</div>
<!-- Кнопки действий -->
<div class="actions">
{#if editing?.id === col.id}
<button class="icon-btn ok" onclick={commitEdit} title="Сохранить"><Check size={16}/></button>
<button class="icon-btn cancel" onclick={cancelEdit} title="Отмена"> <X size={16}/></button>
{:else}
{#if !col.isInbox}
<button class="icon-btn" onclick={() => startEdit(col)} title="Переименовать">
<Pencil size={16}/>
</button>
<button class="icon-btn star" onclick={() => confirmInbox = col.id} title="Сделать основной">
<Star size={16}/>
</button>
<button class="icon-btn del" onclick={() => confirmDelete = col.id} title="Удалить">
<Trash2 size={16}/>
</button>
{/if}
{/if}
</div>
</li>
<!-- Подтверждение удаления -->
{#if confirmDelete === col.id && deleteTarget}
<li class="confirm-panel">
<div class="confirm-text">
{#if deleteTarget.itemCount > 0}
⚠️ Коллекция содержит <strong>{deleteTarget.itemCount}&nbsp;эл.</strong> — они будут удалены безвозвратно.
{:else}
Удалить «{deleteTarget.name}»?
{/if}
</div>
<div class="confirm-actions">
<button class="btn-danger" onclick={commitDelete}>Удалить</button>
<button class="btn-ghost" onclick={() => confirmDelete = null}>Отмена</button>
</div>
</li>
{/if}
<!-- Подтверждение смены Inbox -->
{#if confirmInbox === col.id && inboxTarget}
<li class="confirm-panel">
<div class="confirm-text">
Сделать «{inboxTarget.name}» основной коллекцией? Новые медиа будут сохраняться в неё.
</div>
<div class="confirm-actions">
<button class="btn-primary" onclick={commitSetInbox}>Подтвердить</button>
<button class="btn-ghost" onclick={() => confirmInbox = null}>Отмена</button>
</div>
</li>
{/if}
{/each}
</ul>
{/if}
</div>
<style>
.page { padding: 8px; }
/* Создание */
.new-row { display: flex; gap: 6px; margin-bottom: 8px; }
.new-input {
flex: 1; padding: 8px 12px; border-radius: 8px; border: none;
background: var(--tg-secondary-bg, #f0f0f0);
color: var(--tg-text, #000); font-size: 14px;
}
.btn-create {
width: 36px; border-radius: 8px; border: none;
background: var(--tg-button, #2196f3); color: var(--tg-btn-text, #fff);
font-size: 20px; cursor: pointer;
}
.btn-create:disabled { opacity: .4; cursor: default; }
/* Список */
.hint { text-align: center; padding: 32px; color: var(--tg-hint, #888); }
.error { color: #e53935; }
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.item {
display: flex; align-items: center; justify-content: space-between;
background: var(--tg-secondary-bg, #f5f5f5);
border-radius: 10px; padding: 10px 10px 10px 12px;
gap: 8px;
}
.col-info { flex: 1; min-width: 0; }
.col-name {
font-size: 15px; font-weight: 500;
white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
display: flex; align-items: center; gap: 6px;
}
.col-meta { font-size: 12px; color: var(--tg-hint, #888); margin-top: 2px; }
.badge-inbox {
font-size: 11px; font-weight: 600; letter-spacing: .02em;
background: var(--tg-button, #2196f3); color: var(--tg-btn-text, #fff);
border-radius: 4px; padding: 1px 5px;
}
/* Inline-редактор */
.edit-input {
width: 100%; padding: 4px 8px; border-radius: 6px; border: none;
background: var(--tg-bg, #fff); color: var(--tg-text, #000);
font-size: 15px; font-weight: 500;
outline: 2px solid var(--tg-button, #2196f3);
}
/* Кнопки-иконки */
.actions { display: flex; gap: 2px; flex-shrink: 0; }
.icon-btn {
display: flex; align-items: center; justify-content: center;
width: 32px; height: 32px; border-radius: 8px; border: none;
background: none; color: var(--tg-hint, #999); cursor: pointer;
transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--tg-secondary-bg, #e8e8e8); color: var(--tg-text, #000); }
.icon-btn.star:hover { color: #f5a623; }
.icon-btn.del:hover { color: #e53935; }
.icon-btn.ok { color: #43a047; }
.icon-btn.cancel { color: var(--tg-hint, #999); }
/* Подтверждение */
.confirm-panel {
background: var(--tg-secondary-bg, #f5f5f5);
border-radius: 10px; padding: 10px 12px;
border-left: 3px solid var(--tg-button, #2196f3);
}
.confirm-text { font-size: 13px; color: var(--tg-text, #000); margin-bottom: 8px; line-height: 1.4; }
.confirm-actions { display: flex; gap: 6px; }
.btn-danger, .btn-primary, .btn-ghost {
padding: 6px 14px; border-radius: 8px; border: none;
font-size: 13px; font-weight: 500; cursor: pointer;
}
.btn-danger { background: #e53935; color: #fff; }
.btn-primary { background: var(--tg-button, #2196f3); color: var(--tg-btn-text, #fff); }
.btn-ghost { background: none; color: var(--tg-hint, #888); }
.btn-ghost:hover { background: var(--tg-secondary-bg, #e8e8e8); }
</style>
+227
View File
@@ -0,0 +1,227 @@
<script>
import { onMount } from 'svelte'
import { api } from '../lib/api.js'
import { isAttachmentMode, getQueryId, close as tgClose, showAlert } from '../lib/telegram.js'
const attachMode = isAttachmentMode()
const queryId = getQueryId()
// Types that have a downloadable file preview
const PREVIEWABLE = new Set(['Photo', 'Video', 'Animation', 'Sticker'])
let items = []
let query = ''
let loading = true
let error = null
// Map of itemId → { url: string|null, loading: boolean, error: boolean }
let previews = {}
async function load() {
loading = true
error = null
previews = {}
try {
items = await api.getMedia(query)
// Kick off preview loading for each previewable item (fire & forget)
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[id] = { url: null, loading: true, error: false }
try {
const url = await api.getDownloadUrl(id)
previews[id] = { url, loading: false, error: false }
} catch {
previews[id] = { url: null, loading: false, error: true }
}
}
async function handleSendToChat(id) {
try {
await api.sendToChat(id, queryId)
tgClose()
} catch (e) {
showAlert(e.message)
}
}
async function handleDelete(id) {
try {
await api.deleteMedia(id)
items = items.filter((i) => i.id !== id)
const { [id]: _, ...rest } = previews
previews = rest
} catch (e) {
showAlert(e.message)
}
}
let searchTimer
function onSearch() {
clearTimeout(searchTimer)
searchTimer = setTimeout(load, 400)
}
onMount(load)
</script>
<div class="page">
{#if attachMode}
<div class="attach-banner">Выберите медиа для отправки в чат</div>
{/if}
<input
class="search"
placeholder="Поиск: #мем -#кот слово"
bind:value={query}
on:input={onSearch}
/>
{#if loading}
<div class="hint">Загрузка…</div>
{:else if error}
<div class="hint error">{error}</div>
{:else if items.length === 0}
<div class="hint">Нет медиа</div>
{:else}
<ul class="list">
{#each items as item (item.id)}
{@const preview = previews[item.id]}
<!-- svelte-ignore a11y-click-events-have-key-events a11y-no-noninteractive-element-interactions -->
<li
class="item"
class:item--attach={attachMode}
on:click={attachMode ? () => handleSendToChat(item.id) : undefined}
role={attachMode ? 'button' : undefined}
>
{#if PREVIEWABLE.has(item.mediaType)}
<div class="thumb-wrap">
{#if preview?.loading || !preview}
<div class="thumb-skeleton" aria-hidden="true"></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" title="Не удалось загрузить">🖼</div>
{/if}
</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>
{#if attachMode}
<span class="attach-hint">Отправить</span>
{:else}
<button class="del" on:click={() => handleDelete(item.id)} title="Удалить">✕</button>
{/if}
</li>
{/each}
</ul>
{/if}
</div>
<style>
.page { padding: 8px; }
.attach-banner {
text-align: center;
font-size: 13px;
color: var(--tg-hint, #888);
padding: 6px 0 4px;
}
.search {
width: 100%; box-sizing: border-box;
padding: 8px 12px; border-radius: 8px; border: none;
background: var(--tg-secondary-bg, #f0f0f0);
color: var(--tg-text, #000);
font-size: 14px; margin-bottom: 8px;
}
.hint { text-align: center; padding: 32px; color: var(--tg-hint, #888); }
.error { color: #e53935; }
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.item {
display: flex; align-items: flex-start; gap: 10px;
background: var(--tg-secondary-bg, #f5f5f5);
border-radius: 10px; padding: 10px 12px;
}
/* ── Thumbnail ──────────────────────────────────────────────────────────── */
.thumb-wrap {
flex-shrink: 0;
width: 64px; height: 64px;
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;
border-radius: 6px;
}
.thumb-error {
width: 100%; height: 100%;
display: flex; align-items: center; justify-content: center;
font-size: 24px;
background: var(--tg-secondary-bg, #eee);
}
@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: 4px; margin-top: 4px; }
.tag {
font-size: 12px; background: var(--tg-button, #2196f3);
color: var(--tg-btn-text, #fff); border-radius: 4px; padding: 1px 6px;
}
.item--attach {
cursor: pointer;
}
.item--attach:active {
opacity: 0.7;
}
.attach-hint {
font-size: 12px;
color: var(--tg-button, #2196f3);
white-space: nowrap;
flex-shrink: 0;
align-self: center;
}
.del {
background: none; border: none; color: var(--tg-hint, #888);
font-size: 16px; cursor: pointer; padding: 0; flex-shrink: 0;
align-self: flex-start;
}
</style>