From 1682a0aa0abffec51772609c087e748f63c0d55b Mon Sep 17 00:00:00 2001 From: Alex Freska Date: Wed, 28 Aug 2024 09:56:17 -0400 Subject: [PATCH] refactor: pluralize --- .changeset/serious-grapes-grab.md | 5 ++++ .../hostd/components/Config/ConfigActions.tsx | 3 ++- .../components/Config/ConfigActions.tsx | 3 ++- .../components/Config/Recommendations.tsx | 9 +++---- .../WalletAddresses/AddressesFilterBar.tsx | 5 ++-- .../WalletsList/WalletsFiltersBar.tsx | 9 +++---- .../dialogs/WalletAddressesAddDialog.tsx | 18 ++++--------- .../index.tsx | 24 ++++++++--------- .../index.tsx | 11 +++----- libs/units/src/index.ts | 1 + libs/units/src/pluralize.ts | 27 +++++++++++++++++++ 11 files changed, 67 insertions(+), 48 deletions(-) create mode 100644 .changeset/serious-grapes-grab.md create mode 100644 libs/units/src/pluralize.ts diff --git a/.changeset/serious-grapes-grab.md b/.changeset/serious-grapes-grab.md new file mode 100644 index 000000000..e81b9bee9 --- /dev/null +++ b/.changeset/serious-grapes-grab.md @@ -0,0 +1,5 @@ +--- +'@siafoundation/units': minor +--- + +Add pluralization helper pluralize. diff --git a/apps/hostd/components/Config/ConfigActions.tsx b/apps/hostd/components/Config/ConfigActions.tsx index 53c6c99af..12f23fcea 100644 --- a/apps/hostd/components/Config/ConfigActions.tsx +++ b/apps/hostd/components/Config/ConfigActions.tsx @@ -4,6 +4,7 @@ import { AnnounceButton } from './AnnounceButton' import { useConfig } from '../../contexts/config' import { ConfigContextMenu } from './ConfigContextMenu' import { ConfigViewDropdownMenu } from './ConfigViewDropdownMenu' +import { pluralize } from '@siafoundation/units' export function ConfigActions() { const { changeCount, revalidateAndResetForm, form, onSubmit } = useConfig() @@ -11,7 +12,7 @@ export function ConfigActions() {
{!!changeCount && ( - {changeCount === 1 ? '1 change' : `${changeCount} changes`} + {pluralize(changeCount, 'change', 'changes')} )}
)}
- {datasetCount === 1 - ? '1 wallet' - : `${datasetCount.toLocaleString()} wallets`} + {pluralize(datasetCount, 'wallet')}
) diff --git a/apps/walletd/dialogs/WalletAddressesAddDialog.tsx b/apps/walletd/dialogs/WalletAddressesAddDialog.tsx index ac8c7fd4a..50f1ecba9 100644 --- a/apps/walletd/dialogs/WalletAddressesAddDialog.tsx +++ b/apps/walletd/dialogs/WalletAddressesAddDialog.tsx @@ -14,7 +14,7 @@ import { useWalletAddressAdd } from '@siafoundation/walletd-react' import { useCallback } from 'react' import { useForm } from 'react-hook-form' import { useWallets } from '../contexts/wallets' -import { isValidAddress } from '@siafoundation/units' +import { isValidAddress, pluralize } from '@siafoundation/units' import { uniq } from '@technically/lodash' import { FieldRescan, @@ -169,13 +169,9 @@ export function WalletAddressesAddDialog({ } return } - if (result.total === 1) { - triggerSuccessToast({ title: 'Added 1 address' }) - } else { - triggerSuccessToast({ - title: `Added ${result.successful} addresses`, - }) - } + triggerSuccessToast({ + title: `Added ${pluralize(1, 'address', 'addresses')}`, + }) triggerRescan(values) closeAndReset() }, @@ -203,11 +199,7 @@ export function WalletAddressesAddDialog({ variant={shouldRescan ? 'red' : 'accent'} size="medium" > - {addressCount === 0 - ? 'Add addresses' - : addressCount === 1 - ? 'Add 1 address' - : `Add ${addressCount.toLocaleString()} addresses`} + Add {pluralize(addressCount, 'address', 'addresses')} {shouldRescan ? ' and rescan' : ''} diff --git a/apps/walletd/dialogs/WalletAddressesGenerateLedgerDialog/index.tsx b/apps/walletd/dialogs/WalletAddressesGenerateLedgerDialog/index.tsx index ef67f771c..3749ad913 100644 --- a/apps/walletd/dialogs/WalletAddressesGenerateLedgerDialog/index.tsx +++ b/apps/walletd/dialogs/WalletAddressesGenerateLedgerDialog/index.tsx @@ -29,6 +29,7 @@ import { useTriggerRescan, } from '../FieldRescan' import { useSyncStatus } from '../../hooks/useSyncStatus' +import { pluralize } from '@siafoundation/units' export type WalletAddressesGenerateLedgerDialogParams = { walletId: string @@ -301,11 +302,9 @@ export function WalletAddressesGenerateLedgerDialog({ return } } - if (count === 1) { - triggerSuccessToast({ title: 'Added 1 address' }) - } else { - triggerSuccessToast({ title: `Added ${count} addresses` }) - } + triggerSuccessToast({ + title: `Added ${pluralize(count, 'address', 'addresses')}`, + }) return }, [addressAdd, walletId, newGeneratedAddresses]) @@ -352,9 +351,11 @@ export function WalletAddressesGenerateLedgerDialog({ variant={shouldRescan ? 'red' : 'accent'} > {newGeneratedAddresses.length > 0 - ? `Save ${newGeneratedAddresses.length} ${ - newGeneratedAddresses.length === 1 ? 'address' : 'addresses' - }${shouldRescan ? ' and rescan' : ''}` + ? `Save ${pluralize( + newGeneratedAddresses.length, + 'address', + 'addresses' + )}${shouldRescan ? ' and rescan' : ''}` : 'Rescan'} )} @@ -363,10 +364,9 @@ export function WalletAddressesGenerateLedgerDialog({ >
- Wallet currently has {datasetCount}{' '} - {datasetCount === 1 ? 'address' : 'addresses'} with a highest index of{' '} - {lastIndex}. Select a start index and the number of sequential - addresses you would like to generate. + Wallet currently has {pluralize(datasetCount, 'address', 'addresses')}{' '} + with a highest index of {lastIndex}. Select a start index and the + number of sequential addresses you would like to generate.
diff --git a/apps/walletd/dialogs/WalletAddressesGenerateSeedDialog/index.tsx b/apps/walletd/dialogs/WalletAddressesGenerateSeedDialog/index.tsx index c2611352d..b98be2f5f 100644 --- a/apps/walletd/dialogs/WalletAddressesGenerateSeedDialog/index.tsx +++ b/apps/walletd/dialogs/WalletAddressesGenerateSeedDialog/index.tsx @@ -24,6 +24,7 @@ import { useTriggerRescan, } from '../FieldRescan' import { useSyncStatus } from '../../hooks/useSyncStatus' +import { pluralize } from '@siafoundation/units' export type WalletAddressesGenerateSeedDialogParams = { walletId: string @@ -189,13 +190,9 @@ export function WalletAddressesGenerateSeedDialog({ return } } - if (count === 1) { - triggerSuccessToast({ title: 'Generated 1 address' }) - } else { - triggerSuccessToast({ - title: `Generated ${count} addresses`, - }) - } + triggerSuccessToast({ + title: `Generated ${pluralize(count, 'address', 'addresses')}`, + }) // if successfully generated an address, cache the seed cacheWalletMnemonic(walletId, mnemonic) diff --git a/libs/units/src/index.ts b/libs/units/src/index.ts index 79068f0cd..48abd1f19 100644 --- a/libs/units/src/index.ts +++ b/libs/units/src/index.ts @@ -11,3 +11,4 @@ export * from './events' export * from './transactionValue' export * from './transactionTypes' export * from './entityTypes' +export * from './pluralize' diff --git a/libs/units/src/pluralize.ts b/libs/units/src/pluralize.ts new file mode 100644 index 000000000..b350ab97d --- /dev/null +++ b/libs/units/src/pluralize.ts @@ -0,0 +1,27 @@ +type PluralizeOptions = { + plural?: string + zeroForm?: string +} + +export function pluralize( + count: number, + word: string, + pluralFormOrOptions?: string | PluralizeOptions +) { + const pluralForm = + typeof pluralFormOrOptions === 'string' + ? pluralFormOrOptions + : pluralFormOrOptions?.plural + const zeroForm = + typeof pluralFormOrOptions === 'string' + ? `0 ${pluralForm}` + : pluralFormOrOptions?.zeroForm + + if (count === 0) { + return zeroForm + } else if (count === 1) { + return `1 ${word}` + } else { + return `${count.toLocaleString()} ${pluralForm}` + } +}