import { useCallback, useState } from 'react'; import { Alert, ScrollView, TextInput, View } from 'react-native'; import { useFocusEffect } from 'expo-router'; import { api } from '../src/api'; import { describe } from '../src/errors'; import { useStore } from '../src/session'; import { Banner, Button, Card, T } from '../src/ui'; import { font, useColors } from '../src/theme'; interface Overview { defaultTitle: string; allowed: boolean; maxLength: number; minIntervalHours: number; audience: { cards: number; appleDevices: number }; nextAllowedAt: string | null; items: { id: string; title: string | null; message: string; sentByName: string; cards: number; createdAt: string }[]; } const when = (d: string) => new Date(d).toLocaleString('fr-FR', { day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit' }); /** Campagnes (dirigeants, formule Pro) : un message envoyé en notification à toutes les cartes du magasin. */ export default function Campaigns() { const c = useColors(); const store = useStore(); const [data, setData] = useState(null); const [message, setMessage] = useState(''); const [title, setTitle] = useState(null); const [error, setError] = useState(null); const [sent, setSent] = useState(false); const [busy, setBusy] = useState(false); const load = useCallback(async () => { try { setData(await api.get('/api/staff/campaigns')); } catch (e) { setError(describe(e).detail); } }, []); useFocusEffect( useCallback(() => { load(); }, [load]), ); const text = message.trim(); const blocked = !data || !data.allowed || !!data.nextAllowedAt; const send = () => Alert.alert('Envoyer la campagne ?', `${data!.audience.cards} cartes de ${store.name} recevront :\n\n${(title ?? data!.defaultTitle).trim()}\n« ${text} »`, [ { text: 'Annuler', style: 'cancel' }, { text: 'Envoyer', onPress: async () => { setBusy(true); setError(null); try { await api.post('/api/staff/campaigns', { message: text, title: (title ?? data!.defaultTitle).trim() }); setMessage(''); setSent(true); await load(); } catch (e) { setError(describe(e).detail); } finally { setBusy(false); } }, }, ]); const shownTitle = (title ?? data?.defaultTitle ?? '').trim() || store.name; const maxLength = data?.maxLength ?? 160; const input = { borderWidth: 1, borderColor: c.border, borderRadius: 14, padding: 14, backgroundColor: c.bg, color: c.text, fontFamily: font.regular, fontSize: 17, opacity: blocked ? 0.6 : 1, } as const; return ( {error && {error}} {sent && Campagne envoyée. Les cartes de vos clients se mettent à jour dans les minutes qui suivent.} {data && !data.allowed && Le module Campagnes n'est pas actif : activez-le dans Configuration → Modules, ou contactez MonEspacePro s'il n'est pas inclus dans votre formule.} {data?.nextAllowedAt && Prochain envoi possible le {when(data.nextAllowedAt)}.} {/* Audience */} 📣 {data?.audience.cards ?? '—'} clients recevront la notification sur leur téléphone {/* Rédaction */} Nouvelle campagne Titre Message 1 campagne toutes les {data?.minIntervalHours ?? 24} h maximum maxLength - 15 ? c.warn : c.text3 }}> {text.length} / {maxLength} {/* Aperçu de la notification */} Aperçu {store.name.charAt(0).toUpperCase()} {shownTitle} maintenant {text || 'Votre message apparaîtra ici.'} Sur iPhone, le nom du magasin remplace toujours le titre.