Skip to content

Commit 018f5d1

Browse files
authored
add reply things (codesandbox#3637)
* add reply things * fix click * make textarea look okay? * good textarea
1 parent 85227b6 commit 018f5d1

File tree

9 files changed

+193
-74
lines changed

9 files changed

+193
-74
lines changed

packages/app/src/app/overmind/effects/fakeGql/comments/mutations.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ export const reply: Query<any, any> = gql`
9797
) {
9898
id
9999
replies {
100+
insertedAt
101+
updatedAt
100102
id
101103
content
102104
author {
@@ -117,6 +119,8 @@ export const deleteReply: Query<
117119
deleteReply(replyId: $replyId, commentId: $commentId) {
118120
id
119121
replies {
122+
insertedAt
123+
updatedAt
120124
id
121125
content
122126
author {
@@ -137,6 +141,8 @@ export const updateReply: Query<
137141
updateReply(replyId: $replyId, commentId: $commentId, comment: $comment) {
138142
id
139143
replies {
144+
insertedAt
145+
updatedAt
140146
id
141147
content
142148
author {

packages/app/src/app/overmind/effects/fakeGql/comments/queries.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ export const allComments: Query<CommentsResponse, CommentsVariables> = gql`
2323
}
2424
}
2525
replies {
26+
insertedAt
27+
updatedAt
2628
id
2729
content
2830
author {
31+
id
2932
avatarUrl
3033
username
3134
}
@@ -54,9 +57,12 @@ export const comment: Query<CommentResponse, CommentVariables> = gql`
5457
id
5558
content
5659
author {
60+
id
5761
avatarUrl
5862
username
5963
}
64+
insertedAt
65+
updatedAt
6066
}
6167
insertedAt
6268
updatedAt

packages/app/src/app/overmind/effects/fakeGql/comments/types.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ export type Message = {
44
author?: Author;
55
};
66

7+
export type Reply = {
8+
id: string;
9+
content?: string;
10+
author?: Author;
11+
insertedAt?: string;
12+
updatedAt?: string;
13+
};
14+
715
export type Author = {
816
id: string;
917
avatarUrl: string;
@@ -14,7 +22,7 @@ export type Comment = {
1422
id: string;
1523
isResolved: boolean;
1624
originalMessage: Message;
17-
replies: Message[];
25+
replies: Reply[];
1826
insertedAt: string;
1927
updatedAt?: string;
2028
metadata?: string;
@@ -29,7 +37,7 @@ export type CommentsResponse = {
2937
{
3038
id: string;
3139
isResolved: boolean;
32-
replies: Pick<Message, 'id'>[];
40+
replies: Pick<Reply, 'id'>[];
3341
originalMessage: Message;
3442
insertedAt: string;
3543
updatedAt: string;
@@ -49,7 +57,7 @@ export type AddCommentResponse = {
4957
addComment: {
5058
id: string;
5159
isResolved: boolean;
52-
replies: Pick<Message, 'id'>[];
60+
replies: Pick<Reply, 'id'>[];
5361
originalMessage: Message;
5462
insertedAt: string;
5563
updatedAt: string;
@@ -98,7 +106,7 @@ export type CommentResponse = {
98106
id: string;
99107
content: string;
100108
};
101-
replies: Message[];
109+
replies: Reply[];
102110
insertedAt: string;
103111
updatedAt: string;
104112
metadata: string;
@@ -110,7 +118,7 @@ export type DeleteReplyVariables = { replyId: string; commentId: string };
110118
export type DeleteReplyResponse = {
111119
deleteReply: {
112120
id: string;
113-
replies: Message[];
121+
replies: Reply[];
114122
};
115123
};
116124

@@ -123,6 +131,6 @@ export type UpdateReplyVariables = {
123131
export type UpdateReplyResponse = {
124132
updateReply: {
125133
id: string;
126-
replies: Message[];
134+
replies: Reply[];
127135
};
128136
};

packages/app/src/app/overmind/namespaces/editor/actions.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,13 +1488,22 @@ export const updateComment: AsyncAction<{
14881488
const sandboxId = state.editor.currentSandbox.id;
14891489
const isResolved = state.editor.comments[sandboxId][id].isResolved;
14901490
const comment = state.editor.comments[sandboxId][id].originalMessage.content;
1491+
const updateIsCurrent =
1492+
state.editor.comments[sandboxId][id].id === state.editor.currentComment.id;
14911493

14921494
if ('isResolved' in data) {
14931495
state.editor.comments[sandboxId][id].isResolved = data.isResolved;
1496+
if (updateIsCurrent) {
1497+
state.editor.currentComment.isResolved = data.isResolved;
1498+
}
14941499
}
14951500

14961501
if ('comment' in data) {
14971502
state.editor.comments[sandboxId][id].originalMessage.content = data.comment;
1503+
1504+
if (updateIsCurrent) {
1505+
state.editor.currentComment.originalMessage.content = data.comment;
1506+
}
14981507
}
14991508

15001509
try {
@@ -1508,6 +1517,10 @@ export const updateComment: AsyncAction<{
15081517
);
15091518
state.editor.comments[sandboxId][id].isResolved = isResolved;
15101519
state.editor.comments[sandboxId][id].originalMessage.content = comment;
1520+
if (updateIsCurrent) {
1521+
state.editor.currentComment.id = id;
1522+
state.editor.currentComment.originalMessage.content = comment;
1523+
}
15111524
}
15121525
};
15131526

@@ -1635,6 +1648,7 @@ export const addReply: AsyncAction<string> = async (
16351648
// sorry
16361649
// @ts-ignore
16371650
replies: state.editor.currentComment.replies.concat({
1651+
insertedAt: new Date(),
16381652
id: fakeId,
16391653
content: comment,
16401654
author: {

packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/Comments/Comment.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export const Comment = React.memo(({ comment }: any) => {
3131

3232
return (
3333
<ListAction
34-
onClick={() => actions.editor.selectComment(comment.id)}
3534
key={comment.id}
3635
paddingTop={5}
3736
css={css({
@@ -44,7 +43,11 @@ export const Comment = React.memo(({ comment }: any) => {
4443
})}
4544
>
4645
<Stack align="flex-start" justify="space-between" marginBottom={4}>
47-
<Stack gap={2} align="center">
46+
<Stack
47+
gap={2}
48+
align="center"
49+
onClick={() => actions.editor.selectComment(comment.id)}
50+
>
4851
<Avatar user={comment.originalMessage.author} />
4952
<Stack direction="vertical" justify="center">
5053
<Link
@@ -93,6 +96,7 @@ export const Comment = React.memo(({ comment }: any) => {
9396
</Stack>
9497
</Stack>
9598
<Element
99+
onClick={() => actions.editor.selectComment(comment.id)}
96100
as="p"
97101
marginY={0}
98102
marginRight={2 /** Adjust for the missing margin in ListAction */}

packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/Comments/Dialog/Code.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/Comments/Dialog/Comment.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export const Comment = ({ source }) => {
4242
color: 'mutedForeground',
4343
content: "counter(counter) '. '",
4444
},
45+
p: {
46+
margin: 0,
47+
},
4548
li: {
4649
listStyle: 'none',
4750
},
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import React from 'react';
2+
import { formatDistance } from 'date-fns';
3+
import css from '@styled-system/css';
4+
import {
5+
Element,
6+
Stack,
7+
Avatar,
8+
Text,
9+
Link,
10+
Menu,
11+
} from '@codesandbox/components';
12+
import { useOvermind } from 'app/overmind';
13+
import { Comment } from './Comment';
14+
15+
type ReplyProps = {
16+
id: string;
17+
author?: any;
18+
insertedAt?: string;
19+
commentId: string;
20+
content?: string;
21+
};
22+
23+
export const Reply = ({
24+
id,
25+
author,
26+
insertedAt,
27+
commentId,
28+
content,
29+
}: ReplyProps) => {
30+
const { state, actions } = useOvermind();
31+
return (
32+
<>
33+
<Element
34+
key={id}
35+
paddingX={4}
36+
paddingTop={6}
37+
css={css({
38+
borderTop: '1px solid',
39+
borderColor: 'sideBar.border',
40+
})}
41+
>
42+
<Stack align="flex-start" justify="space-between" marginBottom={4}>
43+
<Stack gap={2} align="center">
44+
<Avatar user={author} />
45+
<Stack direction="vertical" justify="center">
46+
<Link
47+
href={`/u/${author.username}`}
48+
variant="body"
49+
css={{ fontWeight: 'bold', display: 'block' }}
50+
>
51+
{author.username}
52+
</Link>
53+
<Text size={12} variant="muted">
54+
{formatDistance(new Date(insertedAt), new Date(), {
55+
addSuffix: true,
56+
})}
57+
</Text>
58+
</Stack>
59+
</Stack>
60+
{state.user.id === author.id && (
61+
<Stack align="center">
62+
<Menu>
63+
<Menu.IconButton name="more" title="Reply actions" size={3} />
64+
<Menu.List>
65+
<Menu.Item
66+
onSelect={() =>
67+
actions.editor.deleteReply({
68+
replyId: id,
69+
commentId,
70+
})
71+
}
72+
>
73+
Delete
74+
</Menu.Item>
75+
<Menu.Item>Edit</Menu.Item>
76+
</Menu.List>
77+
</Menu>
78+
</Stack>
79+
)}
80+
</Stack>
81+
</Element>
82+
<Comment source={content} />
83+
</>
84+
);
85+
};

0 commit comments

Comments
 (0)