2022-09-27 17:59:05 +08:00
|
|
|
import { FC } from 'react';
|
2022-11-09 10:22:52 +08:00
|
|
|
import { ListGroup } from 'react-bootstrap';
|
2022-09-27 17:59:05 +08:00
|
|
|
import { NavLink, useParams, useSearchParams } from 'react-router-dom';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
2022-12-02 18:27:42 +08:00
|
|
|
import { pathFactory } from '@/router/pathFactory';
|
2022-11-02 17:14:47 +08:00
|
|
|
import type * as Type from '@/common/interface';
|
2022-09-29 14:56:09 +08:00
|
|
|
import {
|
|
|
|
Tag,
|
|
|
|
Pagination,
|
|
|
|
FormatTime,
|
|
|
|
Empty,
|
|
|
|
BaseUserCard,
|
2022-10-09 18:20:19 +08:00
|
|
|
QueryGroup,
|
2023-01-05 14:22:25 +08:00
|
|
|
QuestionListLoader,
|
2023-01-29 16:33:05 +08:00
|
|
|
Counts,
|
2022-11-02 17:14:47 +08:00
|
|
|
} from '@/components';
|
2022-10-31 11:39:33 +08:00
|
|
|
import { useQuestionList } from '@/services';
|
|
|
|
|
2022-09-27 17:59:05 +08:00
|
|
|
const QuestionOrderKeys: Type.QuestionOrderBy[] = [
|
|
|
|
'newest',
|
|
|
|
'active',
|
|
|
|
'frequent',
|
|
|
|
'score',
|
|
|
|
'unanswered',
|
|
|
|
];
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
source: 'questions' | 'tag';
|
|
|
|
}
|
|
|
|
|
|
|
|
const QuestionList: FC<Props> = ({ source }) => {
|
|
|
|
const { t } = useTranslation('translation', { keyPrefix: 'question' });
|
|
|
|
const { tagName = '' } = useParams();
|
2022-10-09 18:20:19 +08:00
|
|
|
const [urlSearchParams] = useSearchParams();
|
2022-09-27 17:59:05 +08:00
|
|
|
const curOrder = urlSearchParams.get('order') || QuestionOrderKeys[0];
|
|
|
|
const curPage = Number(urlSearchParams.get('page')) || 1;
|
|
|
|
const pageSize = 20;
|
|
|
|
const reqParams: Type.QueryQuestionsReq = {
|
|
|
|
page_size: pageSize,
|
|
|
|
page: curPage,
|
|
|
|
order: curOrder as Type.QuestionOrderBy,
|
2022-10-27 10:50:10 +08:00
|
|
|
tag: tagName,
|
2022-09-27 17:59:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (source === 'questions') {
|
2022-10-27 10:50:10 +08:00
|
|
|
delete reqParams.tag;
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
const { data: listData, isLoading } = useQuestionList(reqParams);
|
|
|
|
const count = listData?.count || 0;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
2022-11-09 10:22:52 +08:00
|
|
|
<div className="mb-3 d-flex flex-wrap justify-content-between">
|
|
|
|
<h5 className="fs-5 text-nowrap mb-3 mb-md-0">
|
|
|
|
{source === 'questions'
|
|
|
|
? t('all_questions')
|
|
|
|
: t('x_questions', { count })}
|
|
|
|
</h5>
|
|
|
|
<QueryGroup
|
|
|
|
data={QuestionOrderKeys}
|
|
|
|
currentSort={curOrder}
|
|
|
|
pathname={source === 'questions' ? '/questions' : ''}
|
|
|
|
i18nKeyPrefix="question"
|
|
|
|
/>
|
|
|
|
</div>
|
2022-12-21 15:19:33 +08:00
|
|
|
<ListGroup className="rounded-0">
|
2023-01-05 14:22:25 +08:00
|
|
|
{isLoading ? (
|
|
|
|
<QuestionListLoader />
|
|
|
|
) : (
|
|
|
|
listData?.list?.map((li) => {
|
|
|
|
return (
|
|
|
|
<ListGroup.Item
|
|
|
|
key={li.id}
|
|
|
|
className="bg-transparent py-3 px-0 border-start-0 border-end-0">
|
|
|
|
<h5 className="text-wrap text-break">
|
|
|
|
<NavLink
|
|
|
|
to={pathFactory.questionLanding(li.id, li.url_title)}
|
|
|
|
className="link-dark">
|
|
|
|
{li.title}
|
|
|
|
{li.status === 2 ? ` [${t('closed')}]` : ''}
|
|
|
|
</NavLink>
|
|
|
|
</h5>
|
|
|
|
<div className="d-flex flex-column flex-md-row align-items-md-center fs-14 mb-2 text-secondary">
|
|
|
|
<div className="d-flex">
|
|
|
|
<BaseUserCard
|
|
|
|
data={li.operator}
|
|
|
|
showAvatar={false}
|
|
|
|
className="me-1"
|
|
|
|
/>
|
|
|
|
•
|
|
|
|
<FormatTime
|
|
|
|
time={li.operated_at}
|
|
|
|
className="text-secondary ms-1"
|
|
|
|
preFix={t(li.operation_type)}
|
2022-10-14 17:30:16 +08:00
|
|
|
/>
|
2023-01-05 14:22:25 +08:00
|
|
|
</div>
|
2023-01-29 16:33:05 +08:00
|
|
|
<Counts
|
|
|
|
data={{
|
|
|
|
votes: li.vote_count,
|
|
|
|
answers: li.answer_count,
|
|
|
|
views: li.view_count,
|
|
|
|
}}
|
|
|
|
isAccepted={li.accepted_answer_id >= 1}
|
|
|
|
className="ms-0 ms-md-3 mt-2 mt-md-0"
|
|
|
|
/>
|
2023-01-05 14:22:25 +08:00
|
|
|
</div>
|
|
|
|
<div className="question-tags m-n1">
|
|
|
|
{Array.isArray(li.tags)
|
|
|
|
? li.tags.map((tag) => {
|
|
|
|
return (
|
|
|
|
<Tag key={tag.slug_name} className="m-1" data={tag} />
|
|
|
|
);
|
|
|
|
})
|
|
|
|
: null}
|
2022-10-14 17:30:16 +08:00
|
|
|
</div>
|
2023-01-05 14:22:25 +08:00
|
|
|
</ListGroup.Item>
|
|
|
|
);
|
|
|
|
})
|
|
|
|
)}
|
2022-09-27 17:59:05 +08:00
|
|
|
</ListGroup>
|
|
|
|
{count <= 0 && !isLoading && <Empty />}
|
|
|
|
<div className="mt-4 mb-2 d-flex justify-content-center">
|
|
|
|
<Pagination
|
|
|
|
currentPage={curPage}
|
|
|
|
totalSize={count}
|
|
|
|
pageSize={pageSize}
|
2022-09-28 15:52:42 +08:00
|
|
|
pathname="/questions"
|
2022-09-27 17:59:05 +08:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default QuestionList;
|