@fayz-ai/plugin-marketing 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 (74) hide show
  1. package/dist/MarketingContext.d.ts +35 -0
  2. package/dist/MarketingContext.d.ts.map +1 -0
  3. package/dist/MarketingPage.d.ts +11 -0
  4. package/dist/MarketingPage.d.ts.map +1 -0
  5. package/dist/components/MarketingBits.d.ts +29 -0
  6. package/dist/components/MarketingBits.d.ts.map +1 -0
  7. package/dist/components/icons.d.ts +6 -0
  8. package/dist/components/icons.d.ts.map +1 -0
  9. package/dist/data/mock.d.ts +10 -0
  10. package/dist/data/mock.d.ts.map +1 -0
  11. package/dist/data/types.d.ts +32 -0
  12. package/dist/data/types.d.ts.map +1 -0
  13. package/dist/format.d.ts +17 -0
  14. package/dist/format.d.ts.map +1 -0
  15. package/dist/index.cjs +1354 -0
  16. package/dist/index.cjs.map +1 -0
  17. package/dist/index.d.ts +33 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +1346 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/locales/en.d.ts +2 -0
  22. package/dist/locales/en.d.ts.map +1 -0
  23. package/dist/locales/index.d.ts +2 -0
  24. package/dist/locales/index.d.ts.map +1 -0
  25. package/dist/locales/pt-BR.d.ts +2 -0
  26. package/dist/locales/pt-BR.d.ts.map +1 -0
  27. package/dist/presets.d.ts +20 -0
  28. package/dist/presets.d.ts.map +1 -0
  29. package/dist/store.d.ts +18 -0
  30. package/dist/store.d.ts.map +1 -0
  31. package/dist/types.d.ts +89 -0
  32. package/dist/types.d.ts.map +1 -0
  33. package/dist/views/CampaignComposer.d.ts +6 -0
  34. package/dist/views/CampaignComposer.d.ts.map +1 -0
  35. package/dist/views/CampaignsView.d.ts +3 -0
  36. package/dist/views/CampaignsView.d.ts.map +1 -0
  37. package/dist/views/ChannelDetailView.d.ts +6 -0
  38. package/dist/views/ChannelDetailView.d.ts.map +1 -0
  39. package/dist/views/ChannelsView.d.ts +5 -0
  40. package/dist/views/ChannelsView.d.ts.map +1 -0
  41. package/dist/views/FunnelView.d.ts +3 -0
  42. package/dist/views/FunnelView.d.ts.map +1 -0
  43. package/dist/views/LandingPagesView.d.ts +3 -0
  44. package/dist/views/LandingPagesView.d.ts.map +1 -0
  45. package/dist/views/OverviewView.d.ts +9 -0
  46. package/dist/views/OverviewView.d.ts.map +1 -0
  47. package/dist/views/SettingsView.d.ts +3 -0
  48. package/dist/views/SettingsView.d.ts.map +1 -0
  49. package/dist/views/dashboardWidgets.d.ts +11 -0
  50. package/dist/views/dashboardWidgets.d.ts.map +1 -0
  51. package/package.json +55 -0
  52. package/src/MarketingContext.tsx +39 -0
  53. package/src/MarketingPage.tsx +94 -0
  54. package/src/components/MarketingBits.tsx +131 -0
  55. package/src/components/icons.tsx +17 -0
  56. package/src/data/mock.ts +233 -0
  57. package/src/data/types.ts +55 -0
  58. package/src/format.ts +40 -0
  59. package/src/index.ts +225 -0
  60. package/src/locales/en.ts +74 -0
  61. package/src/locales/index.ts +7 -0
  62. package/src/locales/pt-BR.ts +74 -0
  63. package/src/presets.ts +115 -0
  64. package/src/store.ts +66 -0
  65. package/src/types.ts +114 -0
  66. package/src/views/CampaignComposer.tsx +89 -0
  67. package/src/views/CampaignsView.tsx +56 -0
  68. package/src/views/ChannelDetailView.tsx +59 -0
  69. package/src/views/ChannelsView.tsx +46 -0
  70. package/src/views/FunnelView.tsx +56 -0
  71. package/src/views/LandingPagesView.tsx +49 -0
  72. package/src/views/OverviewView.tsx +24 -0
  73. package/src/views/SettingsView.tsx +72 -0
  74. package/src/views/dashboardWidgets.tsx +173 -0
