-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathindex.tsx
More file actions
203 lines (188 loc) · 5.4 KB
/
index.tsx
File metadata and controls
203 lines (188 loc) · 5.4 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
194
195
196
197
198
199
200
201
202
203
import { memo, FC, useState, useEffect, useRef } from 'react';
import { Link, useSearchParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { Button, OverlayTrigger, Tooltip } from 'react-bootstrap';
import {
Tag,
Actions,
Operate,
UserCard,
Comment,
FormatTime,
htmlRender,
Icon,
ImgViewer,
} from '@/components';
import { formatCount, guard } from '@/utils';
import { following } from '@/services';
import { pathFactory } from '@/router/pathFactory';
interface Props {
data: any;
hasAnswer: boolean;
isLogged: boolean;
initPage: (type: string) => void;
}
const Index: FC<Props> = ({ data, initPage, hasAnswer, isLogged }) => {
const { t } = useTranslation('translation', {
keyPrefix: 'question_detail',
});
const [searchParams] = useSearchParams();
const [followed, setFollowed] = useState(data?.is_followed);
const ref = useRef<HTMLDivElement>(null);
const handleFollow = (e) => {
e.preventDefault();
if (!guard.tryNormalLogged(true)) {
return;
}
following({
object_id: data?.id,
is_cancel: followed,
}).then((res) => {
setFollowed(res.is_followed);
});
};
useEffect(() => {
if (data) {
setFollowed(data?.is_followed);
}
}, [data]);
useEffect(() => {
if (!ref.current) {
return;
}
htmlRender(ref.current);
}, [ref.current]);
if (!data?.id) {
return null;
}
return (
<div className="answer_question-asked">
<h1 className="answer_question-title h3 mb-3 text-wrap text-break">
{data?.pin === 2 && (
<Icon
name="pin-fill"
className="me-1"
title={t('pinned', { keyPrefix: 'btns' })}
/>
)}
<Link
className="link-dark"
reloadDocument
to={pathFactory.questionLanding(data.id, data.url_title)}>
{data.title}
{data.status === 2
? ` [${t('closed', { keyPrefix: 'question' })}]`
: ''}
</Link>
</h1>
<div className="d-flex flex-wrap align-items-center small mb-3 text-secondary">
<FormatTime
time={data.create_time}
preFix={t('Asked')}
className="me-3"
/>
<FormatTime
time={data.update_time}
preFix={t('update')}
className="me-3"
/>
{data?.view_count > 0 && (
<div className="me-3">
{t('Views')} {formatCount(data.view_count)}
</div>
)}
<OverlayTrigger
placement="bottom"
overlay={<Tooltip id="followTooltip">{t('follow_tip')}</Tooltip>}>
<Button
variant="link"
size="sm"
className="p-0 btn-no-border"
onClick={(e) => handleFollow(e)}>
{t(followed ? 'Following' : 'Follow')}
</Button>
</OverlayTrigger>
</div>
<div className="m-n1">
{data?.tags?.map((item: any) => {
return <Tag className="m-1" key={item.slug_name} data={item} />;
})}
</div>
<ImgViewer>
<article
ref={ref}
className="fmt text-break text-wrap mt-4"
dangerouslySetInnerHTML={{ __html: data?.html }}
/>
</ImgViewer>
<Actions
className="mt-4"
source="question"
data={{
id: data?.id,
isHate: data?.vote_status === 'vote_down',
isLike: data?.vote_status === 'vote_up',
votesCount: data?.vote_count,
collected: data?.collected,
collectCount: data?.collection_count,
username: data.user_info?.username,
}}
/>
<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?.id}
type="question"
memberActions={data?.member_actions}
title={data.title}
slugTitle={data.url_title}
hasAnswer={hasAnswer}
isAccepted={Boolean(data?.accepted_answer_id)}
callback={initPage}
/>
</div>
<div style={{ minWidth: '196px' }} className="mb-3 me-4 mb-md-0">
{data.update_user_info &&
data.update_user_info?.username !== data.user_info?.username ? (
<UserCard
data={data?.update_user_info}
time={data.edit_time}
preFix={t('edit')}
isLogged={isLogged}
timelinePath={`/posts/${data.id}/timeline`}
/>
) : isLogged ? (
<Link to={`/posts/${data.id}/timeline`}>
<FormatTime
time={data.edit_time}
preFix={t('edit')}
className="link-secondary small"
/>
</Link>
) : (
<FormatTime
time={data.edit_time}
preFix={t('edit')}
className="text-secondary small"
/>
)}
</div>
<div style={{ minWidth: '196px' }}>
<UserCard
data={data?.user_info}
time={data.create_time}
preFix={t('asked')}
isLogged={isLogged}
timelinePath={`/posts/${data.id}/timeline`}
/>
</div>
</div>
<Comment
objectId={data?.id}
mode="question"
commentId={searchParams.get('commentId')}
/>
</div>
);
};
export default memo(Index);