Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api-service): Usage insights email #7346

Draft
wants to merge 34 commits into
base: next
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9f7b9dc
feat: insights
scopsy Dec 4, 2024
5728669
feat: wip
scopsy Dec 4, 2024
6eb853e
fix: hello world
scopsy Dec 4, 2024
099fea9
fix: email
scopsy Dec 4, 2024
8fb84e3
fix: email style
scopsy Dec 4, 2024
60a85a3
fix: add marketing section
scopsy Dec 4, 2024
a49b625
fix: upload
scopsy Dec 4, 2024
d56c811
fix: logo
scopsy Dec 4, 2024
8a89c1b
Merge branch 'next' into insights-email
scopsy Dec 22, 2024
747dffd
fix: items
scopsy Dec 22, 2024
5ee4ad9
fix: workflows
scopsy Dec 22, 2024
6d7c867
fix: items
scopsy Dec 22, 2024
63ef939
fix: items
scopsy Dec 22, 2024
836c44f
fix: items
scopsy Dec 22, 2024
0dd5361
fix: refactor
scopsy Dec 22, 2024
9570eff
fix: review
scopsy Dec 22, 2024
2be2bf1
fix: items
scopsy Dec 22, 2024
5464b96
fix: items
scopsy Dec 22, 2024
2945749
fix: bugs
scopsy Dec 22, 2024
4c9bd12
fix: working state
scopsy Dec 22, 2024
3b4e5a9
feat: add controller
scopsy Dec 22, 2024
4a4f6bc
feat: add insights tester
scopsy Dec 22, 2024
88f0530
fix: mixpanel
scopsy Dec 22, 2024
510860a
fix: remove cache
scopsy Dec 22, 2024
efc2df7
fix: remove unused import
scopsy Dec 22, 2024
bd7e05f
fix: trigger
scopsy Dec 22, 2024
65c3d67
fix: empty state
scopsy Dec 22, 2024
9ea8c3c
fix: refactpr
scopsy Dec 22, 2024
69a10f8
fix: r emov unused
scopsy Dec 22, 2024
3173273
fix: validation
scopsy Dec 22, 2024
465427d
fix: remove pr info
scopsy Dec 22, 2024
2c39098
Merge branch 'next' into insights-email
scopsy Dec 22, 2024
ed1d443
Merge branch 'next' into insights-email
scopsy Dec 22, 2024
325762b
fix: import
scopsy Dec 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: items
  • Loading branch information
scopsy committed Dec 22, 2024
commit 6d7c8677f83f5f9ca96ccf46d3f009ac51181f76
77 changes: 48 additions & 29 deletions libs/notifications/src/workflows/usage-insights/email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ import {
} from '@react-email/components';
import { IUsageEmailData } from './types';

function formatDate(dateString: string) {
const date = new Date(dateString);

return date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
});
}

