@fayz-ai/plugin-crm 0.8.0 → 0.8.3

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 (57) hide show
  1. package/dist/components/CrmGeneralSettings.d.ts.map +1 -1
  2. package/dist/data/supabase.d.ts.map +1 -1
  3. package/dist/index.cjs +168 -33
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +169 -34
  7. package/dist/index.js.map +1 -1
  8. package/dist/lib/form-answers.d.ts +10 -0
  9. package/dist/lib/form-answers.d.ts.map +1 -0
  10. package/dist/locales/en.d.ts.map +1 -1
  11. package/dist/locales/pt-BR.d.ts.map +1 -1
  12. package/dist/migrations/index.d.ts +2 -0
  13. package/dist/migrations/index.d.ts.map +1 -1
  14. package/dist/public/context.d.ts +23 -0
  15. package/dist/public/context.d.ts.map +1 -0
  16. package/dist/public/createPublicFormsPlugin.d.ts +38 -0
  17. package/dist/public/createPublicFormsPlugin.d.ts.map +1 -0
  18. package/dist/public/data.supabase.d.ts +7 -0
  19. package/dist/public/data.supabase.d.ts.map +1 -0
  20. package/dist/public/inbox.d.ts +22 -0
  21. package/dist/public/inbox.d.ts.map +1 -0
  22. package/dist/public/index.cjs +306 -0
  23. package/dist/public/index.cjs.map +1 -0
  24. package/dist/public/index.d.ts +10 -0
  25. package/dist/public/index.d.ts.map +1 -0
  26. package/dist/public/index.js +298 -0
  27. package/dist/public/index.js.map +1 -0
  28. package/dist/public/types.d.ts +92 -0
  29. package/dist/public/types.d.ts.map +1 -0
  30. package/dist/views/DealSidebar.d.ts.map +1 -1
  31. package/dist/views/LeadDetailView.d.ts.map +1 -1
  32. package/dist/views/LeadFormView.d.ts.map +1 -1
  33. package/dist/views/LeadListView.d.ts.map +1 -1
  34. package/dist/views/PipelineView.d.ts.map +1 -1
  35. package/dist/views/QuoteFormView.d.ts.map +1 -1
  36. package/package.json +11 -5
  37. package/src/components/CrmGeneralSettings.tsx +35 -15
  38. package/src/data/supabase.ts +47 -4
  39. package/src/index.ts +9 -0
  40. package/src/lib/form-answers.tsx +39 -0
  41. package/src/locales/en.ts +1 -0
  42. package/src/locales/pt-BR.ts +1 -0
  43. package/src/migrations/005_public_lead.sql +189 -0
  44. package/src/migrations/006_lead_enters_pipeline.sql +265 -0
  45. package/src/migrations/index.ts +461 -1
  46. package/src/public/context.tsx +69 -0
  47. package/src/public/createPublicFormsPlugin.ts +188 -0
  48. package/src/public/data.supabase.ts +86 -0
  49. package/src/public/inbox.ts +136 -0
  50. package/src/public/index.ts +30 -0
  51. package/src/public/types.ts +113 -0
  52. package/src/views/DealSidebar.tsx +31 -0
  53. package/src/views/LeadDetailView.tsx +53 -0
  54. package/src/views/LeadFormView.tsx +10 -0
  55. package/src/views/LeadListView.tsx +19 -0
  56. package/src/views/PipelineView.tsx +34 -1
  57. package/src/views/QuoteFormView.tsx +5 -0
@@ -1,11 +1,15 @@
1
1
  import React, { useState, useEffect } from 'react'
2
2
  import { useCrmStore } from '../CrmContext'
3
3
  import { useTranslation } from '@fayz-ai/core'
4
+ import { useLimitGuard, invalidateLimit } from '@fayz-ai/saas'
4
5
  import { SubpageHeader, useSaveBar, toast } from '@fayz-ai/ui'
5
6
 
