1+ import { useState , useEffect } from 'react' ;
2+ import { Card , CardContent } from '@/components/ui/card' ;
3+ import { Badge } from '@/components/ui/badge' ;
4+ import { CheckCircle , AlertCircle , Code , Globe , ShoppingBag } from 'lucide-react' ;
5+ import { collection , query , where , getDocs } from 'firebase/firestore' ;
6+ import { db } from '@/lib/firebase' ;
7+
8+ interface IntegrationStatusCardProps {
9+ campaignId : string ;
10+ activeIntegrationType : 'code' | 'plugin' ;
11+ }
12+
13+ interface IntegrationStatus {
14+ hasCodeIntegration : boolean ;
15+ hasPluginIntegration : boolean ;
16+ pluginConfigs : Array < {
17+ id : string ;
18+ type : 'wordpress' | 'shopify' ;
19+ domain : string ;
20+ active : boolean ;
21+ } > ;
22+ }
23+
24+ export const IntegrationStatusCard = ( { campaignId, activeIntegrationType } : IntegrationStatusCardProps ) => {
25+ const [ status , setStatus ] = useState < IntegrationStatus > ( {
26+ hasCodeIntegration : false ,
27+ hasPluginIntegration : false ,
28+ pluginConfigs : [ ]
29+ } ) ;
30+ const [ loading , setLoading ] = useState ( true ) ;
31+
32+ useEffect ( ( ) => {
33+ const loadIntegrationStatus = async ( ) => {
34+ try {
35+ // Vérifier les plugins configurés
36+ const pluginQuery = query (
37+ collection ( db , 'plugin_configs' ) ,
38+ where ( 'campaignId' , '==' , campaignId )
39+ ) ;
40+
41+ const pluginSnapshot = await getDocs ( pluginQuery ) ;
42+ const plugins = pluginSnapshot . docs . map ( doc => ( {
43+ id : doc . id ,
44+ ...doc . data ( )
45+ } ) ) as Array < {
46+ id : string ;
47+ type : 'wordpress' | 'shopify' ;
48+ domain : string ;
49+ active : boolean ;
50+ } > ;
51+
52+ // Pour l'intégration par code, on considère qu'elle est toujours disponible
53+ // car elle ne nécessite pas de configuration côté serveur
54+ setStatus ( {
55+ hasCodeIntegration : true ,
56+ hasPluginIntegration : plugins . length > 0 ,
57+ pluginConfigs : plugins
58+ } ) ;
59+ } catch ( error ) {
60+ console . error ( 'Erreur lors du chargement du statut:' , error ) ;
61+ } finally {
62+ setLoading ( false ) ;
63+ }
64+ } ;
65+
66+ loadIntegrationStatus ( ) ;
67+ } , [ campaignId ] ) ;
68+
69+ if ( loading ) {
70+ return (
71+ < Card className = "mb-6" >
72+ < CardContent className = "p-4" >
73+ < div className = "flex items-center gap-3" >
74+ < div className = "h-4 w-4 animate-pulse bg-muted rounded-full" />
75+ < div className = "h-4 w-32 animate-pulse bg-muted rounded" />
76+ </ div >
77+ </ CardContent >
78+ </ Card >
79+ ) ;
80+ }
81+
82+ const getStatusForCurrentType = ( ) => {
83+ if ( activeIntegrationType === 'code' ) {
84+ return {
85+ isConnected : status . hasCodeIntegration ,
86+ title : 'Intégration par code' ,
87+ description : 'Scripts prêts à être intégrés sur votre site' ,
88+ icon : < Code className = "h-4 w-4" />
89+ } ;
90+ } else {
91+ const activePlugins = status . pluginConfigs . filter ( p => p . active ) ;
92+ return {
93+ isConnected : activePlugins . length > 0 ,
94+ title : `Plugin${ activePlugins . length > 1 ? 's' : '' } configuré${ activePlugins . length > 1 ? 's' : '' } ` ,
95+ description : activePlugins . length > 0
96+ ? `${ activePlugins . length } site${ activePlugins . length > 1 ? 's' : '' } connecté${ activePlugins . length > 1 ? 's' : '' } `
97+ : 'Aucun plugin configuré' ,
98+ icon : activePlugins . length > 0 ? (
99+ activePlugins [ 0 ] . type === 'wordpress' ? < Globe className = "h-4 w-4" /> : < ShoppingBag className = "h-4 w-4" />
100+ ) : < AlertCircle className = "h-4 w-4" /> ,
101+ connectedSites : activePlugins
102+ } ;
103+ }
104+ } ;
105+
106+ const currentStatus = getStatusForCurrentType ( ) ;
107+
108+ return (
109+ < Card className = "mb-6 border-l-4" style = { { borderLeftColor : currentStatus . isConnected ? 'hsl(var(--success))' : 'hsl(var(--warning))' } } >
110+ < CardContent className = "p-4" >
111+ < div className = "flex items-start justify-between" >
112+ < div className = "flex items-center gap-3" >
113+ { currentStatus . isConnected ? (
114+ < CheckCircle className = "h-5 w-5 text-success" />
115+ ) : (
116+ < AlertCircle className = "h-5 w-5 text-warning" />
117+ ) }
118+ < div >
119+ < div className = "flex items-center gap-2 mb-1" >
120+ { currentStatus . icon }
121+ < h4 className = "font-medium text-foreground" > { currentStatus . title } </ h4 >
122+ </ div >
123+ < p className = "text-sm text-muted-foreground" > { currentStatus . description } </ p >
124+
125+ { activeIntegrationType === 'plugin' && currentStatus . connectedSites && currentStatus . connectedSites . length > 0 && (
126+ < div className = "flex flex-wrap gap-2 mt-2" >
127+ { currentStatus . connectedSites . map ( ( site ) => (
128+ < Badge key = { site . id } variant = "secondary" className = "text-xs" >
129+ { site . type === 'wordpress' ? < Globe className = "h-3 w-3 mr-1" /> : < ShoppingBag className = "h-3 w-3 mr-1" /> }
130+ { site . domain }
131+ </ Badge >
132+ ) ) }
133+ </ div >
134+ ) }
135+ </ div >
136+ </ div >
137+
138+ < Badge variant = { currentStatus . isConnected ? "default" : "secondary" } >
139+ { currentStatus . isConnected ? "Configuré" : "Non configuré" }
140+ </ Badge >
141+ </ div >
142+ </ CardContent >
143+ </ Card >
144+ ) ;
145+ } ;
0 commit comments