aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/RootStore.tsx
blob: a7406d7be30ed2c757dda79cf9a95a9464e8750b (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
import React, { createContext, useContext } from 'react';

import EditorStore from './editor/EditorStore';
import ThemeStore from './theme/ThemeStore';

export default class RootStore {
  editorStore;

  themeStore;

  constructor(initialValue: string) {
    this.themeStore = new ThemeStore();
    this.editorStore = new EditorStore(initialValue, this.themeStore);
  }
}

const StoreContext = createContext<RootStore | undefined>(undefined);

export interface RootStoreProviderProps {
  children: JSX.Element;

  rootStore: RootStore;
}

export function RootStoreProvider({
  children,
  rootStore,
}: RootStoreProviderProps): JSX.Element {
  return (
    <StoreContext.Provider value={rootStore}>{children}</StoreContext.Provider>
  );
}

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