The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Show application statistics in a two-column stats grid (#28456)
martmull752f54587304
diff --git a/packages/twenty-front/src/modules/settings/components/SettingsStatsGrid.tsx b/packages/twenty-front/src/modules/settings/components/SettingsStatsGrid.tsxindex da700e00bbe..241a4239e4b 100644--- a/packages/twenty-front/src/modules/settings/components/SettingsStatsGrid.tsx+++ b/packages/twenty-front/src/modules/settings/components/SettingsStatsGrid.tsx@@ -1,12 +1,12 @@import { styled } from '@linaria/react';-import { Fragment } from 'react';+import { Fragment, type ReactNode } from 'react';import { type IconComponent } from 'twenty-ui/icon';import { useTheme, themeCssVariables } from 'twenty-ui/theme';export type SettingsStatRow = {Icon: IconComponent;label: string;- value: string;+ value: ReactNode;};type SettingsStatsGridProps = {@@ -54,6 +54,8 @@ const StyledLabel = styled.div`const StyledValue = styled.div`color: ${themeCssVariables.font.color.primary};+ max-width: 50%;+ min-width: 0;padding: 0 ${themeCssVariables.spacing[1]};`;diff --git a/packages/twenty-front/src/modules/settings/usage/utils/__tests__/getCurrentMonthPeriodDates.test.ts b/packages/twenty-front/src/modules/settings/usage/utils/__tests__/getCurrentMonthPeriodDates.test.tsnew file mode 100644index 00000000000..41c9ebcadf3--- /dev/null+++ b/packages/twenty-front/src/modules/settings/usage/utils/__tests__/getCurrentMonthPeriodDates.test.ts@@ -0,0 +1,21 @@+import { getCurrentMonthPeriodDates } from '@/settings/usage/utils/getCurrentMonthPeriodDates';++describe('getCurrentMonthPeriodDates', () => {+ it('spans the UTC calendar month', () => {+ expect(+ getCurrentMonthPeriodDates(new Date('2026-10-20T15:30:00.000Z')),+ ).toEqual({+ periodStart: '2026-10-01T00:00:00.000Z',+ periodEnd: '2026-11-01T00:00:00.000Z',+ });+ });++ it('rolls over to the next year in December', () => {+ expect(+ getCurrentMonthPeriodDates(new Date('2026-12-31T23:59:59.999Z')),+ ).toEqual({+ periodStart: '2026-12-01T00:00:00.000Z',+ periodEnd: '2027-01-01T00:00:00.000Z',+ });+ });+});diff --git a/packages/twenty-front/src/modules/settings/usage/utils/getCurrentMonthPeriodDates.ts b/packages/twenty-front/src/modules/settings/usage/utils/getCurrentMonthPeriodDates.tsnew file mode 100644index 00000000000..e56f3012384--- /dev/null+++ b/packages/twenty-front/src/modules/settings/usage/utils/getCurrentMonthPeriodDates.ts@@ -0,0 +1,10 @@+export const getCurrentMonthPeriodDates = (+ now: Date = new Date(),+): { periodStart: string; periodEnd: string } => ({+ periodStart: new Date(+ Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1),+ ).toISOString(),+ periodEnd: new Date(+ Date.UTC(now.getUTCFullYear(), now.getUTCMonth() + 1, 1),+ ).toISOString(),+});diff --git a/packages/twenty-front/src/pages/settings/applications/components/SettingsApplicationRegistrationInstallStats.tsx b/packages/twenty-front/src/pages/settings/applications/components/SettingsApplicationRegistrationInstallStats.tsxindex b6da74273d2..4b2dfb6a04a 100644--- a/packages/twenty-front/src/pages/settings/applications/components/SettingsApplicationRegistrationInstallStats.tsx+++ b/packages/twenty-front/src/pages/settings/applications/components/SettingsApplicationRegistrationInstallStats.tsx@@ -1,17 +1,16 @@import { useQuery } from '@apollo/client/react';-import { styled } from '@linaria/react';import { useLingui } from '@lingui/react/macro';-import { useState } from 'react';import { useApolloAdminClient } from '@/settings/admin-panel/apollo/hooks/useApolloAdminClient';-import { SettingsTableCard } from '@/settings/components/SettingsTableCard';+import { USAGE_LIMIT_OPERATION_TYPE_ICONS } from '@/settings/billing/constants/UsageLimitOperationTypeIcons';+import { isKeyOfRecord } from '@/settings/billing/utils/isKeyOfRecord';+import {+ type SettingsStatRow,+ SettingsStatsGrid,+} from '@/settings/components/SettingsStatsGrid';+import { getCurrentMonthPeriodDates } from '@/settings/usage/utils/getCurrentMonthPeriodDates';import { getUsageOperationTypeLabel } from '@/settings/usage/utils/getUsageOperationTypeLabel';-import { getPeriodDates } from '@/settings/usage/utils/getPeriodDates';-import { getPeriodOptions } from '@/settings/usage/utils/getPeriodOptions';-import { type PeriodPreset } from '@/settings/usage/utils/periodPreset';-import { Select } from '@/ui/input/components/Select';import { isDefined } from 'twenty-shared/utils';-import { Section } from 'twenty-ui/components/layout';import {IconBrandDocker,IconChartBar,@@ -22,17 +21,9 @@ import {} from 'twenty-ui/icon';import { Tooltip } from 'twenty-ui/primitives/surfaces';import { OverflowingTextWithTooltip } from 'twenty-ui/primitives/typography';-import { themeCssVariables } from 'twenty-ui/theme';import { FindAdminApplicationRegistrationStatsDocument } from '~/generated-admin/graphql';import { FindApplicationRegistrationStatsDocument } from '~/generated-metadata/graphql';-const StyledStats = styled.div`- align-items: start;- display: grid;- gap: ${themeCssVariables.spacing[4]};- grid-template-columns: repeat(auto-fit, minmax(min(100%, 280px), 1fr));-`;-export const SettingsApplicationRegistrationInstallStats = ({registration,isAdminView = false,@@ -42,8 +33,7 @@ export const SettingsApplicationRegistrationInstallStats = ({}) => {const { t, i18n } = useLingui();const apolloAdminClient = useApolloAdminClient();- const [period, setPeriod] = useState<PeriodPreset>('30d');- const periodDates = getPeriodDates(period);+ const periodDates = getCurrentMonthPeriodDates();const applicationRegistrationId = registration.id;@@ -73,7 +63,7 @@ export const SettingsApplicationRegistrationInstallStats = ({).join(', ') || '—';- const statsItems = [+ const installStats: SettingsStatRow[] = [{Icon: IconBrandDocker,label: t`Installs`,@@ -113,13 +103,13 @@ export const SettingsApplicationRegistrationInstallStats = ({}): '—';- const creditsUsedItems = [+ const creditsUsedStats: SettingsStatRow[] = [{Icon: IconCoins,- label: t`Total credits used`,+ label: t`Credits used this month`,value: (<Tooltip- content={t`Usage attributed to this application in its current installing workspaces for the selected period. Updated every ten minutes.`}+ content={t`Usage attributed to this application in its current installing workspaces since the start of the month (UTC). Updated every ten minutes.`}><span tabIndex={0}>{formatCredits(stats?.totalCreditsUsed)}</span></Tooltip>@@ -132,6 +122,11 @@ export const SettingsApplicationRegistrationInstallStats = ({: undefined;return {+ Icon:+ isDefined(operation) &&+ isKeyOfRecord(USAGE_LIMIT_OPERATION_TYPE_ICONS, operation)+ ? USAGE_LIMIT_OPERATION_TYPE_ICONS[operation]+ : IconCoins,label: isDefined(operationLabel)? i18n._(operationLabel): (label ?? operation ?? '—'),@@ -141,33 +136,5 @@ export const SettingsApplicationRegistrationInstallStats = ({),];- return (- <StyledStats>- <div role="group" aria-label={t`Install statistics`}>- <SettingsTableCard- items={statsItems}- gridAutoColumns="minmax(0, 200px) minmax(0, 1fr)"- />- </div>- <div role="group" aria-label={t`Credit consumption`}>- <Section.Header- title={t`Credits used`}- actions={- <Select- dropdownId={`application-registration-credits-period-${applicationRegistrationId}`}- value={period}- options={getPeriodOptions()}- onChange={setPeriod}- needIconCheck- selectSizeVariant="small"- />- }- />- <SettingsTableCard- items={creditsUsedItems}- gridAutoColumns="minmax(0, 200px) minmax(0, 1fr)"- />- </div>- </StyledStats>- );+ return <SettingsStatsGrid columns={[installStats, creditsUsedStats]} />;};diff --git a/packages/twenty-front/src/pages/settings/applications/components/__tests__/SettingsApplicationRegistrationInstallStats.test.tsx b/packages/twenty-front/src/pages/settings/applications/components/__tests__/SettingsApplicationRegistrationInstallStats.test.tsxindex 3ffd62c18f2..cb3c764ee2d 100644--- a/packages/twenty-front/src/pages/settings/applications/components/__tests__/SettingsApplicationRegistrationInstallStats.test.tsx+++ b/packages/twenty-front/src/pages/settings/applications/components/__tests__/SettingsApplicationRegistrationInstallStats.test.tsx@@ -3,11 +3,10 @@ import { ApolloProvider } from '@apollo/client/react';import { MockLink } from '@apollo/client/testing';import { i18n } from '@lingui/core';import { I18nProvider } from '@lingui/react';-import { render, screen, within } from '@testing-library/react';-import userEvent from '@testing-library/user-event';+import { render, screen } from '@testing-library/react';import { createStore, Provider as JotaiProvider } from 'jotai';-import { getPeriodDates } from '@/settings/usage/utils/getPeriodDates';+import { getCurrentMonthPeriodDates } from '@/settings/usage/utils/getCurrentMonthPeriodDates';import { ApolloAdminClientContext } from '@/settings/admin-panel/apollo/contexts/ApolloAdminClientContext';import { FindAdminApplicationRegistrationStatsDocument } from '~/generated-admin/graphql';@@ -38,25 +37,27 @@ const STATS: ApplicationRegistrationStats = {const renderStats = (stats = STATS, isAdminView = false) => {const client = new ApolloClient({cache: new InMemoryCache(),- link: new MockLink(- (['30d', '7d', '90d'] as const).map((period) => ({+ link: new MockLink([+ {request: {query: isAdminView? FindAdminApplicationRegistrationStatsDocument: FindApplicationRegistrationStatsDocument,- variables: { id: 'registration-id', ...getPeriodDates(period) },+ variables: {+ id: 'registration-id',+ ...getCurrentMonthPeriodDates(),+ },},result: {data: {[isAdminView? 'findAdminApplicationRegistrationStats'- : 'findApplicationRegistrationStats']:- period === '30d' ? stats : { ...stats, totalCreditsUsed: 7 },+ : 'findApplicationRegistrationStats']: stats,},},delay: 0,- })),- ),+ },+ ]),});return render(@@ -77,11 +78,12 @@ const renderStats = (stats = STATS, isAdminView = false) => {describe('Application registration statistics', () => {it.each([false, true])(- 'shows credit consumption for owner/admin view: %s',+ 'shows this month credit consumption for owner/admin view: %s',async (isAdminView) => {renderStats(STATS, isAdminView);expect(await screen.findByText('1,172')).toBeVisible();+ expect(screen.getByText('Credits used this month')).toBeVisible();expect(screen.getByText('Code Execution')).toBeVisible();expect(screen.getByText('1,171')).toBeVisible();expect(screen.getByText('HISTORICAL_OPERATION')).toBeVisible();@@ -92,15 +94,6 @@ describe('Application registration statistics', () => {},);- it('requests the selected credit period', async () => {- const user = userEvent.setup();- renderStats();- expect(await screen.findByText('1,172')).toBeVisible();- await user.click(screen.getByText('Last 30 days'));- await user.click(await screen.findByText('Last 7 days'));- expect(await screen.findByText('7')).toBeVisible();- });-it.each([null, 0])('distinguishes unavailable usage from zero: %s',async (totalCreditsUsed) => {@@ -111,11 +104,8 @@ describe('Application registration statistics', () => {});await screen.findByText('1.2.3');- const credits = within(- screen.getByRole('group', { name: 'Credit consumption' }),- );expect(- credits.getByText(totalCreditsUsed === null ? '—' : '0'),+ screen.getByText(totalCreditsUsed === null ? '—' : '0'),).toBeVisible();},);