about summary refs log tree commit diff
path: root/src/components/PostControls/util.ts
diff options
context:
space:
mode:
authorEric Bailey <git@esb.lol>2025-09-04 19:36:23 -0500
committerGitHub <noreply@github.com>2025-09-04 19:36:23 -0500
commitc129108b786a3389181c401b0bdfe1a3de528ebb (patch)
treee075d0c41c8829d9c0b66464ba7cb86dd105be4e /src/components/PostControls/util.ts
parent0b480bdaf862b0f93ed480589f81433bd6c93126 (diff)
downloadvoidsky-c129108b786a3389181c401b0bdfe1a3de528ebb.tar.zst
108 fixes (#8977)
* Translation comment

* Fix error handling in starter pack generation

* Allow access to DM settings for age restricted users

* Leave post stat unit formatting up to translators
Diffstat (limited to 'src/components/PostControls/util.ts')
-rw-r--r--src/components/PostControls/util.ts58
1 files changed, 41 insertions, 17 deletions
diff --git a/src/components/PostControls/util.ts b/src/components/PostControls/util.ts
index 5d3ea74e4..b8050a85a 100644
--- a/src/components/PostControls/util.ts
+++ b/src/components/PostControls/util.ts
@@ -1,24 +1,48 @@
-import {type I18n} from '@lingui/core'
+import {useCallback} from 'react'
+import {msg} from '@lingui/macro'
+import {useLingui} from '@lingui/react'
 
 /**
  * This matches `formatCount` from `view/com/util/numeric/format.ts`, but has
  * additional truncation logic for large numbers. `roundingMode` should always
  * match the original impl, regardless of if we add more formatting here.
  */
-export function formatPostStatCount(
-  i18n: I18n,
-  count: number,
-  {
-    compact = false,
-  }: {
-    compact?: boolean
-  } = {},
-): string {
-  const isOver10k = count >= 10_000
-  return i18n.number(count, {
-    notation: 'compact',
-    maximumFractionDigits: isOver10k || compact ? 0 : 1,
-    // @ts-expect-error - roundingMode not in the types
-    roundingMode: 'trunc',
-  })
+export function useFormatPostStatCount() {
+  const {i18n} = useLingui()
+
+  return useCallback(
+    (postStatCount: number) => {
+      const isOver1k = postStatCount >= 1_000
+      const isOver10k = postStatCount >= 10_000
+      const isOver1M = postStatCount >= 1_000_000
+      const formatted = i18n.number(postStatCount, {
+        notation: 'compact',
+        maximumFractionDigits: isOver10k ? 0 : 1,
+        // @ts-expect-error - roundingMode not in the types
+        roundingMode: 'trunc',
+      })
+      const count = formatted.replace(/\D+$/g, '')
+
+      if (isOver1M) {
+        return i18n._(
+          msg({
+            message: `${count}M`,
+            comment:
+              'For post statistics. Indicates a number in the millions. Please use the shortest format appropriate for your language.',
+          }),
+        )
+      } else if (isOver1k) {
+        return i18n._(
+          msg({
+            message: `${count}K`,
+            comment:
+              'For post statistics. Indicates a number in the thousands. Please use the shortest format appropriate for your language.',
+          }),
+        )
+      } else {
+        return count
+      }
+    },
+    [i18n],
+  )
 }