@fayz-ai/plugin-crm 0.8.1 → 0.8.4

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 (63) hide show
  1. package/dist/cascade.d.ts.map +1 -1
  2. package/dist/components/QuoteStatusDropdown.d.ts.map +1 -1
  3. package/dist/data/mock.d.ts +5 -0
  4. package/dist/data/mock.d.ts.map +1 -1
  5. package/dist/data/supabase.d.ts +6 -0
  6. package/dist/data/supabase.d.ts.map +1 -1
  7. package/dist/data/types.d.ts +9 -1
  8. package/dist/data/types.d.ts.map +1 -1
  9. package/dist/index.cjs +698 -103
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +701 -106
  13. package/dist/index.js.map +1 -1
  14. package/dist/lib/access.d.ts +8 -0
  15. package/dist/lib/access.d.ts.map +1 -0
  16. package/dist/locales/en.d.ts.map +1 -1
  17. package/dist/locales/pt-BR.d.ts.map +1 -1
  18. package/dist/migrations/index.d.ts +5 -1
  19. package/dist/migrations/index.d.ts.map +1 -1
  20. package/dist/public/index.cjs +1 -1
  21. package/dist/public/index.cjs.map +1 -1
  22. package/dist/public/index.js +2 -2
  23. package/dist/public/index.js.map +1 -1
  24. package/dist/registries.d.ts +11 -0
  25. package/dist/registries.d.ts.map +1 -1
  26. package/dist/store.d.ts +5 -0
  27. package/dist/store.d.ts.map +1 -1
  28. package/dist/types.d.ts +14 -2
  29. package/dist/types.d.ts.map +1 -1
  30. package/dist/views/ActivityListView.d.ts +6 -1
  31. package/dist/views/ActivityListView.d.ts.map +1 -1
  32. package/dist/views/DealSidebar.d.ts.map +1 -1
  33. package/dist/views/LeadDetailView.d.ts.map +1 -1
  34. package/dist/views/PipelineView.d.ts.map +1 -1
  35. package/dist/views/QuoteDetailView.d.ts.map +1 -1
  36. package/dist/views/QuoteListView.d.ts.map +1 -1
  37. package/package.json +5 -5
  38. package/src/CrmPage.tsx +1 -1
  39. package/src/cascade.ts +2 -0
  40. package/src/components/QuoteStatusDropdown.tsx +15 -3
  41. package/src/data/mock.ts +110 -4
  42. package/src/data/supabase.ts +186 -8
  43. package/src/data/types.ts +9 -1
  44. package/src/index.ts +85 -3
  45. package/src/lib/access.ts +11 -0
  46. package/src/locales/en.ts +4 -0
  47. package/src/locales/pt-BR.ts +9 -5
  48. package/src/migrations/003_crm_views_rls.sql +10 -2
  49. package/src/migrations/007_activity_log.sql +55 -0
  50. package/src/migrations/008_view_field_aliases.sql +43 -0
  51. package/src/migrations/009_deal_archived_status.sql +30 -0
  52. package/src/migrations/010_quote_archived_status.sql +40 -0
  53. package/src/migrations/index.ts +191 -3
  54. package/src/public/data.supabase.ts +2 -2
  55. package/src/registries.ts +32 -8
  56. package/src/store.ts +76 -0
  57. package/src/types.ts +19 -2
  58. package/src/views/ActivityListView.tsx +79 -11
  59. package/src/views/DealSidebar.tsx +39 -33
  60. package/src/views/LeadDetailView.tsx +29 -10
  61. package/src/views/PipelineView.tsx +22 -4
  62. package/src/views/QuoteDetailView.tsx +7 -3
  63. package/src/views/QuoteListView.tsx +26 -12
@@ -1,9 +1,13 @@
1
1
  import React, { useEffect, useState } from 'react'
2
- import { Phone, Mail, Users, FileText, CheckSquare, MessageCircle, Check, Clock, Filter, Calendar } from 'lucide-react'
3
- import { useCrmStore } from '../CrmContext'
2
+ import {
3
+ Phone, Mail, Users, FileText, CheckSquare, MessageCircle, Check, Clock, Filter, Calendar,
4
+ UserPlus, ArrowRightLeft, Target, MoveRight, Trophy, XCircle, FilePlus2, Send, FileCheck2, FileX2, Sparkles, ArrowRight, Archive,
5
+ } from 'lucide-react'
6
+ import { useCrmStore, useCrmProvider } from '../CrmContext'
4
7
  import { useTranslation } from '@fayz-ai/core'
5
8
  import { SubpageHeader } from '@fayz-ai/ui'
