Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat(reputation): format reputation count

haitao(lj)9422a45e3f1e
diff --git a/ui/src/components/BaseUserCard/index.tsx b/ui/src/components/BaseUserCard/index.tsx
index 5472f1b2..81d5a397 100644
--- a/ui/src/components/BaseUserCard/index.tsx
+++ b/ui/src/components/BaseUserCard/index.tsx
@@ -3,6 +3,8 @@ import { Link } from 'react-router-dom';
import { Avatar } from '@answer/components';
+import { formatCount } from '@/utils';
+
interface Props {
data: any;
showAvatar?: boolean;
@@ -34,7 +36,9 @@ const Index: FC<Props> = ({
</>
)}
- <span className="fw-bold">{data?.rank}</span>
+ <span className="fw-bold" title="Reputation">
+ {formatCount(data?.rank)}
+ </span>
</div>
);
};
diff --git a/ui/src/components/UserCard/index.tsx b/ui/src/components/UserCard/index.tsx
index d0087dc1..65e6eaf6 100644
--- a/ui/src/components/UserCard/index.tsx
+++ b/ui/src/components/UserCard/index.tsx
@@ -3,6 +3,8 @@ import { Link } from 'react-router-dom';
import { Avatar, FormatTime } from '@answer/components';
+import { formatCount } from '@/utils';
+
interface Props {
data: any;
time: number;
@@ -28,7 +30,9 @@ const Index: FC<Props> = ({ data, time, preFix }) => {
) : (
<span className="me-1 text-break">{data?.display_name}</span>
)}
- <span className="fw-bold">{data?.rank}</span>
+ <span className="fw-bold" title="Reputation">
+ {formatCount(data?.rank)}
+ </span>
</div>
{time && <FormatTime time={time} preFix={preFix} />}
</div>
diff --git a/ui/src/utils/index.ts b/ui/src/utils/index.ts
index e8ce9da3..0e23dfe2 100644
--- a/ui/src/utils/index.ts
+++ b/ui/src/utils/index.ts
@@ -9,9 +9,18 @@ function getQueryString(name: string): string {
return '';
}
+function thousandthDivision(num) {
+ const reg = /\d{1,3}(?=(\d{3})+$)/g;
+ return `${num}`.replace(reg, '$&,');
+}
+
function formatCount($num: number): string {
let res = String($num);
- if ($num >= 1000 && $num < 1000000) {
+ if (!Number.isFinite($num)) {
+ res = '0';
+ } else if ($num < 10000) {
+ res = thousandthDivision($num);
+ } else if ($num < 1000000) {
res = `${Math.round($num / 100) / 10}k`;
} else if ($num >= 1000000) {
res = `${Math.round($num / 100000) / 10}m`;
@@ -97,6 +106,7 @@ function parseUserInfo(markdown) {
export {
getQueryString,
+ thousandthDivision,
formatCount,
isLogin,
scrollTop,