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
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
import { observer } from "mobx-react";
import type React from "react";
import { type FC, useCallback, useContext, useMemo, useState } from "react";
import { Tooltip, Userpic } from "@humansignal/ui";
import { IconCheck, IconEllipsis } from "@humansignal/icons";
import { Button } from "@humansignal/ui";
import { Dropdown } from "@humansignal/ui";
import { Menu } from "../../../common/Menu/Menu";
import { Space } from "../../../common/Space/Space";
import { cn } from "../../../utils/bem";
import { humanDateDiff, userDisplayName } from "../../../utils/utilities";
import { CommentFormBase } from "../CommentFormBase";
import { CommentsContext } from "./CommentsList";
import { NewTaxonomy as Taxonomy, type TaxonomyPath } from "../../../components/NewTaxonomy/NewTaxonomy";
import { taxonomyPathsToSelectedItems, COMMENT_TAXONOMY_OPTIONS } from "../../../utils/commentClassification";
 
import "./CommentItem.scss";
import { LinkState } from "./LinkState";
 
interface CommentItemProps {
  comment: {
    isEditMode: boolean;
    isConfirmDelete: boolean;
    createdAt: string;
    updatedAt: string;
    isPersisted: boolean;
    isDeleted: boolean;
    createdBy: any;
    text: string;
    regionRef: any;
    classifications: any;
    isResolved: boolean;
    updateComment: (comment: string, classifications?: any) => void;
    deleteComment: () => void;
    setConfirmMode: (confirmMode: boolean) => void;
    setClassifications: (classifications: any) => void;
    setEditMode: (isGoingIntoEditMode: boolean) => void;
    toggleResolve: () => void;
    canResolveAny: boolean;
    unsetLink: () => {};
    isHighlighted: boolean;
    setHighlighted: (value: boolean) => {};
    _commentRef: React.Ref<HTMLElement>;
  };
  listComments: ({
    suppressClearComments,
  }: {
    suppressClearComments: boolean;
  }) => void;
  classificationsItems: any;
}
 
