Bin
2025-12-17 2b99d77d73ba568beff0a549534017caaad8a6de
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
import { flow, getRoot, types } from "mobx-state-tree";
import { DataStore, DataStoreItem } from "../../mixins/DataStore";
import { DynamicModel } from "../DynamicModel";
 
export const create = (columns) => {
  const AnnotationModelBase = DynamicModel("AnnotationModelBase", columns);
 
  const AnnotationModel = types.compose("AnnotationModel", AnnotationModelBase, DataStoreItem);
 
  return DataStore("AnnotationStore", {
    apiMethod: "annotations",
    listItemType: AnnotationModel,
  }).actions((self) => ({
    loadTask: flow(function* (annotationID) {
      let remoteTask;
      const rootStore = getRoot(self);
 
      if (annotationID !== undefined) {
        remoteTask = yield rootStore.apiCall("task", { taskID: annotationID });
      } else {
        remoteTask = yield rootStore.apiCall("nextTask", {
          projectID: getRoot(self).project.id,
        });
      }
 
      annotationID = annotationID ?? remoteTask.id;
 
      const annotation = self.updateItem(annotationID, {
        ...remoteTask,
        source: JSON.stringify(remoteTask),
      });
 
      self.setSelected(annotation.id);
 
      return annotation;
    }),
 
    unsetTask() {
      self.unset();
    },
  }));
};