Bin
2025-12-17 d616898802dfe7e5dd648bcf53c6d1f86b6d3642
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
import { types } from "mobx-state-tree";
 
type UserLabel = {
  path: string[];
  origin: "user" | "session";
};
 
type ControlsWithLabels = Record<string, UserLabel[]>;
type ControlsWithPaths = Record<string, string[][]>;
 
// Subset of user generated labels per control tag, currently only for taxonomy
const UserLabels = types
  .model({
    // controls: types.map(types.array(types.array(types.string))),
    controls: types.frozen<ControlsWithLabels>({}),
  })
  .actions((self) => ({
    addLabel(control: string, path: string[]) {
      const label: UserLabel = { path, origin: "session" };
      const labels = [...(self.controls[control] ?? []), label];
 
      self.controls = { ...self.controls, [control]: labels };
    },
 
    deleteLabel(control: string, path: string[]) {
      if (!self.controls[control]) return;
      const labels = self.controls[control].filter(
        (existed) => existed.path.length !== path.length || !existed.path.every((item, index) => item === path[index]),
      );
 
      self.controls = { ...self.controls, [control]: labels };
    },
 
    init(controls: ControlsWithPaths) {
      const adjusted: ControlsWithLabels = {};
 
      for (const control in controls) {
        adjusted[control] = controls[control].map((path) => ({
          origin: "user",
          path,
        }));
      }
      self.controls = adjusted;
    },
  }));
 
export { UserLabels };