From f83028b105ef6f258236dbad50554a439746eeb1 Mon Sep 17 00:00:00 2001 From: Drobysh Date: Sun, 27 Sep 2026 12:16:51 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=84=20Lectures:=20=D0=BE=D1=82=D0=BA?= =?UTF-8?q?=D1=80=D1=8B=D1=82=D0=B8=D0=B5=20+=20=D1=81=D0=BA=D0=B0=D1=87?= =?UTF-8?q?=D0=B8=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D1=87=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=B7=20Telegram=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - api.ts: LectureListItem обновлён (title, file_url, level_code, lecture_language) - LecturesPage.tsx: открытие через Telegram.WebApp.openLink, скачивание через Telegram.WebApp.downloadFile - telegram.d.ts: типы openLink, openTelegramLink, downloadFile --- src/api.ts | 13 +- src/pages/LecturesPage.tsx | 351 ++++++++++++------------------------- src/types/telegram.d.ts | 6 + 3 files changed, 119 insertions(+), 251 deletions(-) diff --git a/src/api.ts b/src/api.ts index 25e71a7..a58637b 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1358,17 +1358,14 @@ export const getWrongBecause = (): Promise<{ items: WrongBecauseItem[] }> => export interface LectureListItem { id: number; - title_pl: string; - title_ru: string; - title_uk: string; - title_en: string; - description_pl: string; - description_ru: string; - description_uk: string; - description_en: string; + level_code: string; + lecture_language: string; + title: string; file_type: string; file_size_kb: number; + file_url: string; order_number: number; + created_at: string | null; } export interface LecturesListResponse { diff --git a/src/pages/LecturesPage.tsx b/src/pages/LecturesPage.tsx index a4d5b26..3995b19 100644 --- a/src/pages/LecturesPage.tsx +++ b/src/pages/LecturesPage.tsx @@ -2,21 +2,21 @@ import { useEffect, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { useLanguage } from '../i18n/LanguageContext'; import { fonts, radius } from '../styles/theme'; -import { getLectures, getLecture, downloadLecture, type LectureListItem, type LectureDetail } from '../api'; +import { getLectures, type LectureListItem } from '../api'; /** * LecturesPage — список лекций по уровню пользователя. - * Открывает PDF/HTML в viewer, даёт скачать. + * + * Открытие: через Telegram.WebApp.openLink() — открывает PDF в браузере. + * Скачивание: через Telegram.WebApp.downloadFile() — скачивает файл. */ const LecturesPage: React.FC = () => { const navigate = useNavigate(); - const { t, language } = useLanguage(); + const { t } = useLanguage(); const [lectures, setLectures] = useState([]); const [level, setLevel] = useState('A1'); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); - const [activeLecture, setActiveLecture] = useState(null); - const [downloading, setDownloading] = useState(false); const brandColor = '#2ECC71'; const lectureColor = '#4FA9E8'; @@ -46,183 +46,51 @@ const LecturesPage: React.FC = () => { }); }, []); - const getTitle = (lecture: LectureListItem): string => { - const field = `title_${language}` as keyof LectureListItem; - return (lecture[field] as string) || lecture.title_en || lecture.title_pl || ''; + const getFileUrl = (lecture: LectureListItem): string => { + return `https://api.filwordspl.com${lecture.file_url}`; }; - const getDescription = (lecture: LectureListItem): string => { - const field = `description_${language}` as keyof LectureListItem; - return (lecture[field] as string) || lecture.description_en || lecture.description_pl || ''; - }; + /** + * Открыть лекцию: в Telegram — через openLink (Safari/Chrome), + * в браузере — через window.open. + */ + const handleOpenLecture = (lecture: LectureListItem) => { + const url = getFileUrl(lecture); - const handleOpenLecture = async (lectureId: number) => { - try { - const detail = await getLecture(lectureId); - setActiveLecture(detail); - } catch (e) { - console.error('Failed to open lecture:', e); - alert(t('loadError') || 'Ошибка загрузки'); + if (window.Telegram?.WebApp?.openLink) { + window.Telegram.WebApp.openLink(url); + } else { + window.open(url, '_blank'); } }; - const handleDownload = async () => { - if (!activeLecture || downloading) return; - setDownloading(true); - try { - const blob = await downloadLecture(activeLecture.id); - const url = URL.createObjectURL(blob); - const a = document.createElement('a'); - a.href = url; - a.download = `${activeLecture.title_pl || 'lecture'}.${activeLecture.file_type}`; - document.body.appendChild(a); - a.click(); - document.body.removeChild(a); - URL.revokeObjectURL(url); - } catch (e) { - console.error('Download failed:', e); - alert(t('loadError') || 'Ошибка скачивания'); - } finally { - setDownloading(false); + /** + * Скачать лекцию: в Telegram — через downloadFile, + * в браузере — через скрытую ссылку. + */ + const handleDownloadLecture = (lecture: LectureListItem) => { + const url = getFileUrl(lecture); + + // Telegram WebApp имеет свой метод downloadFile + if (window.Telegram?.WebApp?.downloadFile) { + window.Telegram.WebApp.downloadFile({ + url, + file_name: `${lecture.title}.${lecture.file_type}`, + }); + return; } + + // Fallback — обычное скачивание через ссылку + const a = document.createElement('a'); + a.href = url; + a.download = `${lecture.title}.${lecture.file_type}`; + a.target = '_blank'; + a.rel = 'noopener noreferrer'; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); }; - // ============ VIEWER ЛЕКЦИИ ============ - if (activeLecture) { - return ( -
-
- -
- - {/* Заголовок */} -
- -

- {activeLecture.title_pl} -

- -
- - {/* Просмотр */} -
- {activeLecture.file_type === 'pdf' ? ( -