Skip to content

Commit ce0333e

Browse files
fixed time issues and multi comment sorting and click (#3784)
1 parent 9ffdc3b commit ce0333e

File tree

2 files changed

+53
-24
lines changed

2 files changed

+53
-24
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import React from 'react';
2-
import { Stack, Avatar, Link, Text } from '@codesandbox/components';
3-
import { formatDistance } from 'date-fns';
1+
import { Avatar, Link, Stack, Text } from '@codesandbox/components';
42
import { CommentFragment } from 'app/graphql/types';
3+
import { formatDistance } from 'date-fns';
4+
import { zonedTimeToUtc } from 'date-fns-tz';
5+
import React from 'react';
56

67
export const AvatarBlock: React.FC<{ comment: CommentFragment }> = ({
78
comment,
@@ -18,9 +19,13 @@ export const AvatarBlock: React.FC<{ comment: CommentFragment }> = ({
1819
{comment.user.username}
1920
</Link>
2021
<Text size={2} variant="muted">
21-
{formatDistance(new Date(comment.insertedAt), new Date(), {
22-
addSuffix: true,
23-
})}
22+
{formatDistance(
23+
zonedTimeToUtc(comment.insertedAt, 'Etc/UTC'),
24+
new Date(),
25+
{
26+
addSuffix: true,
27+
}
28+
)}
2429
</Text>
2530
</Stack>
2631
</Stack>

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

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import React from 'react';
2-
import { formatDistanceStrict } from 'date-fns';
1+
import { Element, Text } from '@codesandbox/components';
32
import { css } from '@styled-system/css';
43
import { useOvermind } from 'app/overmind';
5-
import { Text, Element } from '@codesandbox/components';
4+
import { formatDistanceStrict } from 'date-fns';
5+
import { zonedTimeToUtc } from 'date-fns-tz';
6+
import React from 'react';
67

78
type MultiCommentProps = {
89
x: number;
@@ -12,10 +13,7 @@ type MultiCommentProps = {
1213

1314
export const MultiComment = ({ x, y, ids }: MultiCommentProps) => {
1415
const {
15-
state: {
16-
editor,
17-
comments,
18-
},
16+
state: { editor, comments },
1917
actions,
2018
} = useOvermind();
2119

@@ -78,21 +76,48 @@ export const MultiComment = ({ x, y, ids }: MultiCommentProps) => {
7876
});
7977

8078
const date = comment =>
81-
formatDistanceStrict(new Date(comment.insertedAt), new Date(), {
82-
addSuffix: true,
83-
});
79+
formatDistanceStrict(
80+
zonedTimeToUtc(comment.insertedAt, 'Etc/UTC'),
81+
new Date(),
82+
{
83+
addSuffix: true,
84+
}
85+
);
8486

8587
return (
8688
<Element as="ul" css={list}>
87-
{ids.map(id => {
88-
const comment = comments.comments[editor.currentSandbox.id][id];
89-
return (
90-
<Element as="li" key={id}>
89+
{ids
90+
.map(id => comments.comments[editor.currentSandbox.id][id])
91+
.sort((commentA, commentB) => {
92+
const dateA = new Date(commentA.insertedAt);
93+
const dateB = new Date(commentB.insertedAt);
94+
if (dateA < dateB) {
95+
return 1;
96+
}
97+
98+
if (dateA > dateB) {
99+
return -1;
100+
}
101+
102+
return 0;
103+
})
104+
.map(comment => (
105+
<Element as="li" key={comment.id}>
91106
<Element
92107
as="button"
93108
type="button"
94-
// @ts-ignore
95-
onClick={() => actions.comments.selectComment(id)}
109+
onClick={event => {
110+
const bounds = event.currentTarget.getBoundingClientRect();
111+
actions.comments.selectComment({
112+
commentId: comment.id,
113+
bounds: {
114+
left: bounds.left,
115+
right: bounds.right,
116+
top: bounds.top,
117+
bottom: bounds.bottom,
118+
},
119+
});
120+
}}
96121
css={item}
97122
>
98123
<Text
@@ -110,8 +135,7 @@ export const MultiComment = ({ x, y, ids }: MultiCommentProps) => {
110135
</Text>
111136
</Element>
112137
</Element>
113-
);
114-
})}
138+
))}
115139
</Element>
116140
);
117141
};

0 commit comments

Comments
 (0)