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(admin): dashboard module

robin44da3c24fd80
diff --git a/ui/src/common/interface.ts b/ui/src/common/interface.ts
index 732964d1..98f7c046 100644
--- a/ui/src/common/interface.ts
+++ b/ui/src/common/interface.ts
@@ -323,3 +323,21 @@ export interface SearchResItem {
export interface SearchRes extends ListResult<SearchResItem> {
extra: any;
}
+
+export interface AdminDashboard {
+ info: {
+ question_count: number;
+ answer_count: number;
+ comment_count: number;
+ vote_count: number;
+ user_count: number;
+ report_count: number;
+ uploading_files: boolean;
+ smtp: boolean;
+ time_zone: string;
+ occupying_storage_space: string;
+ app_start_time: number;
+ app_version: string;
+ https: boolean;
+ };
+}
diff --git a/ui/src/pages/Admin/Dashboard/components/AnswerLinks/index.tsx b/ui/src/pages/Admin/Dashboard/components/AnswerLinks/index.tsx
new file mode 100644
index 00000000..8f5048f8
--- /dev/null
+++ b/ui/src/pages/Admin/Dashboard/components/AnswerLinks/index.tsx
@@ -0,0 +1,31 @@
+import { Card, Row, Col } from 'react-bootstrap';
+import { useTranslation } from 'react-i18next';
+
+const AnswerLinks = () => {
+ const { t } = useTranslation('translation', { keyPrefix: 'admin.dashboard' });
+
+ return (
+ <Card className="mb-4">
+ <Card.Body>
+ <h6 className="mb-3">{t('answer_links')}</h6>
+ <Row>
+ <Col xs={6}>
+ <a href="https://answer.dev" target="_blank" rel="noreferrer">
+ {t('documents')}
+ </a>
+ </Col>
+ <Col xs={6}>
+ <a
+ href="https://github.com/answerdev/answer/issues"
+ target="_blank"
+ rel="noreferrer">
+ {t('feedback')}
+ </a>
+ </Col>
+ </Row>
+ </Card.Body>
+ </Card>
+ );
+};
+
+export default AnswerLinks;
diff --git a/ui/src/pages/Admin/Dashboard/components/HealthStatus/index.tsx b/ui/src/pages/Admin/Dashboard/components/HealthStatus/index.tsx
new file mode 100644
index 00000000..72c3b9a6
--- /dev/null
+++ b/ui/src/pages/Admin/Dashboard/components/HealthStatus/index.tsx
@@ -0,0 +1,54 @@
+import { FC } from 'react';
+import { Card, Row, Col, Badge } from 'react-bootstrap';
+import { useTranslation } from 'react-i18next';
+import { Link } from 'react-router-dom';
+
+import type * as Type from '@answer/common/interface';
+
+interface IProps {
+ data: Type.AdminDashboard['info'];
+}
+
+const HealthStatus: FC<IProps> = ({ data }) => {
+ const { t } = useTranslation('translation', { keyPrefix: 'admin.dashboard' });
+
+ return (
+ <Card className="mb-4">
+ <Card.Body>
+ <h6 className="mb-3">{t('site_health_status')}</h6>
+ <Row>
+ <Col xs={6} className="mb-1 d-flex align-items-center">
+ <span className="text-secondary me-1">{t('version')}</span>
+ <strong>90</strong>
+ <Badge pill bg="warning" text="dark" className="ms-1">
+ {t('update_to')} {data.app_version}
+ </Badge>
+ </Col>
+ <Col xs={6} className="mb-1">
+ <span className="text-secondary me-1">{t('https')}</span>
+ <strong>{data.https ? t('yes') : t('yes')}</strong>
+ </Col>
+ <Col xs={6} className="mb-1">
+ <span className="text-secondary me-1">{t('uploading_files')}</span>
+ <strong>
+ {data.uploading_files ? t('allowed') : t('not_allowed')}
+ </strong>
+ </Col>
+ <Col xs={6}>
+ <span className="text-secondary me-1">{t('smtp')}</span>
+ <strong>{data.smtp ? t('enabled') : t('disabled')}</strong>
+ <Link to="/admin/smtp" className="ms-2">
+ {t('config')}
+ </Link>
+ </Col>
+ <Col xs={6}>
+ <span className="text-secondary me-1">{t('timezone')}</span>
+ <strong>{data.time_zone}</strong>
+ </Col>
+ </Row>
+ </Card.Body>
+ </Card>
+ );
+};
+
+export default HealthStatus;
diff --git a/ui/src/pages/Admin/Dashboard/components/Statistics/index.tsx b/ui/src/pages/Admin/Dashboard/components/Statistics/index.tsx
new file mode 100644
index 00000000..43e1fa2d
--- /dev/null
+++ b/ui/src/pages/Admin/Dashboard/components/Statistics/index.tsx
@@ -0,0 +1,51 @@
+import { FC } from 'react';
+import { Card, Row, Col } from 'react-bootstrap';
+import { useTranslation } from 'react-i18next';
+
+import type * as Type from '@answer/common/interface';
+
+interface IProps {
+ data: Type.AdminDashboard['info'];
+}
+const Statistics: FC<IProps> = ({ data }) => {
+ const { t } = useTranslation('translation', { keyPrefix: 'admin.dashboard' });
+
+ return (
+ <Card className="mb-4">
+ <Card.Body>
+ <h6 className="mb-3">{t('site_statistics')}</h6>
+ <Row>
+ <Col xs={6} className="mb-1">
+ <span className="text-secondary me-1">{t('questions')}</span>
+ <strong>{data.question_count}</strong>
+ </Col>
+ <Col xs={6} className="mb-1">
+ <span className="text-secondary me-1">{t('answers')}</span>
+ <strong>{data.answer_count}</strong>
+ </Col>
+ <Col xs={6} className="mb-1">
+ <span className="text-secondary me-1">{t('comments')}</span>
+ <strong>{data.comment_count}</strong>
+ </Col>
+ <Col xs={6} className="mb-1">
+ <span className="text-secondary me-1">{t('votes')}</span>
+ <strong>{data.vote_count}</strong>
+ </Col>
+ <Col xs={6}>
+ <span className="text-secondary me-1">{t('active_users')}</span>
+ <strong>{data.user_count}</strong>
+ </Col>
+ <Col xs={6}>
+ <span className="text-secondary me-1">{t('flags')}</span>
+ <strong>{data.report_count}</strong>
+ <a href="###" className="ms-2">
+ {t('review')}
+ </a>
+ </Col>
+ </Row>
+ </Card.Body>
+ </Card>
+ );
+};
+
+export default Statistics;
diff --git a/ui/src/pages/Admin/Dashboard/components/SystemInfo/index.tsx b/ui/src/pages/Admin/Dashboard/components/SystemInfo/index.tsx
new file mode 100644
index 00000000..2bf222f5
--- /dev/null
+++ b/ui/src/pages/Admin/Dashboard/components/SystemInfo/index.tsx
@@ -0,0 +1,34 @@
+import { FC } from 'react';
+import { Card, Row, Col } from 'react-bootstrap';
+import { useTranslation } from 'react-i18next';
+
+import type * as Type from '@answer/common/interface';
+
+import { formatUptime } from '@/utils';
+
+interface IProps {
+ data: Type.AdminDashboard['info'];
+}
+const SystemInfo: FC<IProps> = ({ data }) => {
+ const { t } = useTranslation('translation', { keyPrefix: 'admin.dashboard' });
+
+ return (
+ <Card className="mb-4">
+ <Card.Body>
+ <h6 className="mb-3">{t('system_info')}</h6>
+ <Row>
+ <Col xs={6}>
+ <span className="text-secondary me-1">{t('storage_used')}</span>
+ <strong>{data.occupying_storage_space}</strong>
+ </Col>
+ <Col xs={6}>
+ <span className="text-secondary me-1">{t('uptime')}</span>
+ <strong>{formatUptime(data.app_start_time)}</strong>
+ </Col>
+ </Row>
+ </Card.Body>
+ </Card>
+ );
+};
+
+export default SystemInfo;
diff --git a/ui/src/pages/Admin/Dashboard/components/index.ts b/ui/src/pages/Admin/Dashboard/components/index.ts
new file mode 100644
index 00000000..877f643f
--- /dev/null
+++ b/ui/src/pages/Admin/Dashboard/components/index.ts
@@ -0,0 +1,6 @@
+import SystemInfo from './SystemInfo';
+import Statistics from './Statistics';
+import AnswerLinks from './AnswerLinks';
+import HealthStatus from './HealthStatus';
+
+export { SystemInfo, Statistics, AnswerLinks, HealthStatus };
diff --git a/ui/src/pages/Admin/Dashboard/index.tsx b/ui/src/pages/Admin/Dashboard/index.tsx
index 45c19721..47393831 100644
--- a/ui/src/pages/Admin/Dashboard/index.tsx
+++ b/ui/src/pages/Admin/Dashboard/index.tsx
@@ -1,12 +1,41 @@
import { FC } from 'react';
+import { Row, Col } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
+import {
+ AnswerLinks,
+ HealthStatus,
+ Statistics,
+ SystemInfo,
+} from './components';
+
+import { useDashBoard } from '@/services';
+
const Dashboard: FC = () => {
const { t } = useTranslation('translation', { keyPrefix: 'admin.dashboard' });
+ const { data } = useDashBoard();
+
+ if (!data) {
+ return null;
+ }
return (
<>
<h3 className="text-capitalize">{t('title')}</h3>
<p className="mt-4">{t('welcome')}</p>
+ <Row>
+ <Col lg={6}>
+ <Statistics data={data.info} />
+ </Col>
+ <Col lg={6}>
+ <HealthStatus data={data.info} />
+ </Col>
+ <Col lg={6}>
+ <SystemInfo data={data.info} />
+ </Col>
+ <Col lg={6}>
+ <AnswerLinks />
+ </Col>
+ </Row>
{process.env.REACT_APP_VERSION && (
<p className="mt-4">
{`${t('version')} `}
diff --git a/ui/src/services/admin/settings.ts b/ui/src/services/admin/settings.ts
index 274e24eb..5f370260 100644
--- a/ui/src/services/admin/settings.ts
+++ b/ui/src/services/admin/settings.ts
@@ -70,3 +70,16 @@ export const updateSmtpSetting = (params: Type.AdminSettingsSmtp) => {
const apiUrl = `/answer/admin/api/setting/smtp`;
return request.put(apiUrl, params);
};
+
+export const useDashBoard = () => {
+ const apiUrl = `/answer/admin/api/dashboard`;
+ const { data, error } = useSWR<Type.AdminDashboard, Error>(
+ [apiUrl],
+ request.instance.get,
+ );
+ return {
+ data,
+ isLoading: !data && !error,
+ error,
+ };
+};
diff --git a/ui/src/utils/common.ts b/ui/src/utils/common.ts
index 169dc1ad..dcc2d084 100644
--- a/ui/src/utils/common.ts
+++ b/ui/src/utils/common.ts
@@ -1,3 +1,5 @@
+import i18next from 'i18next';
+
function getQueryString(name: string): string {
const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
const r = window.location.search.substr(1).match(reg);
@@ -70,6 +72,19 @@ function parseUserInfo(markdown) {
return markdown.replace(globalReg, '[@$1](/u/$1)');
}
+function formatUptime(value) {
+ const t = i18next.t.bind(i18next);
+ const second = parseInt(value, 10);
+
+ if (second > 60 * 60 && second < 60 * 60 * 24) {
+ return `${Math.floor(second / 3600)} ${t('dates.hour')}`;
+ }
+ if (second > 60 * 60 * 24) {
+ return `${Math.floor(second / 3600 / 24)} ${t('dates.day')}`;
+ }
+
+ return `< 1 ${t('dates.hour')}`;
+}
export {
getQueryString,
thousandthDivision,
@@ -77,4 +92,5 @@ export {
scrollTop,
matchedUsers,
parseUserInfo,
+ formatUptime,
};