@fayz-ai/plugin-crm 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/dist/CrmContext.d.ts +32 -0
  2. package/dist/CrmContext.d.ts.map +1 -0
  3. package/dist/CrmPage.d.ts +13 -0
  4. package/dist/CrmPage.d.ts.map +1 -0
  5. package/dist/cascade.d.ts +13 -0
  6. package/dist/cascade.d.ts.map +1 -0
  7. package/dist/components/CrmGeneralSettings.d.ts +3 -0
  8. package/dist/components/CrmGeneralSettings.d.ts.map +1 -0
  9. package/dist/components/CrmOnboarding.d.ts +5 -0
  10. package/dist/components/CrmOnboarding.d.ts.map +1 -0
  11. package/dist/components/PipelineSettings.d.ts +3 -0
  12. package/dist/components/PipelineSettings.d.ts.map +1 -0
  13. package/dist/components/QuoteStatusDropdown.d.ts +8 -0
  14. package/dist/components/QuoteStatusDropdown.d.ts.map +1 -0
  15. package/dist/data/index.d.ts +3 -0
  16. package/dist/data/index.d.ts.map +1 -0
  17. package/dist/data/mock.d.ts +9 -0
  18. package/dist/data/mock.d.ts.map +1 -0
  19. package/dist/data/supabase.d.ts +9 -0
  20. package/dist/data/supabase.d.ts.map +1 -0
  21. package/dist/data/types.d.ts +32 -0
  22. package/dist/data/types.d.ts.map +1 -0
  23. package/dist/index.cjs +4601 -0
  24. package/dist/index.cjs.map +1 -0
  25. package/dist/index.d.ts +72 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +4595 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/lib/tenant.d.ts +3 -0
  30. package/dist/lib/tenant.d.ts.map +1 -0
  31. package/dist/locales/en.d.ts +2 -0
  32. package/dist/locales/en.d.ts.map +1 -0
  33. package/dist/locales/index.d.ts +2 -0
  34. package/dist/locales/index.d.ts.map +1 -0
  35. package/dist/locales/pt-BR.d.ts +2 -0
  36. package/dist/locales/pt-BR.d.ts.map +1 -0
  37. package/dist/registries.d.ts +3 -0
  38. package/dist/registries.d.ts.map +1 -0
  39. package/dist/schema/index.d.ts +1036 -0
  40. package/dist/schema/index.d.ts.map +1 -0
  41. package/dist/store.d.ts +43 -0
  42. package/dist/store.d.ts.map +1 -0
  43. package/dist/types.d.ts +238 -0
  44. package/dist/types.d.ts.map +1 -0
  45. package/dist/views/ActivityListView.d.ts +3 -0
  46. package/dist/views/ActivityListView.d.ts.map +1 -0
  47. package/dist/views/DashboardView.d.ts +9 -0
  48. package/dist/views/DashboardView.d.ts.map +1 -0
  49. package/dist/views/DealSidebar.d.ts +9 -0
  50. package/dist/views/DealSidebar.d.ts.map +1 -0
  51. package/dist/views/LeadDetailView.d.ts +8 -0
  52. package/dist/views/LeadDetailView.d.ts.map +1 -0
  53. package/dist/views/LeadFormView.d.ts +5 -0
  54. package/dist/views/LeadFormView.d.ts.map +1 -0
  55. package/dist/views/LeadListView.d.ts +6 -0
  56. package/dist/views/LeadListView.d.ts.map +1 -0
  57. package/dist/views/PipelineView.d.ts +8 -0
  58. package/dist/views/PipelineView.d.ts.map +1 -0
  59. package/dist/views/QuoteDetailView.d.ts +8 -0
  60. package/dist/views/QuoteDetailView.d.ts.map +1 -0
  61. package/dist/views/QuoteFormView.d.ts +7 -0
  62. package/dist/views/QuoteFormView.d.ts.map +1 -0
  63. package/dist/views/QuoteListView.d.ts +7 -0
  64. package/dist/views/QuoteListView.d.ts.map +1 -0
  65. package/dist/views/dashboardWidgets.d.ts +11 -0
  66. package/dist/views/dashboardWidgets.d.ts.map +1 -0
  67. package/package.json +55 -0
  68. package/src/CrmContext.tsx +30 -0
  69. package/src/CrmPage.tsx +152 -0
  70. package/src/README.md +97 -0
  71. package/src/cascade.ts +24 -0
  72. package/src/components/CrmGeneralSettings.tsx +36 -0
  73. package/src/components/CrmOnboarding.tsx +52 -0
  74. package/src/components/PipelineSettings.tsx +45 -0
  75. package/src/components/QuoteStatusDropdown.tsx +214 -0
  76. package/src/data/index.ts +2 -0
  77. package/src/data/mock.ts +426 -0
  78. package/src/data/supabase.ts +634 -0
  79. package/src/data/types.ts +50 -0
  80. package/src/index.ts +240 -0
  81. package/src/lib/tenant.ts +4 -0
  82. package/src/locales/en.ts +292 -0
  83. package/src/locales/index.ts +7 -0
  84. package/src/locales/pt-BR.ts +292 -0
  85. package/src/migrations/001_crm_base.sql +73 -0
  86. package/src/migrations/002_activities.sql +33 -0
  87. package/src/migrations/003_crm_views_rls.sql +61 -0
  88. package/src/migrations/004_seed_default_pipeline.sql +31 -0
  89. package/src/registries.ts +88 -0
  90. package/src/schema/index.ts +102 -0
  91. package/src/store.ts +375 -0
  92. package/src/types.ts +280 -0
  93. package/src/views/ActivityListView.tsx +183 -0
  94. package/src/views/DashboardView.tsx +12 -0
  95. package/src/views/DealSidebar.tsx +414 -0
  96. package/src/views/LeadDetailView.tsx +264 -0
  97. package/src/views/LeadFormView.tsx +56 -0
  98. package/src/views/LeadListView.tsx +81 -0
  99. package/src/views/PipelineView.tsx +299 -0
  100. package/src/views/QuoteDetailView.tsx +408 -0
  101. package/src/views/QuoteFormView.tsx +489 -0
  102. package/src/views/QuoteListView.tsx +101 -0
  103. package/src/views/dashboardWidgets.tsx +248 -0
