answer/ui/src/components/QuestionList/index.tsx

149 lines
4.8 KiB
TypeScript
Raw Normal View History

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';
import { pathFactory } from '@/router/pathFactory';
import type * as Type from '@/common/interface';
2022-09-29 14:56:09 +08:00
import {
Icon,
Tag,
Pagination,
FormatTime,
Empty,
BaseUserCard,
2022-10-09 18:20:19 +08:00
QueryGroup,
QuestionListLoader,
} 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,
tag: tagName,
2022-09-27 17:59:05 +08:00
};
if (source === 'questions') {
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>
<ListGroup className="rounded-0">
{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
/>
</div>
<div className="ms-0 ms-md-3 mt-2 mt-md-0">
<span>
<Icon name="hand-thumbs-up-fill" />
<em className="fst-normal ms-1">{li.vote_count}</em>
</span>
<span
className={`ms-3 ${
li.accepted_answer_id >= 1 ? 'text-success' : ''
}`}>
<Icon
name={
li.accepted_answer_id >= 1
? 'check-circle-fill'
: 'chat-square-text-fill'
}
/>
<em className="fst-normal ms-1">{li.answer_count}</em>
</span>
<span className="summary-stat ms-3">
<Icon name="eye-fill" />
<em className="fst-normal ms-1">{li.view_count}</em>
</span>
</div>
</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>
</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;