diff --git a/clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/(home)/ClientPage.tsx b/clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/(home)/ClientPage.tsx index d5c699babe..1c2eb51e5f 100644 --- a/clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/(home)/ClientPage.tsx +++ b/clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/(home)/ClientPage.tsx @@ -17,7 +17,13 @@ import { ParsedMetricPeriod, useMetrics, useProducts } from '@/hooks/queries' import { MaintainerOrganizationContext } from '@/providers/maintainerOrganization' import { defaultMetricMarks, metricDisplayNames } from '@/utils/metrics' import { ChevronRight } from '@mui/icons-material' -import { Metric, Metrics, MetricType, Organization } from '@polar-sh/api' +import { + Interval, + Metric, + Metrics, + MetricType, + Organization, +} from '@polar-sh/api' import Button from '@polar-sh/ui/components/atoms/Button' import FormattedDateTime from '@polar-sh/ui/components/atoms/FormattedDateTime' import { @@ -28,6 +34,7 @@ import { SelectValue, } from '@polar-sh/ui/components/atoms/Select' import ShadowBox from '@polar-sh/ui/components/atoms/ShadowBox' +import { Tabs, TabsList, TabsTrigger } from '@polar-sh/ui/components/atoms/Tabs' import { getCentsInDollarString } from '@polar-sh/ui/lib/money' import Link from 'next/link' import React, { useCallback, useContext, useMemo } from 'react' @@ -36,17 +43,49 @@ interface HeroChartProps { organization: Organization } +const intervalDisplayNames: Record = { + [Interval.YEAR]: '3y', + [Interval.MONTH]: '12m', + [Interval.WEEK]: '3m', + [Interval.DAY]: '30d', + [Interval.HOUR]: '24h', +} + +const getIntervalStartDate = ( + interval: Interval, + organization: Organization, +) => { + switch (interval) { + case Interval.YEAR: + const createdAt = new Date(organization.created_at) + const threeYearsAgo = new Date() + threeYearsAgo.setFullYear(threeYearsAgo.getFullYear() - 3) + return createdAt < threeYearsAgo ? createdAt : threeYearsAgo + case Interval.MONTH: + return new Date(new Date().setFullYear(new Date().getFullYear() - 1)) + case Interval.WEEK: + return new Date(new Date().setMonth(new Date().getMonth() - 3)) + case Interval.DAY: + return new Date(new Date().setDate(new Date().getDate() - 30)) + case Interval.HOUR: + return new Date(new Date().setHours(new Date().getHours() - 24)) + } +} + const HeroChart = ({ organization }: HeroChartProps) => { const [selectedMetric, setSelectedMetric] = React.useState('cumulative_revenue') + const [selectedInterval, setSelectedInterval] = React.useState( + Interval.DAY, + ) const [hoveredMetricPeriod, setHoveredMetricPeriod] = React.useState(null) const { data: metricsData, isLoading: metricsLoading } = useMetrics({ organizationId: organization.id, - startDate: new Date(new Date().setDate(new Date().getDate() - 31)), + startDate: getIntervalStartDate(selectedInterval, organization), endDate: new Date(), - interval: 'day', + interval: selectedInterval, }) const getMetricValue = useCallback((metric?: Metric, value?: number) => { @@ -103,6 +142,9 @@ const HeroChart = ({ organization }: HeroChartProps) => { ) : ( 'Today' @@ -111,9 +153,25 @@ const HeroChart = ({ organization }: HeroChartProps) => { - - - + setSelectedInterval(value as Interval)} + > + + {Object.entries(intervalDisplayNames) + .filter(([key]) => key !== Interval.YEAR) + .map(([key, value]) => ( + + {value} + + ))} + +
{metricsLoading ? ( @@ -124,7 +182,7 @@ const HeroChart = ({ organization }: HeroChartProps) => { @@ -134,8 +192,8 @@ const HeroChart = ({ organization }: HeroChartProps) => { } /> ) : ( -
- No data available +
+ No data available
)}
diff --git a/clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/customers/[id]/ClientPage.tsx b/clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/customers/[id]/ClientPage.tsx index a5ff418cab..dc41e3952c 100644 --- a/clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/customers/[id]/ClientPage.tsx +++ b/clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/customers/[id]/ClientPage.tsx @@ -190,8 +190,8 @@ const ClientPage: React.FC = ({ organization, customer }) => { } /> ) : ( -
- No data available +
+ No data available
)}