From 34ba5a362847976d30eaffbede0218e1c0ec73e0 Mon Sep 17 00:00:00 2001 From: kawamataryo Date: Sat, 23 Sep 2023 06:13:46 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20connection=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/constants.ts | 2 ++ src/popup.tsx | 32 ++++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 9c0009b..5d4c43e 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -33,3 +33,5 @@ export const BSKY_USER_MATCH_TYPE = { DESCRIPTION: "description", NONE: "none", } as const + +export const MAX_RELOAD_COUNT = 1 diff --git a/src/popup.tsx b/src/popup.tsx index 25ef680..3a2fbd2 100644 --- a/src/popup.tsx +++ b/src/popup.tsx @@ -7,6 +7,7 @@ import { sendToContentScript } from "@plasmohq/messaging" import { useStorage } from "@plasmohq/storage/hook" import { + MAX_RELOAD_COUNT, MESSAGE_NAMES, MESSAGE_TYPE, STORAGE_KEYS, @@ -14,11 +15,13 @@ import { } from "~lib/constants" import { debugLog } from "./lib/utils" +import { ReloadButton } from "~lib/components/ReloadBtn" function IndexPopup() { const [isLoading, setIsLoading] = useState(false) const [password, setPassword] = useStorage(STORAGE_KEYS.BSKY_PASSWORD, "") const [userId, setUserId] = useStorage(STORAGE_KEYS.BSKY_USER_ID, "") + const [reloadCount, setReloadCount] = useState(0) const [message, setMessage] = useState { - e.preventDefault() + const reloadActiveTab = async () => { + const [{ id: tabId }] = await chrome.tabs.query({ + active: true, + currentWindow: true + }) + await chrome.tabs.reload(tabId) + } + + const searchBskyUser = async (e?: FormEvent) => { + if(e) { + e.preventDefault() + } const [{ url: currentUrl }] = await chrome.tabs.query({ active: true, @@ -82,10 +95,17 @@ function IndexPopup() { }) } } catch (e) { - setErrorMessage( - "Error: Something went wrong. Please reload the web page and try again." - ) - console.error(e) + if(e.message && e.message.includes("Could not establish connection") && reloadCount < MAX_RELOAD_COUNT) { + setReloadCount((prev) => prev + 1) + await reloadActiveTab() + await new Promise(r => setTimeout(r, 3000)) + await searchBskyUser() + } else { + setErrorMessage( + "Error: Something went wrong. Please reload the web page and try again." + ) + console.error(e) + } } finally { setIsLoading(false) }