Bin
2025-12-17 d616898802dfe7e5dd648bcf53c6d1f86b6d3642
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import { when } from "mobx";
import { inject, observer } from "mobx-react";
import { type FC, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import {
  IconAnnotationAccepted,
  IconAnnotationImported,
  IconAnnotationPrediction,
  IconAnnotationPropagated,
  IconAnnotationRejected,
  IconAnnotationReviewRemoved,
  IconAnnotationSkipped,
  IconAnnotationSubmitted,
  IconCheck,
  IconDraftCreated,
  IconSparks,
  IconHistoryRewind,
} from "@humansignal/icons";
import { Tooltip, Userpic } from "@humansignal/ui";
import { Space } from "../../common/Space/Space";
import { cn } from "../../utils/bem";
import { humanDateDiff, userDisplayName } from "../../utils/utilities";
import { EmptyState } from "../SidePanels/Components/EmptyState";
import "./AnnotationHistory.scss";
 
type HistoryItemType =
  | "prediction"
  | "imported"
  | "submitted"
  | "updated"
  | "skipped"
  | "accepted"
  | "rejected"
  | "fixed_and_accepted"
  | "draft_created"
  | "deleted_review"
  | "propagated_annotation";
 
const injector = inject(({ store }) => {
  const as = store.annotationStore;
  const selected = as?.selected;
 
  return {
    annotationStore: as,
    selected: as?.selected,
    createdBy: selected?.user ?? { email: selected?.createdBy },
    createdDate: selected?.createdDate,
    history: as?.history,
    selectedHistory: as?.selectedHistory,
  };
});
 
const DraftState: FC<{
  annotation: any;
  inline?: boolean;
  isSelected?: boolean;
}> = observer(({ annotation, inline, isSelected }: { annotation: any; inline?: boolean; isSelected?: boolean }) => {
  const hasChanges = annotation.history.hasChanges;
  const store = annotation.list; // @todo weird name
  const infoIsHidden = store.store.hasInterface("annotations:hide-info");
  const hiddenUser = infoIsHidden ? { email: "Me" } : null;
 
  const [hasUnsavedChanges, setChanges] = useState(false);
 
  // turn it on when changes just made; off when they we saved
  useEffect(() => {
    setChanges(true);
  }, [annotation.history.history.length]);
  useEffect(() => {
    setChanges(false);
  }, [annotation.draftSaved]);
 
  if (!hasChanges && !annotation.versions.draft) return null;
 
  return (
    <HistoryItem
      key="draft"
      user={hiddenUser ?? annotation.user ?? { email: annotation.createdBy }}
      date={annotation.draftSaved}
      extra={
        annotation.isDraftSaving ? (
          <div className={cn("annotation-history").elem("saving").toClassName()}>
            <div className={cn("annotation-history").elem("spin").toClassName()} />
          </div>
        ) : hasUnsavedChanges ? (
          <div className={cn("annotation-history").elem("saving").toClassName()}>
            <div className={cn("annotation-history").elem("dot").toClassName()} />
          </div>
        ) : hasChanges ? (
          <div className={cn("annotation-history").elem("saving").toClassName()}>
            <IconCheck className={cn("annotation-history").elem("saved").toClassName()} />
          </div>
        ) : null
      }
      inline={inline}
      comment=""
      acceptedState="draft_created"
      selected={isSelected}
      hideInfo={infoIsHidden}
      onClick={() => {
        store.selectHistory(null);
        annotation.toggleDraft(true);
      }}
    />
  );
});
 
const AnnotationHistoryComponent: FC<any> = ({
  annotationStore,
  selectedHistory,
  history,
  enabled = true,
  inline = false,
  showEmptyState = true,
  sectionHeader,
  renderEmptyState,
}) => {
  const annotation = annotationStore.selected;
  const lastItem = history?.length ? history[0] : null;
  const hasChanges = annotation.history.hasChanges;
  const infoIsHidden = annotationStore.store.hasInterface("annotations:hide-info");
  const currentUser = window.APP_SETTINGS?.user;
 
  // if user makes changes at the first time there are no draft yet
  const isDraftSelected =
    !annotationStore.selectedHistory && (annotation.draftSelected || (!annotation.versions.draft && hasChanges));
 
  // Determine if the empty state should be shown
  const hasDraft = annotation?.versions?.draft;
  const hasHistory = history && history.length > 0;
  const shouldShowEmptyState = showEmptyState && !hasChanges && !hasDraft && !hasHistory;
 
  // Default empty state component
  const defaultEmptyState = (
    <EmptyState
      icon={<IconHistoryRewind width={24} height={24} />}
      header="View annotation activity"
      description={<>See a log of user actions for this annotation</>}
    />
  );
 
  // If we should show empty state, render it
  if (shouldShowEmptyState) {
    return (
      <div className={cn("annotation-history").mod({ inline, empty: true }).toClassName()}>
        {sectionHeader && (
          <div className={`${cn("annotation-history").elem("section-head").toString()} sr-only`}>{sectionHeader}</div>
        )}
        {renderEmptyState ? renderEmptyState() : defaultEmptyState}
      </div>
    );
  }
 
  return (
    <div className={cn("annotation-history").mod({ inline }).toClassName()}>
      {sectionHeader && (
        <div className={`${cn("annotation-history").elem("section-head").toString()} sr-only`}>{sectionHeader}</div>
      )}
      <DraftState annotation={annotation} isSelected={isDraftSelected} inline={inline} />
      {enabled &&
        history.length > 0 &&
        history.map((item: any) => {
          const { id, user, createdDate } = item;
          const isLastItem = lastItem?.id === item.id;
          const isSelected = isLastItem && !selectedHistory ? !isDraftSelected : selectedHistory?.id === item.id;
          const hiddenUser = infoIsHidden ? { email: currentUser?.id === user.id ? "Me" : "User" } : null;
 
          return (
            <HistoryItem
              key={id}
              inline={inline}
              user={hiddenUser ?? user ?? { email: item?.createdBy }}
              date={createdDate}
              comment={item.comment}
              acceptedState={item.actionType}
              selected={isSelected}
              disabled={item.results.length === 0}
              hideInfo={infoIsHidden}
              onClick={async () => {
                if (hasChanges) {
                  annotation.saveDraftImmediately();
                  // wait for draft to be saved before switching to history
                  await when(() => !annotation.isDraftSaving);
                }
                if (isLastItem || isSelected) {
                  // last history state and draft are actual annotation, not from history
                  // and if user clicks on already selected item we should switch to last state
                  annotationStore.selectHistory(null);
                  // if user clicks on last history state we should disable draft to see submitted state
                  annotation.toggleDraft(isSelected);
                } else {
                  annotationStore.selectHistory(item);
                }
              }}
            />
          );
        })}
    </div>
  );
};
 
const HistoryItemComponent: FC<{
  entity?: any;
  user: any;
  date: string | number;
  extra?: any;
  comment: string;
  acceptedState: HistoryItemType;
  selected?: boolean;
  disabled?: boolean;
  inline?: boolean;
  hideInfo?: boolean;
  onClick: any;
}> = ({
  entity,
  user,
  date,
  extra,
  comment,
  acceptedState,
  selected = false,
  disabled = false,
  inline = false,
  hideInfo: infoIsHidden,
  onClick,
}) => {
  const isPrediction = entity?.type === "prediction";
 
  const reason = useMemo(() => {
    switch (acceptedState) {
      case "accepted":
        return "Accepted";
      case "rejected":
        return "Rejected";
      case "fixed_and_accepted":
        return "Fixed";
      case "updated":
        return "Updated";
      case "submitted":
        return "Submitted";
      case "prediction":
        return "From prediction";
      case "imported":
        return "Imported";
      case "skipped":
        return "Skipped";
      case "draft_created":
        return "Draft";
      case "deleted_review":
        return "Review deleted";
      case "propagated_annotation":
        return "Propagated";
      default:
        return null;
    }
  }, []);
 
  const handleClick = useCallback(
    (e) => {
      if (disabled) return;
 
      onClick(e);
    },
    [onClick, disabled],
  );
 
  return (
    <div className={cn("history-item").mod({ inline, selected, disabled }).toClassName()} onClick={handleClick}>
      <Space spread size="medium" truncated>
        <Space size="small" truncated>
          <Userpic
            className={cn("history-item").elem("userpic").mod({ prediction: isPrediction }).toClassName()}
            user={user}
            showUsernameTooltip
            username={isPrediction ? entity.createdBy : null}
          >
            {isPrediction && <IconSparks style={{ width: 16, height: 16 }} />}
          </Userpic>
          <span className={cn("history-item").elem("name").toClassName()}>
            {isPrediction ? entity.createdBy : userDisplayName(user)}
          </span>
        </Space>
 
        {!infoIsHidden && (
          <Space size="small">
            {extra && <div className={cn("history-item").elem("date").toClassName()}>{extra}</div>}
            {date && (
              <div className={cn("history-item").elem("date").toClassName()}>
                <Tooltip alignment="top-right" title={new Date(date).toLocaleString()}>
                  <span>{humanDateDiff(date)}</span>
                </Tooltip>
              </div>
            )}
          </Space>
        )}
      </Space>
      {(reason || comment) && (
        <Space className={cn("history-item").elem("action").toClassName()} size="small">
          {acceptedState && <HistoryIcon type={acceptedState} />}
          <HistoryComment comment={comment} reason={reason} />
        </Space>
      )}
    </div>
  );
};
 
const HistoryComment: FC<{
  reason: string | null;
  comment: string;
}> = ({ reason, comment }) => {
  const [collapsed, setCollapsed] = useState(false);
  const [collapsible, setCollapsible] = useState(false);
  const commentRef = useRef();
 
  useLayoutEffect(() => {
    if (commentRef.current) {
      const { clientHeight } = commentRef.current;
      // 3 lines of text 22px height each
      const heightExceeded = clientHeight > 66;
 
      setCollapsible(heightExceeded);
      setCollapsed(heightExceeded);
    }
  }, []);
 
  return (
    <div className={cn("history-item").elem("comment").mod({ collapsed }).toClassName()} ref={commentRef as any}>
      <div
        className={cn("history-item").elem("comment-content").toClassName()}
        data-reason={`${reason}${comment ? ": " : ""}`}
      >
        {comment}
      </div>
 
      {collapsible && (
        <div
          className={cn("history-item").elem("collapse-comment").mod({ collapsed }).toClassName()}
          onClick={(e: any) => {
            e.stopPropagation();
            setCollapsed((v) => !v);
          }}
        >
          {collapsed ? "Show more" : "Show less"}
        </div>
      )}
    </div>
  );
};
 
const HistoryIcon: FC<{ type: HistoryItemType }> = ({ type }) => {
  const icon = useMemo(() => {
    switch (type) {
      case "submitted":
        return <IconAnnotationSubmitted style={{ color: "#617ADA" }} />;
      case "updated":
        return <IconAnnotationSubmitted style={{ color: "#617ADA" }} />;
      case "draft_created":
        return <IconDraftCreated style={{ color: "#617ADA" }} />;
      case "accepted":
        return <IconAnnotationAccepted style={{ color: "#2AA000" }} />;
      case "rejected":
        return <IconAnnotationRejected style={{ color: "#dd0000" }} />;
      case "fixed_and_accepted":
        return <IconAnnotationAccepted style={{ color: "#FA8C16" }} />;
      case "prediction":
        return <IconAnnotationPrediction style={{ color: "#944BFF" }} />;
      case "imported":
        return <IconAnnotationImported style={{ color: "#2AA000" }} />;
      case "skipped":
        return <IconAnnotationSkipped style={{ color: "#dd0000" }} />;
      case "deleted_review":
        return <IconAnnotationReviewRemoved style={{ color: "#dd0000" }} />;
      case "propagated_annotation":
        return <IconAnnotationPropagated style={{ color: "#2AA000" }} />;
      default:
        return null;
    }
  }, [type]);
 
  return icon && <div className={cn("history-item").elem("history-icon").toClassName()}>{icon}</div>;
};
 
const HistoryItem = observer(HistoryItemComponent);
 
HistoryItem.displayName = "HistoryItem";
 
export const AnnotationHistory = injector(observer(AnnotationHistoryComponent));
 
AnnotationHistory.displayName = "AnnotationHistory";