Skip to content

Commit

Permalink
🐛 fix: fix format short number (#5294)
Browse files Browse the repository at this point in the history
* pin pdfjs dist

* refactor

* fix number format

* Revert "pin pdfjs dist"

This reverts commit 1a2eae1.

* fix

* fix

* fix
  • Loading branch information
arvinxx authored Jan 6, 2025
1 parent 89a60e3 commit d8a29ec
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 32 deletions.
10 changes: 2 additions & 8 deletions next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,7 +27,6 @@ const nextConfig: NextConfig = {
],
webVitalsAttribution: ['CLS', 'LCP'],
},

async headers() {
return [
{
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 4 additions & 20 deletions src/features/User/DataStatistics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 }) => ({
Expand Down Expand Up @@ -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<Omit<FlexboxProps, 'children'>>(({ style, ...rest }) => {
const mobile = useServerConfigStore((s) => s.isMobile);
// sessions
Expand Down Expand Up @@ -128,7 +112,7 @@ const DataStatistics = memo<Omit<FlexboxProps, 'children'>>(({ style, ...rest })
key={item.key}
>
<Flexbox gap={2}>
<div className={styles.count}>{formatNumber(item.count)}</div>
<div className={styles.count}>{formatShortenNumber(item.count)}</div>
<div className={styles.title}>{item.title}</div>
</Flexbox>
{showBadge && (
Expand All @@ -150,7 +134,7 @@ const DataStatistics = memo<Omit<FlexboxProps, 'children'>>(({ style, ...rest })
return (
<Flexbox className={styles.card} flex={1} gap={2} key={item.key}>
<Flexbox horizontal>
<div className={styles.count}>{formatNumber(item.count)}</div>
<div className={styles.count}>{formatShortenNumber(item.count)}</div>
</Flexbox>
<div className={styles.title}>{item.title}</div>
</Flexbox>
Expand Down
8 changes: 5 additions & 3 deletions src/utils/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
formatTokenNumber,
} from './format';

// 保留你已经编写的测试用例
describe('format', () => {
describe('formatSize', () => {
it('should format bytes to KB correctly', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit d8a29ec

Please sign in to comment.