chahinebrini 51697c3aa4 feat(tier): plan-change briefing sheet + over-limit cards (Phase 2 UI)
- components/plan/PlanChangeSheet.tsx — upgrade/downgrade briefing per pricing-tiers.md §4
  (fetches GET /api/plan/change-preview; gains/keeps/changes; recovery-safety line;
  billing hint w/o purchase button; CTA row, no 'are you sure?' interstitial)
- debug.tsx: PlanOverrideToggle routes every flip through PlanChangeSheet first
- devices.tsx + protectedDevices.ts: 'degraded' status (red, inline 'protection expired —
  remove the profile yourself' hint, no green checkmark); maxProtectedDevices limit hint
- mail.tsx + MailAccountCard.tsx + useMailStatus.ts: over-limit banner + paused-account
  greyed-out + PausedBadge (all defensive — only shows if backend sends the  field)
- blocker.tsx: free-tier transparency hint ('Grundschutz aktiv — voller Schutz: Pro/Legend')
  + custom-domain over-limit banner
- locales: plan.change.* + plan_limit.* (de + en)

tsc clean. Backend side (GET /api/plan/change-preview, paused/degraded fields) in progress
in parallel — UI built defensively to work before it lands.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 16:21:47 +02:00

142 lines
3.8 KiB
TypeScript

import { useCallback, useEffect, useRef, useState } from 'react';
import { AppState, type AppStateStatus } from 'react-native';
import { apiFetch } from '../lib/api';
export type MailAccount = {
id: string;
email: string;
provider: string;
isActive: boolean;
paused?: boolean;
lastScannedAt: string | null;
nextScanAt: string | null;
totalBlocked: number;
totalScanned: number;
scanInterval: number;
blockRate: number;
lastConnectError?: string | null;
lastConnectErrorAt?: string | null;
lastIdleHeartbeatAt?: string | null;
};
export type DailyStat = {
date: string;
label: string;
count: number;
};
export type MailStatusResponse = {
connected: boolean;
accounts: MailAccount[];
totalBlocked: number;
totalScanned: number;
dailyStats: DailyStat[];
};
export type Plan = 'free' | 'pro' | 'legend';
export type UseMailStatusReturn = {
connected: boolean;
accounts: MailAccount[];
totalBlocked: number;
totalScanned: number;
dailyStats: DailyStat[];
/** Plan-derived account limit: free=1, pro=3, legend=Infinity */
maxAccounts: number;
loading: boolean;
error: string | null;
refresh: () => Promise<void>;
};
const POLL_INTERVAL_MS = 30_000;
function deriveMaxAccounts(plan: Plan): number {
if (plan === 'free') return 1;
if (plan === 'pro') return 3;
return Infinity;
}
/**
* Fetched GET /api/mail/status mit:
* - initialem Fetch on mount
* - 30s-Polling solange App im Vordergrund (AppState === 'active')
* - manuell triggerbar via refresh()
*
* TODO: Ersetze Polling durch IDLE-Realtime-Websocket wenn Phase-10-Backend fertig ist.
*/
export function useMailStatus(plan: Plan): UseMailStatusReturn {
const [data, setData] = useState<MailStatusResponse | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
const appStateRef = useRef<AppStateStatus>(AppState.currentState);
const fetchStatus = useCallback(async () => {
try {
const res = await apiFetch<MailStatusResponse>('/api/mail/status');
setData(res);
setError(null);
} catch (e: any) {
console.error('[useMailStatus] fetch failed:', e?.message ?? e);
setError(e?.message ?? 'unknown');
} finally {
setLoading(false);
}
}, []);
// Polling starten / stoppen je nach AppState
const startPolling = useCallback(() => {
if (intervalRef.current) return;
intervalRef.current = setInterval(() => {
fetchStatus();
}, POLL_INTERVAL_MS);
}, [fetchStatus]);
const stopPolling = useCallback(() => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
}, []);
useEffect(() => {
fetchStatus();
startPolling();
const sub = AppState.addEventListener('change', (nextState: AppStateStatus) => {
const wasActive = appStateRef.current === 'active';
const isNowActive = nextState === 'active';
appStateRef.current = nextState;
if (!wasActive && isNowActive) {
// App kommt in Vordergrund — sofort refreshen + Polling neu starten
fetchStatus();
startPolling();
} else if (wasActive && !isNowActive) {
// App geht in Hintergrund — Polling stoppen
stopPolling();
}
});
return () => {
stopPolling();
sub.remove();
};
}, [fetchStatus, startPolling, stopPolling]);
const maxAccounts = deriveMaxAccounts(plan);
return {
connected: data?.connected ?? false,
accounts: data?.accounts ?? [],
totalBlocked: data?.totalBlocked ?? 0,
totalScanned: data?.totalScanned ?? 0,
dailyStats: data?.dailyStats ?? [],
maxAccounts,
loading,
error,
refresh: fetchStatus,
};
}