rebreak-monorepo/apps/rebreak-native/components/mail/MailDistributionChart.tsx
chahinebrini 1dfb0c647c feat(mail-page): polish v3 + shared HalfDonut + status-dot heartbeat-aware
User-Feedback nach Live-Test:

Frontend (mail page):
- HalfDonut als shared component in components/common/HalfDonut.tsx
  extrahiert (vorher local in ProtectionDetailsSheet). Mail-Page nutzt
  jetzt dieselbe SVG-Math, Animation und Stroke-Style wie der
  Blocker-Schutz-Details-Sheet — visuelle Konsistenz auf einen Blick.
  Mail-Donut: width=168 (kompakter als die 220 in Blocker, weil Legend
  rechts daneben sitzt).
- Donut zeigt Total in der Mitte mit kompaktem Format:
  < 1000 → "999", >=1000 → "1.2k+" / "12k+" / "27k+"
  Headline-Zahl oben links entfällt — Total ist im Donut-Center.
- "Mehr Infos" + "Kürzlich blockiert" zu EINER Top-Level-Collapsible
  zusammengefasst. Beim Aufklappen: Bar-Chart direkt sichtbar, nested
  Collapsible "Kürzlich blockiert" darunter (default zu).
- Account-Card Expanded: per-Connection-Bar-Chart mit adaptive
  Granularität nach Connection-Age:
  · <24h → Empty-State "Daten werden gesammelt, Auswertung nach 24h"
  · 1-14d → Day-Buckets (echte Daten via /api/mail/stats/blocked-by-day
    ?connectionId=)
  · 15-90d → Week-Buckets (client-aggregiert)
  · >90d → Month-Buckets (client-aggregiert)
- Settings-Sheet komplett refactored: State-Machine `mode: 'list' |
  'edit-title' | 'edit-email' | 'edit-password'` mit Back-Pfeil. Inline-
  Edit im selben Sheet statt Sub-Sheet öffnen (FormSheet-Pattern).
  Email-Edit-Row vorbereitet (Backend-PATCH-Endpoint kommt separat).
- Pen-Icons app-weit entfernt: SheetFieldStack-Row, alle Settings-Rows
  auf chevron-forward (Memory-Konvention).

Frontend (MailAccountCard status fix):
- resolveStatusDot nutzt jetzt heartbeat-as-fallback. Vorher: "waiting"
  wenn lastScannedAt=null, egal ob Daemon längst connected war. Jetzt:
  "waiting" nur wenn weder lebendiger Heartbeat noch vergangener Scan
  existiert → frisch verbundene Connections (z.B. OAuth-Outlook 5s nach
  Connect) zeigen direkt "live".
- Behebt User-Beobachtung: "wartet auf erste verbindung" bei Outlook
  obwohl Daemon-Log "connected, auth=xoauth2" zeigt.

Backend (imap-idle daemon):
- getMailboxLock("INBOX") jetzt mit 30s Promise.race-Timeout gewrappt.
- Outlook/XOAUTH2 hat den Edge-Case, dass der Mailbox-Lock lautlos
  hängt nach erfolgreichem connect — die Session bleibt offen ohne
  Fortschritt bis der Renew-Timer (10min) ein imap.close() schickt.
  Mit Timeout wird das Failure-Mode explizit → Auth-Retry-Loop greift
  sauber + last_connect_error mit klarem Text (statt stiller Hänger).
- Root-Cause "warum hängt es" noch nicht behoben — Diagnose nach
  Deploy in Logs (mo).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 23:23:45 +02:00

251 lines
6.4 KiB
TypeScript