6
7
  export function LeadFormView({ onSaved }: { onSaved?: (id?: string) => void }) {
7
8
  const t = useTranslation()
8
9
  const createLead = useCrmStore((s) => s.createLead)
10
+ // Creating a lead also auto-creates a deal (store.createLead), so this path is
11
+ // the create surface for both the pipeline board ("+ Add") and the lead form.
12
+ const guardLeads = useLimitGuard('leads')
9
13
  const fetchPipelines = useCrmStore((s) => s.fetchPipelines)
10
14
  const pipelines = useCrmStore((s) => s.pipelines)
11
15
 
@@ -21,9 +25,15 @@ export function LeadFormView({ onSaved }: { onSaved?: (id?: string) => void }) {
21
25
 
22
26
  async function handleSave() {
23
27
  if (!name.trim()) { toast.error(t('common.formIncomplete')); return }
28
+ // Plan quantity guard (client-side, before the store call): opens the global
29
+ // UpgradeModal and aborts when the lead cap is reached.
30
+ if ((await guardLeads()) === 'blocked') return
24
31
  setSaving(true)
25
32
  try {
26
33
  const lead = await createLead({ name, email: email || undefined, phone: phone || undefined, company: company || undefined, notes: notes || undefined })
34
+ // A lead spawns a deal too — refresh both live counts.
35
+ invalidateLimit('leads')
36
+ invalidateLimit('deals')
27
37
  onSaved?.(lead.id)
28
38
  } finally { setSaving(false) }
29
39
  }
@@ -37,6 +37,25 @@ const columns: ColumnDef<Lead, any>[] = [
37
37
  )
38
38
  },
39
39
  },
40
+ {
41
+ // When the lead came in. The list is already sorted by it (provider orders
42
+ // created_at desc) but never showed it, so "is this enquiry from today or
43
+ // from March?" was unanswerable without opening the record.
44
+ accessorKey: 'createdAt', header: 'Entrada',
45
+ cell: ({ getValue }) => {
46
+ const raw = getValue() as string | undefined
47
+ if (!raw) return <span className="text-muted-foreground text-xs">—</span>
48
+ const at = new Date(raw)
49
+ if (Number.isNaN(at.getTime())) return <span className="text-muted-foreground text-xs">—</span>
50
+ return (
51
+ <span className="text-muted-foreground text-xs whitespace-nowrap" title={at.toLocaleString()}>
52
+ {at.toLocaleDateString(undefined, { day: '2-digit', month: '2-digit', year: '2-digit' })}
53
+ {' · '}
54
+ {at.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' })}
55
+ </span>
56
+ )
57
+ },
58
+ },
40
59
  ]
41
60
 
