Skip to content

Commit

Permalink
Refactor number formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
corneliusroemer committed Oct 2, 2024
1 parent 5834b93 commit 08fe1be
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
7 changes: 3 additions & 4 deletions website/src/components/IndexPage/OrganismCard.astro
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
import type { OrganismStatistics } from './getOrganismStatistics';
import { routes } from '../../routes/routes';
import { DEFAULT_LOCALE } from '../../settings';
import { formatNumberWithDefaultLocale } from '../../utils/formatNumber';
interface Props {
key: string;
Expand All @@ -13,7 +13,6 @@ interface Props {
const { key, image, displayName, organismStatistics, numberDaysAgoStatistics } = Astro.props;
const formatNumber = (num: number) => new Intl.NumberFormat(DEFAULT_LOCALE).format(num);
---

<a
Expand All @@ -23,8 +22,8 @@ const formatNumber = (num: number) => new Intl.NumberFormat(DEFAULT_LOCALE).form
{image !== undefined && <img src={image} class='h-32 mx-auto mb-4' alt={displayName} />}
<h3 class='font-semibold'>{displayName}</h3>
<p class='text-sm'>
{formatNumber(organismStatistics.totalSequences)} sequences<br />
(+{formatNumber(organismStatistics.recentSequences)} in last {numberDaysAgoStatistics} days)<br />
{formatNumberWithDefaultLocale(organismStatistics.totalSequences)} sequences<br />
(+{formatNumberWithDefaultLocale(organismStatistics.recentSequences)} in last {numberDaysAgoStatistics} days)<br />
<span class='hidden'
>{
organismStatistics.lastUpdatedAt && <>Last updated {organismStatistics.lastUpdatedAt.toRelative()}</>
Expand Down
22 changes: 13 additions & 9 deletions website/src/components/SearchPage/SearchFullUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Table, type TableSequenceData } from './Table';
import useQueryAsState from './useQueryAsState.js';
import { getLapisUrl } from '../../config.ts';
import { lapisClientHooks } from '../../services/serviceHooks.ts';
import { DEFAULT_LOCALE, pageSize } from '../../settings';
import { pageSize } from '../../settings';
import type { Group } from '../../types/backend.ts';
import {
type MetadataFilter,
Expand All @@ -24,6 +24,7 @@ import {
import { type OrderBy } from '../../types/lapis.ts';
import type { ReferenceGenomesSequenceNames } from '../../types/referencesGenomes.ts';
import type { ClientConfig } from '../../types/runtimeConfig.ts';
import { formatNumberWithDefaultLocale } from '../../utils/formatNumber.tsx';
import {
getFieldValuesFromQuery,
getColumnVisibilitiesFromQuery,
Expand Down Expand Up @@ -51,6 +52,16 @@ interface QueryState {
[key: string]: string;
}

// Helper function at the top of the file
const buildSequenceCountText = (totalSequences: number | undefined, oldCount: number | null, initialCount: number) => {
const sequenceCount = totalSequences !== undefined ? totalSequences : oldCount !== null ? oldCount : initialCount;

const formattedCount = formatNumberWithDefaultLocale(sequenceCount);
const pluralSuffix = sequenceCount === 1 ? '' : 's';

return `Search returned ${formattedCount} sequence${pluralSuffix}`;
};

export const InnerSearchFullUI = ({
accessToken,
referenceGenomesSequenceNames,
Expand Down Expand Up @@ -303,14 +314,7 @@ export const InnerSearchFullUI = ({
>
<div className='text-sm text-gray-800 mb-6 justify-between flex md:px-6 items-baseline'>
<div className='mt-auto'>
Search returned{' '}
{totalSequences !== undefined
? totalSequences.toLocaleString(DEFAULT_LOCALE)
: oldCount !== null
? oldCount.toLocaleString(DEFAULT_LOCALE)
: initialCount.toLocaleString(DEFAULT_LOCALE)}{' '}
sequence
{totalSequences === 1 ? '' : 's'}
{buildSequenceCountText(totalSequences, oldCount, initialCount)}
{detailsHook.isLoading ||
aggregatedHook.isLoading ||
!firstClientSideLoadOfCountCompleted ||
Expand Down
4 changes: 2 additions & 2 deletions website/src/components/SearchPage/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import type { FC, ReactElement } from 'react';
import { Tooltip } from 'react-tooltip';

import { routes } from '../../routes/routes.ts';
import { DEFAULT_LOCALE } from '../../settings.ts';
import type { Schema } from '../../types/config.ts';
import type { Metadatum, OrderBy } from '../../types/lapis.ts';
import { formatNumberWithDefaultLocale } from '../../utils/formatNumber.tsx';
import MdiTriangle from '~icons/mdi/triangle';
import MdiTriangleDown from '~icons/mdi/triangle-down';

Expand All @@ -20,7 +20,7 @@ function formatField(value: any, maxLength: number, type: string): string {
if (type === 'timestamp') {
return new Date(value * 1000).toISOString().slice(0, 10);
}
return value.toLocaleString(DEFAULT_LOCALE);
return formatNumberWithDefaultLocale(value);
} else if (typeof value === 'boolean') {
return value ? 'True' : 'False';
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useEffect, useMemo, useState, useRef, forwardRef } from 'react';
import { TextField } from './TextField.tsx';
import { getClientLogger } from '../../../clientLogger.ts';
import { lapisClientHooks } from '../../../services/serviceHooks.ts';
import { DEFAULT_LOCALE } from '../../../settings.ts';
import { type GroupedMetadataFilter, type MetadataFilter, type SetAFieldValue } from '../../../types/config.ts';
import { formatNumberWithDefaultLocale } from '../../../utils/formatNumber.tsx';

type AutoCompleteFieldProps = {
field: MetadataFilter | GroupedMetadataFilter;
Expand Down Expand Up @@ -158,7 +158,7 @@ export const AutoCompleteField = ({
{option.option}
</span>
<span className='inline-block ml-1'>
({option.count.toLocaleString(DEFAULT_LOCALE)})
({formatNumberWithDefaultLocale(option.count)})
</span>
{selected && (
<span
Expand Down
3 changes: 3 additions & 0 deletions website/src/utils/formatNumber.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { DEFAULT_LOCALE } from '../settings';

export const formatNumberWithDefaultLocale = (num: number) => new Intl.NumberFormat(DEFAULT_LOCALE).format(num);

0 comments on commit 08fe1be

Please sign in to comment.