aboutsummaryrefslogtreecommitdiff
path: root/src/console/app/hooks.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2021-04-12 13:09:09 +0000
committerGitHub <noreply@github.com>2021-04-12 13:09:09 +0000
commitd80d0f87b82ba4bd74ed9b2bb7354421a28a11b3 (patch)
tree691185ad88418d0f44c236d0913cf5c425b29b23 /src/console/app/hooks.ts
parentea73c900f66107fd4a5b2f3b05080bcf643c94ea (diff)
parent8a5bba1da639355a25da8c279a9f1cf0a7300a9f (diff)
Merge pull request #1098 from ueokande/replace-redux-with-react-hooks
Refactor state management with React Hooks on Console
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;
+};