aboutsummaryrefslogtreecommitdiffstats
path: root/language-web/src/main/js/RootStore.jsx
blob: 1ee2823d45e08533073180c5c14e47c30bc942bb (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
import { makeAutoObservable } from 'mobx';
import React, { createContext, useContext } from 'react';

import EditorStore from './editor/EditorStore';

export default class RootStore {
  editorStore;

  constructor() {
    this.editorStore = new EditorStore();
    makeAutoObservable(this);
  }
}

const StoreContext = createContext(undefined);

export const RootStoreProvider = ({ children, rootStore }) => (
  <StoreContext.Provider value={rootStore}>
    {children}
  </StoreContext.Provider>
);

/** @returns {RootStore} */
export const useRootStore = () => {
  const rootStore = useContext(StoreContext);
  if (!rootStore) {
    throw new Error('useRootStore must be used within RootStoreProvider');
  }
  return rootStore;
};