aboutsummaryrefslogtreecommitdiff
path: root/src/console/app/hooks.ts
blob: 00ac05cfd464604c3b2ba333d89350a46bd959b0 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React from "react";
import * as actions from "./actions";
import { AppDispatchContext, AppStateContext } from "./contexts";
import * as messages from "../../shared/messages";

export const useHide = () => {
  const dispatch = React.useContext(AppDispatchContext);
  const hide = React.useCallback(() => {
    window.top.postMessage(
      JSON.stringify({
        type: messages.CONSOLE_UNFOCUS,
      }),
      "*"
    );
    dispatch(actions.hide());
  }, [dispatch]);

  return hide;
};

export const useCommandMode = () => {
  const state = React.useContext(AppStateContext);
  const dispatch = React.useContext(AppDispatchContext);

  const show = React.useCallback(
    (initialInputValue: string) => {
      dispatch(actions.showCommand(initialInputValue));
    },
    [dispatch]
  );

  return {
    visible: state.mode === "command",
    initialInputValue: state.consoleText,
    show,
  };
};

export const useFindMode = () => {
  const state = React.useContext(AppStateContext);
  const dispatch = React.useContext(AppDispatchContext);

  const show = React.useCallback(() => {
    dispatch(actions.showFind());
  }, [dispatch]);

  return {
    visible: state.mode === "find",
    show,
  };
};

export const useInfoMessage = () => {
  const state = React.useContext(AppStateContext);
  const dispatch = React.useContext(AppDispatchContext);

  const show = React.useCallback(
    (message: string) => {
      dispatch(actions.showInfo(message));
    },
    [dispatch]
  );

  return {
    visible: state.mode === "info",
    message: state.mode === "info" ? state.messageText : "",
    show,
  };
};

export const useErrorMessage = () => {
  const state = React.useContext(AppStateContext);
  const dispatch = React.useContext(AppDispatchContext);

  const show = React.useCallback(
    (message: string) => {
      dispatch(actions.showError(message));
    },
    [dispatch]
  );

  return {
    visible: state.mode === "error",
    message: state.mode === "error" ? state.messageText : "",
    show,
  };
};

export const getInitialInputValue = () => {
  const state = React.useContext(AppStateContext);
  return state.consoleText;
};

export const useExecCommand = () => {
  const execCommand = React.useCallback((text: string) => {
    browser.runtime.sendMessage({
      type: messages.CONSOLE_ENTER_COMMAND,
      text,
    });
  }, []);
  return execCommand;
};

export const useExecFind = () => {
  const execFind = React.useCallback((text?: string) => {
    browser.runtime.sendMessage({
      type: messages.CONSOLE_ENTER_FIND,
      keyword: text,
    });
  }, []);
  return execFind;
};