import { useCallback, useEffect, useState } from 'react' import { FlatList, Image, Pressable, RefreshControl, StyleSheet, Text, View } from 'react-native' import { useRouter } from 'expo-router' import Ionicons from '@expo/vector-icons/Ionicons' import { useSafeAreaInsets } from 'react-native-safe-area-context' import type { HistoryItem } from '../../src/lib/types' import { api, imageUrl } from '../../src/lib/api' import { useAuth } from '../../src/lib/AuthContext' import { colors, radius, spacing } from '../../src/lib/theme' import Loader from '../../src/components/Loader' function formatDate(iso: string) { return new Date(iso).toLocaleString('ru-RU', { day: '2-digit', month: '2-digit', hour: '2-digit', minute: '2-digit', }) } export default function HistoryScreen() { const router = useRouter() const insets = useSafeAreaInsets() const { user, loading: authLoading } = useAuth() const [items, setItems] = useState(null) const [refreshing, setRefreshing] = useState(false) const load = useCallback(() => { return api .historyList() .then((res) => setItems(res.data.filter((item) => item.release && item.release_episode))) .catch(() => setItems([])) }, []) useEffect(() => { if (!user) return load() }, [user, load]) async function onRefresh() { setRefreshing(true) await load() setRefreshing(false) } return ( router.back()} hitSlop={10} style={styles.backButton}> История просмотра {authLoading && } {!authLoading && !user && ( Войдите, чтобы видеть историю просмотра router.push('/login')}> Войти )} {!authLoading && user && !items && } {!authLoading && user && items && items.length === 0 && ( История пуста )} {!authLoading && user && items && items.length > 0 && ( String(item.id)} contentContainerStyle={[styles.list, { paddingBottom: insets.bottom + spacing(6) }]} refreshControl={ } renderItem={({ item }) => { const thumb = imageUrl( item.release.poster?.optimized?.thumbnail || item.release.poster?.thumbnail ) return ( router.push(`/title/${item.release.alias || item.release.id}`)} > {thumb && } {item.release.name.main} Серия {item.release_episode.ordinal} {item.release_episode.name ? ` — ${item.release_episode.name}` : ''} {formatDate(item.updated_at)} {item.is_watched ? ( Просмотрено ) : ( В процессе )} ) }} /> )} ) } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: colors.background, }, header: { flexDirection: 'row', alignItems: 'center', gap: spacing(3), paddingHorizontal: spacing(4), paddingBottom: spacing(3), }, backButton: { width: 36, height: 36, borderRadius: 18, alignItems: 'center', justifyContent: 'center', backgroundColor: colors.surfaceRaised, }, headerTitle: { color: colors.text, fontSize: 19, fontWeight: '700', }, empty: { flex: 1, alignItems: 'center', justifyContent: 'center', paddingHorizontal: spacing(8), gap: spacing(4), }, emptyText: { color: colors.textFaint, fontSize: 14, textAlign: 'center', lineHeight: 20, }, loginButton: { backgroundColor: colors.accent, paddingHorizontal: spacing(6), paddingVertical: spacing(3), borderRadius: 999, }, loginButtonText: { color: colors.text, fontSize: 14, fontWeight: '700', }, list: { padding: spacing(4), gap: spacing(2), }, row: { flexDirection: 'row', alignItems: 'center', gap: spacing(3), backgroundColor: colors.surfaceCard, padding: spacing(2.5), borderRadius: radius.md, }, thumb: { width: 48, height: 64, borderRadius: radius.sm, backgroundColor: colors.surfaceRaised, }, rowInfo: { flex: 1, minWidth: 0, gap: spacing(0.5), }, rowTitle: { color: colors.text, fontSize: 14, fontWeight: '600', }, rowMeta: { color: colors.textFaint, fontSize: 12, }, rowDate: { color: colors.textFaint, fontSize: 11, }, badgeMuted: { backgroundColor: colors.surfaceRaised, paddingHorizontal: spacing(2), paddingVertical: spacing(1), borderRadius: radius.sm, }, badgeMutedText: { color: colors.textDim, fontSize: 10, fontWeight: '700', }, badgeAccent: { backgroundColor: 'rgba(254,54,53,0.2)', paddingHorizontal: spacing(2), paddingVertical: spacing(1), borderRadius: radius.sm, }, badgeAccentText: { color: colors.accent, fontSize: 10, fontWeight: '700', }, })