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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import { flow, getEnv, getParent, getRoot, getSnapshot, types } from "mobx-state-tree";
import { when } from "mobx";
import uniqBy from "lodash/uniqBy";
import Utils from "../../utils";
import { snakeizeKeys } from "../../utils/utilities";
import { parseCommentClassificationConfig } from "../../utils/commentClassification";
import { Comment } from "./Comment";
import { FF_DEV_3034, isFF } from "../../utils/feature-flags";
 
export const CommentStore = types
  .model("CommentStore", {
    loading: types.optional(types.maybeNull(types.string), "list"),
    comments: types.optional(types.array(Comment), []),
    highlightedComment: types.safeReference(Comment),
  })
  .volatile(() => ({
    addedCommentThisSession: false,
    commentFormSubmit: () => {},
    currentComment: {},
    inputRef: {},
    tooltipMessage: "",
    /**
     * A key that indicates affiliation of the current loaded comment list to the annotation/draft.
     * It's used to check if the current comment list is related to the current opened annotation.
     * It should be removed in case we start to use separate comment stores per annotation.
     */
    commentsKey: null,
  }))
  .views((self) => ({
    get store() {
      return getParent(self);
    },
    get task() {
      return getParent(self).task;
    },
    get annotationStore() {
      return getParent(self).annotationStore;
    },
    get annotation() {
      return self.annotationStore.selected;
    },
    get annotationId() {
      return isNaN(self.annotation?.pk) ? undefined : self.annotation.pk;
    },
    get draftId() {
      if (!self.annotation?.draftId) return null;
      return self.annotation.draftId;
    },
    get currentUser() {
      return getRoot(self).user;
    },
    get commentClassificationsItems() {
      return parseCommentClassificationConfig(getRoot(self).commentClassificationConfig);
    },
    get sdk() {
      return getEnv(self).events;
    },
    get isListLoading() {
      return self.loading === "list";
    },
    get taskId() {
      return self.task?.id;
    },
    get canPersist() {
      if (isFF(FF_DEV_3034)) {
        return self.taskId !== null && self.taskId !== undefined;
      }
      return self.annotationId !== null && self.annotationId !== undefined;
    },
    get isCommentable() {
      return !self.annotation || ["annotation"].includes(self.annotation.type);
    },
    get queuedComments() {
      const queued = self.comments.filter((comment) => !comment.isPersisted);
 
      return queued.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
    },
    get hasUnsaved() {
      return self.queuedComments.length > 0;
    },
    get commentInProgress() {
      if (!self.annotation) return undefined;
      return self.currentComment[self.annotation.id];
    },
    /**
     * A subset of comments that should be displayed on the overlay.
     * For now, it uses only the last comment from the group of ones linked to the same target.
     */
    get overlayComments() {
      const uniqTargetKeys = new Set();
      return self.comments.filter((comment) => {
        const { regionRef } = comment;
 
        if (!regionRef) return false;
        if (uniqTargetKeys.has(regionRef.targetKey)) return false;
        uniqTargetKeys.add(regionRef.targetKey);
        return true;
      });
    },
    get isHighlighting() {
      return !!self.highlightedComment;
    },
 
    /**
     * It gets the key that indicates the target of the comment list
     * we should expect for the current state of stores.
     * Basically, it's based on the current annotation or the current draft.
     * @returns Record<string,string> | null
     */
    get targetCommentsKey() {
      if (self.annotationId) {
        return { annotation: self.annotationId };
      }
      if (self.draftId) {
        return { draft: self.draftId };
      }
      return null;
    },
 
    /**
     * Indicates if the currently loaded list of comments is related to the currently displaying annotation.
     * @returns {boolean}
     */
    get isRelevantList() {
      if (!self.commentsKey || !self.targetCommentsKey) return false;
      if (Object.keys(self.commentsKey).length !== Object.keys(self.targetCommentsKey).length) return false;
      return Object.keys(self.commentsKey).every((key) => {
        return self.commentsKey[key] === self.targetCommentsKey[key];
      });
    },
  }))
  .actions((self) => {
    function serialize({ commentsFilter, queueComments } = { commentsFilter: "all", queueComments: false }) {
      const serializedComments = getSnapshot(commentsFilter === "queued" ? self.queuedComments : self.comments);
 
      return {
        comments: queueComments
          ? serializedComments.map((comment) => ({ id: comment.id > 0 ? comment.id * -1 : comment.id, ...comment }))
          : serializedComments,
      };
    }
 
    function setCurrentComment(comment) {
      self.currentComment = { ...self.currentComment, [self.annotation.id]: comment };
    }
 
    function setHighlightedComment(comment) {
      self.highlightedComment = comment;
    }
 
    function setCommentFormSubmit(submitCallback) {
      self.commentFormSubmit = submitCallback;
    }
 
    function setInputRef(inputRef) {
      self.inputRef = inputRef;
    }
 
    function setLoading(loading = null) {
      self.loading = loading;
    }
 
    function setTooltipMessage(tooltipMessage) {
      self.tooltipMessage = tooltipMessage;
    }
 
    function setAddedCommentThisSession(isAddedCommentThisSession = false) {
      self.addedCommentThisSession = isAddedCommentThisSession;
    }
 
    function replaceId(id, newComment) {
      const comments = self.comments;
 
      const index = comments.findIndex((comment) => comment.id === id);
 
      if (index > -1) {
        const snapshot = getSnapshot(comments[index]);
 
        comments[index] = { ...snapshot, id: newComment.id || snapshot.id };
      }
    }
 
    function removeCommentById(id) {
      const comments = self.comments;
 
      const index = comments.findIndex((comment) => comment.id === id);
 
      if (index > -1) {
        comments.splice(index, 1);
      }
    }
 
    async function persistQueuedComments() {
      const toPersist = self.queuedComments;
 
      if (!self.canPersist || !toPersist.length) return;
 
      if (isFF(FF_DEV_3034) && !self.annotationId && !self.draftId) {
        await self.store.submitDraft(self.annotation);
      }
 
      try {
        self.setLoading("persistQueuedComments");
        for (const comment of toPersist) {
          if (self.annotationId) {
            comment.annotation = self.annotationId;
          } else if (self.draftId) {
            comment.draft = self.draftId;
          } else {
            comment.task = self.taskId;
          }
          const [persistedComment] = await self.sdk.invoke("comments:create", comment);
 
          if (persistedComment) {
            self.replaceId(comment.id, persistedComment);
          }
        }
      } catch (err) {
        console.error(err);
      } finally {
        self.setLoading(null);
      }
    }
 
    const addComment = flow(function* (props) {
      if (self.loading === "addComment") return;
      if (typeof props === "string") {
        props = { text: props };
      }
 
      self.setLoading("addComment");
 
      const now = Date.now() * -1;
 
      const comment = {
        ...snakeizeKeys(props),
        id: now,
        task: self.taskId,
        created_by: self.currentUser.id,
        created_at: Utils.UDate.currentISODate(),
      };
 
      let refetchList = false;
      const { annotation } = self;
 
      if (isFF(FF_DEV_3034) && !self.annotationId && !self.draftId) {
        // rare case: draft is already saving, commit the outstanding draft before adding a comment
        if (annotation.history.hasChanges && !annotation.draftSaved) {
          // commit the pending draft
          annotation.saveDraftImmediately();
 
          // wait for the draft to be saved entirely before adding the comment
          yield when(() => annotation.draftSaved);
        } else {
          // replicate actions from autosave()
          // if versions.draft is empty, the current state (prediction actually) is in result
          annotation.versions.draft = annotation.versions.result;
          annotation.setDraftSelected();
          annotation.setDraftSaving(true);
          yield self.store.submitDraft(self.annotation);
          annotation.onDraftSaved();
        }
        refetchList = true;
      }
 
      if (self.annotationId) {
        comment.annotation = self.annotationId;
      }
      if (self.draftId) {
        comment.draft = self.draftId;
      }
      // @todo setComments?
      self.comments.unshift(comment);
      self.setAddedCommentThisSession(true);
      if (self.canPersist) {
        try {
          const [newComment] = yield self.sdk.invoke("comments:create", comment);
 
          if (newComment) {
            self.replaceId(now, newComment);
            self.setCurrentComment(undefined);
            if (refetchList) self.listComments();
          }
        } catch (err) {
          self.removeCommentById(now);
          throw err;
        } finally {
          self.setLoading(null);
        }
      } else {
        self.setLoading(null);
      }
    });
 
    const addCurrentComment = flow(function* () {
      if (!self.currentComment) return;
 
      yield addComment(self.currentComment);
    });
 
    function setComments(comments, commentsKey = null) {
      if (comments) {
        self.comments.replace(comments);
        self.commentsKey = commentsKey;
      }
    }
 
    function hasCache(key) {
      localStorage.getItem(`commentStore.${key}`) !== null;
    }
 
    function removeCache(key) {
      localStorage.removeItem(`commentStore.${key}`);
    }
 
    function toCache(key, options = { commentsFilter: "all", queueComments: true }) {
      localStorage.setItem(`commentStore.${key}`, JSON.stringify(self.serialize(options)));
    }
 
    function fromCache(key, { merge = true, queueRestored = false } = {}) {
      const value = localStorage.getItem(`commentStore.${key}`);
 
      if (value) {
        const restored = JSON.parse(value);
 
        if (Array.isArray(restored?.comments)) {
          let restoreIds = [];
 
          if (queueRestored) {
            restoreIds = restored.comments.map((comment) => comment.id);
          }
          if (merge) {
            restored.comments = uniqBy([...restored.comments, ...getSnapshot(self.comments)], "id").sort(
              (a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(),
            );
          }
          if (restoreIds.length) {
            restored.comments = restored.comments.map((comment) =>
              restoreIds.includes(comment.id)
                ? {
                    id: comment.id > 0 ? comment.id * -1 : comment.id,
                    ...comment,
                  }
                : comment,
            );
          }
          self.setComments(restored.comments);
        }
      }
    }
 
    async function restoreCommentsFromCache(key) {
      self.fromCache(key, { merge: true, queueRestored: true });
    }
 
    const listComments = flow(function* ({ mounted = { current: true }, suppressClearComments } = {}) {
      if (!suppressClearComments) self.setComments([]);
      if (!self.draftId && !self.annotationId) return;
 
      try {
        if (mounted.current) {
          self.setLoading("list");
        }
 
        const annotation = self.annotationId;
        const commentsKey = self.targetCommentsKey;
        const [comments] = yield self.sdk.invoke("comments:list", {
          annotation,
          draft: self.draftId,
        });
 
        if (mounted.current && annotation === self.annotationId) {
          self.setComments(comments, commentsKey);
        }
      } catch (err) {
        console.error(err);
      } finally {
        if (mounted.current) {
          self.setLoading(null);
        }
      }
    });
 
    return {
      serialize,
      hasCache,
      removeCache,
      toCache,
      fromCache,
      restoreCommentsFromCache,
      setAddedCommentThisSession,
      setCommentFormSubmit,
      setInputRef,
      setLoading,
      setTooltipMessage,
      replaceId,
      removeCommentById,
      persistQueuedComments,
      setCurrentComment,
      addCurrentComment,
      addComment,
      setComments,
      listComments,
      setHighlightedComment,
    };
  });