Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit ed04af1

Browse files
Gondragosnick-skriabinmakseq
authored
[ext] Add displaying created date for annotations (#349)
* [ext] Add annotation created date displaying * [fix] Fix created date by default * Make TimeAgo typed, add some comments * Run actions on sub-branches (cherry picked from commit c1f1744) * Restore safeUnfreeze method (cherry picked from commit 3bcbf6d) Co-authored-by: Nick <[email protected]> Co-authored-by: Nick Skriabin <[email protected]> Co-authored-by: Max Tkachenko <[email protected]>
1 parent 8cf800d commit ed04af1

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

src/common/TimeAgo/TimeAgo.tsx

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { format, formatDistanceToNow } from "date-fns";
2+
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
3+
4+
const SECS = 1000;
5+
const MINS = 60 * SECS;
6+
7+
// Each stage is a tuple of limit and step
8+
// limit is a time from base date, until which stage is active.
9+
// step is a time-step which we do between two adjusted ticks.
10+
// stages are chosen to match formatDistanceToNow function from date-fns
11+
const STAGES: [limit: number, step: number][] = [
12+
// from 0 seconds
13+
[30 * SECS, 30 * SECS], // to 30 seconds with 30 seconds step
14+
[44 * MINS + 30 * SECS, MINS], // to 44 minutes 30 seconds with 1 minute step
15+
[Number.MAX_SAFE_INTEGER, 30 * MINS], // to 30 seconds with 30 seconds step
16+
];
17+
18+
function getNextTick(passedTime = 0) {
19+
const idx = STAGES.findIndex(([timeLimit], idx) => {
20+
return timeLimit > passedTime || idx === STAGES.length - 1;
21+
});
22+
const baseLimit = idx > 0 ? STAGES[idx - 1][0] : 0;
23+
const baseStep = STAGES[idx][1];
24+
25+
return Math.ceil((passedTime - baseLimit + 1) / baseStep) * baseStep + baseLimit;
26+
}
27+
28+
type TimeAgoProps = React.ComponentPropsWithoutRef<"time"> & {
29+
date: number | string | Date
30+
}
31+
32+
export const TimeAgo = ({ date, ...rest }: TimeAgoProps) => {
33+
const [timestamp, forceUpdate] = useState(Date.now());
34+
const fromTS = useMemo(() => {
35+
return (new Date(date)).valueOf();
36+
}, [date]);
37+
const timeoutId = useRef<number>();
38+
const scheduleNext = useCallback(() => {
39+
const passedTime = Date.now() - fromTS;
40+
const tickValue = getNextTick(passedTime);
41+
42+
timeoutId.current = window.setTimeout(() => {
43+
forceUpdate(Date.now());
44+
}, tickValue - passedTime);
45+
}, [date]);
46+
47+
useEffect(() => {
48+
scheduleNext();
49+
return () => {
50+
clearTimeout(timeoutId.current);
51+
};
52+
}, [date, timestamp]);
53+
54+
return (
55+
<time
56+
dateTime={format(fromTS, "yyyy-MM-dd'T'HH:mm:ss.SSSxxx")}
57+
title={format(fromTS, "PPpp")}
58+
{...rest}
59+
>
60+
{formatDistanceToNow(fromTS, { addSuffix: true })}
61+
</time>
62+
);
63+
};

src/components/TopBar/Annotations.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Block, Elem } from "../../utils/bem";
77
import { isDefined, userDisplayName } from "../../utils/utilities";
88
import { GroundTruth } from "../CurrentEntity/GroundTruth";
99
import "./Annotations.styl";
10+
import { TimeAgo } from "../../common/TimeAgo/TimeAgo";
1011

1112
export const Annotations = observer(({ store, annotationStore }) => {
1213
const dropdownRef = useRef();
@@ -146,10 +147,14 @@ const Annotation = observer(({ entity, selected, onClick, extra, ...props }) =>
146147
<Elem tag="span" name="entity-id">#{entity.pk ?? entity.id}</Elem>
147148
</Elem>
148149

149-
{isDefined(entity.acceptedState) && (
150+
{isDefined(entity.acceptedState) ? (
150151
<Elem name="review" mod={{ state: entity.acceptedState }}>
151152
{entity.acceptedState}
152153
</Elem>
154+
) : (
155+
<Elem name="created">
156+
created, <Elem name="date" component={TimeAgo} date={entity.createdDate}/>
157+
</Elem>
153158
)}
154159
</Space>
155160
</Space>

src/components/TopBar/Annotations.styl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@
5757
&_rejected
5858
color #dd0000
5959

60+
&__created
61+
font-size 11px
62+
line-height 13px
63+
color rgba(0, 0, 0, 0.4)
64+
65+
&__date
66+
color rgba(0, 0, 0, 0.4)
67+
6068
&__counter
6169
font-size 11px
6270
line-height 13px

src/stores/AnnotationStore.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,7 @@ export default types
12241224
//
12251225
const node = {
12261226
userGenerate: false,
1227+
createdDate: Utils.UDate.currentISODate(),
12271228

12281229
...options,
12291230

0 commit comments

Comments
 (0)