import { useMemo } from 'react';
import { Text, View } from 'react-native';
import { useTranslation } from 'react-i18next';
import { useColors } from '../../lib/theme';
import { HalfDonut } from '../common/HalfDonut';
import type { BlockedByConnectionEntry } from '../../hooks/useMailStats';
type Props = {
data: BlockedByConnectionEntry[];
hero?: boolean;
totalBlocked?: number;
accountCount?: number;
isLegend?: boolean;
};
const SLICE_COLORS = ['#ef4444', '#3b82f6', '#f59e0b', '#8b5cf6'];
const OTHER_COLOR = '#a3a3a3';
const MAX_LEGEND_ENTRIES = 3;
const DONUT_WIDTH = 168;
function formatCompact(n: number): string {
if (n < 1000) return n.toLocaleString();
const k = n / 1000;
if (k < 10) return `${Math.floor(k * 10) / 10}k+`;
return `${Math.floor(k)}k+`;
}
function domainFromEmail(email: string): string {
return email.split('@')[1] ?? email;
}
function displayLabel(entry: BlockedByConnectionEntry): string {
return entry.title ?? domainFromEmail(entry.email);
}
export function MailDistributionChart({ data, hero, totalBlocked, isLegend }: Props) {
const { t } = useTranslation();
const colors = useColors();
const total = data.reduce((s, d) => s + d.count, 0);
const slices = useMemo(() => {
if (data.length === 0 || total === 0) return [];
const sorted = [...data].sort((a, b) => b.count - a.count);
if (sorted.length <= MAX_LEGEND_ENTRIES) {
return sorted.map((e, i) => ({
label: displayLabel(e),
count: e.count,
color: SLICE_COLORS[i] ?? OTHER_COLOR,
isOther: false,
}));
}
const top3 = sorted.slice(0, MAX_LEGEND_ENTRIES);
const rest = sorted.slice(MAX_LEGEND_ENTRIES);
const restCount = rest.reduce((s, e) => s + e.count, 0);
const restConnectionCount = rest.length;
const items = top3.map((e, i) => ({
label: displayLabel(e),
count: e.count,
color: SLICE_COLORS[i],
isOther: false,
}));
items.push({
label: t('mail.stats.distribution_other_n', { n: restConnectionCount }),
count: restCount,
color: OTHER_COLOR,
isOther: true,
});
return items;
}, [data, total, t]);
if (data.length <= 1 || total === 0) return null;
const displayTotal = totalBlocked ?? total;
const centerValue = formatCompact(displayTotal);
const centerLabel = t('mail.stats.distribution_center_label');
const segments = slices.map((s) => ({ value: s.count, color: s.color }));
if (hero) {
return (
<View
style={{
backgroundColor: colors.surface,
borderRadius: 16,
borderWidth: 1,
borderColor: colors.border,
paddingHorizontal: 16,
paddingTop: 16,
paddingBottom: 16,
}}
>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
marginBottom: 14,
}}
>
<Text
style={{
flex: 1,
fontSize: 13,
fontFamily: 'Nunito_700Bold',
color: colors.textMuted,
textTransform: 'uppercase',
letterSpacing: 0.7,
}}
>
{t('mail.stats.distribution_heading')}
</Text>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 10,
paddingVertical: 5,
borderRadius: 999,
backgroundColor: isLegend ? '#f0fdf4' : '#eff6ff',
}}
>
<View
style={{
width: 6,
height: 6,
borderRadius: 3,
backgroundColor: isLegend ? '#16a34a' : '#2563eb',
marginRight: 6,
}}
/>
<Text
style={{
fontSize: 12,
fontFamily: 'Nunito_700Bold',
color: isLegend ? '#16a34a' : '#2563eb',
}}
>
{isLegend ? t('mail.live') : t('mail.scheduled')}
</Text>
</View>
</View>
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 16 }}>
<HalfDonut
segments={segments}
centerValue={centerValue}
centerLabel={centerLabel}
width={DONUT_WIDTH}
/>
<View style={{ flex: 1, gap: 5 }}>
{slices.map((slice) => (
<LegendRow key={slice.label} slice={slice} colors={colors} />
))}
</View>
</View>
</View>
);
}
return (
<View
style={{
backgroundColor: colors.surface,
borderRadius: 16,
borderWidth: 1,
borderColor: colors.border,
paddingHorizontal: 16,
paddingTop: 14,
paddingBottom: 14,
}}
>
<Text
style={{
fontSize: 11,
fontFamily: 'Nunito_700Bold',
color: colors.textMuted,
textTransform: 'uppercase',
letterSpacing: 0.7,
marginBottom: 12,
}}
>
{t('mail.stats.distribution_heading')}
</Text>
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 16 }}>
<HalfDonut
segments={segments}
centerValue={centerValue}
centerLabel={centerLabel}
width={DONUT_WIDTH}
/>
<View style={{ flex: 1, gap: 6 }}>
{slices.map((slice) => (
<LegendRow key={slice.label} slice={slice} colors={colors} />
))}
</View>
</View>
</View>
);
}
function LegendRow({
slice,
colors,
}: {
slice: { label: string; count: number; color: string; isOther: boolean };
colors: ReturnType<typeof useColors>;
}) {
return (
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 6 }}>
<View
style={{
width: 8,
height: 8,
borderRadius: 4,
backgroundColor: slice.color,
}}
/>
<Text
style={{
flex: 1,
fontSize: 12,
fontFamily: slice.isOther ? 'Nunito_400Regular' : 'Nunito_600SemiBold',
color: slice.isOther ? colors.textMuted : colors.text,
}}
numberOfLines={1}
>
{slice.label}
</Text>
<Text
style={{
fontSize: 12,
fontFamily: 'Nunito_700Bold',
color: colors.textMuted,
}}
>
{slice.count}
</Text>
</View>
);
}