aboutsummaryrefslogtreecommitdiff
path: root/src/console/app/hooks.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/console/app/hooks.ts')
-rw-r--r--src/console/app/hooks.ts115
1 files changed, 115 insertions, 0 deletions
diff --git a/src/console/app/hooks.ts b/src/console/app/hooks.ts
new file mode 100644
index 0000000..eefdea3
--- /dev/null
+++ b/src/console/app/hooks.ts
@@ -0,0 +1,115 @@
+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) => {
+ window.top.postMessage(
+ JSON.stringify({
+ type: messages.CONSOLE_ENTER_FIND,
+ text,
+ }),
+ "*"
+ );
+ }, []);
+ return execFind;
+};