@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.
- package/LICENSE +201 -0
- package/README.md +74 -2
- package/dist/account.d.ts +36 -0
- package/dist/account.d.ts.map +1 -0
- package/dist/account.js +48 -0
- package/dist/account.js.map +1 -0
- package/dist/backend.d.ts +104 -0
- package/dist/backend.d.ts.map +1 -0
- package/dist/backend.js +328 -0
- package/dist/backend.js.map +1 -0
- package/dist/crm-client.d.ts +164 -0
- package/dist/crm-client.d.ts.map +1 -0
- package/dist/crm-client.js +252 -0
- package/dist/crm-client.js.map +1 -0
- package/dist/deal-order.d.ts +42 -0
- package/dist/deal-order.d.ts.map +1 -0
- package/dist/deal-order.js +80 -0
- package/dist/deal-order.js.map +1 -0
- package/dist/errors.d.ts +21 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +33 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/invoices.d.ts +53 -0
- package/dist/invoices.d.ts.map +1 -0
- package/dist/invoices.js +155 -0
- package/dist/invoices.js.map +1 -0
- package/dist/leads.d.ts +29 -0
- package/dist/leads.d.ts.map +1 -0
- package/dist/leads.js +56 -0
- package/dist/leads.js.map +1 -0
- package/dist/mock/index.d.ts +43 -0
- package/dist/mock/index.d.ts.map +1 -0
- package/dist/mock/index.js +303 -0
- package/dist/mock/index.js.map +1 -0
- package/dist/money.d.ts +29 -0
- package/dist/money.d.ts.map +1 -0
- package/dist/money.js +82 -0
- package/dist/money.js.map +1 -0
- package/dist/period.d.ts +30 -0
- package/dist/period.d.ts.map +1 -0
- package/dist/period.js +49 -0
- package/dist/period.js.map +1 -0
- package/dist/pipeline.d.ts +36 -0
- package/dist/pipeline.d.ts.map +1 -0
- package/dist/pipeline.js +58 -0
- package/dist/pipeline.js.map +1 -0
- package/dist/react/Charts.d.ts +19 -0
- package/dist/react/Charts.d.ts.map +1 -0
- package/dist/react/Charts.js +38 -0
- package/dist/react/Charts.js.map +1 -0
- package/dist/react/ChoiceCombobox.d.ts +36 -0
- package/dist/react/ChoiceCombobox.d.ts.map +1 -0
- package/dist/react/ChoiceCombobox.js +105 -0
- package/dist/react/ChoiceCombobox.js.map +1 -0
- package/dist/react/CompanyAccountView.d.ts +21 -0
- package/dist/react/CompanyAccountView.d.ts.map +1 -0
- package/dist/react/CompanyAccountView.js +27 -0
- package/dist/react/CompanyAccountView.js.map +1 -0
- package/dist/react/DashboardCards.d.ts +32 -0
- package/dist/react/DashboardCards.d.ts.map +1 -0
- package/dist/react/DashboardCards.js +38 -0
- package/dist/react/DashboardCards.js.map +1 -0
- package/dist/react/DealsBoard.d.ts +49 -0
- package/dist/react/DealsBoard.d.ts.map +1 -0
- package/dist/react/DealsBoard.js +82 -0
- package/dist/react/DealsBoard.js.map +1 -0
- package/dist/react/Details.d.ts +85 -0
- package/dist/react/Details.d.ts.map +1 -0
- package/dist/react/Details.js +268 -0
- package/dist/react/Details.js.map +1 -0
- package/dist/react/Forms.d.ts +190 -0
- package/dist/react/Forms.d.ts.map +1 -0
- package/dist/react/Forms.js +494 -0
- package/dist/react/Forms.js.map +1 -0
- package/dist/react/LeadsTable.d.ts +22 -0
- package/dist/react/LeadsTable.d.ts.map +1 -0
- package/dist/react/LeadsTable.js +79 -0
- package/dist/react/LeadsTable.js.map +1 -0
- package/dist/react/Sales.d.ts +68 -0
- package/dist/react/Sales.d.ts.map +1 -0
- package/dist/react/Sales.js +270 -0
- package/dist/react/Sales.js.map +1 -0
- package/dist/react/SidePanel.d.ts +31 -0
- package/dist/react/SidePanel.d.ts.map +1 -0
- package/dist/react/SidePanel.js +49 -0
- package/dist/react/SidePanel.js.map +1 -0
- package/dist/react/format.d.ts +28 -0
- package/dist/react/format.d.ts.map +1 -0
- package/dist/react/format.js +30 -0
- package/dist/react/format.js.map +1 -0
- package/dist/react/index.d.ts +15 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +15 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/parts.d.ts +55 -0
- package/dist/react/parts.d.ts.map +1 -0
- package/dist/react/parts.js +82 -0
- package/dist/react/parts.js.map +1 -0
- package/dist/react/pipeline-choice.d.ts +19 -0
- package/dist/react/pipeline-choice.d.ts.map +1 -0
- package/dist/react/pipeline-choice.js +39 -0
- package/dist/react/pipeline-choice.js.map +1 -0
- package/dist/react/text.d.ts +232 -0
- package/dist/react/text.d.ts.map +1 -0
- package/dist/react/text.js +229 -0
- package/dist/react/text.js.map +1 -0
- package/dist/react/ui/alert.d.ts +11 -0
- package/dist/react/ui/alert.d.ts.map +1 -0
- package/dist/react/ui/alert.js +29 -0
- package/dist/react/ui/alert.js.map +1 -0
- package/dist/react/ui/badge.d.ts +10 -0
- package/dist/react/ui/badge.d.ts.map +1 -0
- package/dist/react/ui/badge.js +26 -0
- package/dist/react/ui/badge.js.map +1 -0
- package/dist/react/ui/button.d.ts +11 -0
- package/dist/react/ui/button.d.ts.map +1 -0
- package/dist/react/ui/button.js +37 -0
- package/dist/react/ui/button.js.map +1 -0
- package/dist/react/ui/card.d.ts +12 -0
- package/dist/react/ui/card.d.ts.map +1 -0
- package/dist/react/ui/card.js +26 -0
- package/dist/react/ui/card.js.map +1 -0
- package/dist/react/ui/chart.d.ts +45 -0
- package/dist/react/ui/chart.d.ts.map +1 -0
- package/dist/react/ui/chart.js +116 -0
- package/dist/react/ui/chart.js.map +1 -0
- package/dist/react/ui/input.d.ts +4 -0
- package/dist/react/ui/input.d.ts.map +1 -0
- package/dist/react/ui/input.js +8 -0
- package/dist/react/ui/input.js.map +1 -0
- package/dist/react/ui/label.d.ts +5 -0
- package/dist/react/ui/label.d.ts.map +1 -0
- package/dist/react/ui/label.js +10 -0
- package/dist/react/ui/label.js.map +1 -0
- package/dist/react/ui/progress.d.ts +5 -0
- package/dist/react/ui/progress.d.ts.map +1 -0
- package/dist/react/ui/progress.js +10 -0
- package/dist/react/ui/progress.js.map +1 -0
- package/dist/react/ui/select.d.ts +16 -0
- package/dist/react/ui/select.d.ts.map +1 -0
- package/dist/react/ui/select.js +37 -0
- package/dist/react/ui/select.js.map +1 -0
- package/dist/react/ui/separator.d.ts +5 -0
- package/dist/react/ui/separator.d.ts.map +1 -0
- package/dist/react/ui/separator.js +10 -0
- package/dist/react/ui/separator.js.map +1 -0
- package/dist/react/ui/sheet.d.ts +15 -0
- package/dist/react/ui/sheet.d.ts.map +1 -0
- package/dist/react/ui/sheet.js +38 -0
- package/dist/react/ui/sheet.js.map +1 -0
- package/dist/react/ui/skeleton.d.ts +3 -0
- package/dist/react/ui/skeleton.d.ts.map +1 -0
- package/dist/react/ui/skeleton.js +7 -0
- package/dist/react/ui/skeleton.js.map +1 -0
- package/dist/react/ui/table.d.ts +11 -0
- package/dist/react/ui/table.d.ts.map +1 -0
- package/dist/react/ui/table.js +29 -0
- package/dist/react/ui/table.js.map +1 -0
- package/dist/react/ui/tabs.d.ts +12 -0
- package/dist/react/ui/tabs.d.ts.map +1 -0
- package/dist/react/ui/tabs.js +31 -0
- package/dist/react/ui/tabs.js.map +1 -0
- package/dist/react/views.d.ts +82 -0
- package/dist/react/views.d.ts.map +1 -0
- package/dist/react/views.js +241 -0
- package/dist/react/views.js.map +1 -0
- package/dist/sales.d.ts +82 -0
- package/dist/sales.d.ts.map +1 -0
- package/dist/sales.js +87 -0
- package/dist/sales.js.map +1 -0
- package/dist/store.d.ts +159 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +305 -0
- package/dist/store.js.map +1 -0
- package/dist/target.d.ts +22 -0
- package/dist/target.d.ts.map +1 -0
- package/dist/target.js +40 -0
- package/dist/target.js.map +1 -0
- package/dist/types.d.ts +204 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +100 -4
- package/src/account.ts +79 -0
- package/src/backend.ts +501 -0
- package/src/crm-client.ts +393 -0
- package/src/deal-order.ts +116 -0
- package/src/errors.ts +34 -0
- package/src/index.ts +13 -0
- package/src/invoices.ts +177 -0
- package/src/leads.ts +78 -0
- package/src/mock/index.ts +398 -0
- package/src/money.ts +104 -0
- package/src/period.ts +71 -0
- package/src/pipeline.ts +82 -0
- package/src/react/Charts.tsx +143 -0
- package/src/react/ChoiceCombobox.tsx +227 -0
- package/src/react/CompanyAccountView.tsx +198 -0
- package/src/react/DashboardCards.tsx +171 -0
- package/src/react/DealsBoard.tsx +344 -0
- package/src/react/Details.tsx +870 -0
- package/src/react/Forms.tsx +1486 -0
- package/src/react/LeadsTable.tsx +234 -0
- package/src/react/Sales.tsx +827 -0
- package/src/react/SidePanel.tsx +104 -0
- package/src/react/format.ts +53 -0
- package/src/react/index.ts +14 -0
- package/src/react/parts.tsx +183 -0
- package/src/react/pipeline-choice.ts +54 -0
- package/src/react/text.ts +232 -0
- package/src/react/ui/README.md +6 -0
- package/src/react/ui/alert.tsx +75 -0
- package/src/react/ui/badge.tsx +48 -0
- package/src/react/ui/button.tsx +66 -0
- package/src/react/ui/card.tsx +102 -0
- package/src/react/ui/chart.tsx +304 -0
- package/src/react/ui/input.tsx +18 -0
- package/src/react/ui/label.tsx +23 -0
- package/src/react/ui/progress.tsx +30 -0
- package/src/react/ui/select.tsx +189 -0
- package/src/react/ui/separator.tsx +27 -0
- package/src/react/ui/sheet.tsx +145 -0
- package/src/react/ui/skeleton.tsx +13 -0
- package/src/react/ui/table.tsx +113 -0
- package/src/react/ui/tabs.tsx +89 -0
- package/src/react/views.tsx +633 -0
- package/src/sales.ts +167 -0
- package/src/store.ts +528 -0
- package/src/target.ts +54 -0
- package/src/types.ts +230 -0
|
@@ -0,0 +1,633 @@
|
|
|
1
|
+
import { Plus } from 'lucide-react';
|
|
2
|
+
import { useEffect, useId, useMemo, useState, useSyncExternalStore, type ReactNode } from 'react';
|
|
3
|
+
import { ref } from '@baack-software/baack-ts-tasks';
|
|
4
|
+
import { DEAL_ORDERS, filterDeals, type DealFilter, type DealOrder } from '../deal-order.ts';
|
|
5
|
+
import { periodRange, type DateRange, type ReportingPeriod } from '../period.ts';
|
|
6
|
+
import { pipelineOf } from '../pipeline.ts';
|
|
7
|
+
import { selectAccount, selectDashboard, selectSales, type CrmStore, type CrmStoreState } from '../store.ts';
|
|
8
|
+
import type { CrmConcept, Deal, DealOutcome, Invoice, Lead, PipelineStage } from '../types.ts';
|
|
9
|
+
import { LeadsBySourceCard, RevenueChart } from './Charts.tsx';
|
|
10
|
+
import { MetricCards, PipelineCard, TargetCard } from './DashboardCards.tsx';
|
|
11
|
+
import { DealFilters, DealsBoard } from './DealsBoard.tsx';
|
|
12
|
+
import { DealDetail, LeadDetail, type ContactDetailsAccess } from './Details.tsx';
|
|
13
|
+
import { CompanyAccountView } from './CompanyAccountView.tsx';
|
|
14
|
+
import type { CrmNames } from './format.ts';
|
|
15
|
+
import { LeadsTable } from './LeadsTable.tsx';
|
|
16
|
+
import {
|
|
17
|
+
InvoiceDetail,
|
|
18
|
+
InvoicesTable,
|
|
19
|
+
OutstandingByAgeCard,
|
|
20
|
+
PaymentMethodsCard,
|
|
21
|
+
SalesMetricCards,
|
|
22
|
+
invoiceTitle,
|
|
23
|
+
} from './Sales.tsx';
|
|
24
|
+
import {
|
|
25
|
+
NewDealForm,
|
|
26
|
+
NewInvoiceForm,
|
|
27
|
+
NewLeadForm,
|
|
28
|
+
type CrmChoice,
|
|
29
|
+
type CrmCompanyChoice,
|
|
30
|
+
type CrmPersonChoice,
|
|
31
|
+
} from './Forms.tsx';
|
|
32
|
+
import { recallDeals, rememberDeals, usePipeline } from './pipeline-choice.ts';
|
|
33
|
+
import { SidePanelLayout, recordItemId } from './SidePanel.tsx';
|
|
34
|
+
import { PeriodSelect, SampleDataNotice, periodText } from './parts.tsx';
|
|
35
|
+
import { withText, type CrmText } from './text.ts';
|
|
36
|
+
import { Alert, AlertDescription, AlertTitle } from './ui/alert.tsx';
|
|
37
|
+
import { Label } from './ui/label.tsx';
|
|
38
|
+
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './ui/select.tsx';
|
|
39
|
+
import { Button } from './ui/button.tsx';
|
|
40
|
+
import { Skeleton } from './ui/skeleton.tsx';
|
|
41
|
+
|
|
42
|
+
/** The store's state, re-rendering on every change. */
|
|
43
|
+
export function useCrmStore(store: CrmStore): CrmStoreState {
|
|
44
|
+
return useSyncExternalStore(store.subscribe, store.getState, store.getState);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The host's names, falling back to the companies the store made (a converted lead's prospect) for theirs. */
|
|
48
|
+
export function useCrmNames(names: CrmNames | undefined, state: CrmStoreState): CrmNames {
|
|
49
|
+
const { companies, people } = state;
|
|
50
|
+
return useMemo(
|
|
51
|
+
() => ({
|
|
52
|
+
...names,
|
|
53
|
+
company: (urn: string) => names?.company?.(urn) ?? companies.find((company) => company.urn === urn)?.name,
|
|
54
|
+
person: (urn: string) => names?.person?.(urn) ?? people.find((person) => person.urn === urn)?.name,
|
|
55
|
+
}),
|
|
56
|
+
[names, companies, people],
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The store's reading and setting of people's email and telephone, kept stable so the fields don't reload. */
|
|
61
|
+
const useContactDetails = (store: CrmStore): ContactDetailsAccess =>
|
|
62
|
+
useMemo(
|
|
63
|
+
() => ({
|
|
64
|
+
load: (identity) => store.contactDetails(identity),
|
|
65
|
+
save: (identity, details) => store.setContactDetails(identity, details),
|
|
66
|
+
}),
|
|
67
|
+
[store],
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
/** Choices with the records the store made while open, such as a lead's new contact, added where they're missing. */
|
|
71
|
+
const withMade = (choices: readonly CrmChoice[], made: readonly CrmChoice[]): readonly CrmChoice[] => [
|
|
72
|
+
...choices,
|
|
73
|
+
...made.filter((record) => !choices.some((choice) => choice.urn === record.urn)),
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
const CONCEPT_NAMES: Record<CrmConcept, string> = {
|
|
77
|
+
leads: 'Leads',
|
|
78
|
+
deals: 'Deals',
|
|
79
|
+
pipelines: 'Pipelines',
|
|
80
|
+
invoices: 'Invoices',
|
|
81
|
+
remittances: 'Payments',
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/** The notice naming which of a view's concepts come from sample data. */
|
|
85
|
+
function Sample({
|
|
86
|
+
store,
|
|
87
|
+
concepts,
|
|
88
|
+
text,
|
|
89
|
+
}: {
|
|
90
|
+
store: CrmStore;
|
|
91
|
+
concepts: CrmConcept[];
|
|
92
|
+
text?: Partial<CrmText> | undefined;
|
|
93
|
+
}) {
|
|
94
|
+
const sample = concepts.filter((concept) => store.client.isSample(concept)).map((concept) => CONCEPT_NAMES[concept]);
|
|
95
|
+
return <SampleDataNotice concepts={sample} text={text} />;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function Loading({ label }: { label: string }) {
|
|
99
|
+
return (
|
|
100
|
+
<div role="status" aria-label={label} className="grid gap-4 sm:grid-cols-2 xl:grid-cols-4">
|
|
101
|
+
{Array.from({ length: 4 }, (_, n) => (
|
|
102
|
+
<Skeleton key={n} className="h-28 rounded-xl" />
|
|
103
|
+
))}
|
|
104
|
+
</div>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Says which concepts couldn't be loaded, and why: not in the API yet, or an error. */
|
|
109
|
+
function Problems({ state, concepts, text }: { state: CrmStoreState; concepts: CrmConcept[]; text: CrmText }) {
|
|
110
|
+
const problems = concepts.filter((concept) => ['unavailable', 'error'].includes(state.concepts[concept].status));
|
|
111
|
+
if (problems.length === 0) return null;
|
|
112
|
+
return (
|
|
113
|
+
<Alert>
|
|
114
|
+
<AlertTitle>{text.unavailable}</AlertTitle>
|
|
115
|
+
<AlertDescription>
|
|
116
|
+
{problems.map((concept) => {
|
|
117
|
+
const { status, error } = state.concepts[concept];
|
|
118
|
+
return (
|
|
119
|
+
<p key={concept}>
|
|
120
|
+
{CONCEPT_NAMES[concept]}:{' '}
|
|
121
|
+
{status === 'unavailable' ? text.sampleDataHint : error instanceof Error ? error.message : String(error)}
|
|
122
|
+
</p>
|
|
123
|
+
);
|
|
124
|
+
})}
|
|
125
|
+
</AlertDescription>
|
|
126
|
+
</Alert>
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
interface ViewProps {
|
|
131
|
+
store: CrmStore;
|
|
132
|
+
names?: CrmNames | undefined;
|
|
133
|
+
text?: Partial<CrmText> | undefined;
|
|
134
|
+
/** The companies new records can be linked to: the host's customers, named. */
|
|
135
|
+
companies?: readonly CrmCompanyChoice[] | undefined;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** The currency new records start in: the one the company's records use, else GBP. */
|
|
139
|
+
const currencyOf = (state: CrmStoreState): string =>
|
|
140
|
+
state.invoices[0]?.currencyCode ??
|
|
141
|
+
[...state.deals, ...state.leads].find((record) => record.targetValue)?.targetValue?.currencyCode ??
|
|
142
|
+
'GBP';
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* "New ...": opens the form in the side panel, in place of a record. Its id is the panel's, so closing the form
|
|
146
|
+
* returns focus to it.
|
|
147
|
+
*/
|
|
148
|
+
function NewButton({ panel, label, onClick }: { panel: string; label: string; onClick: () => void }) {
|
|
149
|
+
return (
|
|
150
|
+
<div className="flex justify-end">
|
|
151
|
+
<Button id={recordItemId(panel)} onClick={onClick}>
|
|
152
|
+
<Plus aria-hidden="true" />
|
|
153
|
+
{label}
|
|
154
|
+
</Button>
|
|
155
|
+
</div>
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The CRM dashboard for a period: `range`, else the one picked (the last 30 days to start with), with figures against
|
|
161
|
+
* the period before, the target, leads by source, the pipeline, the host's tasks panel and the leads table.
|
|
162
|
+
*/
|
|
163
|
+
export function CrmDashboard({
|
|
164
|
+
store,
|
|
165
|
+
names,
|
|
166
|
+
text,
|
|
167
|
+
range,
|
|
168
|
+
ownCompanies,
|
|
169
|
+
tasks,
|
|
170
|
+
onOpenLead,
|
|
171
|
+
}: ViewProps & {
|
|
172
|
+
range?: DateRange | undefined;
|
|
173
|
+
/** The companies the business counts as itself (the owner and its child companies), to leave its costs out. */
|
|
174
|
+
ownCompanies?: readonly string[] | undefined;
|
|
175
|
+
/** The host's tasks panel, such as baack-ts-tasks' TasksAbout. */
|
|
176
|
+
tasks?: ReactNode;
|
|
177
|
+
onOpenLead?: ((lead: Lead) => void) | undefined;
|
|
178
|
+
}) {
|
|
179
|
+
const state = useCrmStore(store);
|
|
180
|
+
names = useCrmNames(names, state);
|
|
181
|
+
const words = withText(text);
|
|
182
|
+
const [picked, setPicked] = useState<ReportingPeriod>('last30');
|
|
183
|
+
const period = useMemo(() => range ?? periodRange(picked), [range, picked]);
|
|
184
|
+
const ownKey = ownCompanies?.join(',');
|
|
185
|
+
const [pipeline] = usePipeline(ref(store.options.owner).urn, state.pipelines, state.deals);
|
|
186
|
+
const dashboard = useMemo(
|
|
187
|
+
() => selectDashboard(state, period, ownKey === undefined ? undefined : new Set(ownKey.split(',')), pipeline?.urn),
|
|
188
|
+
[state, period, ownKey, pipeline?.urn],
|
|
189
|
+
);
|
|
190
|
+
if (state.status === 'idle' || state.status === 'loading') return <Loading label={words.loading} />;
|
|
191
|
+
const concepts: CrmConcept[] = ['leads', 'deals', 'pipelines', 'remittances'];
|
|
192
|
+
return (
|
|
193
|
+
<div className="grid gap-6">
|
|
194
|
+
<Sample store={store} concepts={concepts} text={text} />
|
|
195
|
+
<Problems state={state} concepts={concepts} text={words} />
|
|
196
|
+
{!range && <PeriodSelect value={picked} onChange={setPicked} text={text} />}
|
|
197
|
+
<MetricCards dashboard={dashboard} text={range ? text : periodText(picked, text)} />
|
|
198
|
+
<RevenueChart days={dashboard.revenueByDay} currencyCode={dashboard.revenue.currencyCode} text={text} />
|
|
199
|
+
<div className={tasks ? 'grid items-start gap-6 lg:grid-cols-3' : 'grid items-start gap-6 lg:grid-cols-2'}>
|
|
200
|
+
<TargetCard target={dashboard.target} text={text} />
|
|
201
|
+
<LeadsBySourceCard counts={dashboard.leadsBySource} text={text} />
|
|
202
|
+
{tasks ?? null}
|
|
203
|
+
</div>
|
|
204
|
+
<PipelineCard stages={dashboard.pipeline} text={text} />
|
|
205
|
+
<LeadsTable leads={state.leads} names={names} onOpen={onOpenLead} text={text} />
|
|
206
|
+
</div>
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Leads in a table; the opened lead shows to the right, as tasks do, where it can be converted into a deal. */
|
|
211
|
+
export function CrmLeadsView({
|
|
212
|
+
store,
|
|
213
|
+
names,
|
|
214
|
+
text,
|
|
215
|
+
companies,
|
|
216
|
+
people = [],
|
|
217
|
+
team = [],
|
|
218
|
+
selected,
|
|
219
|
+
onSelect,
|
|
220
|
+
onConverted,
|
|
221
|
+
}: ViewProps & {
|
|
222
|
+
/** People a lead's contact can be chosen from: the host's customers' people. */
|
|
223
|
+
people?: readonly CrmPersonChoice[] | undefined;
|
|
224
|
+
/** People a lead can be assigned to: the host's team. */
|
|
225
|
+
team?: readonly CrmPersonChoice[] | undefined;
|
|
226
|
+
selected?: string | undefined;
|
|
227
|
+
onSelect?: ((lead: Lead | undefined) => void) | undefined;
|
|
228
|
+
/** After a lead becomes a deal, for example to open the deal. */
|
|
229
|
+
onConverted?: ((deal: Deal) => void) | undefined;
|
|
230
|
+
}) {
|
|
231
|
+
const state = useCrmStore(store);
|
|
232
|
+
names = useCrmNames(names, state);
|
|
233
|
+
const words = withText(text);
|
|
234
|
+
const contacts = withMade(people, state.people);
|
|
235
|
+
const contactDetails = useContactDetails(store);
|
|
236
|
+
const customers = withMade(
|
|
237
|
+
companies ?? [],
|
|
238
|
+
state.companies.map((made) => ({ urn: made.urn, name: made.name ?? words.unnamed })),
|
|
239
|
+
);
|
|
240
|
+
const [own, setOwn] = useState<string>();
|
|
241
|
+
const urn = onSelect ? selected : (own ?? selected);
|
|
242
|
+
const lead = state.leads.find((candidate) => candidate.urn === urn);
|
|
243
|
+
const [pipeline] = usePipeline(ref(store.options.owner).urn, state.pipelines, state.deals);
|
|
244
|
+
const [creating, setCreating] = useState(false);
|
|
245
|
+
const select = (next: Lead | undefined) => {
|
|
246
|
+
setCreating(false);
|
|
247
|
+
if (onSelect) onSelect(next);
|
|
248
|
+
else setOwn(next?.urn);
|
|
249
|
+
};
|
|
250
|
+
if (state.status === 'idle' || state.status === 'loading') return <Loading label={words.loading} />;
|
|
251
|
+
return (
|
|
252
|
+
<div className="grid gap-6">
|
|
253
|
+
<Sample store={store} concepts={['leads']} text={text} />
|
|
254
|
+
<Problems state={state} concepts={['leads']} text={words} />
|
|
255
|
+
<NewButton panel="new-lead" label={words.newLead} onClick={() => setCreating(true)} />
|
|
256
|
+
<SidePanelLayout
|
|
257
|
+
open={creating ? 'new-lead' : lead?.urn}
|
|
258
|
+
title={creating ? words.newLead : (lead?.title ?? words.lead)}
|
|
259
|
+
onClose={() => select(undefined)}
|
|
260
|
+
text={text}
|
|
261
|
+
panel={
|
|
262
|
+
creating ? (
|
|
263
|
+
<NewLeadForm
|
|
264
|
+
owner={store.options.owner}
|
|
265
|
+
companies={customers}
|
|
266
|
+
people={contacts}
|
|
267
|
+
team={team}
|
|
268
|
+
currencyCode={currencyOf(state)}
|
|
269
|
+
text={text}
|
|
270
|
+
onCreate={async (input, options) => select(await store.createLead(input, options))}
|
|
271
|
+
/>
|
|
272
|
+
) : (
|
|
273
|
+
lead && (
|
|
274
|
+
<LeadDetail
|
|
275
|
+
key={lead.urn}
|
|
276
|
+
lead={lead}
|
|
277
|
+
names={names}
|
|
278
|
+
companies={customers}
|
|
279
|
+
people={contacts}
|
|
280
|
+
team={team}
|
|
281
|
+
onChange={(changes, options) => store.updateLead(lead, changes, options)}
|
|
282
|
+
text={text}
|
|
283
|
+
converting={state.saving.has(lead.urn)}
|
|
284
|
+
contactDetails={contactDetails}
|
|
285
|
+
onSetOutcome={(closing, outcome, closedReason) =>
|
|
286
|
+
void store
|
|
287
|
+
.setLeadOutcome(closing, outcome, closedReason === undefined ? {} : { closedReason })
|
|
288
|
+
.catch(() => undefined)
|
|
289
|
+
}
|
|
290
|
+
onConvert={
|
|
291
|
+
pipeline
|
|
292
|
+
? async (open, choice) => {
|
|
293
|
+
// A refused conversion is rolled back and kept as the store's writeError.
|
|
294
|
+
const result = await store.convertLead(open, { pipeline, ...choice }).catch(() => undefined);
|
|
295
|
+
if (result) onConverted?.(result.deal);
|
|
296
|
+
}
|
|
297
|
+
: undefined
|
|
298
|
+
}
|
|
299
|
+
/>
|
|
300
|
+
)
|
|
301
|
+
)
|
|
302
|
+
}
|
|
303
|
+
>
|
|
304
|
+
<LeadsTable leads={state.leads} names={names} onOpen={select} selected={lead?.urn} text={text} />
|
|
305
|
+
</SidePanelLayout>
|
|
306
|
+
</div>
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** The deals board's own part of what's remembered (see `rememberDeals`): its order. The workflow is the shared choice. */
|
|
311
|
+
interface DealsBoardMemory {
|
|
312
|
+
order?: DealOrder | undefined;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function recall(owner: string): DealsBoardMemory {
|
|
316
|
+
const saved = recallDeals(owner).order;
|
|
317
|
+
return { order: DEAL_ORDERS.find((order) => order === saved) };
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Deals as a board, one workflow at a time: the one this user last chose, else the one with the most deals (see
|
|
322
|
+
* `usePipeline`). Filters
|
|
323
|
+
* and the columns' order sit above it; the opened deal shows to the right, as tasks do, and the board moves to its
|
|
324
|
+
* workflow.
|
|
325
|
+
*/
|
|
326
|
+
export function CrmDealsView({
|
|
327
|
+
store,
|
|
328
|
+
names,
|
|
329
|
+
text,
|
|
330
|
+
companies,
|
|
331
|
+
people = [],
|
|
332
|
+
team = [],
|
|
333
|
+
selected,
|
|
334
|
+
onSelect,
|
|
335
|
+
}: ViewProps & {
|
|
336
|
+
/** People a deal's contact can be chosen from: the host's customers' people. */
|
|
337
|
+
people?: readonly CrmPersonChoice[] | undefined;
|
|
338
|
+
/** People a deal can be assigned to: the host's team. */
|
|
339
|
+
team?: readonly CrmPersonChoice[] | undefined;
|
|
340
|
+
selected?: string | undefined;
|
|
341
|
+
onSelect?: ((deal: Deal | undefined) => void) | undefined;
|
|
342
|
+
}) {
|
|
343
|
+
const state = useCrmStore(store);
|
|
344
|
+
names = useCrmNames(names, state);
|
|
345
|
+
const words = withText(text);
|
|
346
|
+
const ids = useId();
|
|
347
|
+
const [own, setOwn] = useState<string>();
|
|
348
|
+
const urn = onSelect ? selected : (own ?? selected);
|
|
349
|
+
const deal = state.deals.find((candidate) => candidate.urn === urn);
|
|
350
|
+
const owner = ref(store.options.owner).urn;
|
|
351
|
+
const [memory, setMemory] = useState<DealsBoardMemory>(() => recall(owner));
|
|
352
|
+
const update = (changes: DealsBoardMemory) =>
|
|
353
|
+
setMemory((current) => {
|
|
354
|
+
const next = { ...current, ...changes };
|
|
355
|
+
rememberDeals(owner, next);
|
|
356
|
+
return next;
|
|
357
|
+
});
|
|
358
|
+
const [shown, setShown] = usePipeline(owner, state.pipelines, state.deals);
|
|
359
|
+
const order = memory.order ?? 'weighted';
|
|
360
|
+
const [filter, setFilter] = useState<DealFilter>({});
|
|
361
|
+
// An opened deal in another workflow, such as one followed from a customer's page, brings the board to it.
|
|
362
|
+
const opened = deal && pipelineOf(deal, state.pipelines)?.urn;
|
|
363
|
+
useEffect(() => {
|
|
364
|
+
if (opened) setShown(opened);
|
|
365
|
+
// setShown is stable in effect: it only sets state and storage.
|
|
366
|
+
}, [opened]);
|
|
367
|
+
const pipeline = shown;
|
|
368
|
+
const visible = useMemo(
|
|
369
|
+
() => filterDeals(state.deals, filter, (urn) => names?.company?.(urn) ?? names?.person?.(urn)),
|
|
370
|
+
[state.deals, filter, names],
|
|
371
|
+
);
|
|
372
|
+
const contacts = withMade(people, state.people);
|
|
373
|
+
const contactDetails = useContactDetails(store);
|
|
374
|
+
const customers = withMade(
|
|
375
|
+
companies ?? [],
|
|
376
|
+
state.companies.map((made) => ({ urn: made.urn, name: made.name ?? words.unnamed })),
|
|
377
|
+
);
|
|
378
|
+
const [creating, setCreating] = useState(false);
|
|
379
|
+
const select = (next: Deal | undefined) => {
|
|
380
|
+
setCreating(false);
|
|
381
|
+
if (onSelect) onSelect(next);
|
|
382
|
+
else setOwn(next?.urn);
|
|
383
|
+
};
|
|
384
|
+
const move = (moving: Deal, stage: PipelineStage) => void store.moveDeal(moving, stage).catch(() => undefined);
|
|
385
|
+
const setOutcome = (closing: Deal, outcome: DealOutcome, lostReason?: string) =>
|
|
386
|
+
void store.setOutcome(closing, outcome, lostReason === undefined ? {} : { lostReason }).catch(() => undefined);
|
|
387
|
+
if (state.status === 'idle' || state.status === 'loading') return <Loading label={words.loading} />;
|
|
388
|
+
return (
|
|
389
|
+
<div className="grid gap-6">
|
|
390
|
+
<Sample store={store} concepts={['deals', 'pipelines']} text={text} />
|
|
391
|
+
<Problems state={state} concepts={['deals', 'pipelines']} text={words} />
|
|
392
|
+
{pipeline && (
|
|
393
|
+
<div className="flex flex-wrap items-end justify-between gap-3">
|
|
394
|
+
{state.pipelines.length > 1 ? (
|
|
395
|
+
<div className="grid gap-1.5">
|
|
396
|
+
<Label htmlFor={`${ids}-workflow`}>{words.workflow}</Label>
|
|
397
|
+
<Select value={pipeline.urn} onValueChange={setShown}>
|
|
398
|
+
<SelectTrigger id={`${ids}-workflow`} className="w-56">
|
|
399
|
+
<SelectValue />
|
|
400
|
+
</SelectTrigger>
|
|
401
|
+
<SelectContent>
|
|
402
|
+
{state.pipelines.map((option) => (
|
|
403
|
+
<SelectItem key={option.urn} value={option.urn}>
|
|
404
|
+
{option.name}
|
|
405
|
+
</SelectItem>
|
|
406
|
+
))}
|
|
407
|
+
</SelectContent>
|
|
408
|
+
</Select>
|
|
409
|
+
</div>
|
|
410
|
+
) : (
|
|
411
|
+
<span />
|
|
412
|
+
)}
|
|
413
|
+
<NewButton panel="new-deal" label={words.newDeal} onClick={() => setCreating(true)} />
|
|
414
|
+
</div>
|
|
415
|
+
)}
|
|
416
|
+
{pipeline && (
|
|
417
|
+
<DealFilters
|
|
418
|
+
value={filter}
|
|
419
|
+
onChange={setFilter}
|
|
420
|
+
order={order}
|
|
421
|
+
onOrder={(next) => update({ order: next })}
|
|
422
|
+
team={team}
|
|
423
|
+
companies={customers}
|
|
424
|
+
shown={visible.filter((shownDeal) => pipelineOf(shownDeal, [pipeline])).length}
|
|
425
|
+
text={text}
|
|
426
|
+
/>
|
|
427
|
+
)}
|
|
428
|
+
{pipeline && (
|
|
429
|
+
<SidePanelLayout
|
|
430
|
+
open={creating ? 'new-deal' : deal?.urn}
|
|
431
|
+
title={creating ? words.newDeal : (deal?.title ?? words.deals)}
|
|
432
|
+
onClose={() => select(undefined)}
|
|
433
|
+
text={text}
|
|
434
|
+
panel={
|
|
435
|
+
creating ? (
|
|
436
|
+
<NewDealForm
|
|
437
|
+
// The workflow shown on the board first, so a new deal lands where it's being looked at.
|
|
438
|
+
owner={store.options.owner}
|
|
439
|
+
pipelines={[pipeline, ...state.pipelines.filter((other) => other.urn !== pipeline.urn)]}
|
|
440
|
+
companies={customers}
|
|
441
|
+
people={contacts}
|
|
442
|
+
team={team}
|
|
443
|
+
currencyCode={currencyOf(state)}
|
|
444
|
+
text={text}
|
|
445
|
+
onCreate={async (input) => select(await store.createDeal(input))}
|
|
446
|
+
/>
|
|
447
|
+
) : (
|
|
448
|
+
deal && (
|
|
449
|
+
<DealDetail
|
|
450
|
+
deal={deal}
|
|
451
|
+
pipelines={state.pipelines}
|
|
452
|
+
names={names}
|
|
453
|
+
companies={customers}
|
|
454
|
+
people={contacts}
|
|
455
|
+
team={team}
|
|
456
|
+
onMove={(moving, stage) => {
|
|
457
|
+
move(moving, stage);
|
|
458
|
+
// Moved to another workflow: the board follows it.
|
|
459
|
+
const to = state.pipelines.find((option) => option.stages.some((s) => s.urn === stage.urn));
|
|
460
|
+
if (to) setShown(to.urn);
|
|
461
|
+
}}
|
|
462
|
+
onSetOutcome={setOutcome}
|
|
463
|
+
onChange={(changes) => store.updateDeal(deal, changes)}
|
|
464
|
+
contactDetails={contactDetails}
|
|
465
|
+
saving={state.saving.has(deal.urn)}
|
|
466
|
+
text={text}
|
|
467
|
+
/>
|
|
468
|
+
)
|
|
469
|
+
)
|
|
470
|
+
}
|
|
471
|
+
>
|
|
472
|
+
<DealsBoard
|
|
473
|
+
deals={visible}
|
|
474
|
+
pipeline={pipeline}
|
|
475
|
+
order={order}
|
|
476
|
+
names={names}
|
|
477
|
+
onOpen={select}
|
|
478
|
+
onMove={move}
|
|
479
|
+
selected={deal?.urn}
|
|
480
|
+
saving={state.saving}
|
|
481
|
+
text={text}
|
|
482
|
+
/>
|
|
483
|
+
</SidePanelLayout>
|
|
484
|
+
)}
|
|
485
|
+
</div>
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Sales for a period (`range`, else the one picked, the last 30 days to start with): invoiced, received, outstanding,
|
|
491
|
+
* sales tax, costs and drafts against the period before, payments over time, outstanding by age, payments by method,
|
|
492
|
+
* and the invoices; the opened invoice shows to the right with its payments, where another can be recorded.
|
|
493
|
+
*/
|
|
494
|
+
export function CrmSalesView({
|
|
495
|
+
store,
|
|
496
|
+
names,
|
|
497
|
+
text,
|
|
498
|
+
companies,
|
|
499
|
+
people = [],
|
|
500
|
+
ownCompanies,
|
|
501
|
+
range,
|
|
502
|
+
selected,
|
|
503
|
+
onSelect,
|
|
504
|
+
}: ViewProps & {
|
|
505
|
+
/**
|
|
506
|
+
* The companies the business counts as itself (the owner and its child companies): invoices they pay are costs,
|
|
507
|
+
* apart from the sales figures. The owner alone when not given.
|
|
508
|
+
*/
|
|
509
|
+
ownCompanies?: readonly string[] | undefined;
|
|
510
|
+
/** People an invoice's remitter or recipient can be. */
|
|
511
|
+
people?: readonly CrmPersonChoice[] | undefined;
|
|
512
|
+
range?: DateRange | undefined;
|
|
513
|
+
selected?: string | undefined;
|
|
514
|
+
onSelect?: ((invoice: Invoice | undefined) => void) | undefined;
|
|
515
|
+
}) {
|
|
516
|
+
const state = useCrmStore(store);
|
|
517
|
+
names = useCrmNames(names, state);
|
|
518
|
+
const words = withText(text);
|
|
519
|
+
const [picked, setPicked] = useState<ReportingPeriod>('last30');
|
|
520
|
+
const period = useMemo(() => range ?? periodRange(picked), [range, picked]);
|
|
521
|
+
const ownKey = ownCompanies?.join(',');
|
|
522
|
+
const ownSet = useMemo(() => (ownKey === undefined ? undefined : new Set(ownKey.split(','))), [ownKey]);
|
|
523
|
+
const summary = useMemo(() => selectSales(state, period, new Date(), ownSet), [state, period, ownSet]);
|
|
524
|
+
const [own, setOwn] = useState<string>();
|
|
525
|
+
const urn = onSelect ? selected : (own ?? selected);
|
|
526
|
+
const invoice = summary.invoices.find((candidate) => candidate.urn === urn);
|
|
527
|
+
const [creating, setCreating] = useState(false);
|
|
528
|
+
// Either party of an invoice: the host's companies, with any the store made since.
|
|
529
|
+
const parties = withMade(
|
|
530
|
+
companies ?? [],
|
|
531
|
+
state.companies.map((made) => ({ urn: made.urn, name: made.name ?? words.unnamed })),
|
|
532
|
+
);
|
|
533
|
+
const select = (next: Invoice | undefined) => {
|
|
534
|
+
setCreating(false);
|
|
535
|
+
if (onSelect) onSelect(next);
|
|
536
|
+
else setOwn(next?.urn);
|
|
537
|
+
};
|
|
538
|
+
if (state.status === 'idle' || state.status === 'loading') return <Loading label={words.loading} />;
|
|
539
|
+
const concepts: CrmConcept[] = ['invoices', 'remittances'];
|
|
540
|
+
return (
|
|
541
|
+
<div className="grid gap-6">
|
|
542
|
+
<Sample store={store} concepts={concepts} text={text} />
|
|
543
|
+
<Problems state={state} concepts={concepts} text={words} />
|
|
544
|
+
{!range && <PeriodSelect value={picked} onChange={setPicked} text={text} />}
|
|
545
|
+
<SalesMetricCards summary={summary} text={range ? text : periodText(picked, text)} />
|
|
546
|
+
<RevenueChart days={summary.byDay} currencyCode={summary.currencyCode} text={text} />
|
|
547
|
+
<div className="grid items-start gap-6 lg:grid-cols-2">
|
|
548
|
+
<OutstandingByAgeCard summary={summary} text={text} />
|
|
549
|
+
<PaymentMethodsCard summary={summary} text={text} />
|
|
550
|
+
</div>
|
|
551
|
+
<NewButton panel="new-invoice" label={words.newInvoice} onClick={() => setCreating(true)} />
|
|
552
|
+
<SidePanelLayout
|
|
553
|
+
open={creating ? 'new-invoice' : invoice?.urn}
|
|
554
|
+
title={creating ? words.newInvoice : invoice ? invoiceTitle(invoice) : words.invoice}
|
|
555
|
+
onClose={() => select(undefined)}
|
|
556
|
+
text={text}
|
|
557
|
+
panel={
|
|
558
|
+
creating ? (
|
|
559
|
+
<NewInvoiceForm
|
|
560
|
+
owner={store.options.owner}
|
|
561
|
+
companies={parties}
|
|
562
|
+
people={withMade(people, state.people)}
|
|
563
|
+
deals={state.deals}
|
|
564
|
+
currencyCode={currencyOf(state)}
|
|
565
|
+
text={text}
|
|
566
|
+
onCreate={async (input, lines, options) => select(await store.createInvoice(input, lines, options))}
|
|
567
|
+
/>
|
|
568
|
+
) : (
|
|
569
|
+
invoice && (
|
|
570
|
+
<InvoiceDetail
|
|
571
|
+
invoice={invoice}
|
|
572
|
+
payments={summary.payments.filter((payment) => payment.paymentInvoice.urn === invoice.urn)}
|
|
573
|
+
names={names}
|
|
574
|
+
companies={parties}
|
|
575
|
+
people={withMade(people, state.people)}
|
|
576
|
+
onChange={(changes, options) => store.updateInvoice(invoice, changes, options)}
|
|
577
|
+
saving={state.saving.has(invoice.urn)}
|
|
578
|
+
onRecord={(input) => store.recordPayment(input)}
|
|
579
|
+
text={text}
|
|
580
|
+
/>
|
|
581
|
+
)
|
|
582
|
+
)
|
|
583
|
+
}
|
|
584
|
+
>
|
|
585
|
+
<InvoicesTable
|
|
586
|
+
invoices={summary.invoices}
|
|
587
|
+
names={names}
|
|
588
|
+
own={ownSet}
|
|
589
|
+
onOpen={select}
|
|
590
|
+
selected={invoice?.urn}
|
|
591
|
+
text={text}
|
|
592
|
+
/>
|
|
593
|
+
</SidePanelLayout>
|
|
594
|
+
</div>
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/** One customer company's account from the store, with the host's people and tasks panels. */
|
|
599
|
+
export function CrmCompanyAccount({
|
|
600
|
+
store,
|
|
601
|
+
text,
|
|
602
|
+
company,
|
|
603
|
+
people,
|
|
604
|
+
tasks,
|
|
605
|
+
onOpenDeal,
|
|
606
|
+
onOpenLead,
|
|
607
|
+
}: ViewProps & {
|
|
608
|
+
company: string;
|
|
609
|
+
people?: ReactNode;
|
|
610
|
+
tasks?: ReactNode;
|
|
611
|
+
onOpenDeal?: ((deal: Deal) => void) | undefined;
|
|
612
|
+
onOpenLead?: ((lead: Lead) => void) | undefined;
|
|
613
|
+
}) {
|
|
614
|
+
const state = useCrmStore(store);
|
|
615
|
+
const words = withText(text);
|
|
616
|
+
const account = useMemo(() => selectAccount(state, company), [state, company]);
|
|
617
|
+
if (state.status === 'idle' || state.status === 'loading') return <Loading label={words.loading} />;
|
|
618
|
+
return (
|
|
619
|
+
<div className="grid gap-6">
|
|
620
|
+
<Sample store={store} concepts={['leads', 'deals', 'invoices', 'remittances']} text={text} />
|
|
621
|
+
<Problems state={state} concepts={['leads', 'deals', 'invoices', 'remittances']} text={words} />
|
|
622
|
+
<CompanyAccountView
|
|
623
|
+
account={account}
|
|
624
|
+
pipelines={state.pipelines}
|
|
625
|
+
onOpenDeal={onOpenDeal}
|
|
626
|
+
onOpenLead={onOpenLead}
|
|
627
|
+
people={people}
|
|
628
|
+
tasks={tasks}
|
|
629
|
+
text={text}
|
|
630
|
+
/>
|
|
631
|
+
</div>
|
|
632
|
+
);
|
|
633
|
+
}
|