Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
/**
 * @deprecated this file is not used; AnnotationsCarousel is used instead
 */
 
import { observer } from "mobx-react";
import { useCallback, useEffect, useRef, useState } from "react";
import { Space } from "../../common/Space/Space";
import { IconPlusCircle, IconComment, IconCommentRed, IconSparks } from "@humansignal/icons";
import { Userpic } from "@humansignal/ui";
import { cn } from "../../utils/bem";
import { isDefined, userDisplayName } from "../../utils/utilities";
import { GroundTruth } from "../CurrentEntity/GroundTruth";
import "./Annotations.scss";
import { TimeAgo } from "../../common/TimeAgo/TimeAgo";
import { reaction } from "mobx";
 
export const Annotations = observer(({ store, annotationStore, commentStore }) => {
  const dropdownRef = useRef();
  const [opened, setOpened] = useState(false);
  const enableAnnotations = store.hasInterface("annotations:tabs");
  const enablePredictions = store.hasInterface("predictions:tabs");
  const enableCreateAnnotation = store.hasInterface("annotations:add-new");
  const groundTruthEnabled = store.hasInterface("ground-truth");
 
  const entities = [];
 
  if (enablePredictions) entities.push(...annotationStore.predictions);
 
  if (enableAnnotations) entities.push(...annotationStore.annotations);
 
  const onAnnotationSelect = useCallback(
    (entity, isPrediction) => {
      if (!entity.selected) {
        if (isPrediction) {
          annotationStore.selectPrediction(entity.id);
        } else {
          annotationStore.selectAnnotation(entity.id);
        }
      }
    },
    [annotationStore],
  );
 
  useEffect(() => {
    const handleClick = (e) => {
      const target = e.target;
      const dropdown = dropdownRef.current;
 
      if (target !== dropdown && !dropdown?.contains(target)) {
        setOpened(false);
      }
    };
 
    document.addEventListener("click", handleClick);
 
    const runOnPropertyChange = (value) => {
      let _unresolvedComments = 0;
      let _comments = 0;
 
      value.forEach((obj) => {
        _comments++;
 
        if (!obj) _unresolvedComments++;
      });
 
      commentStore.annotation.setUnresolvedCommentCount(_unresolvedComments);
      commentStore.annotation.setCommentCount(_comments);
    };
 
    const reactionDisposer = reaction(
      () => [...commentStore.comments.map((item) => item.isResolved)],
      runOnPropertyChange,
    );
 
    return () => {
      document.removeEventListener("click", handleClick);
      reactionDisposer();
    };
  }, []);
 
  const renderCommentIcon = (ent) => {
    if (ent.unresolved_comment_count > 0) {
      return <IconCommentRed />;
    }
    if (ent.comment_count > 0) {
      return <IconComment />;
    }
 
    return null;
  };
 
  const renderAnnotation = (ent, i) => {
    return (
      <Annotation
        key={`${ent.pk ?? ent.id}${ent.type}`}
        entity={ent}
        aria-label={`${ent.type} ${i + 1}`}
        selected={ent === annotationStore.selected}
        onClick={(e) => {
          e.preventDefault();
          e.stopPropagation();
          setOpened(false);
          onAnnotationSelect?.(ent, ent.type === "prediction");
        }}
        extra={
          <div className={cn("annotations-list").elem("icons").toClassName()}>
            <div className={cn("annotations-list").elem("icon-column").toClassName()}>{renderCommentIcon(ent)}</div>
            <div className={cn("annotations-list").elem("icon-column").toClassName()}>
              {groundTruthEnabled && <GroundTruth entity={ent} disabled />}
            </div>
          </div>
        }
      />
    );
  };
 
  const renderAnnotationList = (entities) => {
    const _drafts = [];
    const _annotations = [];
 
    entities.forEach((obj, i) => {
      if (obj.pk) {
        _annotations.push(renderAnnotation(obj, i));
      } else {
        _drafts.push(renderAnnotation(obj, i));
      }
    });
 
    return (
      <>
        <div className={cn("annotations-list").elem("draft").toClassName()}>{_drafts}</div>
        <div className={cn("annotations-list").elem("annotation").toClassName()}>{_annotations}</div>
      </>
    );
  };
 
  return enableAnnotations || enablePredictions || enableCreateAnnotation ? (
    <div className={cn("topbar").elem("section").mod({ flat: true }).toClassName()}>
      <div className={cn("annotations-list").toClassName()} ref={dropdownRef}>
        <div className={cn("annotations-list").elem("selected").toClassName()}>
          <Annotation
            aria-label="Annotations List Toggle"
            entity={annotationStore.selected}
            onClick={(e) => {
              e.stopPropagation();
              setOpened(!opened);
            }}
            extra={
              entities.length > 0 ? (
                <Space size="none" style={{ marginRight: -8, marginLeft: 8 }}>
                  <div className={cn("annotations-list").elem("counter").toClassName()}>
                    {entities.indexOf(annotationStore.selected) + 1}/{entities.length}
                  </div>
                  <div className={cn("annotations-list").elem("toggle").mod({ opened }).toClassName()} />
                </Space>
              ) : null
            }
          />
        </div>
 
        {opened && (
          <div className={cn("annotations-list").elem("list").toClassName()}>
            {store.hasInterface("annotations:add-new") && (
              <CreateAnnotation annotationStore={annotationStore} onClick={() => setOpened(false)} />
            )}
 
            {renderAnnotationList(entities)}
          </div>
        )}
      </div>
    </div>
  ) : null;
});
 
