From d8a29ecc8c0b32c47d9ab403a74052aa74e71c97 Mon Sep 17 00:00:00 2001 From: Arvin Xu Date: Mon, 6 Jan 2025 09:55:19 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20fix=20format=20short=20nu?= =?UTF-8?q?mber=20(#5294)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * pin pdfjs dist * refactor * fix number format * Revert "pin pdfjs dist" This reverts commit 1a2eae1494d2355c0354dd44b1af767a77c8a548. * fix * fix * fix --- next.config.ts | 10 ++-------- src/features/User/DataStatistics.tsx | 24 ++++-------------------- src/utils/format.test.ts | 8 +++++--- src/utils/format.ts | 2 +- 4 files changed, 12 insertions(+), 32 deletions(-) diff --git a/next.config.ts b/next.config.ts index 35095f78efe4..4aa829ffaf03 100644 --- a/next.config.ts +++ b/next.config.ts @@ -10,7 +10,6 @@ const enableReactScan = !!process.env.REACT_SCAN_MONITOR_API_KEY; const isUsePglite = process.env.NEXT_PUBLIC_CLIENT_DB === 'pglite'; // if you need to proxy the api endpoint to remote server -const API_PROXY_ENDPOINT = process.env.API_PROXY_ENDPOINT || ''; const basePath = process.env.NEXT_PUBLIC_BASE_PATH; @@ -28,7 +27,6 @@ const nextConfig: NextConfig = { ], webVitalsAttribution: ['CLS', 'LCP'], }, - async headers() { return [ { @@ -166,14 +164,10 @@ const nextConfig: NextConfig = { source: '/welcome', }, ], - rewrites: async () => [ - // due to google api not work correct in some countries - // we need a proxy to bypass the restriction - { destination: `${API_PROXY_ENDPOINT}/api/chat/google`, source: '/api/chat/google' }, - ], - serverExternalPackages: ['@electric-sql/pglite'], + transpilePackages: ['pdfjs-dist', 'mermaid'], + webpack(config) { config.experiments = { asyncWebAssembly: true, diff --git a/src/features/User/DataStatistics.tsx b/src/features/User/DataStatistics.tsx index c5589bd53c7b..dc337093ab71 100644 --- a/src/features/User/DataStatistics.tsx +++ b/src/features/User/DataStatistics.tsx @@ -3,7 +3,7 @@ import { Icon, Tooltip } from '@lobehub/ui'; import { Badge } from 'antd'; import { createStyles } from 'antd-style'; -import { isNumber, isUndefined } from 'lodash-es'; +import { isUndefined } from 'lodash-es'; import { LoaderCircle } from 'lucide-react'; import { memo, useMemo } from 'react'; import { useTranslation } from 'react-i18next'; @@ -14,6 +14,7 @@ import { messageService } from '@/services/message'; import { sessionService } from '@/services/session'; import { topicService } from '@/services/topic'; import { useServerConfigStore } from '@/store/serverConfig'; +import { formatShortenNumber } from '@/utils/format'; import { today } from '@/utils/time'; const useStyles = createStyles(({ css, token }) => ({ @@ -42,23 +43,6 @@ const useStyles = createStyles(({ css, token }) => ({ `, })); -const formatNumber = (num: any) => { - if (!isNumber(num)) return num; - // 使用Intl.NumberFormat来添加千分号 - const formattedWithComma = new Intl.NumberFormat('en-US').format(num); - - // 格式化为 K 或 M - if (num >= 10_000_000) { - return (num / 1_000_000).toFixed(1) + 'M'; - } else if (num >= 10_000) { - return (num / 1000).toFixed(1) + 'K'; - } else if (num === 0) { - return 0; - } else { - return formattedWithComma; - } -}; - const DataStatistics = memo>(({ style, ...rest }) => { const mobile = useServerConfigStore((s) => s.isMobile); // sessions @@ -128,7 +112,7 @@ const DataStatistics = memo>(({ style, ...rest }) key={item.key} > -
{formatNumber(item.count)}
+
{formatShortenNumber(item.count)}
{item.title}
{showBadge && ( @@ -150,7 +134,7 @@ const DataStatistics = memo>(({ style, ...rest }) return ( -
{formatNumber(item.count)}
+
{formatShortenNumber(item.count)}
{item.title}
diff --git a/src/utils/format.test.ts b/src/utils/format.test.ts index 537e45049345..470d9c4e256e 100644 --- a/src/utils/format.test.ts +++ b/src/utils/format.test.ts @@ -16,7 +16,6 @@ import { formatTokenNumber, } from './format'; -// 保留你已经编写的测试用例 describe('format', () => { describe('formatSize', () => { it('should format bytes to KB correctly', () => { @@ -128,10 +127,13 @@ describe('format', () => { expect(formatShortenNumber(9999)).toBe('9,999'); }); - it('should format numbers between 10,000 and 9,999,999 correctly', () => { + it('should format numbers between 10,000 and 999,999 correctly', () => { expect(formatShortenNumber(10000)).toBe('10.0K'); expect(formatShortenNumber(123456)).toBe('123.5K'); - expect(formatShortenNumber(9999999)).toBe('10000.0K'); + expect(formatShortenNumber(998000)).toBe('998.0K'); + expect(formatShortenNumber(999999)).toBe('1000.0K'); + expect(formatShortenNumber(1000000)).toBe('1.0M'); + expect(formatShortenNumber(9999999)).toBe('10.0M'); }); it('should format numbers 10,000,000 and above correctly', () => { diff --git a/src/utils/format.ts b/src/utils/format.ts index b13aea94ba5f..46a0c1af0882 100644 --- a/src/utils/format.ts +++ b/src/utils/format.ts @@ -69,7 +69,7 @@ export const formatShortenNumber = (num: any) => { const formattedWithComma = new Intl.NumberFormat('en-US').format(num); // 格式化为 K 或 M - if (num >= 10_000_000) { + if (num >= 1_000_000) { return (num / 1_000_000).toFixed(1) + 'M'; } else if (num >= 10_000) { return (num / 1000).toFixed(1) + 'K';