@@ -0,0 +1,414 @@
1
+ import React, { useEffect, useState } from 'react'
2
+ import { Phone, Mail, Users, FileText, CheckSquare, MessageCircle, Check, Calendar, DollarSign, User, Target, Clock, ArrowRight } from 'lucide-react'
3
+ import { useTranslation } from '@fayz-ai/core'
4
+ import { Sheet, SheetContent, SheetHeader, SheetBody, SheetFooter, SheetTitle, SheetDescription } from '@fayz-ai/ui'
5
+ import { PersonLink } from '@fayz-ai/saas'
6
+ import { useCrmProvider, useCrmConfig, formatCurrency } from '../CrmContext'
7
+ import { QuoteStatusDropdown } from '../components/QuoteStatusDropdown'
8
+ import type { Deal, Lead, Quote, Activity } from '../types'
9
+
10
+ // ---------------------------------------------------------------------------
11
+ // Activity icons
12
+ // ---------------------------------------------------------------------------
13
+
14
+ const TYPE_ICONS: Record<string, React.ElementType> = {
15
+ call: Phone, email: Mail, meeting: Users, note: FileText, task: CheckSquare, whatsapp: MessageCircle,
16
+ }
17
+
18
+
19
+ // ---------------------------------------------------------------------------
20
+ // Tabs
21
+ // ---------------------------------------------------------------------------
22
+
23
+ type TabId = 'overview' | 'activity' | 'quotes'
24
+
25
+ function TabBar({ active, counts, onChange }: { active: TabId; counts: { activity: number; quotes: number }; onChange: (id: TabId) => void }) {
26
+ const t = useTranslation()
27
+ const tabs: { id: TabId; label: string; count?: number }[] = [
28
+ { id: 'overview', label: t('crm.dealSidebar.overview') },
29
+ { id: 'activity', label: t('crm.dealSidebar.activity'), count: counts.activity },
30
+ { id: 'quotes', label: t('crm.dealSidebar.quotes'), count: counts.quotes },
31
+ ]
32
+
33
+ return (
34
+ <div className="flex items-center gap-0.5 px-5 py-2 border-b shrink-0 bg-muted/20">
35
+ {tabs.map((tab) => (
36
+ <button
37
+ key={tab.id}
38
+ onClick={() => onChange(tab.id)}
39
+ className={`relative px-3 py-1.5 text-xs font-medium rounded-md transition-colors ${
40
+ active === tab.id
41
+ ? 'bg-card text-foreground shadow-sm'
42
+ : 'text-muted-foreground hover:text-foreground'
43
+ }`}
44
+ >
45
+ {tab.label}
46
+ {tab.count != null && tab.count > 0 && (
47
+ <span className={`ml-1 inline-flex items-center justify-center min-w-[16px] h-4 rounded-full px-1 text-[10px] font-semibold ${
48
+ active === tab.id ? 'bg-primary/10 text-primary' : 'bg-muted text-muted-foreground'
49
+ }`}>
50
+ {tab.count}
51
+ </span>
52
+ )}
53
+ </button>
54
+ ))}
55
+ </div>
56
+ )
57
+ }
58
+
59
+ // ---------------------------------------------------------------------------
60
+ // Overview tab
61
+ // ---------------------------------------------------------------------------
62
+
63
+ function OverviewTab({ deal, lead, currency }: { deal: Deal; lead: Lead | null; currency: { code: string; locale: string; symbol: string } }) {
64
+ const t = useTranslation()
65
+ const initial = (lead?.name ?? deal.contactName ?? deal.title).charAt(0).toUpperCase()
66
+
67
+ return (
68
+ <div className="space-y-5">
69
+ {/* Lead / contact hero */}
70
+ <div className="flex items-center gap-3.5">
71
+ <div className="flex h-14 w-14 shrink-0 items-center justify-center rounded-2xl bg-primary/10 text-primary text-xl font-bold">
72
+ {initial}
73
+ </div>
74
+ <div className="min-w-0 flex-1">
75
+ <PersonLink
76
+ personId={lead?.id ?? deal.contactId}
77
+ name={lead?.name ?? deal.contactName ?? deal.title}
78
+ size="default"
79
+ className="text-base"
80
+ />
81
+ {lead && (lead.email || lead.phone) && (
82
+ <p className="text-xs text-muted-foreground truncate">{lead.email || lead.phone}</p>
83
+ )}
84
+ {lead?.company && (
85
+ <p className="text-[11px] text-muted-foreground">{lead.company}</p>
86
+ )}
87
+ </div>
88
+ </div>
89
+
90
+ {/* Deal metrics */}
91
+ <div className="rounded-xl border divide-y">
92
+ <div className="flex items-center gap-3 px-4 py-3">
93
+ <DollarSign className="h-4 w-4 text-muted-foreground shrink-0" />
94
+ <span className="text-xs text-muted-foreground flex-1">{t('crm.dealSidebar.value')}</span>
95
+ <span className="text-sm font-bold">{formatCurrency(deal.value, currency)}</span>
96
+ </div>
97
+ <div className="flex items-center gap-3 px-4 py-3">
98
+ <Target className="h-4 w-4 text-muted-foreground shrink-0" />
99
+ <span className="text-xs text-muted-foreground flex-1">{t('crm.dealSidebar.probability')}</span>
100
+ <span className="text-sm font-semibold">{deal.probability}%</span>
101
+ </div>
102
+ {deal.stageName && (
103
+ <div className="flex items-center gap-3 px-4 py-3">
104
+ <div className="h-4 w-4 flex items-center justify-center shrink-0">
105
+ <div className="h-2.5 w-2.5 rounded-full" style={{ backgroundColor: deal.stageColor }} />
106
+ </div>
107
+ <span className="text-xs text-muted-foreground flex-1">{t('crm.dealSidebar.stage')}</span>
108
+ <span className="text-sm font-medium">{deal.stageName}</span>
109
+ </div>
110
+ )}
111
+ {deal.expectedCloseDate && (
112
+ <div className="flex items-center gap-3 px-4 py-3">
113
+ <Calendar className="h-4 w-4 text-muted-foreground shrink-0" />
114
+ <span className="text-xs text-muted-foreground flex-1">{t('crm.dealSidebar.expectedClose')}</span>
115
+ <span className="text-xs font-medium">{deal.expectedCloseDate}</span>
116
+ </div>
117
+ )}
118
+ {deal.assignedToName && (
119
+ <div className="flex items-center gap-3 px-4 py-3">
120
+ <User className="h-4 w-4 text-muted-foreground shrink-0" />
121
+ <span className="text-xs text-muted-foreground flex-1">{t('crm.dealSidebar.assignedTo')}</span>
122
+ <span className="text-xs font-medium">{deal.assignedToName}</span>
123
+ </div>
124
+ )}
125
+ </div>
126
+
127
+ {/* Contact links */}
128
+ {lead && (lead.email || lead.phone) && (
129
+ <div className="space-y-2">
130
+ <h4 className="text-[11px] font-semibold text-muted-foreground uppercase tracking-wider">{t('crm.dealSidebar.contact')}</h4>
131
+ <div className="rounded-xl border divide-y">
132
+ {lead.email && (
133
+ <a href={`mailto:${lead.email}`} className="flex items-center gap-3 px-4 py-2.5 text-xs text-primary hover:bg-muted/30 transition-colors">
134
+ <Mail className="h-3.5 w-3.5 shrink-0" /> <span className="truncate">{lead.email}</span>
135
+ </a>
136
+ )}
137
+ {lead.phone && (
138
+ <a href={`tel:${lead.phone}`} className="flex items-center gap-3 px-4 py-2.5 text-xs text-primary hover:bg-muted/30 transition-colors">
139
+ <Phone className="h-3.5 w-3.5 shrink-0" /> <span>{lead.phone}</span>
140
+ </a>
141
+ )}
142
+ </div>
143
+ </div>
144
+ )}
145
+
146
+ {/* Tags */}
147
+ {deal.tags && deal.tags.length > 0 && (
148
+ <div className="space-y-2">
149
+ <h4 className="text-[11px] font-semibold text-muted-foreground uppercase tracking-wider">{t('crm.dealSidebar.tags')}</h4>
150
+ <div className="flex gap-1.5 flex-wrap">
151
+ {deal.tags.map((tag) => (
152
+ <span key={tag} className="inline-flex items-center rounded-full bg-muted px-2.5 py-0.5 text-[10px] font-medium text-muted-foreground">{tag}</span>
153
+ ))}
154
+ </div>
155
+ </div>
156
+ )}
157
+
158
+ {/* Notes */}
159
+ {deal.notes && deal.notes !== deal.title && (
160
+ <div className="space-y-2">
161
+ <h4 className="text-[11px] font-semibold text-muted-foreground uppercase tracking-wider">{t('crm.dealSidebar.notes')}</h4>
162
+ <p className="text-xs text-muted-foreground whitespace-pre-wrap leading-relaxed">{deal.notes}</p>
163
+ </div>
164
+ )}
165
+
166
+ {/* Dates */}
167
+ <div className="flex items-center gap-4 pt-3 border-t text-[10px] text-muted-foreground">
168
+ <span>{t('crm.dealSidebar.created')} {deal.createdAt?.slice(0, 10)}</span>
169
+ {deal.updatedAt && <span>{t('crm.dealSidebar.updated')} {deal.updatedAt?.slice(0, 10)}</span>}
170
+ </div>
171
+ </div>
172
+ )
173
+ }
174
+
175
+ // ---------------------------------------------------------------------------
176
+ // Activity tab
177
+ // ---------------------------------------------------------------------------
178
+
179
+ function ActivityTab({ activities, loading }: { activities: Activity[]; loading: boolean }) {
180
+ const t = useTranslation()
181
+ if (loading) {
182
+ return (
183
+ <div className="space-y-3">
184
+ {[1, 2, 3].map((i) => (
185
+ <div key={i} className="flex items-start gap-3">
186
+ <div className="h-8 w-8 rounded-lg bg-muted/40 animate-pulse shrink-0" />
187
+ <div className="flex-1 space-y-1.5 pt-0.5">
188
+ <div className="h-3.5 w-2/3 rounded bg-muted/40 animate-pulse" />
189
+ <div className="h-2.5 w-1/3 rounded bg-muted/30 animate-pulse" />
190
+ </div>
191
+ </div>
192
+ ))}
193
+ </div>
194
+ )
195
+ }
196
+
197
+ if (activities.length === 0) {
198
+ return (
199
+ <div className="flex flex-col items-center justify-center py-12 text-center">
200
+ <div className="flex h-12 w-12 items-center justify-center rounded-2xl bg-muted/30 mb-3">
201
+ <Clock className="h-5 w-5 text-muted-foreground/40" />
202
+ </div>
203
+ <p className="text-sm text-muted-foreground">{t('crm.dealSidebar.noActivities')}</p>
204
+ <p className="text-[10px] text-muted-foreground/60 mt-0.5">{t('crm.dealSidebar.activitiesAppear')}</p>
205
+ </div>
206
+ )
207
+ }
208
+
209
+ return (
210
+ <div className="space-y-1">
211
+ {activities.map((a, i) => {
212
+ const Icon = TYPE_ICONS[a.activityType] ?? FileText
213
+ return (
214
+ <div key={a.id} className="flex items-start gap-3 rounded-lg px-2 py-2.5 hover:bg-muted/20 transition-colors">
215
+ <div className="flex h-8 w-8 items-center justify-center rounded-lg bg-muted shrink-0">
216
+ <Icon className="h-3.5 w-3.5 text-muted-foreground" />
217
+ </div>
218
+ <div className="flex-1 min-w-0 pt-0.5">
219
+ <div className="flex items-center gap-1.5">
220
+ <p className="text-xs font-medium truncate">{a.title}</p>
221
+ {a.completedAt && <Check className="h-3 w-3 text-success shrink-0" />}
222
+ </div>
223
+ {a.description && <p className="text-[11px] text-muted-foreground mt-0.5 line-clamp-2">{a.description}</p>}
224
+ <p className="text-[10px] text-muted-foreground/50 mt-1 capitalize">{a.activityType} · {new Date(a.createdAt).toLocaleDateString()}</p>
225
+ </div>
226
+ </div>
227
+ )
228
+ })}
229
+ </div>
230
+ )
231
+ }
232
+
233
+ // ---------------------------------------------------------------------------
234
+ // Quotes tab
235
+ // ---------------------------------------------------------------------------
236
+
237
+ function QuotesTab({ quotes, loading, currency, onViewQuote, onQuoteUpdated }: { quotes: Quote[]; loading: boolean; currency: { code: string; locale: string; symbol: string }; onViewQuote?: (id: string) => void; onQuoteUpdated?: (q: Quote) => void }) {
238
+ const t = useTranslation()
239
+ if (loading) {
240
+ return (
241
+ <div className="space-y-2">
242
+ {[1, 2].map((i) => (
243
+ <div key={i} className="rounded-xl border p-4 space-y-2.5">
244
+ <div className="h-3.5 w-20 rounded bg-muted/40 animate-pulse" />
245
+ <div className="h-3 w-28 rounded bg-muted/30 animate-pulse" />
246
+ </div>
247
+ ))}
248
+ </div>
249
+ )
250
+ }
251
+
252
+ if (quotes.length === 0) {
253
+ return (
254
+ <div className="flex flex-col items-center justify-center py-12 text-center">
255
+ <div className="flex h-12 w-12 items-center justify-center rounded-2xl bg-muted/30 mb-3">
256
+ <FileText className="h-5 w-5 text-muted-foreground/40" />
257
+ </div>
258
+ <p className="text-sm text-muted-foreground">{t('crm.dealSidebar.noQuotes')}</p>
259
+ <p className="text-[10px] text-muted-foreground/60 mt-0.5">{t('crm.dealSidebar.quotesAppear')}</p>
260
+ </div>
261
+ )
262
+ }
263
+
264
+ return (
265
+ <div className="space-y-2">
266
+ {quotes.map((q) => (
267
+ <button
268
+ key={q.id}
269
+ onClick={() => onViewQuote?.(q.id)}
270
+ className="w-full text-left rounded-xl border p-4 hover:bg-muted/20 hover:shadow-sm transition-all group"
271
+ >
272
+ <div className="flex items-center justify-between" onClick={(e) => e.stopPropagation()}>
273
+ <span className="text-sm font-semibold">{q.quoteNumber}</span>
274
+ <QuoteStatusDropdown quote={q} size="sm" onStatusChange={onQuoteUpdated} />
275
+ </div>
276
+ <div className="flex items-center justify-between mt-2">
277
+ <span className="text-[11px] text-muted-foreground">{q.quoteDate}</span>
278
+ <span className="text-sm font-bold">{formatCurrency(q.totalAmount, currency)}</span>
279
+ </div>
280
+ {q.contactName && (
281
+ <p className="text-[10px] text-muted-foreground mt-1.5 truncate">{q.contactName}</p>
282
+ )}
283
+ </button>
284
+ ))}
285
+ </div>
286
+ )
287
+ }
288
+
289
+ // ---------------------------------------------------------------------------
290
+ // Deal sidebar
291
+ // ---------------------------------------------------------------------------
292
+
293
+ export function DealSidebar({ dealId, open, onClose, onViewLead, onViewQuote }: {
294
+ dealId: string
295
+ open: boolean
296
+ onClose: () => void
297
+ onViewLead?: (id: string) => void
298
+ onViewQuote?: (id: string) => void
299
+ }) {
300
+ const t = useTranslation()
301
+ const provider = useCrmProvider()
302
+ const { currency } = useCrmConfig()
303
+ const [deal, setDeal] = useState<Deal | null>(null)
304
+ const [lead, setLead] = useState<Lead | null>(null)
305
+ const [activities, setActivities] = useState<Activity[]>([])
306
+ const [quotes, setQuotes] = useState<Quote[]>([])
307
+ const [loading, setLoading] = useState(true)
308
+ const [activitiesLoading, setActivitiesLoading] = useState(true)
309
+ const [quotesLoading, setQuotesLoading] = useState(true)
310
+ const [activeTab, setActiveTab] = useState<TabId>('overview')
311
+
312
+ useEffect(() => {
313
+ if (!open) return
314
+ setLoading(true)
315
+ setActivitiesLoading(true)
316
+ setQuotesLoading(true)
317
+ setActiveTab('overview')
318
+ setLead(null)
319
+ setActivities([])
320
+ setQuotes([])
321
+
322
+ provider.getDealById(dealId).then(async (d) => {
323
+ setDeal(d)
324
+ if (d?.leadId) {
325
+ const l = await provider.getLeadById(d.leadId)
326
+ setLead(l)
327
+ }
328
+ setLoading(false)
329
+ })
330
+
331
+ provider.getActivities({ dealId }).then((r) => {
332
+ setActivities(r.data)
333
+ setActivitiesLoading(false)
334
+ })
335
+
336
+ provider.getQuotesByDealId(dealId).then((q) => {
337
+ setQuotes(q)
338
+ setQuotesLoading(false)
339
+ })
340
+ }, [dealId, open])
341
+
342
+ return (
343
+ <Sheet open={open} onOpenChange={(v) => { if (!v) onClose() }}>
344
+ <SheetContent width="max-w-md">
345
+ {/* Header */}
346
+ <SheetHeader>
347
+ {loading ? (
348
+ <div className="space-y-1.5 pr-8">
349
+ <div className="h-4 w-40 rounded bg-muted/40 animate-pulse" />
350
+ <div className="h-3 w-24 rounded bg-muted/30 animate-pulse" />
351
+ </div>
352
+ ) : (
353
+ <div className="pr-8">
354
+ <SheetTitle>{deal?.title ?? t('crm.dealSidebar.deal')}</SheetTitle>
355
+ {deal?.contactName && <SheetDescription>{deal.contactName}</SheetDescription>}
356
+ </div>
357
+ )}
358
+ </SheetHeader>
359
+
360
+ {/* Tabs */}
361
+ <TabBar
362
+ active={activeTab}
363
+ counts={{ activity: activities.length, quotes: quotes.length }}
364
+ onChange={setActiveTab}
365
+ />
366
+
367
+ {/* Content */}
368
+ <SheetBody>
369
+ {loading ? (
370
+ <div className="space-y-5">
371
+ <div className="flex items-center gap-3.5">
372
+ <div className="h-14 w-14 rounded-2xl bg-muted/40 animate-pulse" />
373
+ <div className="space-y-2 flex-1">
374
+ <div className="h-4 w-32 rounded bg-muted/40 animate-pulse" />
375
+ <div className="h-3 w-24 rounded bg-muted/30 animate-pulse" />
376
+ </div>
377
+ </div>
378
+ <div className="rounded-xl border divide-y">
379
+ {[1, 2, 3].map((i) => (
380
+ <div key={i} className="flex items-center justify-between px-4 py-3">
381
+ <div className="h-3 w-20 rounded bg-muted/30 animate-pulse" />
382
+ <div className="h-3 w-24 rounded bg-muted/40 animate-pulse" />
383
+ </div>
384
+ ))}
385
+ </div>
386
+ </div>
387
+ ) : deal ? (
388
+ <>
389
+ {activeTab === 'overview' && <OverviewTab deal={deal} lead={lead} currency={currency} />}
390
+ {activeTab === 'activity' && <ActivityTab activities={activities} loading={activitiesLoading} />}
391
+ {activeTab === 'quotes' && <QuotesTab quotes={quotes} loading={quotesLoading} currency={currency} onViewQuote={(id) => { onViewQuote?.(id); onClose() }} onQuoteUpdated={(updated) => setQuotes(quotes.map((q) => q.id === updated.id ? updated : q))} />}
392
+ </>
393
+ ) : (
394
+ <div className="flex flex-col items-center justify-center py-12 text-center">
395
+ <p className="text-sm text-muted-foreground">{t('crm.dealSidebar.dealNotFound')}</p>
396
+ </div>
397
+ )}
398
+ </SheetBody>
399
+
400
+ {/* Footer */}
401
+ {deal && lead && onViewLead && (
402
+ <SheetFooter>
403
+ <button
404
+ onClick={() => { onViewLead(lead.id); onClose() }}
405
+ className="flex-1 inline-flex items-center justify-center gap-2 rounded-lg border px-3 py-2.5 text-xs font-medium hover:bg-muted bg-card shadow-button active:shadow-button-inset transition-colors"
406
+ >
407
+ {t('crm.dealSidebar.viewLeadProfile')} <ArrowRight className="h-3 w-3" />
408
+ </button>
409
+ </SheetFooter>
410
+ )}
411
+ </SheetContent>
412
+ </Sheet>
413
+ )
414
+ }
@@ -0,0 +1,264 @@
1
+ import React, { useEffect, useState } from 'react'
2
+ import { UserPlus, FileText } from 'lucide-react'
3
+ import { useCrmProvider, useCrmConfig, formatCurrency } from '../CrmContext'
4
+ import { useTranslation } from '@fayz-ai/core'
5
+ import { QuoteStatusDropdown } from '../components/QuoteStatusDropdown'
6
+ import { Breadcrumb } from '@fayz-ai/ui'
7
+ import type { Lead, Deal, Quote } from '../types'
8
+
9
+ const STATUS_COLORS: Record<string, string> = {
10
+ new: 'bg-info-soft text-info-soft-foreground',
11
+ contacted: 'bg-magic-soft text-magic-soft-foreground',
12
+ qualified: 'bg-success-soft text-success-soft-foreground',
13
+ unqualified: 'bg-muted text-muted-foreground',
14
+ converted: 'bg-warning-soft text-warning-soft-foreground',
15
+ lost: 'bg-destructive-soft text-destructive-soft-foreground',
16
+ }
17
+
18
+
19
+ function FieldRow({ label, value }: { label: string; value: React.ReactNode }) {
20
+ return (
21
+ <div className="grid grid-cols-3 gap-4 py-3">
22
+ <dt className="text-sm font-medium text-muted-foreground">{label}</dt>
23
+ <dd className="col-span-2 text-sm text-foreground">{value ?? <span className="text-muted-foreground/50">—</span>}</dd>
24
+ </div>
25
+ )
26
+ }
27
+
28
+ export function LeadDetailView({ leadId, onBack, onCreateQuote, onViewQuote }: {
29
+ leadId: string
30
+ onBack: () => void
31
+ onCreateQuote?: (leadId: string) => void
32
+ onViewQuote?: (quoteId: string) => void
33
+ }) {
34
+ const provider = useCrmProvider()
35
+ const { currency } = useCrmConfig()
36
+ const t = useTranslation()
37
+ const [lead, setLead] = useState<Lead | null>(null)
38
+ const [deal, setDeal] = useState<Deal | null>(null)
39
+ const [quotes, setQuotes] = useState<Quote[]>([])
40
+ const [loading, setLoading] = useState(true)
41
+
42
+ useEffect(() => {
43
+ setLoading(true)
44
+ provider.getLeadById(leadId).then(async (l) => {
45
+ setLead(l)
46
+ if (l) {
47
+ const d = await provider.getDealByLeadId(leadId)
48
+ setDeal(d)
49
+ if (d) {
50
+ const q = await provider.getQuotesByDealId(d.id)
51
+ setQuotes(q)
52
+ }
53
+ }
54
+ setLoading(false)
55
+ })
56
+ }, [leadId])
57
+
58
+ if (loading) {
59
+ return (
60
+ <div className="space-y-6">
61
+ <Breadcrumb parent={t('crm.leads.title')} current={t('crm.leadDetail.loading')} onBack={onBack} />
62
+ <div className="flex items-start gap-5">
63
+ <div className="h-20 w-20 shrink-0 rounded-2xl bg-muted/40 animate-pulse" />
64
+ <div className="flex-1 space-y-2 pt-1">
65
+ <div className="h-6 w-40 rounded bg-muted/40 animate-pulse" />
66
+ <div className="h-4 w-28 rounded bg-muted/30 animate-pulse" />
67
+ <div className="h-5 w-16 rounded-full bg-muted/30 animate-pulse" />
68
+ </div>
69
+ </div>
70
+ <div className="border-t" />
71
+ <div className="space-y-4">
72
+ <div className="h-4 w-36 rounded bg-muted/30 animate-pulse" />
73
+ <div className="rounded-xl border bg-card shadow-sm p-5 space-y-3">
74
+ {[1, 2, 3, 4].map((i) => (
75
+ <div key={i} className="grid grid-cols-3 gap-4">
76
+ <div className="h-3 w-16 rounded bg-muted/30 animate-pulse" />
77
+ <div className="col-span-2 h-3 w-24 rounded bg-muted/40 animate-pulse" />
78
+ </div>
79
+ ))}
80
+ </div>
81
+ </div>
82
+ </div>
83
+ )
84
+ }
85
+
86
+ if (!lead) {
87
+ return (
88
+ <div className="space-y-6">
89
+ <Breadcrumb parent={t('crm.leads.title')} current={t('crm.leadDetail.notFound')} onBack={onBack} />
90
+ <div className="flex flex-col items-center justify-center py-12 text-center rounded-lg border-2 border-dashed border-muted">
91
+ <UserPlus className="h-8 w-8 text-muted-foreground/30 mb-2" />
92
+ <p className="text-sm text-muted-foreground">{t('crm.leadDetail.leadNotFound')}</p>
93
+ <button onClick={onBack} className="text-xs text-primary hover:underline mt-1">{t('crm.leadDetail.backToList')}</button>
94
+ </div>
95
+ </div>
96
+ )
97
+ }
98
+
99
+ const initial = lead.name.charAt(0).toUpperCase()
100
+
101
+ return (
102
+ <div className="space-y-6">
103
+ {/* Breadcrumb */}
104
+ <Breadcrumb parent={t('crm.leads.title')} current={lead.name} onBack={onBack} />
105
+
106
+ {/* Hero */}
107
+ <div className="flex items-start gap-5">
108
+ <div className="flex h-20 w-20 shrink-0 items-center justify-center rounded-2xl bg-primary/10 text-primary text-2xl font-bold shadow-sm">
109
+ {initial}
110
+ </div>
111
+ <div className="flex-1 min-w-0">
112
+ <h1 className="text-2xl font-bold text-foreground truncate">{lead.name}</h1>
113
+ {(lead.email || lead.phone) && (
114
+ <p className="text-muted-foreground mt-0.5">{lead.email || lead.phone}</p>
115
+ )}
116
+ <div className="flex items-center gap-2 mt-2">
117
+ <span className={`inline-flex items-center rounded-full px-2.5 py-0.5 text-[10px] font-medium capitalize ${STATUS_COLORS[lead.status] ?? 'bg-muted text-muted-foreground'}`}>
118
+ {lead.status}
119
+ </span>
120
+ {lead.sourceName && (
121
+ <span className="inline-flex items-center rounded-full bg-muted px-2.5 py-0.5 text-[10px] font-medium text-muted-foreground">
122
+ {lead.sourceName}
123
+ </span>
124
+ )}
125
+ </div>
126
+ </div>
127
+ {/* Actions */}
128
+ {onCreateQuote && lead.status !== 'converted' && lead.status !== 'lost' && (
129
+ <button
130
+ onClick={() => onCreateQuote(leadId)}
131
+ className="inline-flex items-center gap-1.5 rounded-lg bg-primary border border-primary px-3 py-1.5 text-xs font-medium text-primary-foreground hover:bg-primary/90 shadow-button-primary active:shadow-button-inset transition-colors shrink-0"
132
+ >
133
+ <FileText className="h-3 w-3" /> {t('crm.leadDetail.createQuote')}
134
+ </button>
135
+ )}
136
+ </div>
137
+
138
+ {/* Separator */}
139
+ <div className="border-t" />
140
+
141
+ {/* Contact Information */}
142
+ <div>
143
+ <h3 className="text-sm font-semibold text-foreground mb-1">{t('crm.leadDetail.contactInfo')}</h3>
144
+ <div className="rounded-xl border bg-card shadow-sm">
145
+ <dl className="grid divide-y md:grid-cols-2 md:divide-y-0">
146
+ <div className="px-5 md:border-b">
147
+ <FieldRow label={t('crm.leadDetail.email')} value={lead.email ? <a href={`mailto:${lead.email}`} className="text-primary hover:underline">{lead.email}</a> : null} />
148
+ </div>
149
+ <div className="px-5 md:border-b">
150
+ <FieldRow label={t('crm.leadDetail.phone')} value={lead.phone ? <a href={`tel:${lead.phone}`} className="text-primary hover:underline">{lead.phone}</a> : null} />
151
+ </div>
152
+ <div className="px-5 md:border-b md:last:border-b-0">
153
+ <FieldRow label={t('crm.leadDetail.company')} value={lead.company} />
154
+ </div>
155
+ <div className="px-5 md:border-b md:last:border-b-0">
156
+ <FieldRow label={t('crm.leadDetail.source')} value={lead.sourceName} />
157
+ </div>
158
+ </dl>
159
+ </div>
160
+ </div>
161
+
162
+ {/* Pipeline / Deal */}
163
+ {deal && (
164
+ <div>
165
+ <h3 className="text-sm font-semibold text-foreground mb-1">{t('crm.leadDetail.pipeline')}</h3>
166
+ <div className="rounded-xl border bg-card shadow-sm">
167
+ <dl className="grid divide-y md:grid-cols-2 md:divide-y-0">
168
+ <div className="px-5 md:border-b">
169
+ <FieldRow label={t('crm.leadDetail.stage')} value={deal.stageName ? (
170
+ <span className="inline-flex items-center gap-1.5">
171
+ {deal.stageColor && <span className="h-2 w-2 rounded-full" style={{ backgroundColor: deal.stageColor }} />}
172
+ {deal.stageName}
173
+ </span>
174
+ ) : null} />
175
+ </div>
176
+ <div className="px-5 md:border-b">
177
+ <FieldRow label={t('crm.leadDetail.dealValue')} value={deal.value > 0 ? formatCurrency(deal.value, currency) : null} />
178
+ </div>
179
+ <div className="px-5 md:border-b md:last:border-b-0">
180
+ <FieldRow label={t('crm.leadDetail.probability')} value={`${deal.probability}%`} />
181
+ </div>
182
+ <div className="px-5 md:border-b md:last:border-b-0">
183
+ <FieldRow label={t('crm.leadDetail.dealStatus')} value={deal.status} />
184
+ </div>
185
+ </dl>
186
+ </div>
187
+ </div>
188
+ )}
189
+
190
+ {/* Quotes */}
191
+ {quotes.length > 0 && (
192
+ <div>
193
+ <h3 className="text-sm font-semibold text-foreground mb-1">{t('crm.leadDetail.quotes')}</h3>
194
+ <div className="rounded-xl border bg-card shadow-sm divide-y">
195
+ {quotes.map((q) => (
196
+ <div
197
+ key={q.id}
198
+ onClick={() => onViewQuote?.(q.id)}
199
+ className="flex items-center justify-between px-5 py-3 hover:bg-muted/20 transition-colors cursor-pointer"
200
+ >
201
+ <div className="flex items-center gap-3 min-w-0">
202
+ <FileText className="h-4 w-4 text-muted-foreground shrink-0" />
203
+ <div className="min-w-0">
204
+ <p className="text-sm font-medium">{q.quoteNumber}</p>
205
+ <p className="text-[10px] text-muted-foreground">{q.quoteDate}</p>
206
+ </div>
207
+ </div>
208
+ <div className="flex items-center gap-3 shrink-0" onClick={(e) => e.stopPropagation()}>
209
+ <span className="text-sm font-semibold">{formatCurrency(q.totalAmount, currency)}</span>
210
+ <QuoteStatusDropdown quote={q} size="sm" onStatusChange={(updated) => setQuotes(quotes.map((existing) => existing.id === updated.id ? updated : existing))} />
211
+ </div>
212
+ </div>
213
+ ))}
214
+ </div>
215
+ </div>
216
+ )}
217
+
218
+ {/* Details */}
219
+ <div>
220
+ <h3 className="text-sm font-semibold text-foreground mb-1">{t('crm.leadDetail.details')}</h3>
221
+ <div className="rounded-xl border bg-card shadow-sm">
222
+ <dl className="grid divide-y md:grid-cols-2 md:divide-y-0">
223
+ <div className="px-5 md:border-b">
224
+ <FieldRow label={t('crm.leadDetail.value')} value={lead.value != null && lead.value > 0 ? lead.value : null} />
225
+ </div>
226
+ <div className="px-5 md:border-b">
227
+ <FieldRow label={t('crm.leadDetail.assignedTo')} value={lead.assignedToName} />
228
+ </div>
229
+ <div className="px-5 md:border-b md:last:border-b-0">
230
+ <FieldRow label={t('crm.leadDetail.created')} value={lead.createdAt?.slice(0, 10)} />
231
+ </div>
232
+ <div className="px-5 md:border-b md:last:border-b-0">
233
+ <FieldRow label={t('crm.leadDetail.updated')} value={lead.updatedAt?.slice(0, 10)} />
234
+ </div>
235
+ </dl>
236
+ </div>
237
+ </div>
238
+
239
+ {/* Tags */}
240
+ {lead.tags && lead.tags.length > 0 && (
241
+ <div>
242
+ <h3 className="text-sm font-semibold text-foreground mb-1">{t('crm.leadDetail.tags')}</h3>
243
+ <div className="rounded-xl border bg-card shadow-sm px-5 py-4">
244
+ <div className="flex gap-1.5 flex-wrap">
245
+ {lead.tags.map((tag) => (
246
+ <span key={tag} className="inline-flex items-center rounded-full bg-muted px-2.5 py-0.5 text-[10px] font-medium text-muted-foreground">{tag}</span>
247
+ ))}
248
+ </div>
249
+ </div>
250
+ </div>
251
+ )}
252
+
253
+ {/* Notes */}
254
+ {lead.notes && (
255
+ <div>
256
+ <h3 className="text-sm font-semibold text-foreground mb-1">{t('crm.leadDetail.notes')}</h3>
257
+ <div className="rounded-xl border bg-card shadow-sm px-5 py-4">
258
+ <p className="text-sm text-muted-foreground whitespace-pre-wrap">{lead.notes}</p>
259
+ </div>
260
+ </div>
261
+ )}
262
+ </div>
263
+ )
264
+ }