chenzhaoyang
2025-12-17 d3e5a4b7658ece4f845bbc0c4f95acf3fbdf8a61
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
import ReactDOM from "react-dom";
import { App } from "../components/App/App";
import { AppStore } from "../stores/AppStore";
import * as DataStores from "../stores/DataStores";
import { registerModel } from "../stores/DynamicModel";
 
const createDynamicModels = (columns) => {
  const grouppedColumns = columns.reduce((res, column) => {
    res.set(column.target, res.get(column.target) ?? []);
    res.get(column.target).push(column);
    return res;
  }, new Map());
 
  grouppedColumns.forEach((columns, target) => {
    const dataStore = DataStores[target].create?.(columns);
 
    if (dataStore) registerModel(`${target}Store`, dataStore);
  });
 
  if (columns.length === 0) {
    registerModel("tasksStore", DataStores.tasks?.create());
  }
 
  /** temporary solution until we'll have annotations */
  registerModel("annotationsStore", DataStores.annotations?.create());
};
 
/**
 * Create DM React app
 * @param {HTMLElement} rootNode
 * @param {import("./dm-sdk").DataManager} datamanager
 * @returns {Promise<AppStore>}
 */
export const createApp = async (rootNode, datamanager) => {
  const isLabelStream = datamanager.mode === "labelstream";
 
  const response = await datamanager.api.columns();
 
  if (!response || response.error) {
    const message = `
      ${response?.error ?? ""}
      LS API not available; check \`API_GATEWAY\` and \`LS_ACCESS_TOKEN\` env vars;
      also check \`data-project-id\` in \`public/index.html\`
    `;
 
    throw new Error(message);
  }
 
  const columns = response.columns ?? (Array.isArray(response) ? response : []);
 
  createDynamicModels(columns);
 
  const appStore = AppStore.create({
    viewsStore: {
      views: [],
      columnsRaw: columns,
    },
    project: datamanager.project ?? {},
    mode: datamanager.mode,
    showPreviews: datamanager.showPreviews,
    interfaces: Object.fromEntries(datamanager.interfaces),
    toolbar: datamanager.toolbar,
    availableActions: Array.from(datamanager.actions.values()).map(({ action }) => action),
  });
 
  appStore._sdk = datamanager;
 
  appStore.fetchData({ isLabelStream });
 
  window.DM = appStore;
 
  ReactDOM.render(<App app={appStore} />, rootNode);
 
  return appStore;
};