function MetricCard({
title,
current,
Expand All @@ -34,7 +44,7 @@ function MetricCard({
const formatNumber = (num: number) => Math.floor(num).toLocaleString('en-US', { maximumFractionDigits: 0 });

return (
<div className="h-[88px] rounded-lg border border-gray-100 bg-gray-50/50 p-1">
<div className="h-[88px] rounded-lg border border-gray-100 bg-gray-50/50 p-2">
<Row className="flex items-start justify-between gap-2">
<Column align="left" className="w-full">
<Text className="mb-0 mt-0 min-h-[28px] text-xs font-medium leading-tight text-gray-600">{title}</Text>
Expand Down Expand Up @@ -69,7 +79,7 @@ function ChannelBreakdown({ channels }: { channels: IUsageEmailData['channelBrea
<SectionHeader title="Channel Breakdown" />
<Row>
{Object.entries(channels).map(([channel, metrics]) => (
<Column key={channel} className="p-2">
<Column key={channel} className="p-2 px-0">
<MetricCard title={channel} current={metrics.current} previous={metrics.previous} change={metrics.change} />
</Column>
))}
Expand Down Expand Up @@ -113,35 +123,44 @@ function InboxMetrics({ metrics }: { metrics: IUsageEmailData['inboxMetrics'] })
}

function WorkflowStats({ workflows }: { workflows: IUsageEmailData['workflowStats'] }) {
const entries = Object.entries(workflows);
const chunks: Array<Array<[string, (typeof workflows)[string]]>> = [];

for (let i = 0; i < entries.length; i += 4) {
chunks.push(entries.slice(i, i + 4));
}
const topWorkflows = Object.entries(workflows)
?.sort((a, b) => b[1].current - a[1].current)
.slice(0, 6);

return (
<Section className="mt-6">
<SectionHeader title="Workflow Performance" />
{chunks.map((chunk, index) => (
<Row key={index}>
{chunk.map(([name, metrics]) => (
<Column key={name} className="p-2">
<MetricCard title={name} current={metrics.current} previous={metrics.previous} change={metrics.change} />
</Column>
))}
</Row>
))}
<SectionHeader title="Top Workflow Activity" />
<div className="rounded-lg border border-gray-100">
{topWorkflows?.map(([name, metrics], index) => {
const isPositive = metrics.change > 0;
const changeColor = isPositive ? 'text-emerald-600' : 'text-rose-600';
const isLast = index === topWorkflows.length - 1;

return (
<Row
key={index}
className={`flex items-center justify-between p-3 px-0 ${!isLast ? 'border-b border-gray-100' : ''}`}
>
<Column align="left" className="w-full">
<Text className="mb-0.5 mt-0 text-sm font-medium text-gray-900">{name}</Text>
<Text className="mb-0 mt-0 text-xs text-gray-500">
{Math.floor(metrics.current).toLocaleString()} notifications sent vs{' '}
{Math.floor(metrics.previous).toLocaleString()}
</Text>
</Column>
<Column align="right">
<Text className={`mb-0 mt-0 whitespace-nowrap text-sm font-medium ${changeColor}`}>
{isPositive ? '↑' : '↓'} {Math.abs(Math.floor(metrics.change))}%
</Text>
</Column>
</Row>
);
})}
</div>
</Section>
);
}

interface IMarketingLink {
href: string;
text: string;
emoji: string;
}

interface IMarketingConfig {
title: string;
links: IMarketingLink[];
Expand Down Expand Up @@ -201,18 +220,18 @@ export default function UsageInsightsEmail(props: IUsageEmailData & { marketingC
alt="Novu"
className="mx-auto my-[32px]"
/>
<Container className="mx-auto w-full">
<Container className="mx-auto w-full max-w-[700px]">
<Section className="rounded-t-lg bg-indigo-600 px-6 py-8">
<Heading className="text-center text-2xl font-bold text-white">Usage Insights Report</Heading>
<Text className="text-center text-sm text-indigo-100">{props.organizationName}</Text>
</Section>

<Container className="rounded-b-lg bg-white px-3 py-3 shadow-sm">
<div className="rounded-b-lg bg-white px-3 py-3 shadow-sm">
<div className="mb-6 rounded-md border border-indigo-100/50 bg-indigo-50/50 p-3 text-center">
<Text className="text-xs font-medium text-indigo-900">
Reporting Period: {props.period.current}
Reporting Period: {formatDate(props.period.current)}
<span className="mx-2">•</span>
Compared to: {props.period.previous}
Compared to: {formatDate(props.period.previous)}
</Text>
</div>

Expand All @@ -236,7 +255,7 @@ export default function UsageInsightsEmail(props: IUsageEmailData & { marketingC
<Section className="mt-6 border-t border-gray-100 pt-6">
<Text className="text-center text-xs text-gray-400">Generated with ❤️ by Novu</Text>
</Section>
</Container>
</div>
</Container>
</Body>
</Tailwind>
Expand Down