All files / src/views/notifications NotificationsView.tsx

81.35% Statements 48/59
78.94% Branches 30/38
68.42% Functions 13/19
82.35% Lines 42/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 2121x 1x               1x 1x 1x   1x       1x 1x 1x 1x 1x                     8x 8x 8x 8x   8x                                                                       1x 10x 10x 10x 10x 10x   10x         10x 10x 6x 6x 6x 6x 6x 6x 8x 8x 8x 2x   12x     10x       1x                                                                     8x 8x           6x                                                   9x                                                                            
import React, { useCallback, useMemo, useRef } from 'react';
import {
  ActivityIndicator,
  SectionList,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
} from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import { router, useFocusEffect, type Href } from 'expo-router';
import { SafeAreaView } from 'react-native-safe-area-context';
import type { NotificationItem } from '@/models/notification';
import {
  getNotificationPresentation,
  isDedicatedParticipationNotification,
} from '@/utils/notificationPresentation';
import { resolveNotificationRoute } from '@/utils/notificationRouting';
import { formatRelativeTime } from '@/utils/relativeTime';
import { useNotificationsViewModel } from '@/viewmodels/notifications/useNotificationsViewModel';
import { useTranslation } from 'react-i18next';
import { useTheme } from '@/theme';
import type { Theme } from '@/theme';
 
interface NotificationRowProps {
  item: NotificationItem;
  onOpen: (item: NotificationItem) => void;
  onDelete: (notificationId: string) => void;
  theme: Theme;
}
 
function NotificationRow({ item, onOpen, onDelete, theme }: NotificationRowProps) {
  const { t, i18n } = useTranslation();
  const styles = useMemo(() => makeStyles(theme), [theme]);
  const presentation = getNotificationPresentation(item);
  const isDedicated = isDedicatedParticipationNotification(item.type);
 
  return (
    <TouchableOpacity
      activeOpacity={0.88}
      style={[styles.notificationRow, !item.is_read && styles.unreadRow, isDedicated && styles.notificationRowDedicated]}
      onPress={() => onOpen(item)}
    >
      <View style={[styles.notificationIcon, isDedicated && { backgroundColor: presentation.accentBackgroundColor }]}>
        <Ionicons name={presentation.iconName as any} size={22} color={isDedicated ? presentation.accentColor : theme.text} />
      </View>
      <View style={styles.notificationBody}>
        <View style={styles.notificationHeader}>
          <Text style={styles.notificationTitle} numberOfLines={1}>{presentation.title ?? item.title}</Text>
          {!item.is_read && <View style={styles.unreadDot} />}
        </View>
        {presentation.badgeLabel && (
          <View style={[styles.typePill, { backgroundColor: presentation.accentBackgroundColor }]}>
            <Text style={[styles.typePillText, { color: presentation.accentColor }]}>{presentation.badgeLabel}</Text>
          </View>
        )}
        <Text style={styles.notificationText} numberOfLines={3}>{presentation.summary}</Text>
        <View style={styles.notificationMetaRow}>
          {presentation.actionLabel && (
            <Text style={[styles.notificationType, isDedicated && { color: presentation.accentColor }]}>{presentation.actionLabel}</Text>
          )}
          <Text style={styles.notificationTime}>
            {formatRelativeTime(item.created_at, t, i18n.language)}
          </Text>
        </View>
      </View>
      <TouchableOpacity style={styles.deleteButton} onPress={() => onDelete(item.id)}>
        <Ionicons name="trash-outline" size={18} color={theme.textMuted} />
      </TouchableOpacity>
    </TouchableOpacity>
  );
}
 
