import React, { useEffect } from 'react'; import { View, FlatList, Pressable, Alert } from 'react-native'; import { Text } from '../ui/Text'; import { Icon } from '../ui/Icon'; import { useCardStore, Card } from '../../store/cardStore'; import { router } from 'expo-router'; import { Card as UICard } from '../ui/Card'; import { Button } from '../ui/Button'; import { useThemeColors } from '~/utils/themeUtils'; interface CardListProps { deckId: string; isCompact?: boolean; showActions?: boolean; limit?: number; onCardPress?: (card: Card) => void; } interface CardItemProps { card: Card; isCompact: boolean; showActions: boolean; onPress: () => void; onEdit: () => void; onDelete: () => void; onToggleFavorite: () => void; } const CardItem: React.FC = ({ card, isCompact, showActions, onPress, onEdit, onDelete, onToggleFavorite, }) => { const colors = useThemeColors(); const getCardTypeIcon = (type: Card['card_type']) => { switch (type) { case 'text': return 'document-text-outline'; case 'flashcard': return 'card-outline'; case 'quiz': return 'help-circle-outline'; case 'mixed': return 'grid-outline'; default: return 'document-outline'; } }; const getCardTypeColor = (type: Card['card_type']) => { switch (type) { case 'text': return colors.primary; case 'flashcard': return colors.accent; case 'quiz': return colors.secondary; case 'mixed': return colors.destructive; default: return colors.mutedForeground; } }; const getContentPreview = (card: Card) => { switch (card.card_type) { case 'text': return (card.content as any).text || ''; case 'flashcard': return (card.content as any).front || ''; case 'quiz': return (card.content as any).question || ''; case 'mixed': const blocks = (card.content as any).blocks || []; const firstTextBlock = blocks.find((block: any) => block.type === 'text'); return firstTextBlock?.data?.text || ''; default: return ''; } }; if (isCompact) { return ( {({ pressed }) => ( {/* Icon Container */} {/* Content */} {card.title || `${card.card_type.charAt(0).toUpperCase() + card.card_type.slice(1)} Karte`} {getContentPreview(card)} {/* Chevron */} )} ); } return ( {card.card_type} {card.is_favorite && ( )} {card.title || `Position ${card.position}`} {getContentPreview(card)} {showActions && ( ({ padding: 8, opacity: pressed ? 0.7 : 1 })}> ({ padding: 8, opacity: pressed ? 0.7 : 1 })}> ({ padding: 8, opacity: pressed ? 0.7 : 1 })}> )} Position {card.position} {new Date(card.updated_at).toLocaleDateString('de-DE')} ); }; export const CardList: React.FC = ({ deckId, isCompact = false, showActions = true, limit, onCardPress, }) => { const { cards, fetchCards, deleteCard, toggleFavorite, isLoading } = useCardStore(); const colors = useThemeColors(); useEffect(() => { fetchCards(deckId); }, [deckId]); const displayCards = limit ? cards.slice(0, limit) : cards; const handleCardPress = (card: Card) => { if (onCardPress) { onCardPress(card); } else { router.push(`/card/${card.id}`); } }; const handleEdit = (card: Card) => { router.push(`/card/edit/${card.id}`); }; const handleDelete = (card: Card) => { Alert.alert('Karte löschen', 'Möchtest du diese Karte wirklich löschen?', [ { text: 'Abbrechen', style: 'cancel' }, { text: 'Löschen', style: 'destructive', onPress: () => deleteCard(card.id), }, ]); }; const handleToggleFavorite = (card: Card) => { toggleFavorite(card.id); }; if (cards.length === 0 && !isLoading) { return ( Noch keine Karten Füge deine erste Karte hinzu, um zu beginnen ); } return ( item.id} renderItem={({ item }) => ( handleCardPress(item)} onEdit={() => handleEdit(item)} onDelete={() => handleDelete(item)} onToggleFavorite={() => handleToggleFavorite(item)} /> )} scrollEnabled={false} ItemSeparatorComponent={isCompact ? undefined : () => } /> {limit && cards.length > limit && ( router.push(`/deck/${deckId}/cards`)} style={({ pressed }) => ({ marginTop: 12, opacity: pressed ? 0.7 : 1 })}> Alle {cards.length} Karten anzeigen )} ); };