"Refactor popup.js: Extract functions into useCallback, simplify useEffect callbacks, and remove unnecessary returns."

This commit is contained in:
aryan262 2024-09-09 12:17:36 +05:30
parent 61fc5377b5
commit 6157de24fa

View file

@ -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">