@baack-software/baack-ts-crm 0.0.0-stage → 0.0.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 (234) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +74 -2
  3. package/dist/account.d.ts +36 -0
  4. package/dist/account.d.ts.map +1 -0
  5. package/dist/account.js +48 -0
  6. package/dist/account.js.map +1 -0
  7. package/dist/backend.d.ts +104 -0
  8. package/dist/backend.d.ts.map +1 -0
  9. package/dist/backend.js +328 -0
  10. package/dist/backend.js.map +1 -0
  11. package/dist/crm-client.d.ts +164 -0
  12. package/dist/crm-client.d.ts.map +1 -0
  13. package/dist/crm-client.js +252 -0
  14. package/dist/crm-client.js.map +1 -0
  15. package/dist/deal-order.d.ts +42 -0
  16. package/dist/deal-order.d.ts.map +1 -0
  17. package/dist/deal-order.js +80 -0
  18. package/dist/deal-order.js.map +1 -0
  19. package/dist/errors.d.ts +21 -0
  20. package/dist/errors.d.ts.map +1 -0
  21. package/dist/errors.js +33 -0
  22. package/dist/errors.js.map +1 -0
  23. package/dist/index.d.ts +14 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +14 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/invoices.d.ts +53 -0
  28. package/dist/invoices.d.ts.map +1 -0
  29. package/dist/invoices.js +155 -0
  30. package/dist/invoices.js.map +1 -0
  31. package/dist/leads.d.ts +29 -0
  32. package/dist/leads.d.ts.map +1 -0
  33. package/dist/leads.js +56 -0
  34. package/dist/leads.js.map +1 -0
  35. package/dist/mock/index.d.ts +43 -0
  36. package/dist/mock/index.d.ts.map +1 -0
  37. package/dist/mock/index.js +303 -0
  38. package/dist/mock/index.js.map +1 -0
  39. package/dist/money.d.ts +29 -0
  40. package/dist/money.d.ts.map +1 -0
  41. package/dist/money.js +82 -0
  42. package/dist/money.js.map +1 -0
  43. package/dist/period.d.ts +30 -0
  44. package/dist/period.d.ts.map +1 -0
  45. package/dist/period.js +49 -0
  46. package/dist/period.js.map +1 -0
  47. package/dist/pipeline.d.ts +36 -0
  48. package/dist/pipeline.d.ts.map +1 -0
  49. package/dist/pipeline.js +58 -0
  50. package/dist/pipeline.js.map +1 -0
  51. package/dist/react/Charts.d.ts +19 -0
  52. package/dist/react/Charts.d.ts.map +1 -0
  53. package/dist/react/Charts.js +38 -0
  54. package/dist/react/Charts.js.map +1 -0
  55. package/dist/react/ChoiceCombobox.d.ts +36 -0
  56. package/dist/react/ChoiceCombobox.d.ts.map +1 -0
  57. package/dist/react/ChoiceCombobox.js +105 -0
  58. package/dist/react/ChoiceCombobox.js.map +1 -0
  59. package/dist/react/CompanyAccountView.d.ts +21 -0
  60. package/dist/react/CompanyAccountView.d.ts.map +1 -0
  61. package/dist/react/CompanyAccountView.js +27 -0
  62. package/dist/react/CompanyAccountView.js.map +1 -0
  63. package/dist/react/DashboardCards.d.ts +32 -0
  64. package/dist/react/DashboardCards.d.ts.map +1 -0
  65. package/dist/react/DashboardCards.js +38 -0
  66. package/dist/react/DashboardCards.js.map +1 -0
  67. package/dist/react/DealsBoard.d.ts +49 -0
  68. package/dist/react/DealsBoard.d.ts.map +1 -0
  69. package/dist/react/DealsBoard.js +82 -0
  70. package/dist/react/DealsBoard.js.map +1 -0
  71. package/dist/react/Details.d.ts +85 -0
  72. package/dist/react/Details.d.ts.map +1 -0
  73. package/dist/react/Details.js +268 -0
  74. package/dist/react/Details.js.map +1 -0
  75. package/dist/react/Forms.d.ts +190 -0
  76. package/dist/react/Forms.d.ts.map +1 -0
  77. package/dist/react/Forms.js +494 -0
  78. package/dist/react/Forms.js.map +1 -0
  79. package/dist/react/LeadsTable.d.ts +22 -0
  80. package/dist/react/LeadsTable.d.ts.map +1 -0
  81. package/dist/react/LeadsTable.js +79 -0
  82. package/dist/react/LeadsTable.js.map +1 -0
  83. package/dist/react/Sales.d.ts +68 -0
  84. package/dist/react/Sales.d.ts.map +1 -0
  85. package/dist/react/Sales.js +270 -0
  86. package/dist/react/Sales.js.map +1 -0
  87. package/dist/react/SidePanel.d.ts +31 -0
  88. package/dist/react/SidePanel.d.ts.map +1 -0
  89. package/dist/react/SidePanel.js +49 -0
  90. package/dist/react/SidePanel.js.map +1 -0
  91. package/dist/react/format.d.ts +28 -0
  92. package/dist/react/format.d.ts.map +1 -0
  93. package/dist/react/format.js +30 -0
  94. package/dist/react/format.js.map +1 -0
  95. package/dist/react/index.d.ts +15 -0
  96. package/dist/react/index.d.ts.map +1 -0
  97. package/dist/react/index.js +15 -0
  98. package/dist/react/index.js.map +1 -0
  99. package/dist/react/parts.d.ts +55 -0
  100. package/dist/react/parts.d.ts.map +1 -0
  101. package/dist/react/parts.js +82 -0
  102. package/dist/react/parts.js.map +1 -0
  103. package/dist/react/pipeline-choice.d.ts +19 -0
  104. package/dist/react/pipeline-choice.d.ts.map +1 -0
  105. package/dist/react/pipeline-choice.js +39 -0
  106. package/dist/react/pipeline-choice.js.map +1 -0
  107. package/dist/react/text.d.ts +232 -0
  108. package/dist/react/text.d.ts.map +1 -0
  109. package/dist/react/text.js +229 -0
  110. package/dist/react/text.js.map +1 -0
  111. package/dist/react/ui/alert.d.ts +11 -0
  112. package/dist/react/ui/alert.d.ts.map +1 -0
  113. package/dist/react/ui/alert.js +29 -0
  114. package/dist/react/ui/alert.js.map +1 -0
  115. package/dist/react/ui/badge.d.ts +10 -0
  116. package/dist/react/ui/badge.d.ts.map +1 -0
  117. package/dist/react/ui/badge.js +26 -0
  118. package/dist/react/ui/badge.js.map +1 -0
  119. package/dist/react/ui/button.d.ts +11 -0
  120. package/dist/react/ui/button.d.ts.map +1 -0
  121. package/dist/react/ui/button.js +37 -0
  122. package/dist/react/ui/button.js.map +1 -0
  123. package/dist/react/ui/card.d.ts +12 -0
  124. package/dist/react/ui/card.d.ts.map +1 -0
  125. package/dist/react/ui/card.js +26 -0
  126. package/dist/react/ui/card.js.map +1 -0
  127. package/dist/react/ui/chart.d.ts +45 -0
  128. package/dist/react/ui/chart.d.ts.map +1 -0
  129. package/dist/react/ui/chart.js +116 -0
  130. package/dist/react/ui/chart.js.map +1 -0
  131. package/dist/react/ui/input.d.ts +4 -0
  132. package/dist/react/ui/input.d.ts.map +1 -0
  133. package/dist/react/ui/input.js +8 -0
  134. package/dist/react/ui/input.js.map +1 -0
  135. package/dist/react/ui/label.d.ts +5 -0
  136. package/dist/react/ui/label.d.ts.map +1 -0
  137. package/dist/react/ui/label.js +10 -0
  138. package/dist/react/ui/label.js.map +1 -0
  139. package/dist/react/ui/progress.d.ts +5 -0
  140. package/dist/react/ui/progress.d.ts.map +1 -0
  141. package/dist/react/ui/progress.js +10 -0
  142. package/dist/react/ui/progress.js.map +1 -0
  143. package/dist/react/ui/select.d.ts +16 -0
  144. package/dist/react/ui/select.d.ts.map +1 -0
  145. package/dist/react/ui/select.js +37 -0
  146. package/dist/react/ui/select.js.map +1 -0
  147. package/dist/react/ui/separator.d.ts +5 -0
  148. package/dist/react/ui/separator.d.ts.map +1 -0
  149. package/dist/react/ui/separator.js +10 -0
  150. package/dist/react/ui/separator.js.map +1 -0
  151. package/dist/react/ui/sheet.d.ts +15 -0
  152. package/dist/react/ui/sheet.d.ts.map +1 -0
  153. package/dist/react/ui/sheet.js +38 -0
  154. package/dist/react/ui/sheet.js.map +1 -0
  155. package/dist/react/ui/skeleton.d.ts +3 -0
  156. package/dist/react/ui/skeleton.d.ts.map +1 -0
  157. package/dist/react/ui/skeleton.js +7 -0
  158. package/dist/react/ui/skeleton.js.map +1 -0
  159. package/dist/react/ui/table.d.ts +11 -0
  160. package/dist/react/ui/table.d.ts.map +1 -0
  161. package/dist/react/ui/table.js +29 -0
  162. package/dist/react/ui/table.js.map +1 -0
  163. package/dist/react/ui/tabs.d.ts +12 -0
  164. package/dist/react/ui/tabs.d.ts.map +1 -0
  165. package/dist/react/ui/tabs.js +31 -0
  166. package/dist/react/ui/tabs.js.map +1 -0
  167. package/dist/react/views.d.ts +82 -0
  168. package/dist/react/views.d.ts.map +1 -0
  169. package/dist/react/views.js +241 -0
  170. package/dist/react/views.js.map +1 -0
  171. package/dist/sales.d.ts +82 -0
  172. package/dist/sales.d.ts.map +1 -0
  173. package/dist/sales.js +87 -0
  174. package/dist/sales.js.map +1 -0
  175. package/dist/store.d.ts +159 -0
  176. package/dist/store.d.ts.map +1 -0
  177. package/dist/store.js +305 -0
  178. package/dist/store.js.map +1 -0
  179. package/dist/target.d.ts +22 -0
  180. package/dist/target.d.ts.map +1 -0
  181. package/dist/target.js +40 -0
  182. package/dist/target.js.map +1 -0
  183. package/dist/types.d.ts +204 -0
  184. package/dist/types.d.ts.map +1 -0
  185. package/dist/types.js +2 -0
  186. package/dist/types.js.map +1 -0
  187. package/package.json +100 -4
  188. package/src/account.ts +79 -0
  189. package/src/backend.ts +501 -0
  190. package/src/crm-client.ts +393 -0
  191. package/src/deal-order.ts +116 -0
  192. package/src/errors.ts +34 -0
  193. package/src/index.ts +13 -0
  194. package/src/invoices.ts +177 -0
  195. package/src/leads.ts +78 -0
  196. package/src/mock/index.ts +398 -0
  197. package/src/money.ts +104 -0
  198. package/src/period.ts +71 -0
  199. package/src/pipeline.ts +82 -0
  200. package/src/react/Charts.tsx +143 -0
  201. package/src/react/ChoiceCombobox.tsx +227 -0
  202. package/src/react/CompanyAccountView.tsx +198 -0
  203. package/src/react/DashboardCards.tsx +171 -0
  204. package/src/react/DealsBoard.tsx +344 -0
  205. package/src/react/Details.tsx +870 -0
  206. package/src/react/Forms.tsx +1486 -0
  207. package/src/react/LeadsTable.tsx +234 -0
  208. package/src/react/Sales.tsx +827 -0
  209. package/src/react/SidePanel.tsx +104 -0
  210. package/src/react/format.ts +53 -0
  211. package/src/react/index.ts +14 -0
  212. package/src/react/parts.tsx +183 -0
  213. package/src/react/pipeline-choice.ts +54 -0
  214. package/src/react/text.ts +232 -0
  215. package/src/react/ui/README.md +6 -0
  216. package/src/react/ui/alert.tsx +75 -0
  217. package/src/react/ui/badge.tsx +48 -0
  218. package/src/react/ui/button.tsx +66 -0
  219. package/src/react/ui/card.tsx +102 -0
  220. package/src/react/ui/chart.tsx +304 -0
  221. package/src/react/ui/input.tsx +18 -0
  222. package/src/react/ui/label.tsx +23 -0
  223. package/src/react/ui/progress.tsx +30 -0
  224. package/src/react/ui/select.tsx +189 -0
  225. package/src/react/ui/separator.tsx +27 -0
  226. package/src/react/ui/sheet.tsx +145 -0
  227. package/src/react/ui/skeleton.tsx +13 -0
  228. package/src/react/ui/table.tsx +113 -0
  229. package/src/react/ui/tabs.tsx +89 -0
  230. package/src/react/views.tsx +633 -0
  231. package/src/sales.ts +167 -0
  232. package/src/store.ts +528 -0
  233. package/src/target.ts +54 -0
  234. package/src/types.ts +230 -0