export default function NotificationsView() {
  const vm = useNotificationsViewModel();
  const { t } = useTranslation();
  const { theme } = useTheme();
  const styles = useMemo(() => makeStyles(theme), [theme]);
  const hasFocusedOnceRef = useRef(false);
 
  useFocusEffect(useCallback(() => {
    if (hasFocusedOnceRef.current) void vm.refresh();
    else hasFocusedOnceRef.current = true;
  }, [vm.refresh]));
 
  const sections = useMemo(() => {
    if (vm.notifications.length === 0) return [];
    const now = new Date();
    const today = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
    const todayKey = t('notifications.today');
    const earlierKey = t('notifications.earlier');
    const groups: { [key: string]: NotificationItem[] } = { [todayKey]: [], [earlierKey]: [] };
    vm.notifications.forEach(item => {
      const d = new Date(item.created_at);
      const itemDate = new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
      if (itemDate === today) groups[todayKey].push(item);
      else groups[earlierKey].push(item);
    });
    return Object.entries(groups).filter(([_, items]) => items.length > 0).map(([title, data]) => ({ title, data }));
  }, [vm.notifications, t]);
 
  return (
    <SafeAreaView edges={['top', 'left', 'right']} style={styles.safeArea}>
      <View style={styles.container}>
        <View style={styles.header}>
          <TouchableOpacity style={styles.backButton} onPress={() => router.back()} accessibilityLabel="Go back">
            <Ionicons name="chevron-back" size={24} color={theme.text} />
          </TouchableOpacity>
          <View style={styles.titleBlock}>
            <Text style={styles.screenTitle}>{t('notifications.title')}</Text>
            <Text style={styles.screenSubtitle}>
              {vm.unreadCount === 0
                ? t('notifications.allCaughtUp')
                : t('notifications.unreadCount', { count: vm.unreadCount })}
            </Text>
          </View>
          <TouchableOpacity
            style={[styles.markAllButton, vm.unreadCount === 0 && styles.markAllButtonDisabled]}
            onPress={() => void vm.markAllRead()}
            disabled={vm.unreadCount === 0}
            accessibilityLabel="Mark all notifications as read"
          >
            <Ionicons name="checkmark-done-outline" size={21} color={vm.unreadCount === 0 ? theme.border : theme.text} />
          </TouchableOpacity>
        </View>
 
        {vm.apiError && (
          <View style={styles.errorBanner}>
            <Text style={styles.errorText}>{vm.apiError}</Text>
          </View>
        )}
 
        {vm.isLoading && !vm.isRefreshing ? (
          <View style={styles.loadingPanel}>
            <ActivityIndicator size="large" color={theme.primary} />
            <Text style={styles.loadingText}>{t('notifications.loading')}</Text>
          </View>
        ) : (
          <SectionList
            sections={sections}
            keyExtractor={item => item.id}
            renderItem={({ item }) => <NotificationRow item={item} onOpen={item => {
              void vm.markRead(item.id);
              const r = resolveNotificationRoute(item);
              Iif (r) router.push(r as Href);
            }} onDelete={id => void vm.removeNotification(id)} theme={theme} />}
            renderSectionHeader={({ section: { title } }) => (
              <View style={styles.sectionHeader}>
                <Text style={styles.sectionHeaderText}>{title}</Text>
              </View>
            )}
            contentContainerStyle={[styles.listContent, sections.length === 0 && { flex: 1 }]}
            stickySectionHeadersEnabled={false}
            refreshing={vm.isRefreshing}
            onRefresh={vm.refresh}
            onEndReached={vm.loadMore}
            ListEmptyComponent={
              <View style={styles.emptyStateContainer}>
                <View style={styles.emptyStateIconCircle}>
                  <Ionicons name="notifications-outline" size={48} color={theme.primary} />
                </View>
                <Text style={styles.emptyTitle}>{t('notifications.emptyTitle')}</Text>
                <Text style={styles.emptySubtitle}>{t('notifications.emptySubtitle')}</Text>
              </View>
            }
          />
        )}
      </View>
    </SafeAreaView>
  );
}
 
