blob: 71f2a27f9747f2288b4ccfbd0539076361c66381 (
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
|
import * as messages from "../shared/messages";
import reducers, { defaultState } from "./reducers/console";
import * as consoleActions from "./actions/console";
import Console from "./components/Console";
import AppContext from "./components/AppContext";
import "./index.css";
import React from "react";
import ReactDOM from "react-dom";
import ColorSchemeProvider from "./colorscheme/providers";
const wrapAsync = <T extends unknown>(
dispatch: React.Dispatch<T>
): React.Dispatch<T | Promise<T>> => {
return (action: T | Promise<T>) => {
if (action instanceof Promise) {
action.then((a) => dispatch(a)).catch(console.error);
} else {
dispatch(action);
}
};
};
const RootComponent: React.FC = () => {
const [state, dispatch] = React.useReducer(reducers, defaultState);
React.useEffect(() => {
const onMessage = async (message: any): Promise<any> => {
const msg = messages.valueOf(message);
switch (msg.type) {
case messages.CONSOLE_SHOW_COMMAND:
return dispatch(await consoleActions.showCommand(msg.command));
case messages.CONSOLE_SHOW_FIND:
return dispatch(consoleActions.showFind());
case messages.CONSOLE_SHOW_ERROR:
return dispatch(consoleActions.showError(msg.text));
case messages.CONSOLE_SHOW_INFO:
return dispatch(consoleActions.showInfo(msg.text));
case messages.CONSOLE_HIDE:
return dispatch(consoleActions.hide());
}
};
browser.runtime.onMessage.addListener(onMessage);
const port = browser.runtime.connect(undefined, {
name: "vimvixen-console",
});
port.onMessage.addListener(onMessage);
}, []);
return (
<AppContext.Provider value={{ state, dispatch: wrapAsync(dispatch) }}>
<ColorSchemeProvider>
<Console />
</ColorSchemeProvider>
</AppContext.Provider>
);
};
window.addEventListener("DOMContentLoaded", () => {
const wrapper = document.getElementById("vimvixen-console");
ReactDOM.render(<RootComponent />, wrapper);
});
|