-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathindex.tsx
More file actions
193 lines (181 loc) · 5.17 KB
/
index.tsx
File metadata and controls
193 lines (181 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { memo, FC, useEffect, useRef } from 'react';
import { Button, Alert, Badge } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
import { Link, useSearchParams } from 'react-router-dom';
import classNames from 'classnames';
import {
Actions,
Operate,
UserCard,
Icon,
Comment,
FormatTime,
htmlRender,
ImgViewer,
} from '@/components';
import { scrollToElementTop, bgFadeOut } from '@/utils';
import { AnswerItem } from '@/common/interface';
import { acceptanceAnswer } from '@/services';
interface Props {
data: AnswerItem;
/** router answer id */
aid?: string;
canAccept: boolean;
questionTitle: string;
slugTitle: string;
isLogged: boolean;
callback: (type: string) => void;
}
const Index: FC<Props> = ({
aid,
data,
isLogged,
questionTitle = '',
slugTitle,
callback,
canAccept = false,
}) => {
const { t } = useTranslation('translation', {
keyPrefix: 'question_detail',
});
const [searchParams] = useSearchParams();
const answerRef = useRef<HTMLDivElement>(null);
const acceptAnswer = () => {
acceptanceAnswer({
question_id: data.question_id,
answer_id: data.accepted === 2 ? '0' : data.id,
}).then(() => {
callback?.('');
});
};
useEffect(() => {
if (!answerRef?.current) {
return;
}
htmlRender(answerRef.current.querySelector('.fmt'));
if (aid === data.id) {
setTimeout(() => {
const element = answerRef.current;
scrollToElementTop(element);
if (!searchParams.get('commentId')) {
bgFadeOut(answerRef.current);
}
}, 100);
}
}, [data.id, answerRef.current]);
if (!data?.id) {
return null;
}
return (
<div
id={data.id}
ref={answerRef}
className={classNames(
'answer_answer answer-item py-4',
data?.accepted === 2 && 'answer_answer-accepted',
)}>
{data.status === 10 && (
<Alert variant="danger" className="mb-4">
{t('post_deleted', { keyPrefix: 'messages' })}
</Alert>
)}
{data?.accepted === 2 && (
<div className="mb-3 lh-1">
<Badge bg="success" pill>
<Icon name="check-circle-fill me-1" />
Best answer
</Badge>
</div>
)}
<ImgViewer>
<article
className="fmt text-break text-wrap"
dangerouslySetInnerHTML={{ __html: data?.html }}
/>
</ImgViewer>
<div className="d-flex align-items-center mt-4">
<Actions
source="answer"
data={{
id: data?.id,
isHate: data?.vote_status === 'vote_down',
isLike: data?.vote_status === 'vote_up',
votesCount: data?.vote_count,
hideCollect: true,
collected: data?.collected,
collectCount: 0,
username: data?.user_info?.username,
}}
/>
{canAccept && (
<Button
variant={data.accepted === 2 ? 'success' : 'outline-success'}
className="ms-3"
onClick={acceptAnswer}>
<Icon name="check-circle-fill" className="me-2" />
<span>
{data.accepted === 2
? t('answers.btn_accepted')
: t('answers.btn_accept')}
</span>
</Button>
)}
</div>
<div className="d-block d-md-flex flex-wrap mt-4 mb-3">
<div className="mb-3 mb-md-0 me-4 flex-grow-1">
<Operate
qid={data.question_id}
aid={data.id}
memberActions={data?.member_actions}
type="answer"
isAccepted={data.accepted === 2}
title={questionTitle}
slugTitle={slugTitle}
callback={callback}
/>
</div>
<div className="mb-3 mb-md-0 me-4" style={{ minWidth: '196px' }}>
{data.update_user_info &&
data.update_user_info?.username !== data.user_info?.username ? (
<UserCard
data={data?.update_user_info}
time={Number(data.update_time)}
preFix={t('edit')}
isLogged={isLogged}
timelinePath={`/posts/${data.question_id}/${data.id}/timeline`}
/>
) : isLogged ? (
<Link to={`/posts/${data.question_id}/${data.id}/timeline`}>
<FormatTime
time={Number(data.update_time)}
preFix={t('edit')}
className="link-secondary small"
/>
</Link>
) : (
<FormatTime
time={Number(data.update_time)}
preFix={t('edit')}
className="text-secondary small"
/>
)}
</div>
<div style={{ minWidth: '196px' }}>
<UserCard
data={data?.user_info}
time={Number(data.create_time)}
preFix={t('answered')}
isLogged={isLogged}
timelinePath={`/posts/${data.question_id}/${data.id}/timeline`}
/>
</div>
</div>
<Comment
objectId={data.id}
mode="answer"
commentId={searchParams.get('commentId')}
/>
</div>
);
};
export default memo(Index);