summaryrefslogtreecommitdiffhomepage
path: root/gui/src/renderer/context.tsx
blob: 3b5fcc14fb7b5e1b7e8de0dbe80a403917b00e49 (plain)
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
import React, { useContext } from 'react';

import App from './app';

export interface IAppContext {
  app: App;
}

export const AppContext = React.createContext<IAppContext | undefined>(undefined);
if (window.env.development) {
  AppContext.displayName = 'AppContext';
}

const missingContextError = new Error(
  'The context value is empty. Make sure to wrap the component in AppContext.Provider.',
);

type PropsWithoutAppContext<Props> = Omit<Props, 'app'>;

export default function withAppContext<Props>(
  BaseComponent: React.ComponentType<PropsWithoutAppContext<Props> & IAppContext>,
) {
  // Exclude the IAppContext from props since those are injected props
  const wrappedComponent = (props: PropsWithoutAppContext<Props>) => {
    const appContext = useAppContext();
    return <BaseComponent app={appContext} {...props} />;
  };

  if (window.env.development) {
    wrappedComponent.displayName =
      'withAppContext(' + (BaseComponent.displayName || BaseComponent.name) + ')';
  }

  return wrappedComponent;
}

export function useAppContext(): App {
  const appContext = useContext(AppContext);
  if (appContext) {
    return appContext.app;
  } else {
    throw missingContextError;
  }
}