aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/graph/GraphStore.ts
blob: b59bfb7d574584da11c303ac48c3146bffa7e565 (plain) (blame)
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
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import { makeAutoObservable, observable } from 'mobx';

import type { SemanticsSuccessResult } from '../xtext/xtextServiceResults';

export type Visibility = 'all' | 'must' | 'none';

export default class GraphStore {
  semantics: SemanticsSuccessResult = {
    nodes: [],
    relations: [],
    partialInterpretation: {},
  };

  visibility = new Map<string, Visibility>();

  constructor() {
    makeAutoObservable(this, {
      semantics: observable.ref,
    });
  }

  getVisiblity(relation: string): Visibility {
    return this.visibility.get(relation) ?? 'none';
  }

  setSemantics(semantics: SemanticsSuccessResult) {
    this.semantics = semantics;
    this.visibility.clear();
    const names = new Set<string>();
    this.semantics.relations.forEach(({ name, detail }) => {
      names.add(name);
      if (!this.visibility.has(name)) {
        const newVisibility = detail.type === 'builtin' ? 'none' : 'all';
        this.visibility.set(name, newVisibility);
      }
    });
    const oldNames = new Set<string>();
    this.visibility.forEach((_, key) => oldNames.add(key));
    oldNames.forEach((key) => {
      if (!names.has(key)) {
        this.visibility.delete(key);
      }
    });
  }
}