Skip to content

Commit

Permalink
reload and init param when change common param
Browse files Browse the repository at this point in the history
  • Loading branch information
tjmtmmnk committed May 15, 2022
1 parent 4228f45 commit 7952faa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/components/popup/OptionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import React, { useEffect } from "react";
import styled from "styled-components";
import { CheckBox } from "./CheckBox";
import { IParameterV2 } from "../../config";
import { PopupDispatch, updateIsUseSameParam, updateParam } from "../../popup";
import {
PopupDispatch,
reloadNotification,
updateIsUseSameParam,
updateParam,
} from "../../popup";

const StyledContainer = styled.div`
width: 320px;
Expand Down Expand Up @@ -62,9 +67,6 @@ export const OptionPage = (props: OptionPageProps) => {
param.considerAuthorLength,
param.isHideCompletely,
]);
useEffect(() => {
updateIsUseSameParam(isUseSameParam);
}, [isUseSameParam]);
return (
<StyledContainer>
<StyledUl>
Expand Down Expand Up @@ -143,11 +145,13 @@ export const OptionPage = (props: OptionPageProps) => {
id={"is-use-same-param"}
label={chrome.i18n.getMessage("isUseSameParam")}
defaultChecked={isUseSameParam}
updateParam={(checked: boolean) => {
updateParam={async (checked: boolean) => {
dispatch({
t: "update-is-use-same-param",
isUseSameParam: checked,
});
await updateIsUseSameParam(checked);
await reloadNotification();
window.alert(chrome.i18n.getMessage("reload"));
window.close();
}}
Expand Down
7 changes: 7 additions & 0 deletions src/content_script.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ chrome.runtime.onMessage.addListener(
}
if (req.type === "UPDATE_PARAM" && req.from === "BACKGROUND") {
if (!req.data || !req.data.param) throw new Error("no param");
console.log(`UPDATE param from background`);
console.log(req.data.param);
state.param = req.data.param;
await sendRequest({
type: "UPDATE_PARAM",
Expand All @@ -88,6 +90,8 @@ chrome.runtime.onMessage.addListener(
});
} else if (req.type === "UPDATE_PARAM" && req.from === "POPUP") {
if (!req.data || !req.data.param) throw new Error("no param");
console.log(`UPDATE param from popup`);
console.log(req.data.param);
state.param = req.data.param;
await sendRequest({
type: "UPDATE_PARAM",
Expand Down Expand Up @@ -134,6 +138,9 @@ chrome.runtime.onMessage.addListener(
to: "BACKGROUND",
data: { isUseSameParam: state.isUseSameParam },
});
} else if (req.type === "RELOAD" && req.from === "POPUP") {
console.log("reload");
await initParam();
}
sendResponse();
}
Expand Down
3 changes: 2 additions & 1 deletion src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export type MessageType =
| "UPDATE_PARAM"
| "GET_PARAM"
| "GET_IS_USE_SAME_PARAM"
| "UPDATE_IS_USE_SAME_PARAM";
| "UPDATE_IS_USE_SAME_PARAM"
| "RELOAD";

export type NetworkNode = "CONTENT_SCRIPT" | "BACKGROUND" | "POPUP";

Expand Down
9 changes: 9 additions & 0 deletions src/popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ export const updateIsUseSameParam = async (isUseSameParam: boolean) => {
});
};

export const reloadNotification = async () => {
console.log("RELOAD from popup");
await sendRequestToContent({
type: "RELOAD",
from: "POPUP",
to: "CONTENT_SCRIPT",
});
};

const Popup = () => {
const [state, dispatch] = useReducer(reducer, initialState);
useEffect(() => {
Expand Down

0 comments on commit 7952faa

Please sign in to comment.