-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
CommentInline.tsx
156 lines (146 loc) · 4.14 KB
/
CommentInline.tsx
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
import React, { memo, useCallback, useState } from 'react';
import { useCache, useController } from 'rest-hooks';
import { Card, Avatar, Button, Tag, Popover } from 'antd';
import Markdown from 'react-markdown';
import { Link, useRoutes } from '@anansi/router';
import remarkGfm from 'remark-gfm';
import remarkRemoveComments from 'remark-remove-comments';
import rehypeHighlight from 'rehype-highlight';
import { UserResource } from 'resources/User';
import { css } from '@linaria/core';
import { EllipsisOutlined } from '@ant-design/icons';
import { CommentResource, Comment } from 'resources/Comment';
import FlexRow from 'components/FlexRow';
import CommentForm from './CommentForm';
const { Meta } = Card;
const commentList = css`
.ant-card-meta-detail {
width: 100%;
}
`;
function CommentInline({ comment }: { comment: Comment }) {
const [editing, setEditing] = useState(false);
return (
<Card style={{ marginTop: 16 }} className={commentList}>
<Meta
avatar={
<Link name="ProfileDetail" props={{ login: comment.user.login }}>
<Avatar src={comment.user.avatarUrl} />
</Link>
}
title={
<FlexRow>
<span>
<Link name="ProfileDetail" props={{ login: comment.user.login }}>
{comment.user.login}
</Link>{' '}
commented on{' '}
{new Intl.DateTimeFormat(navigator.language).format(
comment.createdAt,
)}
</span>
<CommentControls comment={comment} setEditing={setEditing} />
</FlexRow>
}
description={
editing ? (
<EditForm comment={comment} onFinish={() => setEditing(false)} />
) : (
<Markdown
remarkPlugins={[remarkRemoveComments, remarkGfm]}
rehypePlugins={[rehypeHighlight]}
>
{comment.body}
</Markdown>
)
}
/>
</Card>
);
}
export default memo(CommentInline);
function CommentControls({
comment,
setEditing,
}: {
comment: Comment;
setEditing: React.Dispatch<React.SetStateAction<boolean>>;
}) {
const currentUser = useCache(UserResource.current);
if (!currentUser) return null;
if (comment.user.login !== currentUser.login) return null;
return (
<span>
{comment.authorAssociation !== 'NONE' ? (
<Tag>{capFirst(comment.authorAssociation)}</Tag>
) : null}
<PopControls comment={comment} setEditing={setEditing} />
</span>
);
}
function PopControls({
comment,
setEditing,
}: {
comment: Comment;
setEditing: React.Dispatch<React.SetStateAction<boolean>>;
}) {
const route = useRoutes<{ owner: string; repo: string }>()[0];
const { fetch } = useController();
const handleEdit = () => setEditing(true);
const handleDelete = () =>
fetch(CommentResource.delete, {
owner: route.owner,
repo: route.repo,
id: comment.id,
});
return (
<Popover
placement="bottomLeft"
content={
<>
<Button key="edit" size="small" type="text" onClick={handleEdit}>
Edit
</Button>
<Button key="delete" size="small" type="text" onClick={handleDelete}>
Delete
</Button>
</>
}
trigger="click"
>
<Button size="small" icon={<EllipsisOutlined />} shape="circle"></Button>
</Popover>
);
}
function capFirst(string: string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLocaleLowerCase();
}
function EditForm({
comment,
onFinish,
}: {
comment: Comment;
onFinish: () => void;
}) {
const { fetch } = useController();
const handleFinish = useCallback(
({ body }: { body: string }) => {
return fetch(
CommentResource.partialUpdate,
{ owner: comment.owner, repo: comment.repo, id: comment.id },
{ body },
).then(onFinish);
},
[comment.id, comment.owner, comment.repo, fetch, onFinish],
);
const handleCancel = onFinish;
return (
<CommentForm
onFinish={handleFinish}
label="Edit Comment"
initialValues={comment}
onCancel={handleCancel}
/>
);
}