mirror of
https://github.com/responsively-org/responsively-app
synced 2024-11-10 06:44:13 +00:00
"Refactor popup.js: Extract functions into useCallback, simplify useEffect callbacks, and remove unnecessary returns."
This commit is contained in:
parent
61fc5377b5
commit
6157de24fa
1 changed files with 26 additions and 34 deletions
|
@ -27,28 +27,23 @@ const isChrome = () => {
|
|||
|
||||
const URLOpenerNonChrome = () => {
|
||||
const [status, setStatus] = useState("loading");
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
const [tab] = await window.browser.tabs.query({
|
||||
currentWindow: true,
|
||||
active: true,
|
||||
});
|
||||
if (!tab || !tab.url) {
|
||||
setStatus("false");
|
||||
return;
|
||||
}
|
||||
openCustomProtocolURI(
|
||||
`responsively://${tab.url}`,
|
||||
() => {
|
||||
setStatus("false");
|
||||
},
|
||||
() => {
|
||||
setStatus("true");
|
||||
}
|
||||
);
|
||||
})();
|
||||
const checkProtocolAndUpdateStatus = useCallback(async () => {
|
||||
const [tab] = await window.browser.tabs.query({ currentWindow: true, active: true });
|
||||
if (!tab || !tab.url) {
|
||||
setStatus("false");
|
||||
return;
|
||||
}
|
||||
openCustomProtocolURI(
|
||||
`responsively://${tab.url}`,
|
||||
() => setStatus("false"),
|
||||
() => setStatus("true")
|
||||
);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
checkProtocolAndUpdateStatus();
|
||||
}, [checkProtocolAndUpdateStatus]);
|
||||
|
||||
if (status === "loading") {
|
||||
return (
|
||||
<div className="popup">
|
||||
|
@ -87,23 +82,20 @@ const URLOpenerNonChrome = () => {
|
|||
};
|
||||
|
||||
const URLOpenerChrome = () => {
|
||||
const updateTabURL = useCallback(async () => {
|
||||
const [tab] = await window.browser.tabs.query({ currentWindow: true, active: true });
|
||||
if (!tab || !tab.url) {
|
||||
return;
|
||||
}
|
||||
window.browser.tabs.update({ url: `responsively://${tab.url}` });
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
const [tab] = await window.browser.tabs.query({
|
||||
currentWindow: true,
|
||||
active: true,
|
||||
});
|
||||
if (!tab || !tab.url) {
|
||||
return;
|
||||
}
|
||||
window.browser.tabs.update({
|
||||
url: `responsively://${tab.url}`,
|
||||
});
|
||||
})();
|
||||
return setTimeout(() => {
|
||||
updateTabURL();
|
||||
setTimeout(() => {
|
||||
window.close();
|
||||
}, 5000);
|
||||
}, []);
|
||||
}, [updateTabURL]);
|
||||
|
||||
return (
|
||||
<div className="popup">
|
||||
|
|
Loading…
Reference in a new issue