Skip to content

Commit 520139a

Browse files
committed
fix: refactor authorboxes to single component
1 parent 29ba0e4 commit 520139a

File tree

3 files changed

+48
-46
lines changed

3 files changed

+48
-46
lines changed

plugins/qeta-react/src/components/AnswerCard/AnswerCard.tsx

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useState } from 'react';
22
import { VoteButtons } from '../Buttons/VoteButtons';
33
import { AnswerForm } from '../AnswerForm';
4-
import { AuthorBox } from '../AuthorBox/AuthorBox';
54
import { CommentSection } from '../CommentSection/CommentSection';
65
import { LinkButton } from '../Buttons/LinkButton';
76
import DeleteIcon from '@material-ui/icons/Delete';
@@ -23,6 +22,7 @@ import {
2322
} from '@material-ui/core';
2423
import { useTranslationRef } from '@backstage/core-plugin-api/alpha';
2524
import { qetaTranslationRef } from '../../translation.ts';
25+
import { AuthorBoxes } from '../AuthorBox/AuthorBoxes.tsx';
2626

2727
export type AnswerCardClassKeys =
2828
| 'root'
@@ -181,29 +181,7 @@ export const AnswerCard = (props: {
181181
</Box>
182182
)}
183183
</Box>
184-
<Box
185-
display="flex"
186-
minWidth={220}
187-
style={{ gap: '8px' }}
188-
ml={1}
189-
>
190-
{answerEntity.updated && answerEntity.updatedBy && (
191-
<AuthorBox
192-
userEntityRef={answerEntity.updatedBy}
193-
time={answerEntity.updated}
194-
label={t('authorBox.updatedAtTime')}
195-
expert={false}
196-
anonymous={answerEntity.anonymous}
197-
/>
198-
)}
199-
<AuthorBox
200-
userEntityRef={answerEntity.author}
201-
time={answerEntity.created}
202-
label={t('authorBox.answeredAtTime')}
203-
expert={answerEntity.expert}
204-
anonymous={answerEntity.anonymous}
205-
/>
206-
</Box>
184+
<AuthorBoxes entity={answerEntity} />
207185
</Box>
208186
</>
209187
)}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {
2+
Answer,
3+
PostAnswerEntity,
4+
QetaIdEntity,
5+
} from '@drodil/backstage-plugin-qeta-common';
6+
import { Box } from '@material-ui/core';
7+
import { AuthorBox } from './AuthorBox.tsx';
8+
import { useTranslationRef } from '@backstage/core-plugin-api/alpha';
9+
import { qetaTranslationRef } from '../../translation.ts';
10+
11+
function isAnswer(entity: QetaIdEntity): entity is Answer {
12+
return 'postId' in entity && 'correct' in entity;
13+
}
14+
15+
export const AuthorBoxes = (props: { entity: PostAnswerEntity }) => {
16+
const entity = props.entity;
17+
const { t } = useTranslationRef(qetaTranslationRef);
18+
19+
return (
20+
<Box
21+
display="flex"
22+
minWidth={220}
23+
style={{ gap: '8px', justifyContent: 'flex-end' }}
24+
ml={1}
25+
>
26+
{entity.updated && entity.updatedBy && (
27+
<AuthorBox
28+
userEntityRef={entity.updatedBy}
29+
time={entity.updated}
30+
label={t('authorBox.updatedAtTime')}
31+
expert={false}
32+
anonymous={entity.anonymous}
33+
/>
34+
)}
35+
<AuthorBox
36+
userEntityRef={entity.author}
37+
time={entity.created}
38+
label={t('authorBox.answeredAtTime')}
39+
expert={isAnswer(entity) ? entity.expert : false}
40+
anonymous={entity.anonymous}
41+
/>
42+
</Box>
43+
);
44+
};

plugins/qeta-react/src/components/QuestionCard/QuestionCard.tsx

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import DeleteIcon from '@material-ui/icons/Delete';
99
import EditIcon from '@material-ui/icons/Edit';
1010
import RestoreIcon from '@material-ui/icons/Restore';
1111
import { FavoriteButton } from '../Buttons/FavoriteButton';
12-
import { AuthorBox } from '../AuthorBox/AuthorBox';
1312
import { TagsAndEntities } from '../TagsAndEntities/TagsAndEntities';
1413
import { CommentSection } from '../CommentSection/CommentSection';
1514
import { useApi, useRouteRef } from '@backstage/core-plugin-api';
@@ -30,6 +29,7 @@ import {
3029
} from '@material-ui/core';
3130
import { useIsModerator } from '../../hooks';
3231
import { qetaApiRef } from '../../api.ts';
32+
import { AuthorBoxes } from '../AuthorBox/AuthorBoxes.tsx';
3333

3434
export type QuestionCardClassKeys =
3535
| 'root'
@@ -185,27 +185,7 @@ export const QuestionCard = (props: { question: PostResponse }) => {
185185
)}
186186
</Box>
187187
</Box>
188-
<Box
189-
display="flex"
190-
minWidth={230}
191-
style={{ gap: '8px' }}
192-
ml={1}
193-
>
194-
{questionEntity.updated && questionEntity.updatedBy && (
195-
<AuthorBox
196-
userEntityRef={questionEntity.updatedBy}
197-
time={questionEntity.updated}
198-
label={t('authorBox.updatedAtTime')}
199-
anonymous={questionEntity.anonymous}
200-
/>
201-
)}
202-
<AuthorBox
203-
userEntityRef={questionEntity.author}
204-
time={questionEntity.created}
205-
label={t('authorBox.postedAtTime')}
206-
anonymous={questionEntity.anonymous}
207-
/>
208-
</Box>
188+
<AuthorBoxes entity={questionEntity} />
209189
</Box>
210190
</Grid>
211191
</Grid>

0 commit comments

Comments
 (0)