export const CommentItem: FC<CommentItemProps> = observer(
  ({ comment, listComments, classificationsItems }: CommentItemProps) => {
    const {
      classifications,
      updatedAt,
      isEditMode,
      isConfirmDelete,
      createdAt,
      isPersisted,
      isDeleted,
      createdBy,
      text: initialText,
      regionRef,
      isResolved: resolved,
      updateComment,
      deleteComment,
      setConfirmMode,
      setClassifications,
      setEditMode,
      toggleResolve,
      canResolveAny,
      isHighlighted,
      setHighlighted,
      _commentRef,
    } = comment;
    const { startLinkingMode: _startLinkingMode, currentComment, globalLinking } = useContext(CommentsContext);
    const currentUser = window.APP_SETTINGS?.user;
    const isCreator = currentUser?.id === createdBy.id;
    const infoIsHidden = comment.commentsStore?.store?.hasInterface("annotations:hide-info");
    const hiddenUser = infoIsHidden ? { email: isCreator ? "Me" : "User" } : null;
    const [text, setText] = useState(initialText);
 
    const [linkingComment, setLinkingComment] = useState();
    const region = regionRef?.region;
    const result = regionRef?.result;
    const linking = !!(linkingComment && currentComment === linkingComment && globalLinking);
    const hasLinkState = linking || region;
 
    const startLinkingMode = useCallback(
      (comment: any) => {
        setLinkingComment(comment);
        _startLinkingMode(comment);
      },
      [_startLinkingMode],
    );
 
    const toggleLink = useCallback(() => {
      if (regionRef?.region) {
        comment.unsetLink();
      } else {
        startLinkingMode(comment);
      }
    }, [comment, startLinkingMode, regionRef?.region]);
 
    const taxonomyOnChange = useCallback(
      async (_: Node, values: TaxonomyPath[]) => {
        const newClassifications =
          values.length > 0
            ? {
                default: {
                  type: "taxonomy",
                  values,
                },
              }
            : null;
        setClassifications(newClassifications);
      },
      [setClassifications],
    );
 
    const taxonomySelectedItems = useMemo(
      () => taxonomyPathsToSelectedItems(classifications?.default?.values),
      [classifications],
    );
 
    const commentFormBaseOnSubmit = useCallback(
      async (value: any) => {
        await updateComment(value, classifications);
        setText(value);
        await listComments({ suppressClearComments: true });
      },
      [updateComment, listComments, classifications],
    );
 
    if (isDeleted) return null;
 
    const TimeTracker = () => {
      const editedTimeAchondritic = new Date(updatedAt);
      const createdTimeAchondritic = new Date(createdAt);
 
      editedTimeAchondritic.setMilliseconds(0);
      createdTimeAchondritic.setMilliseconds(0);
 
      const isEdited = editedTimeAchondritic > createdTimeAchondritic;
      const time = isEdited ? updatedAt : createdAt;
 
      if (isPersisted && time)
        return (
          <div className={cn("comment-item").elem("date").toClassName()}>
            <Tooltip alignment="top-right" title={new Date(time).toLocaleString()}>
              <span>{`${isEdited ? "updated" : ""} ${humanDateDiff(time)}`}</span>
            </Tooltip>
          </div>
        );
      return null;
    };
 
    return (
      <div
        className={cn("comment-item").mod({ resolved, highlighted: isHighlighted }).toClassName()}
        onMouseEnter={() => {
          setHighlighted(true);
        }}
        onMouseLeave={() => {
          setHighlighted(false);
        }}
        ref={_commentRef as any}
      >
        <Space spread size="medium" truncated>
          <Space size="small" truncated>
            <Userpic
              className={cn("comment-item").elem("userpic").toClassName()}
              user={hiddenUser ?? createdBy}
              showUsernameTooltip
              username={createdBy}
            />
            <span className={cn("comment-item").elem("name").toClassName()}>
              {userDisplayName(hiddenUser ?? createdBy)}
            </span>
          </Space>
 
          <Space size="small">
            <IconCheck className={cn("comment-item").elem("resolved").toClassName()} />
            <div className={cn("comment-item").elem("saving").mod({ hide: isPersisted }).toClassName()}>
              <div className={cn("comment-item").elem("dot").toClassName()} />
            </div>
            {!infoIsHidden && <TimeTracker />}
          </Space>
        </Space>
 
        <div className={cn("comment-item").elem("content").toClassName()}>
          <div className={cn("comment-item").elem("text").toClassName()}>
            {isEditMode ? (
              <>
                <CommentFormBase value={text} onSubmit={commentFormBaseOnSubmit} classifications={classifications} />
                {classificationsItems.length > 0 && (
                  <div className={cn("comment-item").elem("classifications-row").toClassName()}>
                    <Taxonomy
                      selected={taxonomySelectedItems}
                      items={classificationsItems}
                      onChange={taxonomyOnChange}
                      options={COMMENT_TAXONOMY_OPTIONS}
                      defaultSearch={false}
                    />
                  </div>
                )}
              </>
            ) : isConfirmDelete ? (
              <div className={cn("comment-item").elem("confirmForm").toClassName()}>
                <div className={cn("comment-item").elem("question").toClassName()}>Are you sure?</div>
                <div className={cn("comment-item").elem("controls").toClassName()}>
                  <Button
                    onClick={() => deleteComment()}
                    size="small"
                    look="danger"
                    autoFocus
                    aria-label="Delete comment"
                  >
                    Yes
                  </Button>
                  <Button onClick={() => setConfirmMode(false)} size="small" aria-label="Cancel delete">
                    No
                  </Button>
                </div>
              </div>
            ) : (
              <>
                {classifications?.default?.values?.length > 0 && (
                  <ul className={cn("comment-item").elem("classifications").toClassName()}>
                    {classifications?.default?.values?.map((valueArray: string[], index: number) => (
                      <li key={index}>{valueArray.join("/")}</li>
                    ))}
                  </ul>
                )}
                {text}
                {hasLinkState && (
                  <div className={cn("comment-item").elem("linkState").toClassName()}>
                    <LinkState linking={linking} region={region} result={result} interactive />
                  </div>
                )}
              </>
            )}
          </div>
 
          <div
            className={cn("comment-item").elem("actions").toClassName()}
            onClick={(e: any) => {
              e.stopPropagation();
              e.preventDefault();
            }}
          >
            {isPersisted && (isCreator || canResolveAny) && (
              <Dropdown.Trigger
                content={
                  <Menu size="auto">
                    <Menu.Item onClick={toggleResolve}>{resolved ? "Unresolve" : "Resolve"}</Menu.Item>
                    {isCreator && (
                      <>
                        <Menu.Item
                          onClick={() => {
                            const isGoingIntoEditMode = !isEditMode;
 
                            setEditMode(isGoingIntoEditMode);
                            if (!isGoingIntoEditMode) {
                              setText(initialText);
                            }
                          }}
                        >
                          {isEditMode ? "Cancel edit" : "Edit"}
                        </Menu.Item>
                        <Menu.Item onClick={toggleLink}>{regionRef?.region ? "Unlink" : "Link to..."}</Menu.Item>
                        {!isConfirmDelete && (
                          <Menu.Item
                            onClick={() => {
                              setConfirmMode(true);
                            }}
                          >
                            Delete
                          </Menu.Item>
                        )}
                      </>
                    )}
                  </Menu>
                }
              >
                <Button size="small" look="string" icon={<IconEllipsis />} aria-label="Comment options" />
              </Dropdown.Trigger>
            )}
          </div>
        </div>
      </div>
    );
  },
);