@@ -0,0 +1,104 @@
1
+ import { cn } from 'cn';
2
+ import { X } from 'lucide-react';
3
+ import { createContext, useCallback, useContext, useSyncExternalStore, type ReactNode } from 'react';
4
+ import { withText, type CrmText } from './text.ts';
5
+ import { Button } from './ui/button.tsx';
6
+ import { Sheet, SheetContent, SheetTitle } from './ui/sheet.tsx';
7
+
8
+ /** Whether a media query matches, following changes. True where there is no `matchMedia`, such as on the server. */
9
+ export function useMediaQuery(query: string): boolean {
10
+ const subscribe = useCallback(
11
+ (onChange: () => void) => {
12
+ if (typeof matchMedia !== 'function') return () => undefined;
13
+ const list = matchMedia(query);
14
+ list.addEventListener('change', onChange);
15
+ return () => list.removeEventListener('change', onChange);
16
+ },
17
+ [query],
18
+ );
19
+ const matches = () => typeof matchMedia !== 'function' || matchMedia(query).matches;
20
+ return useSyncExternalStore(subscribe, matches, () => true);
21
+ }
22
+
23
+ /** The id of a record's card or row, so closing its panel can put focus back on it. */
24
+ export const recordItemId = (urn: string) => `crm-item-${urn}`;
25
+
26
+ /** The open panel's close action, for the card or form heading it holds to show beside its title. */
27
+ export const PanelClose = createContext<{ close: () => void; label: string } | undefined>(undefined);
28
+
29
+ /**
30
+ * Closes the side panel this is in, beside a card's or form's title so it lines up with it. Nothing outside a panel.
31
+ * A card shows it once: it clears the context for what it holds (see `CrmCard`).
32
+ */
33
+ export function PanelCloseButton() {
34
+ const panel = useContext(PanelClose);
35
+ if (!panel) return null;
36
+ return (
37
+ <Button variant="ghost" size="icon-sm" onClick={panel.close} aria-label={panel.label}>
38
+ <X aria-hidden="true" />
39
+ </Button>
40
+ );
41
+ }
42
+
43
+ /**
44
+ * A list or board with the open record beside it, as baack-ts-tasks' task view does: a sticky panel to the right on
45
+ * wide screens, a sheet from the right on narrow ones. Closing puts focus back on the record's card or row.
46
+ */
47
+ export function SidePanelLayout({
48
+ open,
49
+ title,
50
+ onClose,
51
+ children,
52
+ panel,
53
+ text,
54
+ }: {
55
+ /** The URN of the open record, if any. */
56
+ open: string | undefined;
57
+ /** What the panel holds, for its close button and the sheet's name. */
58
+ title: string;
59
+ onClose: () => void;
60
+ children: ReactNode;
61
+ panel: ReactNode;
62
+ text?: Partial<CrmText> | undefined;
63
+ }) {
64
+ const words = withText(text);
65
+ const wide = useMediaQuery('(min-width: 64rem)');
66
+ const close = () => {
67
+ onClose();
68
+ if (open) requestAnimationFrame(() => document.getElementById(recordItemId(open))?.focus());
69
+ };
70
+ const content = (
71
+ <PanelClose.Provider value={{ close, label: `${words.close}: ${title}` }}>{panel}</PanelClose.Provider>
72
+ );
73
+ return (
74
+ <>
75
+ <div
76
+ className={cn(
77
+ 'grid grid-cols-1 items-start gap-4',
78
+ open && wide && 'grid-cols-[minmax(0,1fr)_minmax(22rem,28rem)]',
79
+ )}
80
+ >
81
+ <div className="min-w-0">{children}</div>
82
+ {open && wide && (
83
+ <aside aria-label={title} className="sticky top-4 max-h-[calc(100vh-2rem)] overflow-y-auto p-px">
84
+ {content}
85
+ </aside>
86
+ )}
87
+ </div>
88
+ {!wide && (
89
+ <Sheet open={open !== undefined} onOpenChange={(isOpen) => !isOpen && close()}>
90
+ <SheetContent
91
+ side="right"
92
+ showCloseButton={false}
93
+ aria-describedby={undefined}
94
+ className="w-full overflow-y-auto p-4 sm:max-w-md"
95
+ onCloseAutoFocus={(event) => event.preventDefault()}
96
+ >
97
+ <SheetTitle className="sr-only">{title}</SheetTitle>
98
+ {content}
99
+ </SheetContent>
100
+ </Sheet>
101
+ )}
102
+ </>
103
+ );
104
+ }
@@ -0,0 +1,53 @@
1
+ import { parseTimestamp } from '@baack-software/baack-ts-tasks';
2
+ import { formatMoney, totalsToMoney, type MoneyTotals } from '../money.ts';
3
+ import type { Money } from '../types.ts';
4
+
5
+ /**
6
+ * Names for the companies and people records point at. The CRM records hold references only; the host knows the
7
+ * names (from the identity and company reads it already makes).
8
+ */
9
+ export interface CrmNames {
10
+ company?: (urn: string) => string | undefined;
11
+ person?: (urn: string) => string | undefined;
12
+ }
13
+
14
+ export const companyNameOf = (names: CrmNames | undefined, urn: string | undefined) =>
15
+ urn ? names?.company?.(urn) : undefined;
16
+
17
+ export const personNameOf = (names: CrmNames | undefined, urn: string | undefined) =>
18
+ urn ? names?.person?.(urn) : undefined;
19
+
20
+ /**
21
+ * Totals in each currency, joined with "and", such as "GBP 1,250.00 and EUR 300.00" written with symbols. An empty
22
+ * total reads as zero in `fallbackCurrency`.
23
+ */
24
+ export function formatTotals(totals: MoneyTotals, fallbackCurrency = 'GBP', locale = 'en-GB'): string {
25
+ const money = totalsToMoney(totals);
26
+ if (money.length === 0) return formatMoney({ unitValue: 0, currencyCode: fallbackCurrency }, locale);
27
+ return money.map((value) => formatMoney(value, locale)).join(' and ');
28
+ }
29
+
30
+ /** Money, or a dash when there's none. */
31
+ export const formatMaybeMoney = (money: Money | null | undefined, locale = 'en-GB') =>
32
+ money ? formatMoney(money, locale) : '-';
33
+
34
+ /** A short date such as "24 Oct 2026", or a dash. */
35
+ export function formatDate(timestamp: string | null | undefined, locale = 'en-GB'): string {
36
+ const date = parseTimestamp(timestamp);
37
+ return date
38
+ ? date.toLocaleDateString(locale, { day: 'numeric', month: 'short', year: 'numeric', timeZone: 'UTC' })
39
+ : '-';
40
+ }
41
+
42
+ /** A fraction as a percentage: 0.104 is "10.4%". */
43
+ export const formatPercent = (fraction: number, locale = 'en-GB') =>
44
+ new Intl.NumberFormat(locale, { style: 'percent', maximumFractionDigits: 1 }).format(fraction);
45
+
46
+ /** A change with its sign: "+10.4%" or "-0.8%". */
47
+ export const formatChange = (fraction: number, locale = 'en-GB') =>
48
+ new Intl.NumberFormat(locale, { style: 'percent', maximumFractionDigits: 1, signDisplay: 'exceptZero' }).format(
49
+ fraction,
50
+ );
51
+
52
+ /** An enum value as words: `BANK_TRANSFER` is "Bank transfer". */
53
+ export const enumLabel = (value: string) => value.charAt(0) + value.slice(1).toLowerCase().replaceAll('_', ' ');
@@ -0,0 +1,14 @@
1
+ export * from './text.ts';
2
+ export * from './format.ts';
3
+ export * from './parts.tsx';
4
+ export * from './DashboardCards.tsx';
5
+ export * from './Charts.tsx';
6
+ export * from './LeadsTable.tsx';
7
+ export * from './DealsBoard.tsx';
8
+ export * from './Details.tsx';
9
+ export * from './CompanyAccountView.tsx';
10
+ export * from './Sales.tsx';
11
+ export * from './Forms.tsx';
12
+ export * from './ChoiceCombobox.tsx';
13
+ export * from './SidePanel.tsx';
14
+ export * from './views.tsx';
@@ -0,0 +1,183 @@
1
+ import { FlaskConical, TrendingDown, TrendingUp } from 'lucide-react';
2
+ import { cn } from 'cn';
3
+ import { useContext, useId, type ReactNode } from 'react';
4
+ import { INVOICE_STATUS_LABELS } from '../invoices.ts';
5
+ import { LEAD_STATE_LABELS, leadState, type LeadState } from '../leads.ts';
6
+ import { REPORTING_PERIODS, type ReportingPeriod } from '../period.ts';
7
+ import type { DealOutcome, InvoiceStatus, Lead } from '../types.ts';
8
+ import { formatChange } from './format.ts';
9
+ import { PanelClose, PanelCloseButton } from './SidePanel.tsx';
10
+ import { withText, type CrmText } from './text.ts';
11
+ import { Badge } from './ui/badge.tsx';
12
+ import { Card, CardAction, CardContent, CardDescription, CardHeader, CardTitle } from './ui/card.tsx';
13
+ import { Label } from './ui/label.tsx';
14
+ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './ui/select.tsx';
15
+
16
+ /** Marks a view or card whose records come from sample data, so nobody takes them for real ones. */
17
+ export function SampleDataBadge({ text }: { text?: Partial<CrmText> | undefined }) {
18
+ const words = withText(text);
19
+ return (
20
+ <Badge variant="outline" title={words.sampleDataHint} className="gap-1 border-dashed">
21
+ <FlaskConical aria-hidden="true" />
22
+ {words.sampleData}
23
+ </Badge>
24
+ );
25
+ }
26
+
27
+ /**
28
+ * Says once, above a view, which of its concepts come from sample data, rather than a badge on every card.
29
+ */
30
+ export function SampleDataNotice({
31
+ concepts,
32
+ text,
33
+ }: {
34
+ concepts: readonly string[];
35
+ text?: Partial<CrmText> | undefined;
36
+ }) {
37
+ const words = withText(text);
38
+ if (concepts.length === 0) return null;
39
+ return (
40
+ <p
41
+ className={cn(
42
+ 'bg-muted/50 text-muted-foreground flex flex-wrap items-center gap-2',
43
+ 'rounded-lg border border-dashed px-3 py-2 text-sm',
44
+ )}
45
+ >
46
+ <FlaskConical aria-hidden="true" className="size-4 shrink-0" />
47
+ <span>
48
+ <span className="text-foreground font-medium">{words.sampleData}:</span> {concepts.join(', ')}.{' '}
49
+ {words.sampleDataHint}
50
+ </span>
51
+ </p>
52
+ );
53
+ }
54
+
55
+ /**
56
+ * A titled card for the CRM views. `sample` adds the sample data badge; `action` sits at the top right. In a side
57
+ * panel, the panel's close button sits there too, in line with the title; cards inside this one don't repeat it.
58
+ */
59
+ export function CrmCard({
60
+ title,
61
+ description,
62
+ sample,
63
+ action,
64
+ text,
65
+ className,
66
+ children,
67
+ }: {
68
+ title: string;
69
+ description?: string;
70
+ sample?: boolean | undefined;
71
+ action?: ReactNode;
72
+ text?: Partial<CrmText> | undefined;
73
+ className?: string;
74
+ children: ReactNode;
75
+ }) {
76
+ const inPanel = useContext(PanelClose) !== undefined;
77
+ return (
78
+ <Card className={className}>
79
+ <CardHeader>
80
+ <CardTitle>
81
+ <h2 className="text-base font-semibold">{title}</h2>
82
+ </CardTitle>
83
+ {description && <CardDescription>{description}</CardDescription>}
84
+ {(sample || action || inPanel) && (
85
+ <CardAction className="flex items-center gap-2">
86
+ {sample && <SampleDataBadge text={text} />}
87
+ {action}
88
+ <PanelCloseButton />
89
+ </CardAction>
90
+ )}
91
+ </CardHeader>
92
+ <CardContent>
93
+ <PanelClose.Provider value={undefined}>{children}</PanelClose.Provider>
94
+ </CardContent>
95
+ </Card>
96
+ );
97
+ }
98
+
99
+ /** A rise or fall against the previous period, in words and an icon, not colour alone. */
100
+ export function ChangeIndicator({ change, text }: { change: number | undefined; text?: Partial<CrmText> | undefined }) {
101
+ const words = withText(text);
102
+ if (change === undefined) return <span className="text-muted-foreground text-xs">{words.noChange}</span>;
103
+ const Icon = change < 0 ? TrendingDown : TrendingUp;
104
+ return (
105
+ <span className="text-muted-foreground inline-flex items-center gap-1 text-xs">
106
+ <Icon aria-hidden="true" className={cn('size-3.5', change < 0 ? 'text-destructive' : 'text-primary')} />
107
+ <span className="text-foreground font-medium">{formatChange(change)}</span> {words.vsPrevious}
108
+ </span>
109
+ );
110
+ }
111
+
112
+ const LEAD_VARIANT: Record<LeadState, 'default' | 'secondary' | 'outline' | 'destructive'> = {
113
+ NEW: 'default',
114
+ CONTACTED: 'secondary',
115
+ QUALIFIED: 'secondary',
116
+ CLOSED: 'outline',
117
+ CONVERTED: 'outline',
118
+ };
119
+
120
+ /** Where a lead stands: how it ended once closed (converted, unqualified), otherwise its status. */
121
+ export function LeadStatusBadge({ lead }: { lead: Lead }) {
122
+ const state = leadState(lead);
123
+ return <Badge variant={LEAD_VARIANT[state]}>{LEAD_STATE_LABELS[state]}</Badge>;
124
+ }
125
+
126
+ /** How a deal stands, in words. Lost is outlined rather than red, which lacks contrast on tinted board columns. */
127
+ export function OutcomeBadge({ outcome, label }: { outcome: DealOutcome; label: string }) {
128
+ return <Badge variant={outcome === 'WON' ? 'default' : outcome === 'LOST' ? 'outline' : 'secondary'}>{label}</Badge>;
129
+ }
130
+
131
+ export function InvoiceStatusBadge({ status, overdue }: { status: InvoiceStatus; overdue?: boolean }) {
132
+ if (overdue) return <Badge variant="destructive">Overdue</Badge>;
133
+ return (
134
+ <Badge
135
+ variant={status === 'PAID' ? 'default' : status === 'DRAFT' || status === 'VOIDED' ? 'outline' : 'secondary'}
136
+ >
137
+ {INVOICE_STATUS_LABELS[status]}
138
+ </Badge>
139
+ );
140
+ }
141
+
142
+ const PERIOD_WORDS: Record<ReportingPeriod, { label: keyof CrmText; versus: keyof CrmText }> = {
143
+ last30: { label: 'periodLast30', versus: 'vsPrevious30' },
144
+ quarter: { label: 'periodQuarter', versus: 'vsLastQuarter' },
145
+ year: { label: 'periodYear', versus: 'vsLastYear' },
146
+ };
147
+
148
+ /** The words for figures in `period`: what each change is against. */
149
+ export const periodText = (period: ReportingPeriod, text?: Partial<CrmText>): Partial<CrmText> => ({
150
+ ...text,
151
+ vsPrevious: withText(text)[PERIOD_WORDS[period].versus],
152
+ });
153
+
154
+ /** Picks the reporting period a view's figures are for. */
155
+ export function PeriodSelect({
156
+ value,
157
+ onChange,
158
+ text,
159
+ }: {
160
+ value: ReportingPeriod;
161
+ onChange: (period: ReportingPeriod) => void;
162
+ text?: Partial<CrmText> | undefined;
163
+ }) {
164
+ const words = withText(text);
165
+ const id = useId();
166
+ return (
167
+ <div className="flex items-center gap-2">
168
+ <Label htmlFor={id}>{words.period}</Label>
169
+ <Select value={value} onValueChange={(next) => onChange(next as ReportingPeriod)}>
170
+ <SelectTrigger id={id} className="w-40">
171
+ <SelectValue />
172
+ </SelectTrigger>
173
+ <SelectContent>
174
+ {REPORTING_PERIODS.map((period) => (
175
+ <SelectItem key={period} value={period}>
176
+ {words[PERIOD_WORDS[period].label]}
177
+ </SelectItem>
178
+ ))}
179
+ </SelectContent>
180
+ </Select>
181
+ </div>
182
+ );
183
+ }
@@ -0,0 +1,54 @@
1
+ import { useState } from 'react';
2
+ import { defaultPipeline } from '../pipeline.ts';
3
+ import type { Deal, Pipeline } from '../types.ts';
4
+
5
+ /**
6
+ * What this user last chose in the deals, per company, kept in the browser: the workflow they work in (shared by the
7
+ * dashboard, converting leads and the deals board) and the board's order. One record, so each writer keeps the other's.
8
+ */
9
+ export interface DealsMemory {
10
+ pipeline?: string | undefined;
11
+ order?: string | undefined;
12
+ }
13
+
14
+ const memoryKey = (owner: string) => `baack-crm:deals-board:${owner}`;
15
+
16
+ export function recallDeals(owner: string): DealsMemory {
17
+ try {
18
+ const saved = JSON.parse(localStorage.getItem(memoryKey(owner)) ?? '{}') as { pipeline?: unknown; order?: unknown };
19
+ return {
20
+ pipeline: typeof saved.pipeline === 'string' ? saved.pipeline : undefined,
21
+ order: typeof saved.order === 'string' ? saved.order : undefined,
22
+ };
23
+ } catch {
24
+ // No storage (private browsing, blocked, or no browser): start afresh.
25
+ return {};
26
+ }
27
+ }
28
+
29
+ /** Keeps `changes`, merged into what's stored, so a change to one field never puts back an old value of another. */
30
+ export function rememberDeals(owner: string, changes: DealsMemory): void {
31
+ try {
32
+ localStorage.setItem(memoryKey(owner), JSON.stringify({ ...recallDeals(owner), ...changes }));
33
+ } catch {
34
+ // Not kept; the views still work.
35
+ }
36
+ }
37
+
38
+ /**
39
+ * The pipeline this user works in, for the company `owner`: the one they last chose (their team may work in a
40
+ * different workflow from other users'), else the default (`defaultPipeline`: the workflow with the most deals).
41
+ * Choosing one remembers it.
42
+ */
43
+ export function usePipeline(
44
+ owner: string,
45
+ pipelines: readonly Pipeline[],
46
+ deals: readonly Deal[],
47
+ ): [Pipeline | undefined, (urn: string) => void] {
48
+ const [chosen, setChosen] = useState(() => recallDeals(owner).pipeline);
49
+ const choose = (urn: string) => {
50
+ setChosen(urn);
51
+ rememberDeals(owner, { pipeline: urn });
52
+ };
53
+ return [defaultPipeline(pipelines, deals, chosen), choose];
54
+ }
@@ -0,0 +1,232 @@
1
+ /**
2
+ * The words the CRM components show, in en-GB. Each composition takes `text` to replace any of them, so a host can
3
+ * supply its own copy (later from Baack content).
4
+ */
5
+ export const CRM_TEXT = {
6
+ sampleData: 'Sample data',
7
+ sampleDataHint: 'Not yet in the Baack API: shown from sample data so the view can be reviewed.',
8
+ unavailable: 'Not available yet',
9
+ loading: 'Loading',
10
+ noChange: 'No earlier figures',
11
+ vsPrevious: 'vs previous period',
12
+ vsPrevious30: 'vs the 30 days before',
13
+ vsLastQuarter: 'vs the same time last quarter',
14
+ vsLastYear: 'vs the same time last year',
15
+ customers: 'Customers',
16
+ customersHint: 'Companies with a won deal',
17
+ openDeals: 'Open deals',
18
+ revenue: 'Revenue',
19
+ revenueHint: 'Payments received less refunds',
20
+ revenueOverTime: 'Revenue over time',
21
+ newLeads: 'New leads',
22
+ target: 'Sales target',
23
+ targetSummary: 'Payments received on deals against the value of leads and deals',
24
+ targetReceived: 'received of',
25
+ leadsBySource: 'Leads by source',
26
+ pipeline: 'Sales pipeline',
27
+ stage: 'Stage',
28
+ workflow: 'Workflow',
29
+ deals: 'Deals',
30
+ value: 'Value',
31
+ weighted: 'Weighted',
32
+ share: 'Share',
33
+ leads: 'Leads',
34
+ lead: 'Lead',
35
+ company: 'Company',
36
+ contact: 'Contact',
37
+ source: 'Source',
38
+ status: 'Status',
39
+ date: 'Date',
40
+ assignee: 'Assignee',
41
+ filterLeads: 'Filter leads',
42
+ allStatuses: 'All statuses',
43
+ allSources: 'All sources',
44
+ export: 'Export CSV',
45
+ previous: 'Previous',
46
+ next: 'Next',
47
+ noLeads: 'No leads match.',
48
+ of: 'of',
49
+ open: 'Open',
50
+ close: 'Close',
51
+ moveTo: 'Move to stage',
52
+ expectedClose: 'Expected close',
53
+ closed: 'Closed',
54
+ lostReason: 'Lost reason',
55
+ outcome: 'Outcome',
56
+ outcomeOpen: 'Open',
57
+ outcomeWon: 'Won',
58
+ outcomeLost: 'Lost',
59
+ markLost: 'Mark as lost',
60
+ markUnqualified: 'Mark unqualified',
61
+ unqualifiedReason: 'Why it was unqualified',
62
+ reopen: 'Reopen',
63
+ cancel: 'Cancel',
64
+ probability: 'Probability',
65
+ convert: 'Convert to deal',
66
+ converting: 'Converting',
67
+ convertSummary: 'Qualify the lead: it becomes a deal at the first stage of the pipeline.',
68
+ companyName: 'Company name',
69
+ companyNameHint: 'Creates the client company. Leave it empty for a person buying for themselves.',
70
+ convertedTo: 'Converted to',
71
+ created: 'Created',
72
+ noDeals: 'No deals here.',
73
+ account: 'Account',
74
+ openValue: 'Open deals',
75
+ wonValue: 'Won',
76
+ outstanding: 'Outstanding',
77
+ overdue: 'Overdue',
78
+ received: 'Received',
79
+ invoices: 'Invoices',
80
+ invoice: 'Invoice',
81
+ total: 'Total',
82
+ due: 'Due',
83
+ paid: 'Paid',
84
+ payments: 'Payments',
85
+ method: 'Method',
86
+ reference: 'Reference',
87
+ amount: 'Amount',
88
+ noInvoices: 'No invoices.',
89
+ noPayments: 'No payments.',
90
+ openLeads: 'Open leads',
91
+ people: 'People',
92
+ sales: 'Sales',
93
+ invoiced: 'Invoiced',
94
+ invoicedHint: 'Invoices issued in the period',
95
+ salesTax: 'Sales tax',
96
+ salesTaxHint: 'On invoices issued in the period',
97
+ outstandingHint: 'Still to pay on invoices sent',
98
+ outstandingByAge: 'Outstanding by age',
99
+ paymentMethods: 'Payments by method',
100
+ customer: 'Customer',
101
+ issued: 'Issued',
102
+ filterInvoices: 'Filter invoices',
103
+ allInvoices: 'All invoices',
104
+ awaitingPayment: 'Awaiting payment',
105
+ noMatchingInvoices: 'No invoices match.',
106
+ recordPayment: 'Record payment',
107
+ recording: 'Recording',
108
+ paymentRecorded: 'Payment recorded.',
109
+ invalidAmount: 'Enter an amount above zero, such as 120.00.',
110
+ tasks: 'Tasks',
111
+ none: 'None',
112
+ unnamed: 'Unnamed',
113
+ newLead: 'New lead',
114
+ newDeal: 'New deal',
115
+ newInvoice: 'New invoice',
116
+ title: 'Title',
117
+ titleRequired: 'Enter a title.',
118
+ noCompany: 'No company',
119
+ newCompany: 'New company',
120
+ newCompanyHint: 'Makes the client company, owned by yours, when the lead is saved.',
121
+ addCompany: 'Add a new company',
122
+ companyNameRequired: 'Enter the company name.',
123
+ noContact: 'No contact',
124
+ newContact: 'New contact',
125
+ newContactHint:
126
+ "Makes a person owned by your company when the lead is saved, linked to the lead's company when it has one.",
127
+ addContact: 'Add a new person',
128
+ addPerson: 'Add a person',
129
+ givenName: 'Given name',
130
+ familyName: 'Family name',
131
+ email: 'Email',
132
+ givenNameRequired: 'Enter a given name.',
133
+ telephone: 'Telephone',
134
+ contactDetails: "Contact's details",
135
+ contactEmail: "Contact's email",
136
+ contactTelephone: "Contact's telephone",
137
+ contactDetailsUnavailable: "The contact's email and telephone couldn't be read.",
138
+ unassigned: 'Unassigned',
139
+ toBeCreated: 'new',
140
+ useThis: 'Use this',
141
+ saving: 'Saving',
142
+ back: 'Back',
143
+ saveFailed: "The change couldn't be saved.",
144
+ createLead: 'Create lead',
145
+ createDeal: 'Create deal',
146
+ createInvoice: 'Create invoice',
147
+ creating: 'Creating',
148
+ currency: 'Currency',
149
+ issuedOn: 'Issued on',
150
+ dueOn: 'Due on',
151
+ forDeal: 'For deal',
152
+ noDeal: 'No deal',
153
+ lines: 'Lines',
154
+ description: 'Description',
155
+ quantity: 'Quantity',
156
+ unitPrice: 'Unit price',
157
+ addLine: 'Add a line',
158
+ removeLine: 'Remove line',
159
+ taxRate: 'Tax rate (%)',
160
+ subTotal: 'Subtotal',
161
+ tax: 'Tax',
162
+ customerRequired: 'Choose the customer.',
163
+ amountAs: 'Amount as',
164
+ amountAsLines: 'Line items',
165
+ amountAsTotal: 'A total',
166
+ totalWithTax: 'Total, including tax',
167
+ totalHint: 'The subtotal and tax are worked out from it at the tax rate.',
168
+ totalRequired: 'Enter the total, above zero, such as 1,200.00.',
169
+ linesRequired: 'Give each line a description, a quantity and a unit price above zero.',
170
+ invalidRate: 'Enter a tax rate from 0 to 100.',
171
+ invalidProbability: 'Enter a probability from 0 to 100.',
172
+ companies: 'Companies',
173
+ contacts: 'Contacts',
174
+ searchCompanies: 'Search companies by name',
175
+ searchContacts: 'Search people by name, email or telephone',
176
+ chosen: 'Chosen',
177
+ remove: 'Remove',
178
+ noMatches: 'No matches.',
179
+ filterDeals: 'Filter deals',
180
+ allOutcomes: 'All outcomes',
181
+ anyone: 'Anyone',
182
+ allCompanies: 'All companies',
183
+ clearFilters: 'Clear filters',
184
+ sortBy: 'Sort by',
185
+ orderWeighted: 'Weighted',
186
+ orderSize: 'Deal size',
187
+ orderUrgency: 'Urgency',
188
+ dealsShown: 'deals shown',
189
+ remitter: 'Remitter: who pays',
190
+ remitterCompany: 'Company that pays',
191
+ remitterPerson: 'Person who pays',
192
+ recipient: 'Recipient: who is paid',
193
+ recipientCompany: 'Company paid',
194
+ recipientPerson: 'Person who is paid',
195
+ recipientHint: 'Usually your company, or one of its companies; another company for an invoice you pay.',
196
+ remitterRequired: 'Choose the company or person who pays.',
197
+ recipientRequired: 'Choose the company or person who is paid.',
198
+ searchPeople: 'Search people by name, email or telephone',
199
+ noPerson: 'No person',
200
+ line: 'Line',
201
+ lineDetails: 'All fields',
202
+ itemCode: 'Item code',
203
+ discountRate: 'Discount (%)',
204
+ taxability: 'Tax treatment',
205
+ taxName: 'Tax name',
206
+ price: 'Price',
207
+ done: 'Done',
208
+ invalidDiscount: 'Enter a discount from 0 to 100.',
209
+ costs: 'Costs',
210
+ costsHint: 'Invoices you pay, issued in the period',
211
+ toPay: 'To pay',
212
+ party: 'Customer or supplier',
213
+ paidBy: 'Paid by',
214
+ paidTo: 'Paid to',
215
+ allKinds: 'Sales and costs',
216
+ salesOnly: 'Sales',
217
+ costsOnly: 'Costs',
218
+ cost: 'Cost',
219
+ drafts: 'Drafts',
220
+ draftsHint: 'Not yet sent, so not counted above',
221
+ draftInvoice: 'draft invoice',
222
+ draftInvoices: 'draft invoices',
223
+ period: 'Period',
224
+ periodLast30: 'Last 30 days',
225
+ periodQuarter: 'This quarter',
226
+ periodYear: 'This year',
227
+ } as const;
228
+
229
+ export type CrmText = { readonly [K in keyof typeof CRM_TEXT]: string };
230
+
231
+ /** The default words with a host's replacements. */
232
+ export const withText = (text?: Partial<CrmText>): CrmText => (text ? { ...CRM_TEXT, ...text } : CRM_TEXT);
@@ -0,0 +1,6 @@
1
+ shadcn/ui primitives, generated from the shadcn registry (`npx shadcn@4.21.0 add …`, style `radix-nova`, base colour
2
+ neutral, lucide icons) and kept as generated, apart from relative imports between them. Regenerate rather than edit,
3
+ and keep task-specific styling in the components one level up.
4
+
5
+ The colours come from the app's theme (the shadcn CSS variables: `--primary`, `--muted`, …), so the components follow
6
+ whatever theme the host app defines.