154 lines
4.8 KiB
TypeScript

import { useEffect } from 'react';
import { View, Text, FlatList, Pressable, RefreshControl } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useRouter } from 'expo-router';
import { Ionicons } from '@expo/vector-icons';
import { HeroShieldCheck } from '../../components/HeroShieldCheck';
import { useTranslation } from 'react-i18next';
import { EmptyState } from '../../components/EmptyState';
import { useNotificationStore, type AppNotification } from '../../stores/notifications';
import { colors } from '../../lib/theme';
export default function NotificationsScreen() {
const router = useRouter();
const { t } = useTranslation();
const items = useNotificationStore((s) => s.items);
const loaded = useNotificationStore((s) => s.loaded);
const load = useNotificationStore((s) => s.load);
const markRead = useNotificationStore((s) => s.markRead);
const remove = useNotificationStore((s) => s.remove);
useEffect(() => {
load();
const tm = setTimeout(() => {
markRead();
}, 400);
return () => clearTimeout(tm);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<SafeAreaView className="flex-1 bg-white" edges={['top']}>
<View className="flex-row items-center gap-3 px-5 pt-3 pb-3 border-b border-neutral-200">
<Pressable
onPress={() => router.back()}
className="w-9 h-9 rounded-full bg-neutral-100 border border-neutral-200 items-center justify-center"
>
<Ionicons name="arrow-back" size={18} color="#737373" />
</Pressable>
<Text
className="text-neutral-900 text-lg flex-1"
style={{ fontFamily: 'Nunito_700Bold' }}
>
{t('notifications.title')}
</Text>
</View>
{items.length === 0 ? (
<EmptyState
icon="notifications-off-outline"
title={t('notifications.empty_title')}
subtitle={t('notifications.empty_subtitle')}
/>
) : (
<FlatList
data={items}
keyExtractor={(n) => n.id}
contentContainerStyle={{ paddingVertical: 8 }}
refreshControl={
<RefreshControl
refreshing={!loaded}
onRefresh={load}
tintColor={colors.brandOrange}
/>
}
renderItem={({ item }) => (
<NotificationRow
notif={item}
onPress={() => {
if (item.postId) {
router.push(`/?postId=${item.postId}` as never);
}
}}
onDelete={() => remove(item.id)}
/>
)}
/>
)}
</SafeAreaView>
);
}
function NotificationRow({
notif,
onPress,
onDelete,
}: {
notif: AppNotification;
onPress: () => void;
onDelete: () => void;
}) {
const isUnread = !notif.readAt;
return (
<Pressable
onPress={onPress}
style={({ pressed }) => ({
opacity: pressed ? 0.7 : 1,
backgroundColor: isUnread ? '#fff7ed' : '#fff',
})}
>
<View
style={{
flexDirection: 'row',
alignItems: 'flex-start',
paddingHorizontal: 16,
paddingVertical: 12,
borderBottomWidth: 1,
borderBottomColor: '#f5f5f5',
}}
>
{/* Pure-Icon — KEIN bg-Circle (User-Wunsch: kein extra Rand). */}
<View style={{ width: 36, alignItems: 'center', justifyContent: 'center', marginRight: 12 }}>
{notif.type === 'domain_accepted' ? (
<HeroShieldCheck size={22} color="#16a34a" />
) : (
<Ionicons name={iconForType(notif.type)} size={22} color="#d97706" />
)}
</View>
<View style={{ flex: 1, minWidth: 0, marginRight: 8 }}>
<Text
style={{ fontSize: 13, fontFamily: 'Nunito_700Bold', color: '#0a0a0a' }}
numberOfLines={1}
>
{notif.actorName}
</Text>
{notif.preview && (
<Text
style={{
fontSize: 12,
fontFamily: 'Nunito_400Regular',
color: '#525252',
marginTop: 2,
}}
numberOfLines={2}
>
{notif.preview}
</Text>
)}
</View>
<Pressable onPress={onDelete} hitSlop={8}>
<Ionicons name="close" size={16} color="#a3a3a3" />
</Pressable>
</View>
</Pressable>
);
}
function iconForType(type: string): React.ComponentProps<typeof Ionicons>['name'] {
if (type.includes('like')) return 'heart';
if (type.includes('comment')) return 'chatbubble';
if (type.includes('follow')) return 'person-add';
if (type.includes('domain')) return 'shield-checkmark';
return 'notifications';
}