aboutsummaryrefslogtreecommitdiff
path: root/src/console/App.tsx
blob: 6b4541839e9ef5bf2f95ef4306bf8f4792970b47 (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
import * as messages from "../shared/messages";
import Console from "./components/Console";
import React from "react";
import {
  useCommandMode,
  useFindMode,
  useInfoMessage,
  useErrorMessage,
  useHide,
} from "./app/hooks";

const App: React.FC = () => {
  const hide = useHide();
  const { show: showCommand } = useCommandMode();
  const { show: showFind } = useFindMode();
  const { show: showError } = useErrorMessage();
  const { show: showInfo } = useInfoMessage();

  React.useEffect(() => {
    const onMessage = async (message: any): Promise<any> => {
      const msg = messages.valueOf(message);
      switch (msg.type) {
        case messages.CONSOLE_SHOW_COMMAND:
          showCommand(msg.command);
          break;
        case messages.CONSOLE_SHOW_FIND:
          showFind();
          break;
        case messages.CONSOLE_SHOW_ERROR:
          showError(msg.text);
          break;
        case messages.CONSOLE_SHOW_INFO:
          showInfo(msg.text);
          break;
        case messages.CONSOLE_HIDE:
          hide();
          break;
      }
    };

    browser.runtime.onMessage.addListener(onMessage);
    const port = browser.runtime.connect(undefined, {
      name: "vimvixen-console",
    });
    port.onMessage.addListener(onMessage);
  }, []);

  return <Console />;
};

export default App;