const CreateAnnotation = observer(({ annotationStore, onClick }) => {
  const onCreateAnnotation = useCallback(() => {
    const c = annotationStore.createAnnotation();
 
    annotationStore.selectAnnotation(c.id);
    onClick();
  }, [annotationStore, onClick]);
 
  return (
    <div
      className={cn("annotations-list").elem("create").toClassName()}
      aria-label="Create Annotation"
      onClick={onCreateAnnotation}
    >
      <Space size="small">
        <Userpic className={cn("annotations-list").elem("userpic").mod({ prediction: true }).toClassName()}>
          <IconPlusCircle />
        </Userpic>
        Create Annotation
      </Space>
    </div>
  );
});
 
const Annotation = observer(({ entity, selected, onClick, extra, ...props }) => {
  const isPrediction = entity.type === "prediction";
  const username = userDisplayName(
    entity.user ?? {
      firstName: entity.createdBy || "Admin",
    },
  );
 
  return (
    <div {...props} className={cn("annotations-list").elem("entity").mod({ selected }).toClassName()} onClick={onClick}>
      <Space spread>
        <Space size="small">
          <Userpic
            className={cn("annotations-list").elem("userpic").mod({ prediction: isPrediction }).toClassName()}
            showUsernameTooltip
            username={isPrediction ? entity.createdBy : null}
            user={entity.user ?? { username }}
          >
            {isPrediction && <IconSparks color="#944BFF" style={{ width: 18, height: 18 }} />}
          </Userpic>
          <Space direction="vertical" size="none">
            <div className={cn("annotations-list").elem("user").toClassName()}>
              <span className={cn("annotations-list").elem("name").toClassName()}>{username}</span>
              <span className={cn("annotations-list").elem("entity-id").toClassName()}>#{entity.pk ?? entity.id}</span>
            </div>
 
            {isDefined(entity.acceptedState) ? (
              <div className={cn("annotations-list").elem("review").mod({ state: entity.acceptedState }).toClassName()}>
                {entity.acceptedState}
              </div>
            ) : (
              <div className={cn("annotations-list").elem("created").toClassName()}>
                created,{" "}
                <TimeAgo className={cn("annotations-list").elem("date").toClassName()} date={entity.createdDate} />
              </div>
            )}
          </Space>
        </Space>
        {extra}
      </Space>
    </div>
  );
});