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.

refactor(router-guard): refactor router guard

haitao(lj)02de299c8692
diff --git a/ui/.eslintrc.js b/ui/.eslintrc.js
index 877a0039..98edc408 100644
--- a/ui/.eslintrc.js
+++ b/ui/.eslintrc.js
@@ -64,7 +64,7 @@ module.exports = {
position: 'before',
},
{
- pattern: '@answer/**',
+ pattern: '@/**',
group: 'internal',
},
{
diff --git a/ui/config-overrides.js b/ui/config-overrides.js
index 06464361..ac52fa63 100644
--- a/ui/config-overrides.js
+++ b/ui/config-overrides.js
@@ -8,13 +8,6 @@ module.exports = {
config.resolve.alias = {
...config.resolve.alias,
'@': path.resolve(__dirname, 'src'),
- '@answer/pages': path.resolve(__dirname, 'src/pages'),
- '@answer/components': path.resolve(__dirname, 'src/components'),
- '@answer/stores': path.resolve(__dirname, 'src/stores'),
- '@answer/hooks': path.resolve(__dirname, 'src/hooks'),
- '@answer/utils': path.resolve(__dirname, 'src/utils'),
- '@answer/common': path.resolve(__dirname, 'src/common'),
- '@answer/api': path.resolve(__dirname, 'src/services/api'),
};
return config;
diff --git a/ui/src/components/AccordionNav/index.tsx b/ui/src/components/AccordionNav/index.tsx
index ccc14d44..b3403bc8 100644
--- a/ui/src/components/AccordionNav/index.tsx
+++ b/ui/src/components/AccordionNav/index.tsx
@@ -5,7 +5,7 @@ import { useNavigate, useMatch } from 'react-router-dom';
import { useAccordionButton } from 'react-bootstrap/AccordionButton';
-import { Icon } from '@answer/components';
+import { Icon } from '@/components';
function MenuNode({ menu, callback, activeKey, isLeaf = false }) {
const { t } = useTranslation('translation', { keyPrefix: 'admin.nav_menus' });
diff --git a/ui/src/components/Actions/index.tsx b/ui/src/components/Actions/index.tsx
index 25402ab2..d9395723 100644
--- a/ui/src/components/Actions/index.tsx
+++ b/ui/src/components/Actions/index.tsx
@@ -4,11 +4,10 @@ import { useTranslation } from 'react-i18next';
import classNames from 'classnames';
-import { Icon } from '@answer/components';
-import { loggedUserInfoStore } from '@answer/stores';
-import { useToast } from '@answer/hooks';
-
-import { tryNormalLogged } from '@/utils/guards';
+import { Icon } from '@/components';
+import { loggedUserInfoStore } from '@/stores';
+import { useToast } from '@/hooks';
+import { tryNormalLogged } from '@/utils/guard';
import { bookmark, postVote } from '@/services';
interface Props {
diff --git a/ui/src/components/BaseUserCard/index.tsx b/ui/src/components/BaseUserCard/index.tsx
index fa524bda..d170d3f8 100644
--- a/ui/src/components/BaseUserCard/index.tsx
+++ b/ui/src/components/BaseUserCard/index.tsx
@@ -1,8 +1,7 @@
import { memo, FC } from 'react';
import { Link } from 'react-router-dom';
-import { Avatar } from '@answer/components';
-
+import { Avatar } from '@/components';
import { formatCount } from '@/utils';
interface Props {
diff --git a/ui/src/components/Comment/components/ActionBar/index.tsx b/ui/src/components/Comment/components/ActionBar/index.tsx
index a801a6bb..a13bff1a 100644
--- a/ui/src/components/Comment/components/ActionBar/index.tsx
+++ b/ui/src/components/Comment/components/ActionBar/index.tsx
@@ -5,7 +5,7 @@ import { Link } from 'react-router-dom';
import classNames from 'classnames';
-import { Icon, FormatTime } from '@answer/components';
+import { Icon, FormatTime } from '@/components';
const ActionBar = ({
nickName,
diff --git a/ui/src/components/Comment/components/Form/index.tsx b/ui/src/components/Comment/components/Form/index.tsx
index 4971c4ef..70c62fdd 100644
--- a/ui/src/components/Comment/components/Form/index.tsx
+++ b/ui/src/components/Comment/components/Form/index.tsx
@@ -4,8 +4,8 @@ import { useTranslation } from 'react-i18next';
import classNames from 'classnames';
-import { TextArea, Mentions } from '@answer/components';
-import { usePageUsers } from '@answer/hooks';
+import { TextArea, Mentions } from '@/components';
+import { usePageUsers } from '@/hooks';
const Form = ({
className = '',
diff --git a/ui/src/components/Comment/components/Reply/index.tsx b/ui/src/components/Comment/components/Reply/index.tsx
index dcbf53bb..16b04822 100644
--- a/ui/src/components/Comment/components/Reply/index.tsx
+++ b/ui/src/components/Comment/components/Reply/index.tsx
@@ -2,8 +2,8 @@ import { useState, memo } from 'react';
import { Button } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import { TextArea, Mentions } from '@answer/components';
-import { usePageUsers } from '@answer/hooks';
+import { TextArea, Mentions } from '@/components';
+import { usePageUsers } from '@/hooks';
const Form = ({ userName, onSendReply, onCancel, mode }) => {
const [value, setValue] = useState('');
diff --git a/ui/src/components/Comment/index.tsx b/ui/src/components/Comment/index.tsx
index a6dcdc98..1f57e737 100644
--- a/ui/src/components/Comment/index.tsx
+++ b/ui/src/components/Comment/index.tsx
@@ -7,14 +7,11 @@ import classNames from 'classnames';
import { unionBy } from 'lodash';
import { marked } from 'marked';
-import * as Types from '@answer/common/interface';
-import { Modal } from '@answer/components';
-import { usePageUsers, useReportModal } from '@answer/hooks';
-import { matchedUsers, parseUserInfo } from '@answer/utils';
-
-import { Form, ActionBar, Reply } from './components';
-
-import { tryNormalLogged } from '@/utils/guards';
+import * as Types from '@/common/interface';
+import { Modal } from '@/components';
+import { usePageUsers, useReportModal } from '@/hooks';
+import { matchedUsers, parseUserInfo } from '@/utils';
+import { tryNormalLogged } from '@/utils/guard';
import {
useQueryComments,
addComment,
@@ -23,6 +20,8 @@ import {
postVote,
} from '@/services';
+import { Form, ActionBar, Reply } from './components';
+
import './index.scss';
const Comment = ({ objectId, mode }) => {
diff --git a/ui/src/components/Editor/ToolBars/image.tsx b/ui/src/components/Editor/ToolBars/image.tsx
index ead20eee..bfa4c530 100644
--- a/ui/src/components/Editor/ToolBars/image.tsx
+++ b/ui/src/components/Editor/ToolBars/image.tsx
@@ -2,10 +2,9 @@ import { FC, useEffect, useState, memo } from 'react';
import { Button, Form, Modal, Tab, Tabs } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import { Modal as AnswerModal } from '@answer/components';
+import { Modal as AnswerModal } from '@/components';
import ToolItem from '../toolItem';
import { IEditorContext } from '../types';
-
import { uploadImage } from '@/services';
const Image: FC<IEditorContext> = ({ editor }) => {
diff --git a/ui/src/components/FollowingTags/index.tsx b/ui/src/components/FollowingTags/index.tsx
index 55e9bc76..06fec6ec 100644
--- a/ui/src/components/FollowingTags/index.tsx
+++ b/ui/src/components/FollowingTags/index.tsx
@@ -3,9 +3,8 @@ import { Card, Button } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
import { NavLink } from 'react-router-dom';
-import { TagSelector, Tag } from '@answer/components';
-
-import { tryNormalLogged } from '@/utils/guards';
+import { TagSelector, Tag } from '@/components';
+import { tryNormalLogged } from '@/utils/guard';
import { useFollowingTags, followTags } from '@/services';
const Index: FC = () => {
diff --git a/ui/src/components/Header/components/NavItems/index.tsx b/ui/src/components/Header/components/NavItems/index.tsx
index 67112758..8a4872d9 100644
--- a/ui/src/components/Header/components/NavItems/index.tsx
+++ b/ui/src/components/Header/components/NavItems/index.tsx
@@ -3,7 +3,7 @@ import { Nav, Dropdown } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
import { Link, NavLink } from 'react-router-dom';
-import { Avatar, Icon } from '@answer/components';
+import { Avatar, Icon } from '@/components';
interface Props {
redDot;
diff --git a/ui/src/components/Header/index.tsx b/ui/src/components/Header/index.tsx
index b4cd4e16..748eb318 100644
--- a/ui/src/components/Header/index.tsx
+++ b/ui/src/components/Header/index.tsx
@@ -17,17 +17,12 @@ import {
useLocation,
} from 'react-router-dom';
-import {
- loggedUserInfoStore,
- siteInfoStore,
- interfaceStore,
-} from '@answer/stores';
-
-import NavItems from './components/NavItems';
-
+import { loggedUserInfoStore, siteInfoStore, interfaceStore } from '@/stores';
import { logout, useQueryNotificationStatus } from '@/services';
import { RouteAlias } from '@/router/alias';
+import NavItems from './components/NavItems';
+
import './index.scss';
const Header: FC = () => {
diff --git a/ui/src/components/HotQuestions/index.tsx b/ui/src/components/HotQuestions/index.tsx
index 920bdce2..da0305d0 100644
--- a/ui/src/components/HotQuestions/index.tsx
+++ b/ui/src/components/HotQuestions/index.tsx
@@ -3,8 +3,7 @@ import { Card, ListGroup, ListGroupItem } from 'react-bootstrap';
import { Link } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
-import { Icon } from '@answer/components';
-
+import { Icon } from '@/components';
import { useHotQuestions } from '@/services';
const HotQuestions: FC = () => {
diff --git a/ui/src/components/Mentions/index.tsx b/ui/src/components/Mentions/index.tsx
index 9b29c86d..23cfa083 100644
--- a/ui/src/components/Mentions/index.tsx
+++ b/ui/src/components/Mentions/index.tsx
@@ -1,7 +1,7 @@
import React, { useEffect, useRef, useState, FC } from 'react';
import { Dropdown } from 'react-bootstrap';
-import * as Types from '@answer/common/interface';
+import * as Types from '@/common/interface';
interface IProps {
children: React.ReactNode;
diff --git a/ui/src/components/Modal/PicAuthCodeModal.tsx b/ui/src/components/Modal/PicAuthCodeModal.tsx
index 3a081a0c..ec194a64 100644
--- a/ui/src/components/Modal/PicAuthCodeModal.tsx
+++ b/ui/src/components/Modal/PicAuthCodeModal.tsx
@@ -2,13 +2,8 @@ import React from 'react';
import { Modal, Form, Button, InputGroup } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import { Icon } from '@answer/components';
-import type {
- FormValue,
- FormDataType,
- ImgCodeRes,
-} from '@answer/common/interface';
-
+import { Icon } from '@/components';
+import type { FormValue, FormDataType, ImgCodeRes } from '@/common/interface';
import { CAPTCHA_CODE_STORAGE_KEY } from '@/common/constants';
import Storage from '@/utils/storage';
diff --git a/ui/src/components/Operate/index.tsx b/ui/src/components/Operate/index.tsx
index 201774c6..cdacc751 100644
--- a/ui/src/components/Operate/index.tsx
+++ b/ui/src/components/Operate/index.tsx
@@ -3,12 +3,11 @@ import { Button } from 'react-bootstrap';
import { Link } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
-import { Modal } from '@answer/components';
-import { useReportModal, useToast } from '@answer/hooks';
+import { Modal } from '@/components';
+import { useReportModal, useToast } from '@/hooks';
import Share from '../Share';
-
import { deleteQuestion, deleteAnswer } from '@/services';
-import { tryNormalLogged } from '@/utils/guards';
+import { tryNormalLogged } from '@/utils/guard';
interface IProps {
type: 'answer' | 'question';
diff --git a/ui/src/components/QuestionList/index.tsx b/ui/src/components/QuestionList/index.tsx
index 85b72ce1..76a6ccd9 100644
--- a/ui/src/components/QuestionList/index.tsx
+++ b/ui/src/components/QuestionList/index.tsx
@@ -3,7 +3,7 @@ import { Row, Col, ListGroup } from 'react-bootstrap';
import { NavLink, useParams, useSearchParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
-import type * as Type from '@answer/common/interface';
+import type * as Type from '@/common/interface';
import {
Icon,
Tag,
@@ -12,8 +12,7 @@ import {
Empty,
BaseUserCard,
QueryGroup,
-} from '@answer/components';
-
+} from '@/components';
import { useQuestionList } from '@/services';
const QuestionOrderKeys: Type.QuestionOrderBy[] = [
diff --git a/ui/src/components/Share/index.tsx b/ui/src/components/Share/index.tsx
index 74b3de34..652dcef1 100644
--- a/ui/src/components/Share/index.tsx
+++ b/ui/src/components/Share/index.tsx
@@ -5,7 +5,7 @@ import { useTranslation } from 'react-i18next';
import { FacebookShareButton, TwitterShareButton } from 'next-share';
import copy from 'copy-to-clipboard';
-import { loggedUserInfoStore } from '@answer/stores';
+import { loggedUserInfoStore } from '@/stores';
interface IProps {
type: 'answer' | 'question';
diff --git a/ui/src/components/TagSelector/index.tsx b/ui/src/components/TagSelector/index.tsx
index cedb85a3..8747f65a 100644
--- a/ui/src/components/TagSelector/index.tsx
+++ b/ui/src/components/TagSelector/index.tsx
@@ -5,9 +5,8 @@ import { useTranslation } from 'react-i18next';
import { marked } from 'marked';
import classNames from 'classnames';
-import { useTagModal } from '@answer/hooks';
-import type * as Type from '@answer/common/interface';
-
+import { useTagModal } from '@/hooks';
+import type * as Type from '@/common/interface';
import { queryTags } from '@/services';
import './index.scss';
diff --git a/ui/src/components/Unactivate/index.tsx b/ui/src/components/Unactivate/index.tsx
index 6b7a26bf..420891cc 100644
--- a/ui/src/components/Unactivate/index.tsx
+++ b/ui/src/components/Unactivate/index.tsx
@@ -3,14 +3,9 @@ import { Button, Col } from 'react-bootstrap';
import { Trans, useTranslation } from 'react-i18next';
import { Link } from 'react-router-dom';
-import { PicAuthCodeModal } from '@answer/components/Modal';
-import type {
- ImgCodeRes,
- ImgCodeReq,
- FormDataType,
-} from '@answer/common/interface';
-import { loggedUserInfoStore } from '@answer/stores';
-
+import { PicAuthCodeModal } from '@/components/Modal';
+import type { ImgCodeRes, ImgCodeReq, FormDataType } from '@/common/interface';
+import { loggedUserInfoStore } from '@/stores';
import { resendEmail, checkImgCode } from '@/services';
import { CAPTCHA_CODE_STORAGE_KEY } from '@/common/constants';
import Storage from '@/utils/storage';
diff --git a/ui/src/components/UserCard/index.tsx b/ui/src/components/UserCard/index.tsx
index 0110c82a..27441231 100644
--- a/ui/src/components/UserCard/index.tsx
+++ b/ui/src/components/UserCard/index.tsx
@@ -3,8 +3,7 @@ import { Link } from 'react-router-dom';
import classnames from 'classnames';
-import { Avatar, FormatTime } from '@answer/components';
-
+import { Avatar, FormatTime } from '@/components';
import { formatCount } from '@/utils';
interface Props {
diff --git a/ui/src/hooks/useChangeModal/index.tsx b/ui/src/hooks/useChangeModal/index.tsx
index f484331c..529f7a3a 100644
--- a/ui/src/hooks/useChangeModal/index.tsx
+++ b/ui/src/hooks/useChangeModal/index.tsx
@@ -4,8 +4,7 @@ import { useTranslation } from 'react-i18next';
import ReactDOM from 'react-dom/client';
-import { Modal as AnswerModal } from '@answer/components';
-
+import { Modal as AnswerModal } from '@/components';
import { changeUserStatus } from '@/services';
const div = document.createElement('div');
diff --git a/ui/src/hooks/usePageUsers/index.tsx b/ui/src/hooks/usePageUsers/index.tsx
index 72203619..2c828695 100644
--- a/ui/src/hooks/usePageUsers/index.tsx
+++ b/ui/src/hooks/usePageUsers/index.tsx
@@ -2,7 +2,7 @@ import { useState } from 'react';
import { uniqBy } from 'lodash';
-import * as Types from '@answer/common/interface';
+import * as Types from '@/common/interface';
let globalUsers: Types.PageUser[] = [];
const usePageUsers = () => {
diff --git a/ui/src/hooks/useReportModal/index.tsx b/ui/src/hooks/useReportModal/index.tsx
index 2f7c8f18..28b85eb5 100644
--- a/ui/src/hooks/useReportModal/index.tsx
+++ b/ui/src/hooks/useReportModal/index.tsx
@@ -4,9 +4,8 @@ import { useTranslation } from 'react-i18next';
import ReactDOM from 'react-dom/client';
-import { useToast } from '@answer/hooks';
-import type * as Type from '@answer/common/interface';
-
+import { useToast } from '@/hooks';
+import type * as Type from '@/common/interface';
import { reportList, postReport, closeQuestion, putReport } from '@/services';
interface Params {
diff --git a/ui/src/i18n/init.ts b/ui/src/i18n/init.ts
index deecc0e2..e8b41e31 100644
--- a/ui/src/i18n/init.ts
+++ b/ui/src/i18n/init.ts
@@ -3,11 +3,11 @@ import { initReactI18next } from 'react-i18next';
import i18next from 'i18next';
import Backend from 'i18next-http-backend';
+import { DEFAULT_LANG } from '@/common/constants';
+
import en from './locales/en.json';
import zh from './locales/zh_CN.json';
-import { DEFAULT_LANG } from '@/common/constants';
-
i18next
// load translation using http
.use(Backend)
diff --git a/ui/src/index.tsx b/ui/src/index.tsx
index 1943b61a..67e5ee50 100644
--- a/ui/src/index.tsx
+++ b/ui/src/index.tsx
@@ -2,9 +2,9 @@ import React from 'react';
import ReactDOM from 'react-dom/client';
-import App from './App';
+import { Guard } from '@/utils';
-import { pullLoggedUser } from '@/utils/guards';
+import App from './App';
import './i18n/init';
import './index.scss';
@@ -17,7 +17,7 @@ async function bootstrapApp() {
/**
* NOTICE: must pre init logged user info for router
*/
- await pullLoggedUser();
+ await Guard.pullLoggedUser();
root.render(
<React.StrictMode>
<App />
diff --git a/ui/src/pages/Admin/Answers/index.tsx b/ui/src/pages/Admin/Answers/index.tsx
index 2a25f53c..2c14c6d7 100644
--- a/ui/src/pages/Admin/Answers/index.tsx
+++ b/ui/src/pages/Admin/Answers/index.tsx
@@ -11,11 +11,10 @@ import {
BaseUserCard,
Empty,
QueryGroup,
-} from '@answer/components';
-import { ADMIN_LIST_STATUS } from '@answer/common/constants';
-import { useEditStatusModal } from '@answer/hooks';
-import * as Type from '@answer/common/interface';
-
+} from '@/components';
+import { ADMIN_LIST_STATUS } from '@/common/constants';
+import { useEditStatusModal } from '@/hooks';
+import * as Type from '@/common/interface';
import { useAnswerSearch, changeAnswerStatus } from '@/services';
import '../index.scss';
diff --git a/ui/src/pages/Admin/Flags/index.tsx b/ui/src/pages/Admin/Flags/index.tsx
index a7842429..353bd382 100644
--- a/ui/src/pages/Admin/Flags/index.tsx
+++ b/ui/src/pages/Admin/Flags/index.tsx
@@ -9,10 +9,9 @@ import {
Empty,
Pagination,
QueryGroup,
-} from '@answer/components';
-import { useReportModal } from '@answer/hooks';
-import * as Type from '@answer/common/interface';
-
+} from '@/components';
+import { useReportModal } from '@/hooks';
+import * as Type from '@/common/interface';
import { useFlagSearch } from '@/services';
import '../index.scss';
diff --git a/ui/src/pages/Admin/General/index.tsx b/ui/src/pages/Admin/General/index.tsx
index 3154caec..14b95978 100644
--- a/ui/src/pages/Admin/General/index.tsx
+++ b/ui/src/pages/Admin/General/index.tsx
@@ -2,10 +2,9 @@ import React, { FC, useEffect, useState } from 'react';
import { Form, Button } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import type * as Type from '@answer/common/interface';
-import { useToast } from '@answer/hooks';
-import { siteInfoStore } from '@answer/stores';
-
+import type * as Type from '@/common/interface';
+import { useToast } from '@/hooks';
+import { siteInfoStore } from '@/stores';
import { useGeneralSetting, updateGeneralSetting } from '@/services';
import '../index.scss';
diff --git a/ui/src/pages/Admin/Interface/index.tsx b/ui/src/pages/Admin/Interface/index.tsx
index e5d68f32..3f179c57 100644
--- a/ui/src/pages/Admin/Interface/index.tsx
+++ b/ui/src/pages/Admin/Interface/index.tsx
@@ -2,15 +2,14 @@ import React, { FC, FormEvent, useEffect, useState } from 'react';
import { Form, Button, Image, Stack } from 'react-bootstrap';
import { Trans, useTranslation } from 'react-i18next';
-import { useToast } from '@answer/hooks';
+import { useToast } from '@/hooks';
import {
LangsType,
FormDataType,
AdminSettingsInterface,
-} from '@answer/common/interface';
-import { interfaceStore } from '@answer/stores';
-import { UploadImg } from '@answer/components';
-
+} from '@/common/interface';
+import { interfaceStore } from '@/stores';
+import { UploadImg } from '@/components';
import {
languages,
uploadAvatar,
diff --git a/ui/src/pages/Admin/Questions/index.tsx b/ui/src/pages/Admin/Questions/index.tsx
index 1a188512..e27a715c 100644
--- a/ui/src/pages/Admin/Questions/index.tsx
+++ b/ui/src/pages/Admin/Questions/index.tsx
@@ -11,11 +11,10 @@ import {
BaseUserCard,
Empty,
QueryGroup,
-} from '@answer/components';
-import { ADMIN_LIST_STATUS } from '@answer/common/constants';
-import { useEditStatusModal, useReportModal } from '@answer/hooks';
-import * as Type from '@answer/common/interface';
-
+} from '@/components';
+import { ADMIN_LIST_STATUS } from '@/common/constants';
+import { useEditStatusModal, useReportModal } from '@/hooks';
+import * as Type from '@/common/interface';
import {
useQuestionSearch,
changeQuestionStatus,
diff --git a/ui/src/pages/Admin/Smtp/index.tsx b/ui/src/pages/Admin/Smtp/index.tsx
index 17abd481..33de30ab 100644
--- a/ui/src/pages/Admin/Smtp/index.tsx
+++ b/ui/src/pages/Admin/Smtp/index.tsx
@@ -2,9 +2,8 @@ import React, { FC, useEffect, useState } from 'react';
import { Form, Button, Stack } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import type * as Type from '@answer/common/interface';
-import { useToast } from '@answer/hooks';
-
+import type * as Type from '@/common/interface';
+import { useToast } from '@/hooks';
import { useSmtpSetting, updateSmtpSetting } from '@/services';
import pattern from '@/common/pattern';
diff --git a/ui/src/pages/Admin/Users/index.tsx b/ui/src/pages/Admin/Users/index.tsx
index de434084..a14f6b74 100644
--- a/ui/src/pages/Admin/Users/index.tsx
+++ b/ui/src/pages/Admin/Users/index.tsx
@@ -9,10 +9,9 @@ import {
BaseUserCard,
Empty,
QueryGroup,
-} from '@answer/components';
-import * as Type from '@answer/common/interface';
-import { useChangeModal } from '@answer/hooks';
-
+} from '@/components';
+import * as Type from '@/common/interface';
+import { useChangeModal } from '@/hooks';
import { useQueryUsers } from '@/services';
import '../index.scss';
diff --git a/ui/src/pages/Admin/index.tsx b/ui/src/pages/Admin/index.tsx
index 8d7b452e..27ef8565 100644
--- a/ui/src/pages/Admin/index.tsx
+++ b/ui/src/pages/Admin/index.tsx
@@ -3,8 +3,8 @@ import { Container, Row, Col } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
import { Outlet } from 'react-router-dom';
-import { AccordionNav, PageTitle } from '@answer/components';
-import { ADMIN_NAV_MENUS } from '@answer/common/constants';
+import { AccordionNav, PageTitle } from '@/components';
+import { ADMIN_NAV_MENUS } from '@/common/constants';
import './index.scss';
diff --git a/ui/src/pages/Layout/index.tsx b/ui/src/pages/Layout/index.tsx
index 7affe2f0..363c9b01 100644
--- a/ui/src/pages/Layout/index.tsx
+++ b/ui/src/pages/Layout/index.tsx
@@ -5,9 +5,8 @@ import { Helmet, HelmetProvider } from 'react-helmet-async';
import { SWRConfig } from 'swr';
-import { siteInfoStore, interfaceStore, toastStore } from '@answer/stores';
-import { Header, AdminHeader, Footer, Toast } from '@answer/components';
-
+import { siteInfoStore, interfaceStore, toastStore } from '@/stores';
+import { Header, AdminHeader, Footer, Toast } from '@/components';
import { useSiteSettings } from '@/services';
import Storage from '@/utils/storage';
import { CURRENT_LANG_STORAGE_KEY } from '@/common/constants';
diff --git a/ui/src/pages/Questions/Ask/components/SearchQuestion/index.tsx b/ui/src/pages/Questions/Ask/components/SearchQuestion/index.tsx
index 16388963..99a8026a 100644
--- a/ui/src/pages/Questions/Ask/components/SearchQuestion/index.tsx
+++ b/ui/src/pages/Questions/Ask/components/SearchQuestion/index.tsx
@@ -2,7 +2,7 @@ import { memo } from 'react';
import { Accordion, ListGroup } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import { Icon } from '@answer/components';
+import { Icon } from '@/components';
import './index.scss';
diff --git a/ui/src/pages/Questions/Ask/index.tsx b/ui/src/pages/Questions/Ask/index.tsx
index f233150e..fe4d3319 100644
--- a/ui/src/pages/Questions/Ask/index.tsx
+++ b/ui/src/pages/Questions/Ask/index.tsx
@@ -6,11 +6,8 @@ import { useTranslation } from 'react-i18next';
import dayjs from 'dayjs';
import classNames from 'classnames';
-import { Editor, EditorRef, TagSelector, PageTitle } from '@answer/components';
-import type * as Type from '@answer/common/interface';
-
-import SearchQuestion from './components/SearchQuestion';
-
+import { Editor, EditorRef, TagSelector, PageTitle } from '@/components';
+import type * as Type from '@/common/interface';
import {
saveQuestion,
questionDetail,
@@ -20,6 +17,8 @@ import {
useQueryQuestionByTitle,
} from '@/services';
+import SearchQuestion from './components/SearchQuestion';
+
interface FormDataItem {
title: Type.FormValue<string>;
tags: Type.FormValue<Type.Tag[]>;
diff --git a/ui/src/pages/Questions/Detail/components/Answer/index.tsx b/ui/src/pages/Questions/Detail/components/Answer/index.tsx
index e97238cf..2fe87d1c 100644
--- a/ui/src/pages/Questions/Detail/components/Answer/index.tsx
+++ b/ui/src/pages/Questions/Detail/components/Answer/index.tsx
@@ -10,10 +10,9 @@ import {
Comment,
FormatTime,
htmlRender,
-} from '@answer/components';
-import { scrollTop } from '@answer/utils';
-import { AnswerItem } from '@answer/common/interface';
-
+} from '@/components';
+import { scrollTop } from '@/utils';
+import { AnswerItem } from '@/common/interface';
import { acceptanceAnswer } from '@/services';
interface Props {
diff --git a/ui/src/pages/Questions/Detail/components/AnswerHead/index.tsx b/ui/src/pages/Questions/Detail/components/AnswerHead/index.tsx
index f2c7efe9..32abc17b 100644
--- a/ui/src/pages/Questions/Detail/components/AnswerHead/index.tsx
+++ b/ui/src/pages/Questions/Detail/components/AnswerHead/index.tsx
@@ -1,7 +1,7 @@
import { memo, FC } from 'react';
import { useTranslation } from 'react-i18next';
-import { QueryGroup } from '@answer/components';
+import { QueryGroup } from '@/components';
interface Props {
count: number;
diff --git a/ui/src/pages/Questions/Detail/components/Question/index.tsx b/ui/src/pages/Questions/Detail/components/Question/index.tsx
index aa3a38ec..1b46a869 100644
--- a/ui/src/pages/Questions/Detail/components/Question/index.tsx
+++ b/ui/src/pages/Questions/Detail/components/Question/index.tsx
@@ -11,9 +11,8 @@ import {
Comment,
FormatTime,
htmlRender,
-} from '@answer/components';
-import { formatCount } from '@answer/utils';
-
+} from '@/components';
+import { formatCount } from '@/utils';
import { following } from '@/services';
interface Props {
diff --git a/ui/src/pages/Questions/Detail/components/RelatedQuestions/index.tsx b/ui/src/pages/Questions/Detail/components/RelatedQuestions/index.tsx
index 8493408c..3dd91c7f 100644
--- a/ui/src/pages/Questions/Detail/components/RelatedQuestions/index.tsx
+++ b/ui/src/pages/Questions/Detail/components/RelatedQuestions/index.tsx
@@ -3,8 +3,7 @@ import { Card, ListGroup } from 'react-bootstrap';
import { Link } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
-import { Icon } from '@answer/components';
-
+import { Icon } from '@/components';
import { useSimilarQuestion } from '@/services';
import { loggedUserInfoStore } from '@/stores';
diff --git a/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx b/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx
index 9659b286..8d1ef1be 100644
--- a/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx
+++ b/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx
@@ -5,9 +5,8 @@ import { useTranslation } from 'react-i18next';
import { marked } from 'marked';
import classNames from 'classnames';
-import { Editor, Modal } from '@answer/components';
-import { FormDataType } from '@answer/common/interface';
-
+import { Editor, Modal } from '@/components';
+import { FormDataType } from '@/common/interface';
import { postAnswer } from '@/services';
interface Props {
diff --git a/ui/src/pages/Questions/Detail/index.tsx b/ui/src/pages/Questions/Detail/index.tsx
index 2f469415..cc19a719 100644
--- a/ui/src/pages/Questions/Detail/index.tsx
+++ b/ui/src/pages/Questions/Detail/index.tsx
@@ -2,15 +2,16 @@ import { useEffect, useState } from 'react';
import { Container, Row, Col } from 'react-bootstrap';
import { useParams, useSearchParams, useNavigate } from 'react-router-dom';
-import { Pagination, PageTitle } from '@answer/components';
-import { loggedUserInfoStore } from '@answer/stores';
-import { scrollTop } from '@answer/utils';
-import { usePageUsers } from '@answer/hooks';
+import { Pagination, PageTitle } from '@/components';
+import { loggedUserInfoStore } from '@/stores';
+import { scrollTop } from '@/utils';
+import { usePageUsers } from '@/hooks';
import type {
ListResult,
QuestionDetailRes,
AnswerItem,
-} from '@answer/common/interface';
+} from '@/common/interface';
+import { questionDetail, getAnswers } from '@/services';
import {
Question,
@@ -21,8 +22,6 @@ import {
Alert,
} from './components';
-import { questionDetail, getAnswers } from '@/services';
-
import './index.scss';
const Index = () => {
diff --git a/ui/src/pages/Questions/EditAnswer/index.tsx b/ui/src/pages/Questions/EditAnswer/index.tsx
index c87733f4..0fab103c 100644
--- a/ui/src/pages/Questions/EditAnswer/index.tsx
+++ b/ui/src/pages/Questions/EditAnswer/index.tsx
@@ -6,9 +6,8 @@ import { useTranslation } from 'react-i18next';
import dayjs from 'dayjs';
import classNames from 'classnames';
-import { Editor, EditorRef, Icon, PageTitle } from '@answer/components';
-import type * as Type from '@answer/common/interface';
-
+import { Editor, EditorRef, Icon, PageTitle } from '@/components';
+import type * as Type from '@/common/interface';
import {
useQueryAnswerInfo,
modifyAnswer,
diff --git a/ui/src/pages/Questions/index.tsx b/ui/src/pages/Questions/index.tsx
index 9e7ed318..ea10ec15 100644
--- a/ui/src/pages/Questions/index.tsx
+++ b/ui/src/pages/Questions/index.tsx
@@ -3,8 +3,7 @@ import { Container, Row, Col } from 'react-bootstrap';
import { useMatch } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
-import { PageTitle, FollowingTags } from '@answer/components';
-
+import { PageTitle, FollowingTags } from '@/components';
import QuestionList from '@/components/QuestionList';
import HotQuestions from '@/components/HotQuestions';
import { siteInfoStore } from '@/stores';
diff --git a/ui/src/pages/Search/components/Head/index.tsx b/ui/src/pages/Search/components/Head/index.tsx
index 1558bc35..095796d2 100644
--- a/ui/src/pages/Search/components/Head/index.tsx
+++ b/ui/src/pages/Search/components/Head/index.tsx
@@ -4,7 +4,7 @@ import { Button } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
import { following } from '@/services';
-import { tryNormalLogged } from '@/utils/guards';
+import { tryNormalLogged } from '@/utils/guard';
interface Props {
data;
diff --git a/ui/src/pages/Search/components/SearchHead/index.tsx b/ui/src/pages/Search/components/SearchHead/index.tsx
index 2d7549a4..fb185bc1 100644
--- a/ui/src/pages/Search/components/SearchHead/index.tsx
+++ b/ui/src/pages/Search/components/SearchHead/index.tsx
@@ -2,7 +2,7 @@ import { FC, memo } from 'react';
import { ListGroupItem } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import { QueryGroup } from '@answer/components';
+import { QueryGroup } from '@/components';
const sortBtns = ['relevance', 'newest', 'active', 'score'];
diff --git a/ui/src/pages/Search/components/SearchItem/index.tsx b/ui/src/pages/Search/components/SearchItem/index.tsx
index ec703277..09cecd32 100644
--- a/ui/src/pages/Search/components/SearchItem/index.tsx
+++ b/ui/src/pages/Search/components/SearchItem/index.tsx
@@ -2,8 +2,8 @@ import { memo, FC } from 'react';
import { ListGroupItem, Badge } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import { Icon, Tag, FormatTime, BaseUserCard } from '@answer/components';
-import type { SearchResItem } from '@answer/common/interface';
+import { Icon, Tag, FormatTime, BaseUserCard } from '@/components';
+import type { SearchResItem } from '@/common/interface';
interface Props {
data: SearchResItem;
diff --git a/ui/src/pages/Search/index.tsx b/ui/src/pages/Search/index.tsx
index 914565ae..f22dc3ce 100644
--- a/ui/src/pages/Search/index.tsx
+++ b/ui/src/pages/Search/index.tsx
@@ -3,12 +3,11 @@ import { Container, Row, Col, ListGroup } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
import { useSearchParams } from 'react-router-dom';
-import { Pagination, PageTitle } from '@answer/components';
+import { Pagination, PageTitle } from '@/components';
+import { useSearch } from '@/services';
import { Head, SearchHead, SearchItem, Tips, Empty } from './components';
-import { useSearch } from '@/services';
-
const Index = () => {
const { t } = useTranslation('translation');
const [searchParams] = useSearchParams();
diff --git a/ui/src/pages/Tags/Detail/index.tsx b/ui/src/pages/Tags/Detail/index.tsx
index e37ab0ab..452ef37d 100644
--- a/ui/src/pages/Tags/Detail/index.tsx
+++ b/ui/src/pages/Tags/Detail/index.tsx
@@ -3,9 +3,8 @@ import { Container, Row, Col, Button } from 'react-bootstrap';
import { useParams, Link, useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
-import * as Type from '@answer/common/interface';
-import { PageTitle, FollowingTags } from '@answer/components';
-
+import * as Type from '@/common/interface';
+import { PageTitle, FollowingTags } from '@/components';
import { useTagInfo, useFollow } from '@/services';
import QuestionList from '@/components/QuestionList';
import HotQuestions from '@/components/HotQuestions';
diff --git a/ui/src/pages/Tags/Edit/index.tsx b/ui/src/pages/Tags/Edit/index.tsx
index 58e7d59c..890bd32e 100644
--- a/ui/src/pages/Tags/Edit/index.tsx
+++ b/ui/src/pages/Tags/Edit/index.tsx
@@ -6,10 +6,9 @@ import { useTranslation } from 'react-i18next';
import dayjs from 'dayjs';
import classNames from 'classnames';
-import { Editor, EditorRef, PageTitle } from '@answer/components';
-import { loggedUserInfoStore } from '@answer/stores';
-import type * as Type from '@answer/common/interface';
-
+import { Editor, EditorRef, PageTitle } from '@/components';
+import { loggedUserInfoStore } from '@/stores';
+import type * as Type from '@/common/interface';
import { useTagInfo, modifyTag, useQueryRevisions } from '@/services';
interface FormDataItem {
diff --git a/ui/src/pages/Tags/Info/index.tsx b/ui/src/pages/Tags/Info/index.tsx
index d8c4ad75..3b792adb 100644
--- a/ui/src/pages/Tags/Info/index.tsx
+++ b/ui/src/pages/Tags/Info/index.tsx
@@ -5,14 +5,7 @@ import { useTranslation } from 'react-i18next';
import classNames from 'classnames';
-import {
- Tag,
- TagSelector,
- FormatTime,
- Modal,
- PageTitle,
-} from '@answer/components';
-
+import { Tag, TagSelector, FormatTime, Modal, PageTitle } from '@/components';
import {
useTagInfo,
useQuerySynonymsTags,
diff --git a/ui/src/pages/Tags/index.tsx b/ui/src/pages/Tags/index.tsx
index 80256d94..a01e3e56 100644
--- a/ui/src/pages/Tags/index.tsx
+++ b/ui/src/pages/Tags/index.tsx
@@ -3,9 +3,8 @@ import { Container, Row, Col, Card, Button, Form } from 'react-bootstrap';
import { useSearchParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
-import { Tag, Pagination, PageTitle, QueryGroup } from '@answer/components';
-import { formatCount } from '@answer/utils';
-
+import { Tag, Pagination, PageTitle, QueryGroup } from '@/components';
+import { formatCount } from '@/utils';
import { useQueryTags, following } from '@/services';
const sortBtns = ['popular', 'name', 'newest'];
diff --git a/ui/src/pages/Users/AccountForgot/components/sendEmail.tsx b/ui/src/pages/Users/AccountForgot/components/sendEmail.tsx
index e95b1354..f919e560 100644
--- a/ui/src/pages/Users/AccountForgot/components/sendEmail.tsx
+++ b/ui/src/pages/Users/AccountForgot/components/sendEmail.tsx
@@ -6,8 +6,7 @@ import type {
ImgCodeRes,
PasswordResetReq,
FormDataType,
-} from '@answer/common/interface';
-
+} from '@/common/interface';
import { resetPassword, checkImgCode } from '@/services';
import { PicAuthCodeModal } from '@/components/Modal';
diff --git a/ui/src/pages/Users/AccountForgot/index.tsx b/ui/src/pages/Users/AccountForgot/index.tsx
index 5065e9b6..e7a77ea5 100644
--- a/ui/src/pages/Users/AccountForgot/index.tsx
+++ b/ui/src/pages/Users/AccountForgot/index.tsx
@@ -2,11 +2,11 @@ import React, { useState, useEffect } from 'react';
import { Container, Col } from 'react-bootstrap';
import { Trans, useTranslation } from 'react-i18next';
-import SendEmail from './components/sendEmail';
-
-import { tryNormalLogged } from '@/utils/guards';
+import { tryNormalLogged } from '@/utils/guard';
import { PageTitle } from '@/components';
+import SendEmail from './components/sendEmail';
+
const Index: React.FC = () => {
const { t } = useTranslation('translation', { keyPrefix: 'account_forgot' });
const [step, setStep] = useState(1);
diff --git a/ui/src/pages/Users/ActiveEmail/index.tsx b/ui/src/pages/Users/ActiveEmail/index.tsx
index f50fbc3e..ac2223a7 100644
--- a/ui/src/pages/Users/ActiveEmail/index.tsx
+++ b/ui/src/pages/Users/ActiveEmail/index.tsx
@@ -1,9 +1,8 @@
import { FC, memo, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
-import { loggedUserInfoStore } from '@answer/stores';
-import { getQueryString } from '@answer/utils';
-
+import { loggedUserInfoStore } from '@/stores';
+import { getQueryString } from '@/utils';
import { activateAccount } from '@/services';
import { PageTitle } from '@/components';
diff --git a/ui/src/pages/Users/ChangeEmail/components/sendEmail.tsx b/ui/src/pages/Users/ChangeEmail/components/sendEmail.tsx
index 4d419091..f87a6adf 100644
--- a/ui/src/pages/Users/ChangeEmail/components/sendEmail.tsx
+++ b/ui/src/pages/Users/ChangeEmail/components/sendEmail.tsx
@@ -7,9 +7,8 @@ import type {
ImgCodeRes,
PasswordResetReq,
FormDataType,
-} from '@answer/common/interface';
-import { loggedUserInfoStore } from '@answer/stores';
-
+} from '@/common/interface';
+import { loggedUserInfoStore } from '@/stores';
import { changeEmail, checkImgCode } from '@/services';
import { PicAuthCodeModal } from '@/components/Modal';
diff --git a/ui/src/pages/Users/ChangeEmail/index.tsx b/ui/src/pages/Users/ChangeEmail/index.tsx
index cbb743a5..cabc5e5f 100644
--- a/ui/src/pages/Users/ChangeEmail/index.tsx
+++ b/ui/src/pages/Users/ChangeEmail/index.tsx
@@ -2,10 +2,10 @@ import { FC, memo } from 'react';
import { Container, Col } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import SendEmail from './components/sendEmail';
-
import { PageTitle } from '@/components';
+import SendEmail from './components/sendEmail';
+
const Index: FC = () => {
const { t } = useTranslation('translation', { keyPrefix: 'change_email' });
diff --git a/ui/src/pages/Users/Login/index.tsx b/ui/src/pages/Users/Login/index.tsx
index 6d9af5c2..423a6654 100644
--- a/ui/src/pages/Users/Login/index.tsx
+++ b/ui/src/pages/Users/Login/index.tsx
@@ -7,13 +7,11 @@ import type {
LoginReqParams,
ImgCodeRes,
FormDataType,
-} from '@answer/common/interface';
-import { PageTitle, Unactivate } from '@answer/components';
-import { loggedUserInfoStore } from '@answer/stores';
-import { getQueryString } from '@answer/utils';
-
+} from '@/common/interface';
+import { PageTitle, Unactivate } from '@/components';
+import { loggedUserInfoStore } from '@/stores';
+import { getQueryString, Guard, floppyNavigation } from '@/utils';
import { login, checkImgCode } from '@/services';
-import { deriveUserStat, tryNormalLogged } from '@/utils/guards';
import { REDIRECT_PATH_STORAGE_KEY } from '@/common/constants';
import { RouteAlias } from '@/router/alias';
import { PicAuthCodeModal } from '@/components/Modal';
@@ -106,8 +104,8 @@ const Index: React.FC = () => {
login(params)
.then((res) => {
updateUser(res);
- const userStat = deriveUserStat();
- if (!userStat.isActivated) {
+ const userStat = Guard.deriveLoginState();
+ if (userStat.isNotActivated) {
// inactive
setStep(2);
setRefresh((pre) => pre + 1);
@@ -115,7 +113,9 @@ const Index: React.FC = () => {
const path =
Storage.get(REDIRECT_PATH_STORAGE_KEY) || RouteAlias.home;
Storage.remove(REDIRECT_PATH_STORAGE_KEY);
- navigate(path, { replace: true });
+ floppyNavigation.navigate(path, () => {
+ navigate(path, { replace: true });
+ });
}
setModalState(false);
@@ -159,7 +159,7 @@ const Index: React.FC = () => {
if ((storeUser.id && storeUser.mail_status === 2) || isInactive) {
setStep(2);
} else {
- tryNormalLogged();
+ Guard.tryNormalLogged();
}
}, []);
diff --git a/ui/src/pages/Users/Notifications/components/Achievements/index.tsx b/ui/src/pages/Users/Notifications/components/Achievements/index.tsx
index 12cfe93b..54bd4cdb 100644
--- a/ui/src/pages/Users/Notifications/components/Achievements/index.tsx
+++ b/ui/src/pages/Users/Notifications/components/Achievements/index.tsx
@@ -4,7 +4,7 @@ import { Link } from 'react-router-dom';
import classNames from 'classnames';
import { isEmpty } from 'lodash';
-import { Empty } from '@answer/components';
+import { Empty } from '@/components';
import './index.scss';
diff --git a/ui/src/pages/Users/Notifications/components/Inbox/index.tsx b/ui/src/pages/Users/Notifications/components/Inbox/index.tsx
index 27366b81..68739b0c 100644
--- a/ui/src/pages/Users/Notifications/components/Inbox/index.tsx
+++ b/ui/src/pages/Users/Notifications/components/Inbox/index.tsx
@@ -4,7 +4,7 @@ import { Link } from 'react-router-dom';
import classNames from 'classnames';
import { isEmpty } from 'lodash';
-import { FormatTime, Empty } from '@answer/components';
+import { FormatTime, Empty } from '@/components';
const Inbox = ({ data, handleReadNotification }) => {
if (!data) {
diff --git a/ui/src/pages/Users/Notifications/index.tsx b/ui/src/pages/Users/Notifications/index.tsx
index b24367b7..1ee7158c 100644
--- a/ui/src/pages/Users/Notifications/index.tsx
+++ b/ui/src/pages/Users/Notifications/index.tsx
@@ -3,11 +3,7 @@ import { Container, Row, Col, ButtonGroup, Button } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
import { useParams, useNavigate } from 'react-router-dom';
-import { PageTitle } from '@answer/components';
-
-import Inbox from './components/Inbox';
-import Achievements from './components/Achievements';
-
+import { PageTitle } from '@/components';
import {
useQueryNotifications,
clearUnreadNotification,
@@ -15,6 +11,9 @@ import {
readNotification,
} from '@/services';
+import Inbox from './components/Inbox';
+import Achievements from './components/Achievements';
+
const PAGE_SIZE = 10;
const Notifications = () => {
diff --git a/ui/src/pages/Users/PasswordReset/index.tsx b/ui/src/pages/Users/PasswordReset/index.tsx
index af97001f..b97bd707 100644
--- a/ui/src/pages/Users/PasswordReset/index.tsx
+++ b/ui/src/pages/Users/PasswordReset/index.tsx
@@ -3,12 +3,11 @@ import { Container, Col, Form, Button } from 'react-bootstrap';
import { Link } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
-import { loggedUserInfoStore } from '@answer/stores';
-import { getQueryString } from '@answer/utils';
-import type { FormDataType } from '@answer/common/interface';
-
+import { loggedUserInfoStore } from '@/stores';
+import { getQueryString } from '@/utils';
+import type { FormDataType } from '@/common/interface';
import { replacementPassword } from '@/services';
-import { tryNormalLogged } from '@/utils/guards';
+import { tryNormalLogged } from '@/utils/guard';
import { PageTitle } from '@/components';
const Index: React.FC = () => {
diff --git a/ui/src/pages/Users/Personal/components/Answers/index.tsx b/ui/src/pages/Users/Personal/components/Answers/index.tsx
index 12ab0059..a7d8c48a 100644
--- a/ui/src/pages/Users/Personal/components/Answers/index.tsx
+++ b/ui/src/pages/Users/Personal/components/Answers/index.tsx
@@ -2,7 +2,7 @@ import { FC, memo } from 'react';
import { ListGroup, ListGroupItem } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import { Icon, FormatTime, Tag } from '@answer/components';
+import { Icon, FormatTime, Tag } from '@/components';
interface Props {
visible: boolean;
diff --git a/ui/src/pages/Users/Personal/components/Comments/index.tsx b/ui/src/pages/Users/Personal/components/Comments/index.tsx
index 483ce361..f53e468e 100644
--- a/ui/src/pages/Users/Personal/components/Comments/index.tsx
+++ b/ui/src/pages/Users/Personal/components/Comments/index.tsx
@@ -1,7 +1,7 @@
import { FC, memo } from 'react';
import { ListGroup, ListGroupItem } from 'react-bootstrap';
-import { FormatTime } from '@answer/components';
+import { FormatTime } from '@/components';
interface Props {
visible: boolean;
diff --git a/ui/src/pages/Users/Personal/components/DefaultList/index.tsx b/ui/src/pages/Users/Personal/components/DefaultList/index.tsx
index 7eb67525..980a2df3 100644
--- a/ui/src/pages/Users/Personal/components/DefaultList/index.tsx
+++ b/ui/src/pages/Users/Personal/components/DefaultList/index.tsx
@@ -2,7 +2,7 @@ import { FC, memo } from 'react';
import { ListGroup, ListGroupItem } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import { Icon, FormatTime, Tag, BaseUserCard } from '@answer/components';
+import { Icon, FormatTime, Tag, BaseUserCard } from '@/components';
interface Props {
visible: boolean;
diff --git a/ui/src/pages/Users/Personal/components/ListHead/index.tsx b/ui/src/pages/Users/Personal/components/ListHead/index.tsx
index e1ce494f..de550754 100644
--- a/ui/src/pages/Users/Personal/components/ListHead/index.tsx
+++ b/ui/src/pages/Users/Personal/components/ListHead/index.tsx
@@ -1,7 +1,7 @@
import { FC, memo } from 'react';
import { useTranslation } from 'react-i18next';
-import { QueryGroup } from '@answer/components';
+import { QueryGroup } from '@/components';
const sortBtns = ['newest', 'score'];
diff --git a/ui/src/pages/Users/Personal/components/Reputation/index.tsx b/ui/src/pages/Users/Personal/components/Reputation/index.tsx
index fa1cb3db..6458e921 100644
--- a/ui/src/pages/Users/Personal/components/Reputation/index.tsx
+++ b/ui/src/pages/Users/Personal/components/Reputation/index.tsx
@@ -2,7 +2,7 @@ import { FC, memo } from 'react';
import { ListGroup, ListGroupItem } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import { FormatTime } from '@answer/components';
+import { FormatTime } from '@/components';
interface Props {
visible: boolean;
diff --git a/ui/src/pages/Users/Personal/components/TopList/index.tsx b/ui/src/pages/Users/Personal/components/TopList/index.tsx
index cdd7ba2e..86179305 100644
--- a/ui/src/pages/Users/Personal/components/TopList/index.tsx
+++ b/ui/src/pages/Users/Personal/components/TopList/index.tsx
@@ -2,7 +2,7 @@ import { FC, memo } from 'react';
import { ListGroup, ListGroupItem } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import { Icon } from '@answer/components';
+import { Icon } from '@/components';
interface Props {
data: any[];
diff --git a/ui/src/pages/Users/Personal/components/UserInfo/index.tsx b/ui/src/pages/Users/Personal/components/UserInfo/index.tsx
index 97caca3e..880b810f 100644
--- a/ui/src/pages/Users/Personal/components/UserInfo/index.tsx
+++ b/ui/src/pages/Users/Personal/components/UserInfo/index.tsx
@@ -3,8 +3,8 @@ import { Badge, OverlayTrigger, Tooltip } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
import { Link } from 'react-router-dom';
-import { Avatar, Icon } from '@answer/components';
-import type { UserInfoRes } from '@answer/common/interface';
+import { Avatar, Icon } from '@/components';
+import type { UserInfoRes } from '@/common/interface';
interface Props {
data: UserInfoRes;
diff --git a/ui/src/pages/Users/Personal/components/Votes/index.tsx b/ui/src/pages/Users/Personal/components/Votes/index.tsx
index 308af877..fa5023db 100644
--- a/ui/src/pages/Users/Personal/components/Votes/index.tsx
+++ b/ui/src/pages/Users/Personal/components/Votes/index.tsx
@@ -1,7 +1,7 @@
import { FC, memo } from 'react';
import { ListGroup, ListGroupItem } from 'react-bootstrap';
-import { FormatTime } from '@answer/components';
+import { FormatTime } from '@/components';
interface Props {
visible: boolean;
diff --git a/ui/src/pages/Users/Personal/index.tsx b/ui/src/pages/Users/Personal/index.tsx
index fcab49ff..4720db93 100644
--- a/ui/src/pages/Users/Personal/index.tsx
+++ b/ui/src/pages/Users/Personal/index.tsx
@@ -3,8 +3,13 @@ import { Container, Row, Col, Button } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
import { useParams, useSearchParams } from 'react-router-dom';
-import { Pagination, FormatTime, PageTitle, Empty } from '@answer/components';
-import { loggedUserInfoStore } from '@answer/stores';
+import { Pagination, FormatTime, PageTitle, Empty } from '@/components';
+import { loggedUserInfoStore } from '@/stores';
+import {
+ usePersonalInfoByName,
+ usePersonalTop,
+ usePersonalListByTabName,
+} from '@/services';
import {
UserInfo,
@@ -19,12 +24,6 @@ import {
Votes,
} from './components';
-import {
- usePersonalInfoByName,
- usePersonalTop,
- usePersonalListByTabName,
-} from '@/services';
-
const Personal: FC = () => {
const { tabName = 'overview', username = '' } = useParams();
const [searchParams] = useSearchParams();
diff --git a/ui/src/pages/Users/Register/components/SignUpForm/index.tsx b/ui/src/pages/Users/Register/components/SignUpForm/index.tsx
index 4f072fe7..0d92c5fa 100644
--- a/ui/src/pages/Users/Register/components/SignUpForm/index.tsx
+++ b/ui/src/pages/Users/Register/components/SignUpForm/index.tsx
@@ -3,8 +3,7 @@ import { Form, Button, Col } from 'react-bootstrap';
import { Link } from 'react-router-dom';
import { Trans, useTranslation } from 'react-i18next';
-import type { FormDataType } from '@answer/common/interface';
-
+import type { FormDataType } from '@/common/interface';
import { register } from '@/services';
import userStore from '@/stores/userInfo';
diff --git a/ui/src/pages/Users/Register/index.tsx b/ui/src/pages/Users/Register/index.tsx
index 5f8370a0..e4d94b2d 100644
--- a/ui/src/pages/Users/Register/index.tsx
+++ b/ui/src/pages/Users/Register/index.tsx
@@ -2,12 +2,11 @@ import React, { useState, useEffect } from 'react';
import { Container } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import { PageTitle, Unactivate } from '@answer/components';
+import { PageTitle, Unactivate } from '@/components';
+import { tryNormalLogged } from '@/utils/guard';
import SignUpForm from './components/SignUpForm';
-import { tryNormalLogged } from '@/utils/guards';
-
const Index: React.FC = () => {
const [showForm, setShowForm] = useState(true);
const { t } = useTranslation('translation', { keyPrefix: 'login' });
diff --git a/ui/src/pages/Users/Settings/Account/components/ModifyEmail/index.tsx b/ui/src/pages/Users/Settings/Account/components/ModifyEmail/index.tsx
index 4ed6b0ff..8bfa6dfa 100644
--- a/ui/src/pages/Users/Settings/Account/components/ModifyEmail/index.tsx
+++ b/ui/src/pages/Users/Settings/Account/components/ModifyEmail/index.tsx
@@ -2,9 +2,8 @@ import React, { FC, FormEvent, useEffect, useState } from 'react';
import { Form, Button } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import type * as Type from '@answer/common/interface';
-import { useToast } from '@answer/hooks';
-
+import type * as Type from '@/common/interface';
+import { useToast } from '@/hooks';
import { getLoggedUserInfo, changeEmail } from '@/services';
const reg = /(?<=.{2}).+(?=@)/gi;
diff --git a/ui/src/pages/Users/Settings/Account/components/ModifyPass/index.tsx b/ui/src/pages/Users/Settings/Account/components/ModifyPass/index.tsx
index 2c7ed9ef..56d8f17b 100644
--- a/ui/src/pages/Users/Settings/Account/components/ModifyPass/index.tsx
+++ b/ui/src/pages/Users/Settings/Account/components/ModifyPass/index.tsx
@@ -2,9 +2,8 @@ import React, { FC, FormEvent, useState } from 'react';
import { Form, Button } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import { useToast } from '@answer/hooks';
-import type { FormDataType } from '@answer/common/interface';
-
+import { useToast } from '@/hooks';
+import type { FormDataType } from '@/common/interface';
import { modifyPassword } from '@/services';
const Index: FC = () => {
diff --git a/ui/src/pages/Users/Settings/Interface/index.tsx b/ui/src/pages/Users/Settings/Interface/index.tsx
index f38f92e8..3a3941b9 100644
--- a/ui/src/pages/Users/Settings/Interface/index.tsx
+++ b/ui/src/pages/Users/Settings/Interface/index.tsx
@@ -6,9 +6,8 @@ import dayjs from 'dayjs';
import en from 'dayjs/locale/en';
import zh from 'dayjs/locale/zh-cn';
-import type { LangsType, FormDataType } from '@answer/common/interface';
-import { useToast } from '@answer/hooks';
-
+import type { LangsType, FormDataType } from '@/common/interface';
+import { useToast } from '@/hooks';
import { languages } from '@/services';
import { DEFAULT_LANG, CURRENT_LANG_STORAGE_KEY } from '@/common/constants';
import Storage from '@/utils/storage';
diff --git a/ui/src/pages/Users/Settings/Notification/index.tsx b/ui/src/pages/Users/Settings/Notification/index.tsx
index 6d0cab61..e7310145 100644
--- a/ui/src/pages/Users/Settings/Notification/index.tsx
+++ b/ui/src/pages/Users/Settings/Notification/index.tsx
@@ -2,9 +2,8 @@ import React, { useState, FormEvent, useEffect } from 'react';
import { Form, Button } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import type { FormDataType } from '@answer/common/interface';
-import { useToast } from '@answer/hooks';
-
+import type { FormDataType } from '@/common/interface';
+import { useToast } from '@/hooks';
import { setNotice, getLoggedUserInfo } from '@/services';
const Index = () => {
diff --git a/ui/src/pages/Users/Settings/Profile/index.tsx b/ui/src/pages/Users/Settings/Profile/index.tsx
index 670bec91..a4442b08 100644
--- a/ui/src/pages/Users/Settings/Profile/index.tsx
+++ b/ui/src/pages/Users/Settings/Profile/index.tsx
@@ -5,11 +5,10 @@ import { Trans, useTranslation } from 'react-i18next';
import { marked } from 'marked';
import MD5 from 'md5';
-import type { FormDataType } from '@answer/common/interface';
-import { UploadImg, Avatar } from '@answer/components';
-import { loggedUserInfoStore } from '@answer/stores';
-import { useToast } from '@answer/hooks';
-
+import type { FormDataType } from '@/common/interface';
+import { UploadImg, Avatar } from '@/components';
+import { loggedUserInfoStore } from '@/stores';
+import { useToast } from '@/hooks';
import { modifyUserInfo, uploadAvatar, getLoggedUserInfo } from '@/services';
const Index: React.FC = () => {
diff --git a/ui/src/pages/Users/Settings/index.tsx b/ui/src/pages/Users/Settings/index.tsx
index 671707fe..dc591323 100644
--- a/ui/src/pages/Users/Settings/index.tsx
+++ b/ui/src/pages/Users/Settings/index.tsx
@@ -3,13 +3,12 @@ import { Container, Row, Col } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
import { Outlet } from 'react-router-dom';
-import type { FormDataType } from '@answer/common/interface';
-
-import Nav from './components/Nav';
-
+import type { FormDataType } from '@/common/interface';
import { getLoggedUserInfo } from '@/services';
import { PageTitle } from '@/components';
+import Nav from './components/Nav';
+
const Index: React.FC = () => {
const { t } = useTranslation('translation', {
keyPrefix: 'settings.profile',
diff --git a/ui/src/pages/Users/Suspended/index.tsx b/ui/src/pages/Users/Suspended/index.tsx
index 4c381d44..403595a9 100644
--- a/ui/src/pages/Users/Suspended/index.tsx
+++ b/ui/src/pages/Users/Suspended/index.tsx
@@ -1,7 +1,6 @@
import { useTranslation } from 'react-i18next';
-import { loggedUserInfoStore } from '@answer/stores';
-
+import { loggedUserInfoStore } from '@/stores';
import { PageTitle } from '@/components';
const Suspended = () => {
diff --git a/ui/src/router/guarder.ts b/ui/src/router/guarder.ts
deleted file mode 100644
index e3b81598..00000000
--- a/ui/src/router/guarder.ts
+++ /dev/null
@@ -1,42 +0,0 @@
-import {
- pullLoggedUser,
- isLoggedAndNormal,
- isAdminLogged,
- isNotLogged,
- isNotLoggedOrNormal,
- isLoggedAndInactive,
- isLoggedAndSuspended,
- isNotLoggedOrInactive,
- isNotLoggedOrNotSuspend,
-} from '@/utils/guards';
-
-const RouteGuarder = {
- base: async () => {
- return isNotLoggedOrNotSuspend();
- },
- loggedAndNormal: async () => {
- await pullLoggedUser(true);
- return isLoggedAndNormal();
- },
- loggedAndInactive: async () => {
- return isLoggedAndInactive();
- },
- loggedAndSuspended: async () => {
- return isLoggedAndSuspended();
- },
- adminLogged: async () => {
- await pullLoggedUser(true);
- return isAdminLogged();
- },
- notLogged: async () => {
- return isNotLogged();
- },
- notLoggedOrNormal: async () => {
- return isNotLoggedOrNormal();
- },
- notLoggedOrInactive: async () => {
- return isNotLoggedOrInactive();
- },
-};
-
-export default RouteGuarder;
diff --git a/ui/src/router/index.tsx b/ui/src/router/index.tsx
index 84eb37a6..e5aa2797 100644
--- a/ui/src/router/index.tsx
+++ b/ui/src/router/index.tsx
@@ -28,18 +28,19 @@ const routeWrapper = (routeNodes: RouteNode[], root: RouteObject[]) => {
}
root.push(rn);
if (rn.guard) {
- const { guard } = rn;
- const loaderRef = rn.loader;
+ const refLoader = rn.loader;
+ const refGuard = rn.guard;
rn.loader = async (args) => {
- const gr = await guard(args);
+ const gr = await refGuard();
if (gr?.redirect && floppyNavigation.differentCurrent(gr.redirect)) {
return redirect(gr.redirect);
}
- let ret;
- if (typeof loaderRef === 'function') {
- ret = await loaderRef(args);
+
+ let lr;
+ if (typeof refLoader === 'function') {
+ lr = await refLoader(args);
}
- return ret;
+ return lr;
};
}
const children = Array.isArray(rn.children) ? rn.children : null;
diff --git a/ui/src/router/routes.ts b/ui/src/router/routes.ts
index 5b49fb1a..126dbac0 100644
--- a/ui/src/router/routes.ts
+++ b/ui/src/router/routes.ts
@@ -1,18 +1,28 @@
import { RouteObject } from 'react-router-dom';
-import RouteGuarder from '@/router/guarder';
+import { Guard } from '@/utils';
+import type { TGuardResult } from '@/utils/guard';
export interface RouteNode extends RouteObject {
page: string;
children?: RouteNode[];
- guard?: Function;
+ /**
+ * a method to auto guard route before route enter
+ * if the `ok` field in guard returned `TGuardResult` is true,
+ * it means the guard passed then enter the route.
+ * if guard returned the `TGuardResult` has `redirect` field,
+ * then auto redirect route to the `redirect` target.
+ */
+ guard?: () => Promise<TGuardResult>;
}
const routes: RouteNode[] = [
{
path: '/',
page: 'pages/Layout',
- guard: RouteGuarder.base,
+ guard: async () => {
+ return Guard.notForbidden();
+ },
children: [
// question and answer
{
@@ -35,12 +45,16 @@ const routes: RouteNode[] = [
{
path: 'questions/ask',
page: 'pages/Questions/Ask',
- guard: RouteGuarder.loggedAndNormal,
+ guard: async () => {
+ return Guard.activated();
+ },
},
{
path: 'posts/:qid/edit',
page: 'pages/Questions/Ask',
- guard: RouteGuarder.loggedAndNormal,
+ guard: async () => {
+ return Guard.activated();
+ },
},
{
path: 'posts/:qid/:aid/edit',
@@ -66,6 +80,9 @@ const routes: RouteNode[] = [
{
path: 'tags/:tagId/edit',
page: 'pages/Tags/Edit',
+ guard: async () => {
+ return Guard.activated();
+ },
},
// users
{
@@ -79,6 +96,9 @@ const routes: RouteNode[] = [
{
path: 'users/settings',
page: 'pages/Users/Settings',
+ guard: async () => {
+ return Guard.logged();
+ },
children: [
{
index: true,
@@ -109,55 +129,85 @@ const routes: RouteNode[] = [
{
path: 'users/login',
page: 'pages/Users/Login',
- guard: RouteGuarder.notLoggedOrInactive,
+ guard: async () => {
+ const notLogged = Guard.notLogged();
+ if (notLogged.ok) {
+ return notLogged;
+ }
+ return Guard.notActivated();
+ },
},
{
path: 'users/register',
page: 'pages/Users/Register',
- guard: RouteGuarder.notLogged,
+ guard: async () => {
+ return Guard.notLogged();
+ },
},
{
path: 'users/account-recovery',
page: 'pages/Users/AccountForgot',
- guard: RouteGuarder.loggedAndNormal,
+ guard: async () => {
+ return Guard.activated();
+ },
},
{
path: 'users/change-email',
page: 'pages/Users/ChangeEmail',
+ // TODO: guard this (change email when user not activated) ?
},
{
path: 'users/password-reset',
page: 'pages/Users/PasswordReset',
- guard: RouteGuarder.loggedAndNormal,
+ guard: async () => {
+ return Guard.activated();
+ },
},
- // TODO: guard '/account-activation/*', '/users/confirm-new-email'
{
path: 'users/account-activation',
page: 'pages/Users/ActiveEmail',
- guard: RouteGuarder.loggedAndInactive,
+ guard: async () => {
+ const notActivated = Guard.notActivated();
+ if (notActivated.ok) {
+ return notActivated;
+ }
+ return Guard.notLogged();
+ },
},
{
path: 'users/account-activation/success',
page: 'pages/Users/ActivationResult',
+ guard: async () => {
+ return Guard.activated();
+ },
},
{
path: '/users/account-activation/failed',
page: 'pages/Users/ActivationResult',
+ guard: async () => {
+ return Guard.notActivated();
+ },
},
{
path: '/users/confirm-new-email',
page: 'pages/Users/ConfirmNewEmail',
+ // TODO: guard this
},
{
path: '/users/account-suspended',
page: 'pages/Users/Suspended',
- guard: RouteGuarder.loggedAndSuspended,
+ guard: async () => {
+ return Guard.forbidden();
+ },
},
// for admin
{
path: 'admin',
page: 'pages/Admin',
- guard: RouteGuarder.adminLogged,
+ guard: async () => {
+ await Guard.pullLoggedUser(true);
+ return Guard.admin();
+ },
children: [
{
index: true,
diff --git a/ui/src/services/admin/answer.ts b/ui/src/services/admin/answer.ts
index 6fd0fbb6..c3340502 100644
--- a/ui/src/services/admin/answer.ts
+++ b/ui/src/services/admin/answer.ts
@@ -1,8 +1,8 @@
import useSWR from 'swr';
import qs from 'qs';
-import request from '@answer/utils/request';
-import type * as Type from '@answer/common/interface';
+import request from '@/utils/request';
+import type * as Type from '@/common/interface';
export const useAnswerSearch = (params: Type.AdminContentsReq) => {
const apiUrl = `/answer/admin/api/answer/page?${qs.stringify(params)}`;
diff --git a/ui/src/services/admin/flag.ts b/ui/src/services/admin/flag.ts
index 710cb447..64ea59f7 100644
--- a/ui/src/services/admin/flag.ts
+++ b/ui/src/services/admin/flag.ts
@@ -1,8 +1,8 @@
import useSWR from 'swr';
import qs from 'qs';
-import request from '@answer/utils/request';
-import type * as Type from '@answer/common/interface';
+import request from '@/utils/request';
+import type * as Type from '@/common/interface';
export const putReport = (params) => {
return request.instance.put('/answer/admin/api/report', params);
diff --git a/ui/src/services/admin/question.ts b/ui/src/services/admin/question.ts
index a6308bf6..9e8d726a 100644
--- a/ui/src/services/admin/question.ts
+++ b/ui/src/services/admin/question.ts
@@ -1,8 +1,8 @@
import qs from 'qs';
import useSWR from 'swr';
-import request from '@answer/utils/request';
-import type * as Type from '@answer/common/interface';
+import request from '@/utils/request';
+import type * as Type from '@/common/interface';
export const changeUserStatus = (params) => {
return request.put('/answer/admin/api/user/status', params);
diff --git a/ui/src/services/admin/settings.ts b/ui/src/services/admin/settings.ts
index e1f486e7..274e24eb 100644
--- a/ui/src/services/admin/settings.ts
+++ b/ui/src/services/admin/settings.ts
@@ -1,7 +1,7 @@
import useSWR from 'swr';
-import request from '@answer/utils/request';
-import type * as Type from '@answer/common/interface';
+import request from '@/utils/request';
+import type * as Type from '@/common/interface';
export const useGeneralSetting = () => {
const apiUrl = `/answer/admin/api/siteinfo/general`;
diff --git a/ui/src/services/client/activity.ts b/ui/src/services/client/activity.ts
index 7b7b539c..6f1a4fdb 100644
--- a/ui/src/services/client/activity.ts
+++ b/ui/src/services/client/activity.ts
@@ -1,7 +1,7 @@
import useSWR from 'swr';
-import request from '@answer/utils/request';
-import type * as Type from '@answer/common/interface';
+import request from '@/utils/request';
+import type * as Type from '@/common/interface';
export const useFollow = (params?: Type.FollowParams) => {
const apiUrl = '/answer/api/v1/follow';
diff --git a/ui/src/services/client/notification.ts b/ui/src/services/client/notification.ts
index dd9d880e..a849db0a 100644
--- a/ui/src/services/client/notification.ts
+++ b/ui/src/services/client/notification.ts
@@ -1,10 +1,9 @@
import useSWR from 'swr';
import qs from 'qs';
-import request from '@answer/utils/request';
-import type * as Type from '@answer/common/interface';
-
-import { tryNormalLogged } from '@/utils/guards';
+import request from '@/utils/request';
+import type * as Type from '@/common/interface';
+import { tryNormalLogged } from '@/utils/guard';
export const useQueryNotifications = (params) => {
const apiUrl = `/answer/api/v1/notification/page?${qs.stringify(params, {
diff --git a/ui/src/services/client/personal.ts b/ui/src/services/client/personal.ts
index b84e260f..6b61aaba 100644
--- a/ui/src/services/client/personal.ts
+++ b/ui/src/services/client/personal.ts
@@ -1,8 +1,8 @@
import useSWR from 'swr';
import qs from 'qs';
-import request from '@answer/utils/request';
-import type * as Type from '@answer/common/interface';
+import request from '@/utils/request';
+import type * as Type from '@/common/interface';
export const usePersonalInfoByName = (username: string) => {
const apiUrl = '/answer/api/v1/personal/user/info';
diff --git a/ui/src/services/client/question.ts b/ui/src/services/client/question.ts
index c35fe544..c418f26c 100644
--- a/ui/src/services/client/question.ts
+++ b/ui/src/services/client/question.ts
@@ -1,8 +1,8 @@
import useSWR from 'swr';
import qs from 'qs';
-import request from '@answer/utils/request';
-import type * as Type from '@answer/common/interface';
+import request from '@/utils/request';
+import type * as Type from '@/common/interface';
export const useQuestionList = (params: Type.QueryQuestionsReq) => {
const apiUrl = `/answer/api/v1/question/page?${qs.stringify(params)}`;
diff --git a/ui/src/services/client/search.ts b/ui/src/services/client/search.ts
index f5fe86fe..8d380294 100644
--- a/ui/src/services/client/search.ts
+++ b/ui/src/services/client/search.ts
@@ -1,8 +1,8 @@
import useSWR from 'swr';
import qs from 'qs';
-import request from '@answer/utils/request';
-import type * as Type from '@answer/common/interface';
+import request from '@/utils/request';
+import type * as Type from '@/common/interface';
export const useSearch = (params?: Type.SearchParams) => {
const apiUrl = '/answer/api/v1/search';
diff --git a/ui/src/services/client/tag.ts b/ui/src/services/client/tag.ts
index 634b4472..42b9f1ac 100644
--- a/ui/src/services/client/tag.ts
+++ b/ui/src/services/client/tag.ts
@@ -1,9 +1,8 @@
import useSWR from 'swr';
-import request from '@answer/utils/request';
-import type * as Type from '@answer/common/interface';
-
-import { tryNormalLogged } from '@/utils/guards';
+import request from '@/utils/request';
+import type * as Type from '@/common/interface';
+import { tryNormalLogged } from '@/utils/guard';
export const deleteTag = (id) => {
return request.delete('/answer/api/v1/tag', {
diff --git a/ui/src/services/common.ts b/ui/src/services/common.ts
index 6f2cf83a..3e9e7872 100644
--- a/ui/src/services/common.ts
+++ b/ui/src/services/common.ts
@@ -1,8 +1,8 @@
import qs from 'qs';
import useSWR from 'swr';
-import request from '@answer/utils/request';
-import type * as Type from '@answer/common/interface';
+import request from '@/utils/request';
+import type * as Type from '@/common/interface';
export const uploadImage = (file) => {
const form = new FormData();
diff --git a/ui/src/stores/userInfo.ts b/ui/src/stores/userInfo.ts
index 9aa540b5..017c3149 100644
--- a/ui/src/stores/userInfo.ts
+++ b/ui/src/stores/userInfo.ts
@@ -1,8 +1,7 @@
import create from 'zustand';
-import type { UserInfoRes } from '@answer/common/interface';
-import Storage from '@answer/utils/storage';
-
+import type { UserInfoRes } from '@/common/interface';
+import Storage from '@/utils/storage';
import {
LOGGED_USER_STORAGE_KEY,
LOGGED_TOKEN_STORAGE_KEY,
@@ -15,6 +14,7 @@ interface UserInfoStore {
}
const initUser: UserInfoRes = {
+ access_token: '',
username: '',
avatar: '',
rank: 0,
diff --git a/ui/src/utils/floppyNavigation.ts b/ui/src/utils/floppyNavigation.ts
index 7edbbaa0..68e6f8a5 100644
--- a/ui/src/utils/floppyNavigation.ts
+++ b/ui/src/utils/floppyNavigation.ts
@@ -25,7 +25,8 @@ const navigate = (pathname: string, callback: Function) => {
const navigateToLogin = () => {
const { pathname } = window.location;
if (pathname !== RouteAlias.login && pathname !== RouteAlias.register) {
- const redirectUrl = window.location.href;
+ const loc = window.location;
+ const redirectUrl = loc.href.replace(loc.origin, '');
Storage.set(REDIRECT_PATH_STORAGE_KEY, redirectUrl);
}
navigate(RouteAlias.login, () => {
diff --git a/ui/src/utils/guard.ts b/ui/src/utils/guard.ts
new file mode 100644
index 00000000..bc73ab9a
--- /dev/null
+++ b/ui/src/utils/guard.ts
@@ -0,0 +1,182 @@
+import { getLoggedUserInfo } from '@/services';
+import { loggedUserInfoStore } from '@/stores';
+import { RouteAlias } from '@/router/alias';
+import Storage from '@/utils/storage';
+import { LOGGED_USER_STORAGE_KEY } from '@/common/constants';
+
+import { floppyNavigation } from './floppyNavigation';
+
+type TLoginState = {
+ isLogged: boolean;
+ isNotActivated: boolean;
+ isActivated: boolean;
+ isForbidden: boolean;
+ isNormal: boolean;
+ isAdmin: boolean;
+};
+
+export type TGuardResult = {
+ ok: boolean;
+ redirect?: string;
+};
+
+export const deriveLoginState = (): TLoginState => {
+ const ls: TLoginState = {
+ isLogged: false,
+ isNotActivated: false,
+ isActivated: false,
+ isForbidden: false,
+ isNormal: false,
+ isAdmin: false,
+ };
+ const { user } = loggedUserInfoStore.getState();
+ if (user.access_token) {
+ ls.isLogged = true;
+ }
+ if (ls.isLogged && user.mail_status === 1) {
+ ls.isActivated = true;
+ }
+ if (ls.isLogged && user.mail_status === 2) {
+ ls.isNotActivated = true;
+ }
+ if (ls.isLogged && user.status === 'forbidden') {
+ ls.isForbidden = true;
+ }
+ if (ls.isActivated && !ls.isForbidden) {
+ ls.isNormal = true;
+ }
+ if (ls.isNormal && user.is_admin === true) {
+ ls.isAdmin = true;
+ }
+
+ return ls;
+};
+
+let pullLock = false;
+let dedupeTimestamp = 0;
+export const pullLoggedUser = async (forceRePull = false) => {
+ // only pull once if not force re-pull
+ if (pullLock && !forceRePull) {
+ return;
+ }
+ // dedupe pull requests in this time span in 10 seconds
+ if (Date.now() - dedupeTimestamp < 1000 * 10) {
+ return;
+ }
+ dedupeTimestamp = Date.now();
+ const loggedUserInfo = await getLoggedUserInfo().catch((ex) => {
+ dedupeTimestamp = 0;
+ if (!deriveLoginState().isLogged) {
+ // load fallback userInfo from local storage
+ const storageLoggedUserInfo = Storage.get(LOGGED_USER_STORAGE_KEY);
+ if (storageLoggedUserInfo) {
+ loggedUserInfoStore.getState().update(storageLoggedUserInfo);
+ }
+ }
+ console.error(ex);
+ });
+ if (loggedUserInfo) {
+ pullLock = true;
+ loggedUserInfoStore.getState().update(loggedUserInfo);
+ }
+};
+
+export const logged = () => {
+ const gr: TGuardResult = { ok: true };
+ const us = deriveLoginState();
+ if (!us.isLogged) {
+ gr.ok = false;
+ gr.redirect = RouteAlias.login;
+ }
+ return gr;
+};
+
+export const notLogged = () => {
+ const gr: TGuardResult = { ok: true };
+ const us = deriveLoginState();
+ if (us.isLogged) {
+ gr.ok = false;
+ gr.redirect = RouteAlias.home;
+ }
+ return gr;
+};
+
+export const notActivated = () => {
+ const gr = logged();
+ const us = deriveLoginState();
+ if (us.isActivated) {
+ gr.ok = false;
+ gr.redirect = RouteAlias.home;
+ }
+ return gr;
+};
+
+export const activated = () => {
+ const gr = logged();
+ const us = deriveLoginState();
+ if (us.isNotActivated) {
+ gr.ok = false;
+ gr.redirect = RouteAlias.activation;
+ }
+ return gr;
+};
+
+export const forbidden = () => {
+ const gr = logged();
+ const us = deriveLoginState();
+ if (gr.ok && !us.isForbidden) {
+ gr.ok = false;
+ gr.redirect = RouteAlias.home;
+ }
+ return gr;
+};
+
+export const notForbidden = () => {
+ const gr: TGuardResult = { ok: true };
+ const us = deriveLoginState();
+ if (us.isForbidden) {
+ gr.ok = false;
+ gr.redirect = RouteAlias.suspended;
+ }
+ return gr;
+};
+
+export const admin = () => {
+ const gr = logged();
+ const us = deriveLoginState();
+ if (gr.ok && !us.isAdmin) {
+ gr.ok = false;
+ gr.redirect = RouteAlias.home;
+ }
+ return gr;
+};
+
+/**
+ * try user was logged and all state ok
+ * @param autoLogin
+ */
+export const tryNormalLogged = (autoLogin: boolean = false) => {
+ const us = deriveLoginState();
+
+ if (us.isNormal) {
+ return true;
+ }
+ // must assert logged state first and return
+ if (!us.isLogged) {
+ if (autoLogin) {
+ floppyNavigation.navigateToLogin();
+ }
+ return false;
+ }
+ if (us.isNotActivated) {
+ floppyNavigation.navigate(RouteAlias.activation, () => {
+ window.location.href = RouteAlias.activation;
+ });
+ } else if (us.isForbidden) {
+ floppyNavigation.navigate(RouteAlias.suspended, () => {
+ window.location.replace(RouteAlias.suspended);
+ });
+ }
+
+ return false;
+};
diff --git a/ui/src/utils/guards.ts b/ui/src/utils/guards.ts
deleted file mode 100644
index 47f79605..00000000
--- a/ui/src/utils/guards.ts
+++ /dev/null
@@ -1,196 +0,0 @@
-import { getLoggedUserInfo } from '@/services';
-import { loggedUserInfoStore } from '@/stores';
-import { RouteAlias } from '@/router/alias';
-import Storage from '@/utils/storage';
-import { LOGGED_USER_STORAGE_KEY } from '@/common/constants';
-import { floppyNavigation } from '@/utils/floppyNavigation';
-
-type UserStat = {
- isLogged: boolean;
- isActivated: boolean;
- isSuspended: boolean;
- isNormal: boolean;
- isAdmin: boolean;
-};
-export const deriveUserStat = (): UserStat => {
- const stat: UserStat = {
- isLogged: false,
- isActivated: false,
- isSuspended: false,
- isNormal: false,
- isAdmin: false,
- };
- const { user } = loggedUserInfoStore.getState();
- if (user.id && user.username) {
- stat.isLogged = true;
- }
- if (stat.isLogged && user.mail_status === 1) {
- stat.isActivated = true;
- }
- if (stat.isLogged && user.status === 'forbidden') {
- stat.isSuspended = true;
- }
- if (stat.isLogged && stat.isActivated && !stat.isSuspended) {
- stat.isNormal = true;
- }
- if (stat.isNormal && user.is_admin === true) {
- stat.isAdmin = true;
- }
-
- return stat;
-};
-
-type GuardResult = {
- ok: boolean;
- redirect?: string;
-};
-let pullLock = false;
-let dedupeTimestamp = 0;
-export const pullLoggedUser = async (forceRePull = false) => {
- // only pull once if not force re-pull
- if (pullLock && !forceRePull) {
- return;
- }
- // dedupe pull requests in this time span in 10 seconds
- if (Date.now() - dedupeTimestamp < 1000 * 10) {
- return;
- }
- dedupeTimestamp = Date.now();
- const loggedUserInfo = await getLoggedUserInfo().catch((ex) => {
- dedupeTimestamp = 0;
- if (!deriveUserStat().isLogged) {
- // load fallback userInfo from local storage
- const storageLoggedUserInfo = Storage.get(LOGGED_USER_STORAGE_KEY);
- if (storageLoggedUserInfo) {
- loggedUserInfoStore.getState().update(storageLoggedUserInfo);
- }
- }
- console.error(ex);
- });
- if (loggedUserInfo) {
- pullLock = true;
- loggedUserInfoStore.getState().update(loggedUserInfo);
- }
-};
-
-export const isLogged = () => {
- const ret: GuardResult = { ok: true, redirect: undefined };
- const userStat = deriveUserStat();
- if (!userStat.isLogged) {
- ret.ok = false;
- ret.redirect = RouteAlias.login;
- }
- return ret;
-};
-
-export const isNotLogged = () => {
- const ret: GuardResult = { ok: true, redirect: undefined };
- const userStat = deriveUserStat();
- if (userStat.isLogged) {
- ret.ok = false;
- ret.redirect = RouteAlias.home;
- }
- return ret;
-};
-
-export const isLoggedAndInactive = () => {
- const ret: GuardResult = { ok: true, redirect: undefined };
- const userStat = deriveUserStat();
- if (userStat.isActivated) {
- ret.ok = false;
- ret.redirect = RouteAlias.home;
- }
- return ret;
-};
-
-export const isLoggedAndSuspended = () => {
- const ret: GuardResult = { ok: true, redirect: undefined };
- const userStat = deriveUserStat();
- if (!userStat.isSuspended) {
- ret.ok = false;
- ret.redirect = RouteAlias.home;
- }
- return ret;
-};
-
-export const isLoggedAndNormal = () => {
- const ret: GuardResult = { ok: false, redirect: undefined };
- const userStat = deriveUserStat();
- if (userStat.isNormal) {
- ret.ok = true;
- } else if (!userStat.isActivated) {
- ret.redirect = RouteAlias.activation;
- } else if (userStat.isSuspended) {
- ret.redirect = RouteAlias.suspended;
- } else if (!userStat.isLogged) {
- ret.redirect = RouteAlias.login;
- }
- return ret;
-};
-
-export const isNotLoggedOrNormal = () => {
- const ret: GuardResult = { ok: true, redirect: undefined };
- const userStat = deriveUserStat();
- const gr = isLoggedAndNormal();
- if (!gr.ok && userStat.isLogged) {
- ret.ok = false;
- ret.redirect = gr.redirect;
- }
- return ret;
-};
-
-export const isNotLoggedOrNotSuspend = () => {
- const ret: GuardResult = { ok: true, redirect: undefined };
- const userStat = deriveUserStat();
- const gr = isLoggedAndNormal();
- if (!gr.ok && userStat.isSuspended) {
- ret.ok = false;
- ret.redirect = gr.redirect;
- }
- return ret;
-};
-
-export const isNotLoggedOrInactive = () => {
- const ret: GuardResult = { ok: true, redirect: undefined };
- const userStat = deriveUserStat();
- if (userStat.isActivated) {
- ret.ok = false;
- ret.redirect = RouteAlias.home;
- } else if (userStat.isSuspended) {
- ret.ok = false;
- ret.redirect = RouteAlias.suspended;
- }
- return ret;
-};
-
-export const isAdminLogged = () => {
- const ret: GuardResult = { ok: true, redirect: undefined };
- const userStat = deriveUserStat();
- if (!userStat.isAdmin) {
- ret.redirect = RouteAlias.home;
- ret.ok = false;
- }
- return ret;
-};
-
-/**
- * try user was logged and all state ok
- * @param autoLogin
- */
-export const tryNormalLogged = (autoLogin: boolean = false) => {
- const gr = isLoggedAndNormal();
- if (gr.ok) {
- return true;
- }
-
- if (gr.redirect === RouteAlias.login && autoLogin) {
- floppyNavigation.navigateToLogin();
- } else if (gr.redirect) {
- floppyNavigation.navigate(gr.redirect, () => {
- // @ts-ignore
- window.location.replace(gr.redirect);
- });
- }
-
- return false;
-};
diff --git a/ui/src/utils/index.ts b/ui/src/utils/index.ts
index a1eaf02c..69f70696 100644
--- a/ui/src/utils/index.ts
+++ b/ui/src/utils/index.ts
@@ -1,6 +1,6 @@
-export * from './common';
-export * as guards from './guards';
-
export { default as request } from './request';
export { default as Storage } from './storage';
export { floppyNavigation } from './floppyNavigation';
+
+export * as Guard from './guard';
+export * from './common';
diff --git a/ui/src/utils/request.ts b/ui/src/utils/request.ts
index d80cadf5..4e878cb3 100644
--- a/ui/src/utils/request.ts
+++ b/ui/src/utils/request.ts
@@ -1,12 +1,8 @@
import axios, { AxiosResponse } from 'axios';
import type { AxiosInstance, AxiosRequestConfig, AxiosError } from 'axios';
-import { Modal } from '@answer/components';
-import { loggedUserInfoStore, toastStore } from '@answer/stores';
-
-import Storage from './storage';
-import { floppyNavigation } from './floppyNavigation';
-
+import { Modal } from '@/components';
+import { loggedUserInfoStore, toastStore } from '@/stores';
import {
LOGGED_TOKEN_STORAGE_KEY,
CURRENT_LANG_STORAGE_KEY,
@@ -14,6 +10,9 @@ import {
} from '@/common/constants';
import { RouteAlias } from '@/router/alias';
+import Storage from './storage';
+import { floppyNavigation } from './floppyNavigation';
+
const API = {
development: '',
production: '',
diff --git a/ui/tsconfig.json b/ui/tsconfig.json
index d7c5decf..f270b720 100644
--- a/ui/tsconfig.json
+++ b/ui/tsconfig.json
@@ -20,18 +20,7 @@
"jsx": "react-jsx",
"baseUrl": "./",
"paths": {
- "@/*": ["src/*"],
- "@answer/pages/*": ["src/pages/*"],
- "@answer/components": ["src/components/index.ts"],
- "@answer/components/*": ["src/components/*"],
- "@answer/stores": ["src/stores"],
- "@answer/stores/*": ["src/stores/*"],
- "@answer/services/*": ["src/services/*"],
- "@answer/hooks": ["src/hooks"],
- "@answer/common": ["src/common"],
- "@answer/common/*": ["src/common/*"],
- "@answer/utils": ["src/utils"],
- "@answer/utils/*": ["src/utils/*"]
+ "@/*": ["src/*"]
}
},
"include": ["src", "node_modules/@testing-library/jest-dom"]