42
61
  export function LeadListView({ onNew, onEdit }: { onNew?: () => void; onEdit?: (id: string) => void }) {
@@ -2,7 +2,7 @@ import React, { useEffect, useRef, useState } from 'react'
2
2
  import { Loader2, Plus } from 'lucide-react'
3
3
  import { toast } from '@fayz-ai/ui'
4
4
  import { useCrmStore, useCrmConfig, formatCurrency } from '../CrmContext'
5
- import { useTranslation } from '@fayz-ai/core'
5
+ import { useTranslation, getSupabaseClientOptional } from '@fayz-ai/core'
6
6
  import { SubpageHeader } from '@fayz-ai/ui'
7
7
  import { DealSidebar } from './DealSidebar'
8
8
  import type { Deal } from '../types'
@@ -177,6 +177,39 @@ export function PipelineView({ onDealClick, onViewLead, onViewQuote, onAddLead }
177
177
  useEffect(() => { fetchPipelines() }, [])
178
178
  useEffect(() => { if (pipelines.length > 0) fetchDealsByStage(pipelines[0].id) }, [pipelines])
179
179
 
180
+ // Live board. The card for an incoming lead is created by a DB trigger
181
+ // (plugin-crm 006), so it appears with no client code involved at all — and
182
+ // without this subscription the only way to see it was a page reload. A
183
+ // pipeline left open on a screen all day is the normal case here, which is
184
+ // exactly when a stale board is worst.
185
+ //
186
+ // Subscribes to plg_crm_deal_extensions rather than orders: it is the row
187
+ // carrying pipeline_id/stage_id, so every board-visible change (card created,
188
+ // moved, removed) touches it and nothing else does. A refetch rather than a
189
+ // local patch — the payload alone lacks the joined stage/contact fields the
190
+ // board renders, and refetching keeps one source of truth for the board.
191
+ const pipelineId = pipelines[0]?.id
192
+ useEffect(() => {
193
+ if (!pipelineId) return
194
+ const supabase = getSupabaseClientOptional() as {
195
+ channel: (name: string) => { on: (...a: unknown[]) => { subscribe: () => unknown } }
196
+ removeChannel: (channel: unknown) => void
197
+ } | null
198
+ if (!supabase) return
199
+
200
+ // RLS scopes the stream to the caller's own tenants — no tenant filter here.
201
+ const channel = supabase
202
+ .channel(`crm:board:${pipelineId}`)
203
+ .on(
204
+ 'postgres_changes',
205
+ { event: '*', schema: 'public', table: 'plg_crm_deal_extensions' },
206
+ () => { void fetchDealsByStage(pipelineId) },
207
+ )
208
+ .subscribe()
209
+
210
+ return () => { supabase.removeChannel(channel) }
211
+ }, [pipelineId])
212
+
180
213
  // Clear optimistic state when real data arrives and no moves are in-flight
181
214
  useEffect(() => {
182
215
  if (savingDealIds.size === 0) setOptimisticBoard(null)
@@ -3,6 +3,7 @@ import { Plus, Trash2, X, Check, ChevronDown } from 'lucide-react'
3
3
  import { useCrmConfig, useCrmStore, useCrmProvider, formatCurrency } from '../CrmContext'
4
4
  import { useTranslation } from '@fayz-ai/core'
5
5
  import type { EntityLookupMap } from '@fayz-ai/saas'
6
+ import { useLimitGuard, invalidateLimit } from '@fayz-ai/saas'
6
7
  import { SubpageHeader, useSaveBar, toast } from '@fayz-ai/ui'
7
8
  import { SearchSelect, type SearchSelectOption } from '@fayz-ai/ui'
8
9
  import { CurrencyInput } from '@fayz-ai/ui'
@@ -195,6 +196,7 @@ export function QuoteFormView({ quoteId, leadId, onSaved }: { quoteId?: string;
195
196
  const { currency, itemTypes } = config
196
197
  const createQuote = useCrmStore((s) => s.createQuote)
197
198
  const updateQuote = useCrmStore((s) => s.updateQuote)
199
+ const guardQuotes = useLimitGuard('quotes')
198
200
 
199
201
  const isEdit = !!quoteId
200
202
  const [loadingExisting, setLoadingExisting] = useState(isEdit || !!leadId)
@@ -281,6 +283,8 @@ export function QuoteFormView({ quoteId, leadId, onSaved }: { quoteId?: string;
281
283
 
282
284
  async function handleSave() {
283
285
  if (items.length === 0) { toast.error(t('crm.quoteForm.errNoItems')); return }
286
+ // Plan quantity guard (client-side) — only a new quote consumes a slot.
287
+ if (!isEdit && (await guardQuotes()) === 'blocked') return
284
288
  setSaving(true)
285
289
  try {
286
290
  const input = {
@@ -306,6 +310,7 @@ export function QuoteFormView({ quoteId, leadId, onSaved }: { quoteId?: string;
306
310
  onSaved?.(quoteId)
307
311
  } else {
308
312
  const created = await createQuote(input)
313
+ invalidateLimit('quotes')
309
314
  onSaved?.(created?.id)
310
315
  }
311
316
  } finally { setSaving(false) }