diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2021-04-12 13:09:09 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-12 13:09:09 +0000 |
commit | d80d0f87b82ba4bd74ed9b2bb7354421a28a11b3 (patch) | |
tree | 691185ad88418d0f44c236d0913cf5c425b29b23 /src/console/index.tsx | |
parent | ea73c900f66107fd4a5b2f3b05080bcf643c94ea (diff) | |
parent | 8a5bba1da639355a25da8c279a9f1cf0a7300a9f (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/index.tsx')
-rw-r--r-- | src/console/index.tsx | 88 |
1 files changed, 56 insertions, 32 deletions
diff --git a/src/console/index.tsx b/src/console/index.tsx index f9313a0..29fa11f 100644 --- a/src/console/index.tsx +++ b/src/console/index.tsx @@ -1,42 +1,66 @@ import * as messages from "../shared/messages"; -import reducers from "./reducers"; -import { createStore, applyMiddleware } from "redux"; -import promise from "redux-promise"; -import * as consoleActions from "./actions/console"; -import { Provider } from "react-redux"; import Console from "./components/Console"; import "./index.css"; import React from "react"; import ReactDOM from "react-dom"; +import ColorSchemeProvider from "./colorscheme/providers"; +import { AppProvider } from "./app/provider"; +import { + useCommandMode, + useFindMode, + useInfoMessage, + useErrorMessage, + useHide, +} from "./app/hooks"; -const store = createStore(reducers, applyMiddleware(promise)); +const RootComponent: React.FC = () => { + const hide = useHide(); + const { show: showCommand } = useCommandMode(); + const { show: showFind } = useFindMode(); + const { show: showError } = useErrorMessage(); + const { show: showInfo } = useInfoMessage(); -window.addEventListener("DOMContentLoaded", () => { - const wrapper = document.getElementById("vimvixen-console"); - ReactDOM.render( - <Provider store={store}> - <Console></Console> - </Provider>, - wrapper - ); -}); + 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); + }, []); -const onMessage = async (message: any): Promise<any> => { - const msg = messages.valueOf(message); - switch (msg.type) { - case messages.CONSOLE_SHOW_COMMAND: - return store.dispatch(await consoleActions.showCommand(msg.command)); - case messages.CONSOLE_SHOW_FIND: - return store.dispatch(consoleActions.showFind()); - case messages.CONSOLE_SHOW_ERROR: - return store.dispatch(consoleActions.showError(msg.text)); - case messages.CONSOLE_SHOW_INFO: - return store.dispatch(consoleActions.showInfo(msg.text)); - case messages.CONSOLE_HIDE: - return store.dispatch(consoleActions.hide()); - } + return <Console />; }; -browser.runtime.onMessage.addListener(onMessage); -const port = browser.runtime.connect(undefined, { name: "vimvixen-console" }); -port.onMessage.addListener(onMessage); +const App: React.FC = () => ( + <AppProvider> + <ColorSchemeProvider> + <RootComponent /> + </ColorSchemeProvider> + </AppProvider> +); + +window.addEventListener("DOMContentLoaded", () => { + const wrapper = document.getElementById("vimvixen-console"); + ReactDOM.render(<App />, wrapper); +}); |