diff --git a/src/popup/main.tsx b/src/popup/main.tsx index a56e961..596ffd1 100644 --- a/src/popup/main.tsx +++ b/src/popup/main.tsx @@ -14,48 +14,6 @@ import { } from "src/constants"; import { MessageType } from "src/types"; -const handleTextChange = - (setter: React.Dispatch>) => - (e: React.ChangeEvent) => { - setter(e.target.value); - }; - -interface ApiTokenInputProps { - token: string; - setToken: (value: string) => void; - showError: boolean; - isSelfHosted: boolean; -} - -function ApiTokenInput({ - token, - setToken, - showError, - isSelfHosted, -}: ApiTokenInputProps) { - const errorMessage = isSelfHosted - ? "Invalid API token or self-hosted Codecov URL. Make sure your token and self-hosted URL are correct." - : "Invalid API token. Make sure your token is correct."; - - return ( -
- - - {showError ? ( -

{errorMessage}

- ) : null} -
- ); -} - interface ToggleUrlInputProps { showInputLabel: string; showInput: boolean; @@ -77,7 +35,6 @@ function ToggleUrlInput({ url, setUrl, errorMessage, - showError, }: ToggleUrlInputProps) { const handleToggle = () => { setShowInput(!showInput); @@ -111,18 +68,18 @@ function ToggleUrlInput({ /> {showInput ? ( -
+
setUrl(e.target.value)} className={clsx({ - "border-red-500": showError, + "border-red-500": errorMessage, })} /> - {showError && errorMessage ? ( + {errorMessage ? (

{errorMessage}

) : null}
@@ -133,37 +90,21 @@ function ToggleUrlInput({ } function Popup() { - // persisted state const [codecovApiToken, setCodecovApiToken] = useState(""); const [useSelfHosted, setUseSelfHosted] = useState(false); const [codecovUrl, setCodecovUrl] = useState(""); const [useGithubEnterprise, setUseGithubEnterprise] = useState(false); const [githubUrl, setGitHubUrl] = useState(""); - // ephemeral state - const [isUrlPermissionGranted, setIsUrlPermissionGranted] = useState(false); - const [isUrlError, setIsUrlError] = useState(false); - const [isTokenError, setIsTokenError] = useState(false); - const [isTabError, setIsTabError] = useState(false); - const [isUnregisterTabError, setIsUnregisterTabError] = useState(false); + const [apiTokenError, setApiTokenError] = useState(""); + const [codecovUrlError, setCodecovUrlError] = useState(""); + const [githubUrlError, setGithubUrlError] = useState(""); const [hasChanges, setHasChanges] = useState(false); - const [isLoadingStorage, setIsLoadingStorage] = useState(true); - const [isDone, setIsDone] = useState(false); - - const isFormInvalid = - useSelfHosted && - !(codecovUrl && codecovApiToken && githubUrl && isUrlPermissionGranted); - const isError = - isUrlError || isTokenError || isTabError || isUnregisterTabError; useEffect(() => { loadPersistedState(); }, []); - useEffect(() => { - resetEphemeralState(); - }, [codecovUrl, githubUrl, codecovApiToken]); - const withDetectChanges = (setter: (value: string) => void) => { return (value: string) => { setHasChanges(true); @@ -196,38 +137,7 @@ function Popup() { }); }; - const resetEphemeralState = () => { - setIsUrlPermissionGranted(false); - setIsUrlError(false); - setIsTokenError(false); - setIsTabError(false); - setIsUnregisterTabError(false); - setIsDone(false); - }; - - const registerContentScripts = async (url: string) => { - const payload = { - url, - }; - - return browser.runtime.sendMessage({ - type: MessageType.REGISTER_CONTENT_SCRIPTS, - payload, - }); - }; - - const unregisterContentScripts = async () => { - const payload = {}; - - return browser.runtime.sendMessage({ - type: MessageType.UNREGISTER_CONTENT_SCRIPTS, - payload, - }); - }; - const handleSave = async () => { - console.log("handleSave"); - if (useSelfHosted) { console.log("useSelfHosted request host permission"); const urlMatch = urlJoin(codecovUrl, "/*"); @@ -236,6 +146,13 @@ function Popup() { }); } + if (codecovApiToken && !isValidTokenFormat(codecovApiToken)) { + setApiTokenError( + "Invalid token format. Token should be a 32 hex digit UUID. See this input's placeholder value for en example." + ); + return; + } + try { const payload = { baseUrl: useSelfHosted ? codecovUrl : codecovCloudApiUrl, @@ -254,29 +171,38 @@ function Popup() { console.log("isAuthOk", isAuthOk); if (!isAuthOk) { - console.log("setIsTokenError"); - setIsTokenError(true); + setApiTokenError( + "API token authentication failed. Make sure your token is correct." + ); + if (useSelfHosted) { + setCodecovUrlError( + "API token authentication failed. Make sure your self-hosted URL is correct." + ); + } return; } + setApiTokenError(""); + setCodecovUrlError(""); } catch (error) { - console.log("setIsUrlError"); - setIsUrlError(true); + setCodecovUrlError( + "Invalid URL. Make sure your self-hosted URL is correct." + ); return; } if (useGithubEnterprise) { - console.log("useGithubEnterprise"); const isScriptRegistered = await registerContentScripts(githubUrl); - console.log("isScriptRegistered", isScriptRegistered); if (!isScriptRegistered) { - console.log("setIsTabError"); - setIsTabError(true); + setGithubUrlError( + "This URL must be loaded in the active tab when you press Save." + ); return; } } else { console.log("not useGithubEnterprise"); await unregisterContentScripts(); } + setGithubUrlError(""); console.log("storage set", { codecovApiToken, @@ -289,11 +215,7 @@ function Popup() { [selfHostedGitHubURLStorageKey]: githubUrl, }); - console.log("done storage set, resetting ephemeral state and setting done"); - - resetEphemeralState(); setHasChanges(false); - setIsDone(true); }; return ( @@ -308,36 +230,46 @@ function Popup() { Save
-
- +
+
+ + + withDetectChanges(setCodecovApiToken)(e.target.value) + } + className={clsx({ + "border-red-500": apiTokenError, + })} + /> + {apiTokenError ? ( +

{apiTokenError}

+ ) : null} +
-
+