6
- import type { ActivityType } from '../types'
9
+ import { DealSidebar } from './DealSidebar'
10
+ import { SYSTEM_ACTIVITY_TYPES, type ActivityType } from '../types'
7
11
 
8
12
  const TYPE_CONFIG: Record<string, { icon: React.ElementType; color: string; label: string }> = {
9
13
  call: { icon: Phone, color: 'bg-info/15 text-info dark:bg-info/20', label: 'Call' },
@@ -12,8 +16,25 @@ const TYPE_CONFIG: Record<string, { icon: React.ElementType; color: string; labe
12
16
  note: { icon: FileText, color: 'bg-warning/15 text-warning dark:bg-warning/20', label: 'Note' },
13
17
  task: { icon: CheckSquare, color: 'bg-pink-100 text-pink-600 dark:bg-pink-500/20 dark:text-pink-400', label: 'Task' },
14
18
  whatsapp: { icon: MessageCircle, color: 'bg-success/15 text-success dark:bg-success/20', label: 'WhatsApp' },
19
+ // System timeline events (auto-logged by the data provider on every CRM write)
20
+ lead_created: { icon: UserPlus, color: 'bg-info/15 text-info dark:bg-info/20', label: 'Lead created' },
21
+ lead_converted: { icon: ArrowRightLeft, color: 'bg-magic/15 text-magic dark:bg-magic/20', label: 'Lead converted' },
22
+ deal_created: { icon: Target, color: 'bg-info/15 text-info dark:bg-info/20', label: 'Deal created' },
23
+ stage_changed: { icon: MoveRight, color: 'bg-warning/15 text-warning dark:bg-warning/20', label: 'Stage changed' },
24
+ deal_won: { icon: Trophy, color: 'bg-success/15 text-success dark:bg-success/20', label: 'Deal won' },
25
+ deal_lost: { icon: XCircle, color: 'bg-destructive/15 text-destructive dark:bg-destructive/20', label: 'Deal lost' },
26
+ quote_created: { icon: FilePlus2, color: 'bg-info/15 text-info dark:bg-info/20', label: 'Quote created' },
27
+ quote_sent: { icon: Send, color: 'bg-magic/15 text-magic dark:bg-magic/20', label: 'Quote sent' },
28
+ quote_approved: { icon: FileCheck2, color: 'bg-success/15 text-success dark:bg-success/20', label: 'Quote approved' },
29
+ quote_rejected: { icon: FileX2, color: 'bg-destructive/15 text-destructive dark:bg-destructive/20', label: 'Quote rejected' },
30
+ lead_archived: { icon: Archive, color: 'bg-muted text-muted-foreground', label: 'Lead archived' },
31
+ deal_archived: { icon: Archive, color: 'bg-muted text-muted-foreground', label: 'Deal archived' },
32
+ quote_archived: { icon: Archive, color: 'bg-muted text-muted-foreground', label: 'Quote archived' },
15
33
  }
16
34
 
35
+ const SYSTEM_TYPES = new Set<string>(SYSTEM_ACTIVITY_TYPES)
36
+ const MANUAL_TYPES = Object.keys(TYPE_CONFIG).filter((t) => !SYSTEM_TYPES.has(t))
37
+
17
38
  function ActivitySkeleton() {
18
39
  return (
19
40
  <div className="space-y-2">
@@ -35,22 +56,34 @@ function ActivitySkeleton() {
35
56
  )
36
57
  }
37
58
 
38
- export function ActivityListView() {
59
+ export function ActivityListView({ onOpenLead, onOpenQuote }: {
60
+ /** Navigate to the lead detail (fallback for activities with no deal behind them). */
61
+ onOpenLead?: (leadId: string) => void
62
+ /** Navigate to a quote detail (links inside the deal sidebar). */
63
+ onOpenQuote?: (quoteId: string) => void
64
+ } = {}) {
39
65
  const t = useTranslation()
66
+ const provider = useCrmProvider()
67
+ // Clicking a row opens the SAME right-hand deal sheet the pipeline uses —
68
+ // the timeline stays underneath instead of navigating away.
69
+ const [selectedDealId, setSelectedDealId] = useState<string | null>(null)
40
70
  const activities = useCrmStore((s) => s.activities)
41
71
  const activitiesLoading = useCrmStore((s) => s.activitiesLoading)
42
72
  const fetchActivities = useCrmStore((s) => s.fetchActivities)
43
73
  const [typeFilter, setTypeFilter] = useState<string | null>(null)
44
74
  const [showCompleted, setShowCompleted] = useState<boolean | null>(null)
75
+ // 'events' = all system timeline types (filtered client-side; the provider
76
+ // query takes a single activityType).
77
+ const eventsFilter = typeFilter === 'events'
45
78
 
46
79
  useEffect(() => {
47
80
  fetchActivities({
48
- activityType: typeFilter as ActivityType | undefined,
49
- completed: showCompleted ?? undefined,
81
+ activityType: eventsFilter ? undefined : (typeFilter as ActivityType | undefined),
82
+ completed: eventsFilter ? undefined : (showCompleted ?? undefined),
50
83
  })
51
84
  }, [typeFilter, showCompleted])
52
85
 
53
- const filtered = activities
86
+ const filtered = eventsFilter ? activities.filter((a) => SYSTEM_TYPES.has(a.activityType)) : activities
54
87
 
55
88
  // Stats
56
89
  const pendingCount = activities.filter((a) => !a.completedAt).length
@@ -73,7 +106,8 @@ export function ActivityListView() {
73
106
  >
74
107
  {t('crm.activities.all')}
75
108
  </button>
76
- {Object.entries(TYPE_CONFIG).map(([type, cfg]) => {
109
+ {MANUAL_TYPES.map((type) => {
110
+ const cfg = TYPE_CONFIG[type]
77
111
  const Icon = cfg.icon
78
112
  return (
79
113
  <button
@@ -86,6 +120,13 @@ export function ActivityListView() {
86
120
  </button>
87
121
  )
88
122
  })}
123
+ <button
124
+ onClick={() => setTypeFilter(eventsFilter ? null : 'events')}
125
+ className={`inline-flex items-center gap-1 rounded-full px-2.5 py-1 text-xs font-medium transition-colors ${eventsFilter ? 'bg-primary/15 text-primary' : 'bg-muted/50 text-muted-foreground hover:bg-muted'}`}
126
+ >
127
+ <Sparkles className="h-3 w-3" />
128
+ {t('crm.activities.systemEvents')}
129
+ </button>
89
130
  </div>
90
131
 
91
132
  <div className="flex-1" />
@@ -123,12 +164,27 @@ export function ActivityListView() {
123
164
  {filtered.map((a) => {
124
165
  const cfg = TYPE_CONFIG[a.activityType] ?? { icon: FileText, color: 'bg-muted text-muted-foreground', label: a.activityType }
125
166
  const Icon = cfg.icon
167
+ const isSystem = SYSTEM_TYPES.has(a.activityType)
126
168
  const isOverdue = !a.completedAt && a.dueDate && a.dueDate < new Date().toISOString().slice(0, 10)
169
+ const openRecord = a.dealId
170
+ ? () => setSelectedDealId(a.dealId!)
171
+ : a.leadId
172
+ ? () => {
173
+ // Lead-only activity: its deal (auto-created with the lead)
174
+ // opens in the sheet; a dealless lead falls back to detail.
175
+ void provider.getDealByLeadId(a.leadId!).then((deal) => {
176
+ if (deal) setSelectedDealId(deal.id)
177
+ else onOpenLead?.(a.leadId!)
178
+ })
179
+ }
180
+ : undefined
127
181
 
128
182
  return (
129
183
  <div
130
184
  key={a.id}
131
- className={`flex items-start gap-3 rounded-lg border bg-card p-4 transition-colors ${isOverdue ? 'border-destructive/30 dark:border-destructive/20' : ''}`}
185
+ onClick={openRecord}
186
+ role={openRecord ? 'button' : undefined}
187
+ className={`group flex items-start gap-3 rounded-lg border bg-card p-4 transition-colors ${isOverdue ? 'border-destructive/30 dark:border-destructive/20' : ''} ${openRecord ? 'cursor-pointer hover:bg-muted/30' : ''}`}
132
188
  >
133
189
  <div className={`flex h-9 w-9 items-center justify-center rounded-lg shrink-0 ${cfg.color}`}>
134
190
  <Icon className="h-4 w-4" />
@@ -136,7 +192,8 @@ export function ActivityListView() {
136
192
  <div className="flex-1 min-w-0 pt-0.5">
137
193
  <div className="flex items-center gap-2">
138
194
  <p className="text-sm font-medium truncate">{a.title}</p>
139
- {a.completedAt && (
195
+ {/* System events are facts, not tasks — no "done" badge noise. */}
196
+ {a.completedAt && !isSystem && (
140
197
  <span className="inline-flex items-center gap-0.5 rounded-full bg-success/15 px-1.5 py-0.5 text-[9px] font-medium text-success dark:bg-success/20">
141
198
  <Check className="h-2.5 w-2.5" /> {t('crm.activities.done')}
142
199
  </span>
@@ -170,14 +227,25 @@ export function ActivityListView() {
170
227
  )}
171
228
  </div>
172
229
  </div>
173
- <span className="text-[10px] text-muted-foreground shrink-0 pt-1">
230
+ <span className="flex items-center gap-1.5 text-[10px] text-muted-foreground shrink-0 pt-1">
174
231
  {new Date(a.createdAt).toLocaleDateString()}
232
+ {openRecord && (
233
+ <ArrowRight className="h-3 w-3 opacity-0 transition-opacity group-hover:opacity-60" />
234
+ )}
175
235
  </span>
176
236
  </div>
177
237
  )
178
238
  })}
179
239
  </div>
180
240
  )}
241
+
242
+ <DealSidebar
243
+ dealId={selectedDealId ?? ''}
244
+ open={!!selectedDealId}
245
+ onClose={() => setSelectedDealId(null)}
246
+ onViewLead={onOpenLead}
247
+ onViewQuote={onOpenQuote}
248
+ />
181
249
  </div>
182
250
  )
183
251
  }
@@ -1,7 +1,7 @@
1
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'
2
+ import { Phone, Mail, Users, FileText, CheckSquare, MessageCircle, Check, Calendar, DollarSign, User, Target, Clock, ArrowRight, UserPlus, ArrowRightLeft, MoveRight, Trophy, XCircle, FilePlus2, Send, FileCheck2, FileX2, Archive } from 'lucide-react'
3
3
  import { useTranslation } from '@fayz-ai/core'
4
- import { Sheet, SheetContent, SheetHeader, SheetBody, SheetFooter, SheetTitle, SheetDescription } from '@fayz-ai/ui'
4
+ import { Sheet, SheetContent, SheetHeader, SheetBody, SheetFooter, SheetTitle, SheetDescription, SegmentedControl } from '@fayz-ai/ui'
5
5
  import { PersonLink } from '@fayz-ai/saas'
6
6
  import { useCrmProvider, useCrmConfig, formatCurrency } from '../CrmContext'
7
7
  import { QuoteStatusDropdown } from '../components/QuoteStatusDropdown'
@@ -14,6 +14,11 @@ import type { Deal, Lead, Quote, Activity } from '../types'
14
14
 
15
15
  const TYPE_ICONS: Record<string, React.ElementType> = {
16
16
  call: Phone, email: Mail, meeting: Users, note: FileText, task: CheckSquare, whatsapp: MessageCircle,
17
+ // System timeline events (auto-logged by the provider)
18
+ lead_created: UserPlus, lead_converted: ArrowRightLeft, deal_created: Target,
19
+ stage_changed: MoveRight, deal_won: Trophy, deal_lost: XCircle,
20
+ quote_created: FilePlus2, quote_sent: Send, quote_approved: FileCheck2, quote_rejected: FileX2,
21
+ lead_archived: Archive, deal_archived: Archive, quote_archived: Archive,
17
22
  }
18
23
 
19
24
 
@@ -25,34 +30,31 @@ type TabId = 'overview' | 'activity' | 'quotes'
25
30
 
26
31
  function TabBar({ active, counts, onChange }: { active: TabId; counts: { activity: number; quotes: number }; onChange: (id: TabId) => void }) {
27
32
  const t = useTranslation()
28
- const tabs: { id: TabId; label: string; count?: number }[] = [
29
- { id: 'overview', label: t('crm.dealSidebar.overview') },
30
- { id: 'activity', label: t('crm.dealSidebar.activity'), count: counts.activity },
31
- { id: 'quotes', label: t('crm.dealSidebar.quotes'), count: counts.quotes },
32
- ]
33
+
34
+ const label = (text: string, count: number | undefined, isActive: boolean) => (
35
+ <span className="inline-flex items-center gap-1.5">
36
+ {text}
37
+ {count != null && count > 0 && (
38
+ <span className={`inline-flex items-center justify-center min-w-[16px] h-4 rounded-full px-1 text-[10px] font-semibold ${
39
+ isActive ? 'bg-background/20 text-background' : 'bg-muted text-muted-foreground'
40
+ }`}>
41
+ {count}
42
+ </span>
43
+ )}
44
+ </span>
45
+ )
33
46
 
34
47
  return (
35
- <div className="flex items-center gap-0.5 px-5 py-2 border-b shrink-0 bg-muted/20">
36
- {tabs.map((tab) => (
37
- <button
38
- key={tab.id}
39
- onClick={() => onChange(tab.id)}
40
- className={`relative px-3 py-1.5 text-xs font-medium rounded-md transition-colors ${
41
- active === tab.id
42
- ? 'bg-card text-foreground shadow-sm'
43
- : 'text-muted-foreground hover:text-foreground'
44
- }`}
45
- >
46
- {tab.label}
47
- {tab.count != null && tab.count > 0 && (
48
- <span className={`ml-1 inline-flex items-center justify-center min-w-[16px] h-4 rounded-full px-1 text-[10px] font-semibold ${
49
- active === tab.id ? 'bg-primary/10 text-primary' : 'bg-muted text-muted-foreground'
50
- }`}>
51
- {tab.count}
52
- </span>
53
- )}
54
- </button>
55
- ))}
48
+ <div className="px-5 py-2 border-b shrink-0">
49
+ <SegmentedControl<TabId>
50
+ value={active}
51
+ onChange={onChange}
52
+ options={[
53
+ { value: 'overview', label: label(t('crm.dealSidebar.overview'), undefined, active === 'overview') },
54
+ { value: 'activity', label: label(t('crm.dealSidebar.activity'), counts.activity, active === 'activity') },
55
+ { value: 'quotes', label: label(t('crm.dealSidebar.quotes'), counts.quotes, active === 'quotes') },
56
+ ]}
57
+ />
56
58
  </div>
57
59
  )
58
60
  }
@@ -194,11 +196,6 @@ function OverviewTab({ deal, lead, currency }: { deal: Deal; lead: Lead | null;
194
196
  </div>
195
197
  )}
196
198
 
197
- {/* Dates */}
198
- <div className="flex items-center gap-4 pt-3 border-t text-[10px] text-muted-foreground">
199
- <span>{t('crm.dealSidebar.created')} {deal.createdAt?.slice(0, 10)}</span>
200
- {deal.updatedAt && <span>{t('crm.dealSidebar.updated')} {deal.updatedAt?.slice(0, 10)}</span>}
201
- </div>
202
199
  </div>
203
200
  )
204
201
  }
@@ -428,6 +425,15 @@ export function DealSidebar({ dealId, open, onClose, onViewLead, onViewQuote }:
428
425
  )}
429
426
  </SheetBody>
430
427
 
428
+ {/* Timestamps pinned to the sheet foot — same pattern as the assistant
429
+ notes detail (border-t hairline, tiny muted line, `·` separator). */}
430
+ {deal && (
431
+ <p className="shrink-0 border-t border-border/40 px-5 py-1.5 text-[10px] text-muted-foreground/60">
432
+ {t('crm.dealSidebar.created')} {deal.createdAt?.slice(0, 10)}
433
+ {deal.updatedAt ? ` · ${t('crm.dealSidebar.updated')} ${deal.updatedAt.slice(0, 10)}` : ''}
434
+ </p>
435
+ )}
436
+
431
437
  {/* Footer */}
432
438
  {deal && lead && onViewLead && (
433
439
  <SheetFooter>
@@ -1,6 +1,7 @@
1
1
  import React, { useEffect, useState } from 'react'
2
- import { UserPlus, FileText } from 'lucide-react'
3
- import { useCrmProvider, useCrmConfig, formatCurrency } from '../CrmContext'
2
+ import { UserPlus, FileText, Archive } from 'lucide-react'
3
+ import { useCrmProvider, useCrmConfig, formatCurrency, useCrmStore } from '../CrmContext'
4
+ import { useCanArchive } from '../lib/access'
4
5
  import { useTranslation } from '@fayz-ai/core'
5
6
  import { QuoteStatusDropdown } from '../components/QuoteStatusDropdown'
6
7
  import { Breadcrumb } from '@fayz-ai/ui'
@@ -49,6 +50,9 @@ export function LeadDetailView({ leadId, onBack, onCreateQuote, onViewQuote }: {
49
50
  const provider = useCrmProvider()
50
51
  const { currency } = useCrmConfig()
51
52
  const t = useTranslation()
53
+ const archiveLead = useCrmStore((s) => s.archiveLead)
54
+ const canArchive = useCanArchive()
55
+ const [archiving, setArchiving] = useState(false)
52
56
  const [lead, setLead] = useState<Lead | null>(null)
53
57
  const [deal, setDeal] = useState<Deal | null>(null)
54
58
  const [quotes, setQuotes] = useState<Quote[]>([])
@@ -140,14 +144,29 @@ export function LeadDetailView({ leadId, onBack, onCreateQuote, onViewQuote }: {
140
144
  </div>
141
145
  </div>
142
146
  {/* Actions */}
143
- {onCreateQuote && lead.status !== 'converted' && lead.status !== 'lost' && (
144
- <button
145
- onClick={() => onCreateQuote(leadId)}
146
- 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"
147
- >
148
- <FileText className="h-3 w-3" /> {t('crm.leadDetail.createQuote')}
149
- </button>
150
- )}
147
+ <div className="flex items-center gap-2 shrink-0">
148
+ {canArchive && (
149
+ <button
150
+ onClick={() => {
151
+ setArchiving(true)
152
+ archiveLead(leadId).then(onBack).catch(() => setArchiving(false))
153
+ }}
154
+ disabled={archiving}
155
+ title={t('crm.leadDetail.archive')}
156
+ className="inline-flex items-center gap-1.5 rounded-lg border px-3 py-1.5 text-xs font-medium text-muted-foreground hover:bg-muted hover:text-foreground bg-card shadow-button active:shadow-button-inset transition-colors disabled:opacity-60"
157
+ >
158
+ <Archive className="h-3 w-3" /> {t('crm.leadDetail.archive')}
159
+ </button>
160
+ )}
161
+ {onCreateQuote && lead.status !== 'converted' && lead.status !== 'lost' && (
162
+ <button
163
+ onClick={() => onCreateQuote(leadId)}
164
+ 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"
165
+ >
166
+ <FileText className="h-3 w-3" /> {t('crm.leadDetail.createQuote')}
167
+ </button>
168
+ )}
169
+ </div>
151
170
  </div>
152
171
 
153
172
  {/* Separator */}
@@ -1,7 +1,8 @@
1
1
  import React, { useEffect, useRef, useState } from 'react'
2
- import { Loader2, Plus } from 'lucide-react'
2
+ import { Archive, Loader2, Plus } from 'lucide-react'
3
3
  import { toast } from '@fayz-ai/ui'
4
4
  import { useCrmStore, useCrmConfig, formatCurrency } from '../CrmContext'
5
+ import { useCanArchive } from '../lib/access'
5
6
  import { useTranslation, getSupabaseClientOptional } from '@fayz-ai/core'
6
7
  import { SubpageHeader } from '@fayz-ai/ui'
7
8
  import { DealSidebar } from './DealSidebar'
@@ -43,25 +44,37 @@ function PipelineSkeleton() {
43
44
  // Deal card
44
45
  // ---------------------------------------------------------------------------
45
46
 
46
- function DealCard({ deal, saving, currency, onClick, onDragStart, onDragEnd }: {
47
+ function DealCard({ deal, saving, currency, onClick, onArchive, onDragStart, onDragEnd }: {
47
48
  deal: Deal
48
49
  saving?: boolean
49
50
  currency: { code: string; locale: string; symbol: string }
50
51
  onClick?: () => void
52
+ /** Present only when the archive permission resolved true (useCanArchive). */
53
+ onArchive?: () => void
51
54
  onDragStart: (e: React.DragEvent) => void
52
55
  onDragEnd: (e: React.DragEvent) => void
53
56
  }) {
57
+ const t = useTranslation()
54
58
  return (
55
59
  <div
56
60
  draggable={!saving}
57
61
  onDragStart={onDragStart}
58
62
  onDragEnd={onDragEnd}
59
63
  onClick={onClick}
60
- className={`rounded-lg border bg-card p-3 transition-shadow ${saving ? 'opacity-70' : 'hover:shadow-sm cursor-grab active:cursor-grabbing active:opacity-70'}`}
64
+ className={`group rounded-lg border bg-card p-3 transition-shadow ${saving ? 'opacity-70' : 'hover:shadow-sm cursor-grab active:cursor-grabbing active:opacity-70'}`}
61
65
  >
62
66
  <div className="flex items-center justify-between gap-2">
63
67
  <p className="text-sm font-medium truncate">{deal.title}</p>
64
68
  {saving && <Loader2 className="h-3 w-3 animate-spin text-muted-foreground shrink-0" />}
69
+ {!saving && onArchive && (
70
+ <button
71
+ onClick={(e) => { e.stopPropagation(); onArchive() }}
72
+ title={t('crm.pipeline.archive')}
73
+ className="flex h-5 w-5 shrink-0 items-center justify-center rounded text-muted-foreground opacity-0 transition-opacity hover:bg-muted hover:text-foreground group-hover:opacity-100"
74
+ >
75
+ <Archive className="h-3 w-3" />
76
+ </button>
77
+ )}
65
78
  </div>
66
79
  {deal.contactName && <p className="text-[10px] text-muted-foreground mt-0.5">{deal.contactName}</p>}
67
80
  <div className="flex items-center justify-between mt-2">
@@ -76,7 +89,7 @@ function DealCard({ deal, saving, currency, onClick, onDragStart, onDragEnd }: {
76
89
  // Stage column
77
90
  // ---------------------------------------------------------------------------
78
91
 
79
- function StageColumn({ stageId, stageName, stageColor, deals, savingDealIds, currency, dragOverStageId, onDealClick, onAddLead, onDragStart, onDragEnd, onDragOver, onDragLeave, onDrop }: {
92
+ function StageColumn({ stageId, stageName, stageColor, deals, savingDealIds, currency, dragOverStageId, onDealClick, onArchiveDeal, onAddLead, onDragStart, onDragEnd, onDragOver, onDragLeave, onDrop }: {
80
93
  stageId: string
81
94
  stageName: string
82
95
  stageColor: string
@@ -85,6 +98,7 @@ function StageColumn({ stageId, stageName, stageColor, deals, savingDealIds, cur
85
98
  currency: { code: string; locale: string; symbol: string }
86
99
  dragOverStageId: string | null
87
100
  onDealClick?: (id: string) => void
101
+ onArchiveDeal?: (id: string) => void
88
102
  onAddLead?: () => void
89
103
  onDragStart: (dealId: string, fromStageId: string) => void
90
104
  onDragEnd: () => void
@@ -135,6 +149,7 @@ function StageColumn({ stageId, stageName, stageColor, deals, savingDealIds, cur
135
149
  saving={savingDealIds.has(deal.id)}
136
150
  currency={currency}
137
151
  onClick={() => onDealClick?.(deal.id)}
152
+ onArchive={onArchiveDeal ? () => onArchiveDeal(deal.id) : undefined}
138
153
  onDragStart={(e) => {
139
154
  e.dataTransfer.effectAllowed = 'move'
140
155
  onDragStart(deal.id, stageId)
@@ -166,6 +181,8 @@ export function PipelineView({ onDealClick, onViewLead, onViewQuote, onAddLead }
166
181
  const fetchPipelines = useCrmStore((s) => s.fetchPipelines)
167
182
  const fetchDealsByStage = useCrmStore((s) => s.fetchDealsByStage)
168
183
  const moveDealToStage = useCrmStore((s) => s.moveDealToStage)
184
+ const archiveDeal = useCrmStore((s) => s.archiveDeal)
185
+ const canArchive = useCanArchive()
169
186
 
170
187
  const [dragOverStageId, setDragOverStageId] = useState<string | null>(null)
171
188
  const [savingDealIds, setSavingDealIds] = useState<Set<string>>(new Set())
@@ -308,6 +325,7 @@ export function PipelineView({ onDealClick, onViewLead, onViewQuote, onAddLead }
308
325
  currency={currency}
309
326
  dragOverStageId={dragOverStageId}
310
327
  onDealClick={handleDealClick}
328
+ onArchiveDeal={canArchive ? (id) => { void archiveDeal(id) } : undefined}
311
329
  onAddLead={onAddLead}
312
330
  onDragStart={handleDragStart}
313
331
  onDragEnd={handleDragEnd}
@@ -340,15 +340,19 @@ export function QuoteDetailView({ quoteId, onBack, onEdit, onInvoiceCreated }: {
340
340
  )}
341
341
  </div>
342
342
 
343
- {/* Invoice link — when approved (quote IS the invoice, same row) */}
344
- {quote.status === 'approved' && (
343
+ {/* Invoice link — the promotion is IN PLACE (fn_invoice_from_order), so
344
+ after it the display status is the financial stage, never 'approved';
345
+ gating on approved alone hid this banner exactly when it mattered.
346
+ Financial has no per-invoice detail route — link to the receivables
347
+ list, where the REC-* reference lands. */}
348
+ {['approved', 'invoiced', 'paid', 'partial', 'overdue'].includes(quote.status) && (
345
349
  <div className="flex items-center justify-between rounded-lg border border-success/30 bg-success/5 px-4 py-2.5">
346
350
  <div className="flex items-center gap-2 text-xs">
347
351
  <CircleCheckBig className="h-3.5 w-3.5 text-success" />
348
352
  <span className="text-muted-foreground">{t('crm.quoteDetail.invoiceCreated')}</span>
349
353
  </div>
350
354
  <a
351
- href={`#/financial/receivables/detail/${quote.id}`}
355
+ href="#/financial/receivables/list"
352
356
  className="inline-flex items-center gap-1 text-xs font-medium text-primary hover:underline"
353
357
  >
354
358
  {t('crm.quoteDetail.viewInvoice')} <ArrowRight className="h-3 w-3" />
@@ -1,10 +1,11 @@
1
1
  import React, { useEffect } from 'react'
2
- import { FileText, Pencil } from 'lucide-react'
2
+ import { FileText, Pencil, Archive } from 'lucide-react'
3
3
  import type { ColumnDef } from '@tanstack/react-table'
4
4
  import { DataTable } from '@fayz-ai/ui'
5
5
  import { useCrmStore, useCrmConfig, formatCurrency } from '../CrmContext'
6
6
  import { useTranslation } from '@fayz-ai/core'
7
7
  import { SubpageHeader } from '@fayz-ai/ui'
8
+ import { useCanArchive } from '../lib/access'
8
9
  import type { Quote } from '../types'
9
10
 
10
11
  const STATUS_COLORS: Record<string, string> = {
@@ -20,7 +21,7 @@ const STATUS_COLORS: Record<string, string> = {
20
21
  cancelled: 'bg-muted text-muted-foreground',
21
22
  }
22
23
 
23
- function useQuoteColumns(currency: { code: string; locale: string; symbol: string }, onEditQuote?: (id: string) => void): ColumnDef<Quote, any>[] {
24
+ function useQuoteColumns(currency: { code: string; locale: string; symbol: string }, onEditQuote?: (id: string) => void, onArchive?: (id: string) => void): ColumnDef<Quote, any>[] {
24
25
  const t = useTranslation()
25
26
  const cols: ColumnDef<Quote, any>[] = [
26
27
  { accessorKey: 'quoteNumber', header: '#', cell: ({ getValue }) => <span className="text-xs text-muted-foreground">{getValue() as string}</span> },
@@ -39,18 +40,29 @@ function useQuoteColumns(currency: { code: string; locale: string; symbol: strin
39
40
  },
40
41
  ]
41
42
 
42
- if (onEditQuote) {
43
+ if (onEditQuote || onArchive) {
43
44
  cols.push({
44
45
  id: 'actions', header: '',
45
46
  cell: ({ row }) => (
46
- <div className="text-right">
47
- <button
48
- onClick={(e) => { e.stopPropagation(); onEditQuote(row.original.id) }}
49
- className="inline-flex h-7 w-7 items-center justify-center rounded-md text-muted-foreground hover:bg-muted hover:text-foreground transition-colors"
50
- title="Edit quote"
51
- >
52
- <Pencil className="h-3.5 w-3.5" />
53
- </button>
47
+ <div className="flex items-center justify-end gap-0.5">
48
+ {onEditQuote && (
49
+ <button
50
+ onClick={(e) => { e.stopPropagation(); onEditQuote(row.original.id) }}
51
+ className="inline-flex h-7 w-7 items-center justify-center rounded-md text-muted-foreground hover:bg-muted hover:text-foreground transition-colors"
52
+ title="Edit quote"
53
+ >
54
+ <Pencil className="h-3.5 w-3.5" />
55
+ </button>
56
+ )}
57
+ {onArchive && (
58
+ <button
59
+ onClick={(e) => { e.stopPropagation(); onArchive(row.original.id) }}
60
+ className="inline-flex h-7 w-7 items-center justify-center rounded-md text-muted-foreground hover:bg-muted hover:text-foreground transition-colors"
61
+ title={t('crm.quotes.archive')}
62
+ >
63
+ <Archive className="h-3.5 w-3.5" />
64
+ </button>
65
+ )}
54
66
  </div>
55
67
  ),
56
68
  })
@@ -65,10 +77,12 @@ export function QuoteListView({ onNew, onEdit, onEditQuote }: { onNew?: () => vo
65
77
  const quotes = useCrmStore((s) => s.quotes)
66
78
  const quotesLoading = useCrmStore((s) => s.quotesLoading)
67
79
  const fetchQuotes = useCrmStore((s) => s.fetchQuotes)
80
+ const archiveQuote = useCrmStore((s) => s.archiveQuote)
81
+ const canArchive = useCanArchive()
68
82
 
69
83
  useEffect(() => { fetchQuotes({}) }, [])
70
84
 
71
- const columns = useQuoteColumns(currency, onEditQuote)
85
+ const columns = useQuoteColumns(currency, onEditQuote, canArchive ? (id) => { void archiveQuote(id) } : undefined)
72
86
 
73
87
  return (
74
88
  <div className="space-y-4">