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
import { applySnapshot, flow, getEnv, getRoot, types } from "mobx-state-tree";
import { createRef } from "react";
import Types from "../../core/Types";
 
import Utils from "../../utils";
import { camelizeKeys, snakeizeKeys } from "../../utils/utilities";
import { UserExtended } from "../UserStore";
 
import { Anchor } from "./Anchor";
 
/**
 * A reduced version of the Comment model.
 * It is used only for creating a new comment, storing values in the similar structure
 * and to handle some actions that should be present in both cases (creating and editing).
 * So that some actions have to be overridden in the Comment model in case we want them to work properly with the backend.
 */
export const CommentBase = types
  .model("CommentBase", {
    text: types.string,
    regionRef: types.optional(types.maybeNull(Anchor), null),
    classifications: types.optional(types.frozen({}), null),
  })
  .views((self) => ({
    get commentsStore() {
      try {
        return Types.getParentOfTypeString(self, "CommentStore");
      } catch (e) {
        return null;
      }
    },
    get annotation() {
      /*
       * The `getEnv` is used in case when we use "CommentBase" separately
       * to provide the same functionality of comment (`setRegionLink`)
       * during creating new comment.
       * In this case, the comment is stored in a volatile field "currentComment"
       * of 'CommentStore' and cannot access the MST tree by itself.
       */
      const env = getEnv(self);
      if (env?.annotationStore) {
        return env.annotationStore.selected;
      }
      // otherwise, we use the standard way to get the annotation
      const commentsStore = self.commentsStore;
      return commentsStore?.annotation;
    },
    get isHighlighted() {
      const highlightedRegionKey = self.commentsStore?.highlightedComment?.regionRef?.targetKey;
      const currentRegionKey = self.regionRef?.targetKey;
      return !!highlightedRegionKey && highlightedRegionKey === currentRegionKey;
    },
  }))
  .actions((self) => {
    return {
      setText(text) {
        self.text = text;
      },
      unsetLink() {
        self.regionRef = null;
      },
      setRegionLink(region) {
        self.regionRef = {
          regionId: region.cleanId,
        };
      },
      setClassifications(classifications) {
        self.classifications = classifications;
      },
      setResultLink(result) {
        self.regionRef = {
          regionId: result.area.cleanId,
          controlName: result.from_name.name,
        };
      },
      setHighlighted(value = true) {
        const commentsStore = self.commentsStore;
        if (commentsStore) {
          if (value) {
            commentsStore.setHighlightedComment(self);
          } else if (self.isHighlighted) {
            commentsStore.setHighlightedComment(undefined);
          }
        }
      },
    };
  });
 
/**
 * The main Comment model.
 * Should be fully functional and used for all cases except creating a new comment.
 */
export const Comment = CommentBase.named("Comment")
  .props({
    id: types.identifierNumber,
    text: types.string,
    createdAt: types.optional(types.string, Utils.UDate.currentISODate()),
    updatedAt: types.optional(types.string, Utils.UDate.currentISODate()),
    resolvedAt: types.optional(types.maybeNull(types.string), null),
    createdBy: types.optional(types.maybeNull(types.safeReference(UserExtended)), null),
    isResolved: false,
    isEditMode: types.optional(types.boolean, false),
    isDeleted: types.optional(types.boolean, false),
    isConfirmDelete: types.optional(types.boolean, false),
    isUpdating: types.optional(types.boolean, false),
  })
  .preProcessSnapshot((sn) => {
    return camelizeKeys(sn ?? {});
  })
  .volatile((self) => {
    return {
      _commentRef: createRef(),
    };
  })
  .views((self) => ({
    get sdk() {
      return getEnv(self).events;
    },
    get isPersisted() {
      return self.id > 0 && !self.isUpdating;
    },
    get canResolveAny() {
      const p = getRoot(self);
      return p.interfaces.includes("comments:resolve-any");
    },
  }))
  .actions((self) => {
    const toggleResolve = flow(function* () {
      if (!self.isPersisted || self.isDeleted) return;
 
      self.isResolved = !self.isResolved;
 
      try {
        yield self.sdk.invoke("comments:update", {
          id: self.id,
          is_resolved: self.isResolved,
        });
      } catch (err) {
        self.isResolved = !self.isResolved;
        throw err;
      }
    });
 
    function setEditMode(newMode) {
      self.isEditMode = newMode;
    }
 
    function setDeleted(newMode) {
      self.isDeleted = newMode;
    }
 
    function setConfirmMode(newMode) {
      self.isConfirmDelete = newMode;
    }
 
    const updateComment = flow(function* (comment, classifications = undefined) {
      if (self.isPersisted && !self.isDeleted) {
        const payload = {
          id: self.id,
          text: comment,
        };
 
        if (classifications !== undefined) {
          payload.classifications = classifications;
        }
 
        yield self.sdk.invoke("comments:update", payload);
      }
 
      self.setEditMode(false);
    });
 
    const update = flow(function* (props) {
      if (self.isPersisted && !self.isDeleted && !self.isUpdating) {
        self.isUpdating = true;
        const [result] = yield self.sdk.invoke("comments:update", {
          id: self.id,
          ...snakeizeKeys(props),
        });
        if (result.error) {
          self.isUpdating = false;
          return;
        }
        const data = camelizeKeys(result);
        applySnapshot(self, data);
        self.isUpdating = false;
      }
    });
 
    function setRegionLink(region) {
      const regionRef = {
        regionId: region.cleanId,
      };
      self.update({ regionRef });
    }
 
    function setResultLink(result) {
      const regionRef = {
        regionId: result.area.cleanId,
        controlName: result.from_name.name,
      };
      self.update({ regionRef });
    }
 
    function unsetLink() {
      const regionRef = null;
      self.update({ regionRef });
    }
 
    const deleteComment = flow(function* () {
      if (self.isPersisted && !self.isDeleted && self.isConfirmDelete) {
        yield self.sdk.invoke("comments:delete", {
          id: self.id,
        });
      }
 
      self.setDeleted(true);
      self.setConfirmMode(false);
    });
 
    const scrollIntoView = () => {
      const commentEl = self._commentRef.current;
      if (!commentEl) return;
 
      if (commentEl.scrollIntoViewIfNeeded) {
        commentEl.scrollIntoViewIfNeeded();
      } else {
        commentEl.scrollIntoView({ block: "center", behavior: "smooth" });
      }
    };
 
    return {
      toggleResolve,
      setEditMode,
      setDeleted,
      setConfirmMode,
      updateComment,
      update,
      deleteComment,
      setRegionLink,
      setResultLink,
      unsetLink,
      scrollIntoView,
    };
  });