@@ -0,0 +1,173 @@
1
+ import React, { useEffect } from 'react'
2
+ import { Target, Percent, DollarSign, Trophy } from 'lucide-react'
3
+ import type { ColumnDef } from '@tanstack/react-table'
4
+ import type { StoreApi } from 'zustand'
5
+ import type { DashboardWidgetDef } from '@fayz-ai/core'
6
+ import { useTranslation } from '@fayz-ai/core'
7
+ import {
8
+ KpiCard, TableWidget, Card, CardHeader, CardTitle, CardContent,
9
+ defineKpiWidget, defineCustomWidget, defineTableWidget,
10
+ } from '@fayz-ai/ui'
11
+ import {
12
+ MarketingContextProvider, useMarketingConfig, useMarketingStore, useChannelLookup,
13
+ type ResolvedMarketingConfig,
14
+ } from '../MarketingContext'
15
+ import type { MarketingDataProvider } from '../data/types'
16
+ import type { MarketingUIState } from '../store'
17
+ import { formatCurrency, formatNumber, formatPercent, formatCompact } from '../format'
18
+ import { ProportionBar, ChannelCell, StatusBadge } from '../components/MarketingBits'
19
+ import type { Campaign } from '../types'
20
+
21
+ // Ensure marketing data is loaded — covers the global home, where MarketingPage
22
+ // (which normally triggers load) is not mounted. Guarded to avoid refetch storms.
23
+ function useEnsureMarketingData() {
24
+ const overview = useMarketingStore((s) => s.overview)
25
+ const loading = useMarketingStore((s) => s.loading)
26
+ const load = useMarketingStore((s) => s.load)
27
+ useEffect(() => { if (!overview && !loading) void load() }, [])
28
+ }
29
+
30
+ // ---------------------------------------------------------------------------
31
+ // KPI widgets (shared KpiCard — trend chip from current/previous)
32
+ // ---------------------------------------------------------------------------
33
+
34
+ function ConversionsKpi() {
35
+ const { conversion } = useMarketingConfig()
36
+ const overview = useMarketingStore((s) => s.overview)
37
+ useEnsureMarketingData()
38
+ return (
39
+ <KpiCard
40
+ icon={Target} label={conversion.labelPlural}
41
+ value={formatNumber(overview?.conversions ?? 0)}
42
+ current={overview?.conversions} previous={overview?.conversionsPrev}
43
+ />
44
+ )
45
+ }
46
+
47
+ function ConversionRateKpi() {
48
+ const t = useTranslation()
49
+ const overview = useMarketingStore((s) => s.overview)
50
+ useEnsureMarketingData()
51
+ return (
52
+ <KpiCard
53
+ icon={Percent} label={t('marketing.metric.conversionRate')}
54
+ value={formatPercent(overview?.cvr ?? 0)}
55
+ current={overview?.cvr} previous={overview?.cvrPrev}
56
+ />
57
+ )
58
+ }
59
+
60
+ function CpaKpi() {
61
+ const t = useTranslation()
62
+ const { currency } = useMarketingConfig()
63
+ const overview = useMarketingStore((s) => s.overview)
64
+ useEnsureMarketingData()
65
+ return (
66
+ <KpiCard
67
+ icon={DollarSign} label={t('marketing.metric.cpa')}
68
+ value={overview && overview.cpa > 0 ? formatCurrency(overview.cpa, currency) : '—'}
69
+ sub={`${formatCurrency(overview?.spend ?? 0, currency)} ${t('marketing.metric.spend')}`}
70
+ />
71
+ )
72
+ }
73
+
74
+ function TopChannelKpi() {
75
+ const t = useTranslation()
76
+ const { conversion, currency } = useMarketingConfig()
77
+ const overview = useMarketingStore((s) => s.overview)
78
+ const lookup = useChannelLookup()
79
+ useEnsureMarketingData()
80
+ return (
81
+ <KpiCard
82
+ icon={Trophy} label={t('marketing.metric.topChannel')}
83
+ value={overview?.topChannelId ? lookup.get(overview.topChannelId)?.label ?? '—' : '—'}
84
+ sub={`${conversion.valueLabel}: ${formatCurrency(overview?.value ?? 0, currency)}`}
85
+ />
86
+ )
87
+ }
88
+
89
+ // ---------------------------------------------------------------------------
90
+ // Channel mix (custom — reuses the existing ProportionBar + channel icons)
91
+ // ---------------------------------------------------------------------------
92
+
93
+ function ChannelMixPanel() {
94
+ const t = useTranslation()
95
+ const { conversion } = useMarketingConfig()
96
+ const overview = useMarketingStore((s) => s.overview)
97
+ useEnsureMarketingData()
98
+ const mix = overview?.channelMix ?? []
99
+ const maxMix = Math.max(...mix.map((m) => m.conversions), 1)
100
+ return (
101
+ <Card>
102
+ <CardHeader>
103
+ <CardTitle>{conversion.labelPlural} {t('marketing.overview.byChannelSuffix')}</CardTitle>
104
+ </CardHeader>
105
+ <CardContent className="space-y-2.5">
106
+ {mix.map((m) => (
107
+ <ProportionBar
108
+ key={m.channelId}
109
+ value={m.conversions}
110
+ max={maxMix}
111
+ color="#6366f1"
112
+ label={<ChannelCell channelId={m.channelId} />}
113
+ right={<span>{formatNumber(m.conversions)}</span>}
114
+ />
115
+ ))}
116
+ </CardContent>
117
+ </Card>
118
+ )
119
+ }
120
+
121
+ // ---------------------------------------------------------------------------
122
+ // Recent campaigns table
123
+ // ---------------------------------------------------------------------------
124
+
125
+ function CampaignsTable() {
126
+ const t = useTranslation()
127
+ const { conversion } = useMarketingConfig()
128
+ const campaigns = useMarketingStore((s) => s.campaigns)
129
+ useEnsureMarketingData()
130
+ const columns: ColumnDef<Campaign, unknown>[] = [
131
+ { accessorKey: 'name', header: t('marketing.col.campaign'), cell: ({ getValue }) => <span className="font-medium text-foreground">{getValue() as string}</span> },
132
+ { accessorKey: 'channelId', header: t('marketing.col.channel'), cell: ({ getValue }) => <ChannelCell channelId={getValue() as string} /> },
133
+ { accessorKey: 'conversions', header: conversion.labelPlural, cell: ({ getValue }) => <span className="text-muted-foreground">{formatCompact(getValue() as number)}</span> },
134
+ { id: 'cvr', accessorFn: (c) => (c.reach > 0 ? (c.conversions / c.reach) * 100 : 0), header: t('marketing.col.cvr'), cell: ({ getValue }) => <span className="text-muted-foreground">{formatPercent(getValue() as number)}</span> },
135
+ { accessorKey: 'status', header: t('marketing.col.status'), cell: ({ getValue }) => <StatusBadge status={getValue() as Campaign['status']} /> },
136
+ ]
137
+ const recent = campaigns.filter((c) => c.status !== 'draft').slice(0, 5)
138
+ return <TableWidget title={t('marketing.overview.recent')} icon="Megaphone" columns={columns} data={recent} emptyMessage={t('marketing.campaigns.empty')} />
139
+ }
140
+
141
+ // ---------------------------------------------------------------------------
142
+ // Registry builder — wraps each widget in MarketingContextProvider so it renders
143
+ // on the global home (no MarketingPage around it) and the marketing plugin-home.
144
+ // ---------------------------------------------------------------------------
145
+
146
+ export function createMarketingDashboardWidgets(ctx: {
147
+ config: ResolvedMarketingConfig
148
+ provider: MarketingDataProvider
149
+ store: StoreApi<MarketingUIState>
150
+ }): DashboardWidgetDef[] {
151
+ const withCtx = (Inner: React.ComponentType): React.ComponentType<unknown> => {
152
+ const Wrapped = () => (
153
+ <MarketingContextProvider config={ctx.config} provider={ctx.provider} store={ctx.store}>
154
+ <Inner />
155
+ </MarketingContextProvider>
156
+ )
157
+ Wrapped.displayName = `MarketingWidget(${Inner.displayName ?? Inner.name})`
158
+ return Wrapped
159
+ }
160
+
161
+ return [
162
+ // Conversions is marketing's headline KPI on the global home; the rest are
163
+ // home-hidden by default (shown on the marketing plugin-home, addable via Customize).
164
+ defineKpiWidget({ id: 'marketing.kpi.conversions', title: 'marketing.metric.conversions', domain: 'marketing', defaultOrder: 0, component: withCtx(ConversionsKpi) }),
165
+ defineKpiWidget({ id: 'marketing.kpi.conversion-rate', title: 'marketing.metric.conversionRate', domain: 'marketing', defaultOrder: 1, defaultVisible: false, component: withCtx(ConversionRateKpi) }),
166
+ defineKpiWidget({ id: 'marketing.kpi.cpa', title: 'marketing.metric.cpa', domain: 'marketing', defaultOrder: 2, defaultVisible: false, component: withCtx(CpaKpi) }),
167
+ defineKpiWidget({ id: 'marketing.kpi.top-channel', title: 'marketing.metric.topChannel', domain: 'marketing', defaultOrder: 3, defaultVisible: false, component: withCtx(TopChannelKpi) }),
168
+ // Channel-mix + campaigns default to the marketing plugin-home only; the
169
+ // global home stays a clean cross-domain KPI overview.
170
+ defineCustomWidget({ id: 'marketing.panel.channel-mix', title: 'marketing.overview.byChannelSuffix', domain: 'marketing', span: 4, defaultOrder: 10, surfaces: ['plugin-home'], component: withCtx(ChannelMixPanel) }),
171
+ defineTableWidget({ id: 'marketing.table.recent-campaigns', title: 'marketing.overview.recent', domain: 'marketing', span: 4, defaultOrder: 20, surfaces: ['plugin-home'], component: withCtx(CampaignsTable) }),
172
+ ]
173
+ }