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
import { getRoot, getSnapshot, types } from "mobx-state-tree";
import {
  IconCommentCheck,
  IconCommentRed,
  IconAnnotation,
  IconBanSquare,
  IconSparkSquare,
  IconStarSquare,
  IconThumbsDown,
  IconThumbsUp,
} from "@humansignal/icons";
import * as CellViews from "../../components/CellViews";
import { normalizeCellAlias } from "../../components/CellViews";
import { all } from "../../utils/utils";
import { StringOrNumberID } from "../types";
 
export const ViewColumnType = types.enumeration([
  "String",
  "Number",
  "Boolean",
  "Datetime",
  "List",
  "Image",
  "Audio",
  "AudioPlus",
  "Video",
  "Text",
  "HyperText",
  "TimeSeries",
  "Unknown",
  "AgreementSelected",
  "TaskState",
]);
 
const typeShortMap = {
  String: "str",
  Number: "num",
  Boolean: "bool",
  Datetime: "date",
  Image: "img",
  Audio: "aud",
  AudioPlus: "aud",
  Video: "vid",
  Text: "txt",
  HyperText: "html",
  TimeSeries: "ts",
};
 
export const ViewColumnTypeShort = (type) => typeShortMap[type] || "str";
 
const typeNameMap = {
  String: "String",
  Number: "Number",
  Boolean: "Boolean",
  Datetime: "Date Time",
  Image: "Image",
  Audio: "Audio",
  AudioPlus: "Audio",
  Video: "Video",
  Text: "Text",
  HyperText: "Hyper Text",
  TimeSeries: "Time Series",
};
 
export const ViewColumnTypeName = (type) => typeNameMap[type] || "String";
 
export const TabColumn = types
  .model("ViewColumn", {
    id: StringOrNumberID,
    title: types.string,
    alias: types.string,
    type: types.optional(ViewColumnType, "String"),
    displayType: types.optional(types.maybeNull(ViewColumnType), null),
    defaultHidden: types.optional(types.boolean, false),
    parent: types.maybeNull(types.late(() => types.reference(TabColumn))),
    children: types.maybeNull(types.array(types.late(() => types.reference(TabColumn)))),
    target: types.enumeration(["tasks", "annotations"]),
    orderable: types.optional(types.boolean, true),
    help: types.maybeNull(types.string),
    // Column alias whose filter should be joined automatically when a filter is created for this column
    child_filter: types.maybeNull(types.string),
    disabled: types.optional(types.boolean, false),
  })
  .views((self) => ({
    get hidden() {
      if (self.children) {
        return all(self.children, (c) => c.hidden);
      }
      return self.disabled || (self.parentView?.hiddenColumns.hasColumn(self) ?? (self.parent.hidden || false));
    },
 
    get parentView() {
      return getRoot(self).viewsStore.selected;
    },
 
    get key() {
      return self.id;
    },
 
    get accessor() {
      return (data) => {
        if (!self.parent) {
          const value = data[self.alias];
 
          return typeof value === "object" ? null : value;
        }
 
        try {
          const value = data?.[self.parent.alias]?.[self.alias];
 
          return value ?? null;
        } catch {
          console.log("Error generating accessor", {
            id: self.alias,
            parent: self.parent?.alias,
            data,
            snapshot: getSnapshot(self),
          });
          return data[self.alias];
        }
      };
    },
 
    get renderer() {
      return ({ value }) => {
        return value?.toString() ?? null;
      };
    },
 
    get canOrder() {
      return self.orderable && !self.children && !getRoot(self).isLabeling;
    },
 
    get order() {
      return self.parentView.currentOrder[self.id];
    },
 
    get currentType() {
      const displayType = self.parentView?.columnsDisplayType?.get(self.id);
 
      return displayType ?? self.type;
    },
 
    get asField() {
      const result = [];
 
      if (self.children) {
        const childColumns = [].concat(...self.children.map((subColumn) => subColumn.asField));
 
        result.push(...childColumns);
      } else if (!self.isAnnotationResultsFilterColumn) {
        result.push({
          ...self,
          id: self.key,
          accessor: self.accessor,
          hidden: self.hidden,
          original: self,
          currentType: self.currentType,
          width: self.width,
        });
      }
 
      return result;
    },
 
    get icon() {
      switch (self.alias) {
        case "total_annotations":
          return <IconAnnotation width="20" height="20" style={{ color: "#617ADA" }} />;
        case "cancelled_annotations":
          return <IconBanSquare width="20" height="20" style={{ color: "#DD0000" }} />;
        case "total_predictions":
          return <IconSparkSquare width="20" height="20" style={{ color: "#944BFF" }} />;
        case "reviews_accepted":
          return <IconThumbsUp width="20" height="20" style={{ color: "#2AA000" }} />;
        case "reviews_rejected":
          return <IconThumbsDown width="20" height="20" style={{ color: "#DD0000" }} />;
        case "ground_truth":
          return <IconStarSquare width="20" height="20" style={{ color: "#FFB700" }} />;
        case "comment_count":
          return <IconCommentCheck width="20" height="20" style={{ color: "#FFB700" }} />;
        case "unresolved_comment_count":
          return <IconCommentRed width="20" height="20" style={{ color: "#FFB700" }} />;
        default:
          return null;
      }
    },
 
    get readableType() {
      return ViewColumnTypeShort(self.currentType);
    },
 
    get width() {
      return self.parentView?.columnsWidth?.get(self.id) ?? null;
    },
 
    get filterable() {
      const cellView = CellViews[self.type] ?? CellViews[normalizeCellAlias(self.alias)];
 
      return cellView?.filterable !== false;
    },
 
    get isAnnotationResultsFilterColumn() {
      // these columns are not visible in the column selector, but are used for filtering
      const hidden_column_ids = ["annotations_results_json", "predictions_results_json"];
      return hidden_column_ids.some((id) => self.id.includes(`${id}.`) || self.id.endsWith(`:${id}`));
    },
  }))
  .actions((self) => ({
    toggleVisibility() {
      self.parentView.toggleColumn(self);
    },
 
    setType(type) {
      self.parentView.setColumnDisplayType(self.id, type);
      self.parentView.save();
    },
 
    setWidth(width) {
      const view = self.parentView;
 
      view.setColumnWidth(self.id, width ?? null);
      view.save();
    },
 
    resetWidth() {
      self.parentView.setColumnWidth(self.id, null);
      self.parentView.save();
    },
  }));