feat(dashboard): split the overview by plan

«تفکیک طرح‌ها» draws one bar per plan by active subscribers, every purchase
or income, with count and share. A refunded plan's negative income takes no
share from the others.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Amiram_se 2026-11-05 19:07:15 +04:00
parent c6da027450
commit 0ed3578565
10 changed files with 193 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import { FlexBox } from '@lumina/island/atoms/flex-box';
import { useDashboardOverviewPart } from '../../../../../../../features/overview';
import { AudienceSection } from './components/AudienceSection';
import { IncomeSection } from './components/IncomeSection';
import { PlanBreakdown } from './components/PlanBreakdown';
import type { DashboardOverviewProps } from './types';
/** داشبورد › نمای کلی (J10-0), the creator's default tab. Loads and fails as one. */
@ -16,6 +17,7 @@ export function DashboardOverview({ layout }: DashboardOverviewProps) {
<FlexBox direction="flex-col" gap="gap-6">
<AudienceSection overview={overview} cols={cols} />
<IncomeSection overview={overview} cols={cols} />
<PlanBreakdown plans={overview.plans} />
</FlexBox>
);
}

View File

@ -0,0 +1,68 @@
'use client';
import { FlexBox } from '@lumina/island/atoms/flex-box';
import { Typography } from '@lumina/island/atoms/typography';
import { Card } from '@lumina/island/molecules/card';
import { FilterChips } from '@lumina/island/molecules/filter-chips';
import { Meter } from '@lumina/island/molecules/meter';
import { SectionTitle } from '@lumina/island/molecules/section-title';
import { number, percent } from '@lumina/tools/format';
import { useId, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { planBreakdownRows } from '../../../../../../../../features/overview';
import type { PlanBreakdownMetric } from '../../../../../../../../features/overview';
import { PLAN_BREAKDOWN_METRICS } from '../constants';
import type { PlanBreakdownProps } from '../types';
/**
* «تفکیک طرح‌ها»: one bar per plan, by active subscribers, every purchase or income, the chips
* choosing which. The note says what «کل خریدها» counts.
*/
export function PlanBreakdown({ plans }: PlanBreakdownProps) {
const { t } = useTranslation(['dashboard', 'common']);
const [metric, setMetric] = useState<PlanBreakdownMetric>('active');
const titleId = useId();
return (
<FlexBox
tag="section"
aria-labelledby={titleId}
direction="flex-col"
gap="gap-3"
>
<SectionTitle title={t('overview.plans.title')} titleId={titleId} />
<FilterChips
label={t('overview.plans.metric')}
size="sm"
scroll
options={PLAN_BREAKDOWN_METRICS.map((value) => ({
value,
label: t(`overview.plans.metrics.${value}`),
}))}
value={metric}
onChange={setMetric}
/>
<Card padding="md">
<FlexBox direction="flex-col" gap="gap-3">
{planBreakdownRows(plans, metric).map((row) => (
<Meter
key={row.tier}
label={t(`common:billing-periods.${row.tier}`)}
value={Math.max(row.value, 0)}
max={row.total || 1}
caption={t(`overview.plans.caption.${metric}`, {
n: number(row.value),
share: percent(row.sharePercent, 0),
currency: t('common:currency'),
})}
/>
))}
</FlexBox>
</Card>
<Typography variant="caption" color="secondary">
{t('overview.plans.note')}
</Typography>
</FlexBox>
);
}

View File

@ -1,4 +1,11 @@
import type { PlanBreakdownMetric } from '../../../../../../../features/overview';
import { ROUTES } from '../../../../../../navigation/navigation';
/** «مشترکان» opened on who ends within a week (J10-1g). */
export const SUBSCRIBERS_EXPIRING_HREF = `${ROUTES.dashboard.subscribers}?filter=expiring`;
export const PLAN_BREAKDOWN_METRICS: readonly PlanBreakdownMetric[] = [
'active',
'purchases',
'income',
];

View File

@ -17,3 +17,7 @@ export type DashboardOverviewSkeletonProps = {
/** Fades in after 300 ms, so a fast route never flashes it (`loading.tsx` only, G-3d). */
delayed?: boolean;
};
export type PlanBreakdownProps = {
plans: DashboardOverviewPartModel['plans'];
};

View File

@ -1,7 +1,14 @@
export { useAudiencePart } from './models/audience';
export type { AudiencePartModel } from './models/audience';
export { useDashboardOverviewPart } from './models/dashboard-overview';
export type { DashboardOverviewPartModel } from './models/dashboard-overview';
export {
planBreakdownRows,
useDashboardOverviewPart,
} from './models/dashboard-overview';
export type {
DashboardOverviewPartModel,
PlanBreakdownMetric,
PlanBreakdownRow,
} from './models/dashboard-overview';
export { useOwnSubscriberCount } from './models/own-subscriber-count';
export type { UseOwnSubscriberCountOptions } from './models/own-subscriber-count';
export {

View File

@ -1,2 +1,7 @@
export { planBreakdownRows } from './plan-breakdown';
export { useDashboardOverviewPart } from './use-dashboard-overview-part';
export type { DashboardOverviewPartModel } from './types';
export type {
DashboardOverviewPartModel,
PlanBreakdownMetric,
PlanBreakdownRow,
} from './types';

View File

@ -0,0 +1,34 @@
import { describe, expect, it } from 'vitest';
import { planBreakdownRows } from './plan-breakdown';
const plan = (
tier: 'monthly' | 'quarterly',
activeSubscribers: number,
income = 0,
) => ({ tier, activeSubscribers, totalPurchases: 0, income });
describe('planBreakdownRows', () => {
it('gives each plan its share of the chosen figure', () => {
const rows = planBreakdownRows(
[plan('monthly', 3), plan('quarterly', 1)],
'active',
);
expect(rows.map((row) => row.sharePercent)).toEqual([75, 25]);
expect(rows[0].total).toBe(4);
});
it('shows zero shares when nothing has happened yet', () => {
const rows = planBreakdownRows([plan('monthly', 0)], 'purchases');
expect(rows[0]).toMatchObject({ value: 0, total: 0, sharePercent: 0 });
});
it('keeps a refunded plan out of the others’ shares', () => {
const rows = planBreakdownRows(
[plan('monthly', 0, 1000), plan('quarterly', 0, -500)],
'income',
);
expect(rows[0].sharePercent).toBe(100);
expect(rows[1]).toMatchObject({ value: -500, sharePercent: 0 });
});
});

View File

@ -0,0 +1,35 @@
import type { DashboardOverviewPlan } from '../../repositories/creator-dashboard';
import type { PlanBreakdownMetric, PlanBreakdownRow } from './types';
const VALUE_OF: Record<
PlanBreakdownMetric,
(plan: DashboardOverviewPlan) => number
> = {
active: (plan) => plan.activeSubscribers,
purchases: (plan) => plan.totalPurchases,
income: (plan) => plan.income,
};
/**
* Each plan's part of the chosen figure, as «تفکیک طرح‌ها» draws it. A refund can leave a plan's
* income below zero: that plan's bar is empty and it takes no share from the others.
*/
export function planBreakdownRows(
plans: DashboardOverviewPlan[],
metric: PlanBreakdownMetric,
): PlanBreakdownRow[] {
const valueOf = VALUE_OF[metric];
const total = plans.reduce(
(sum, plan) => sum + Math.max(valueOf(plan), 0),
0,
);
return plans.map((plan) => {
const value = valueOf(plan);
return {
tier: plan.tier,
value,
total,
sharePercent: total > 0 ? (Math.max(value, 0) / total) * 100 : 0,
};
});
}

View File

@ -1,3 +1,4 @@
import type { BillingPeriod } from '../../../../shared/constants';
import type { DashboardOverviewPayload } from '../../repositories/creator-dashboard';
/** «نمای کلی» (J10-0): the API's figures, read once for the whole tab. */
@ -7,3 +8,15 @@ export type DashboardOverviewPartModel = DashboardOverviewPayload & {
/** No audience and no income yet: the page says how to start (J10-0b). */
isNewcomer: boolean;
};
/** What «تفکیک طرح‌ها» splits by: «مشترکان فعال» · «کل خریدها» · «درآمد». */
export type PlanBreakdownMetric = 'active' | 'purchases' | 'income';
export type PlanBreakdownRow = {
tier: BillingPeriod;
value: number;
/** Every plan's figure together, a refunded plan's negative income left out. */
total: number;
/** 0 to 100. */
sharePercent: number;
};

View File

@ -242,6 +242,21 @@
"month": "درآمد این ماه",
"payable": "قابل واریز",
"payable-link": "رفتن به مالی"
},
"plans": {
"title": "تفکیک طرح‌ها",
"metric": "معیار تفکیک",
"metrics": {
"active": "مشترکان فعال",
"purchases": "کل خریدها",
"income": "درآمد"
},
"caption": {
"active": "{{n}} نفر · {{share}}",
"purchases": "{{n}} خرید · {{share}}",
"income": "{{n}} {{currency}} · {{share}}"
},
"note": "«کل خریدها» همه خریدها از ابتدا است، با تمدید و فعال‌سازی مجدد و منهای بازگشت وجه‌ها."
}
}
}