function makeStyles(t: Theme) {
  return StyleSheet.create({
    safeArea: { flex: 1, backgroundColor: t.background },
    container: { flex: 1, paddingHorizontal: 20 },
    header: { flexDirection: 'row', alignItems: 'center', paddingTop: 14, paddingBottom: 18, gap: 12 },
    backButton: { width: 42, height: 42, borderRadius: 21, backgroundColor: t.surface, borderWidth: 1, borderColor: t.border, alignItems: 'center', justifyContent: 'center' },
    titleBlock: { flex: 1 },
    screenTitle: { fontSize: 28, fontWeight: '800', color: t.text },
    screenSubtitle: { marginTop: 2, color: t.textMuted, fontSize: 14, fontWeight: '600' },
    markAllButton: { width: 42, height: 42, borderRadius: 21, backgroundColor: t.surface, borderWidth: 1, borderColor: t.border, alignItems: 'center', justifyContent: 'center' },
    markAllButtonDisabled: { backgroundColor: t.background },
    errorBanner: { backgroundColor: t.errorBg, borderColor: t.errorBorder, borderWidth: 1, borderRadius: 12, padding: 12, marginBottom: 12 },
    errorText: { color: t.errorText, fontSize: 14 },
    loadingPanel: { flex: 1, alignItems: 'center', justifyContent: 'center', paddingBottom: 60 },
    loadingText: { marginTop: 12, color: t.textMuted, fontSize: 15 },
    listContent: { paddingBottom: 24, gap: 10 },
    notificationRow: { flexDirection: 'row', alignItems: 'flex-start', gap: 12, backgroundColor: t.surface, borderWidth: 1, borderColor: t.border, borderRadius: 18, padding: 12 },
    notificationRowDedicated: { borderColor: t.borderStrong },
    unreadRow: { borderColor: t.unreadBorder, backgroundColor: t.unreadBg },
    notificationIcon: { width: 54, height: 54, borderRadius: 12, backgroundColor: t.infoBg, alignItems: 'center', justifyContent: 'center' },
    notificationBody: { flex: 1, minWidth: 0, gap: 5 },
    notificationHeader: { flexDirection: 'row', alignItems: 'flex-start', gap: 8 },
    notificationTitle: { flex: 1, color: t.text, fontSize: 15, fontWeight: '800', lineHeight: 20 },
    unreadDot: { width: 8, height: 8, borderRadius: 4, backgroundColor: t.unreadDot },
    typePill: { alignSelf: 'flex-start', paddingHorizontal: 10, paddingVertical: 5, borderRadius: 999 },
    typePillText: { fontSize: 11, fontWeight: '800' },
    notificationText: { color: t.textSecondary, fontSize: 14, lineHeight: 19 },
    notificationMetaRow: { flexDirection: 'row', alignItems: 'center', gap: 8, marginTop: 9 },
    notificationType: { flex: 1, color: t.infoText, fontSize: 11, fontWeight: '800', textTransform: 'uppercase', letterSpacing: 0.6 },
    notificationTime: { color: t.textMuted, fontSize: 12, fontWeight: '700' },
    deleteButton: { width: 36, height: 36, borderRadius: 18, alignItems: 'center', justifyContent: 'center' },
    emptyStateContainer: { flex: 1, alignItems: 'center', justifyContent: 'center', paddingBottom: 80 },
    emptyStateIconCircle: { width: 90, height: 90, borderRadius: 45, backgroundColor: t.surface, borderWidth: 1, borderColor: t.border, alignItems: 'center', justifyContent: 'center', marginBottom: 20 },
    emptyTitle: { fontSize: 22, fontWeight: '800', color: t.text },
    emptySubtitle: { marginTop: 10, fontSize: 15, lineHeight: 22, color: t.textMuted, textAlign: 'center', paddingHorizontal: 40 },
    sectionHeader: { backgroundColor: t.background, paddingTop: 24, paddingBottom: 10 },
    sectionHeaderText: { fontSize: 16, fontWeight: '800', color: t.textMuted, textTransform: 'uppercase', letterSpacing: 1.2 },
  });
}