Skip to content

Commit

Permalink
feat: remove health check and add bootUrl field
Browse files Browse the repository at this point in the history
  • Loading branch information
HunnySajid committed Mar 27, 2024
1 parent b6a2a5a commit d436cb2
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 49 deletions.
2 changes: 2 additions & 0 deletions src/_locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"action.toSelectOther": "to select other",
"config.agentUrl.label": "Agent Url:",
"config.agentUrl.placeholder": "Enter agent url",
"config.bootUrl.label": "Boot Url:",
"config.bootUrl.placeholder": "Enter boot url",
"config.error.enterUrl": "Enter a valid url",
"config.error.invalidAgentUrl": "Invalid agent url",
"config.error.invalidVendorUrl": "Invalid url, Vendor configuration not found",
Expand Down
2 changes: 2 additions & 0 deletions src/_locales/es-419.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"action.toSelectOther": "seleccionar otro",
"config.agentUrl.label": "URL del agente:",
"config.agentUrl.placeholder": "Ingrese la URL del agente",
"config.bootUrl.label": "URL de inicio:",
"config.bootUrl.placeholder": "Ingrese la URL de inicio",
"config.error.enterUrl": "Introduce una URL válida",
"config.error.invalidAgentUrl": "URL de agente inválido",
"config.error.invalidVendorUrl": "URL no válida, configuración del proveedor no encontrada",
Expand Down
2 changes: 2 additions & 0 deletions src/_locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"action.toSelectOther": "seleccionar otro",
"config.agentUrl.label": "URL del agente:",
"config.agentUrl.placeholder": "Ingrese la URL del agente",
"config.bootUrl.label": "URL de inicio:",
"config.bootUrl.placeholder": "Ingrese la URL de inicio",
"config.error.enterUrl": "Introduce una URL válida",
"config.error.invalidAgentUrl": "URL de agente inválido",
"config.error.invalidVendorUrl": "URL no válida, configuración del proveedor no encontrada",
Expand Down
1 change: 1 addition & 0 deletions src/components/ui/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const StyledInputLabel = styled.label`
font-size: 14px;
line-height: 20px;
font-weight: 700;
width: fit-content;
`;

const StyledInput = styled.input<Pick<IInput, "error">>`
Expand Down
2 changes: 2 additions & 0 deletions src/pages/background/services/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ const Config = () => {
CONFIG_ENUMS.VENDOR_URL,
CONFIG_ENUMS.VENDOR_DATA,
CONFIG_ENUMS.HAS_ONBOARDED,
CONFIG_ENUMS.BOOT_URL,
])) as any;
return {
vendorUrl: resp[CONFIG_ENUMS.VENDOR_URL],
agentUrl: resp[CONFIG_ENUMS.AGENT_URL],
vendorData: resp[CONFIG_ENUMS.VENDOR_DATA],
hasOnboarded: resp[CONFIG_ENUMS.HAS_ONBOARDED],
bootUrl: resp[CONFIG_ENUMS.BOOT_URL],
};
};

Expand Down
6 changes: 3 additions & 3 deletions src/pages/popup/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
WEB_APP_PERMS,
configService,
} from "@pages/background/services/config";
import { isValidUrl, getBootUrl } from "@pages/background/utils";
import { isValidUrl } from "@pages/background/utils";
import { ThemeProvider, styled } from "styled-components";
import { LocaleProvider } from "@src/_locales";
import { default as defaultVendor } from "@src/config/vendor.json";
Expand Down Expand Up @@ -121,12 +121,12 @@ export default function Popup(): JSX.Element {

const handleBootAndConnect = async (passcode: string) => {
const agentUrl = await configService.getAgentUrl();
const bootUrl = await configService.getBootUrl();
const urlObject = isValidUrl(agentUrl);

if (!urlObject || !urlObject?.origin) return;
setIsLoading(true);

const bootUrl = getBootUrl(urlObject.origin);

const { data, error } = await chrome.runtime.sendMessage<
IMessage<IBootAndConnect>
>({
Expand Down
68 changes: 44 additions & 24 deletions src/screens/config/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useState, useEffect } from "react";
import { useIntl } from "react-intl";
import { configService } from "@pages/background/services/config";
import { useLocale, languageCodeMap } from "@src/_locales";
import { isValidUrl, getBootUrl, setActionIcon } from "@pages/background/utils";
import { isValidUrl, setActionIcon } from "@pages/background/utils";
import { Box, Button, Dropdown, Input, Text, Flex } from "@components/ui";

const langMap = Object.entries(languageCodeMap).map((s) => ({
Expand All @@ -16,14 +16,14 @@ export function Config(props: any): JSX.Element {
const [agentUrl, setAgentUrl] = useState("");
const [agentUrlError, setAgentUrlError] = useState("");

const [bootUrl, setBootUrl] = useState("");
const [bootUrlError, setBootUrlError] = useState("");

const [hasOnboarded, setHasOnboarded] = useState();

const { formatMessage } = useIntl();
const { changeLocale, currentLocale } = useLocale();
const validUrlMsg = formatMessage({ id: "config.error.enterUrl" });
const invalidAgentUrlMsg = formatMessage({
id: "config.error.invalidAgentUrl",
});
const invalidVendorUrlError = formatMessage({
id: "config.error.invalidVendorUrl",
});
Expand All @@ -32,6 +32,7 @@ export function Config(props: any): JSX.Element {
const response = await configService.getAgentAndVendorInfo();
setVendorUrl(response.vendorUrl);
setAgentUrl(response.agentUrl);
setBootUrl(response.bootUrl);
setHasOnboarded(response.hasOnboarded);
};

Expand All @@ -49,26 +50,11 @@ export function Config(props: any): JSX.Element {
}
};

const checkErrorAgentUrl = async (_url: string) => {
const urlObject = isValidUrl(_url);
if (!_url || !urlObject) {
const handleSetAgentUrl = async (_url: string) => {
if (!_url || !isValidUrl(_url)) {
setAgentUrlError(validUrlMsg);
return true;
}
if (urlObject && urlObject?.origin) {
try {
const bootUrl = getBootUrl(urlObject.origin);
await (await fetch(`${bootUrl}/health`)).json();
} catch (error) {
setAgentUrlError(invalidAgentUrlMsg);
return true;
}
return;
}
};

const handleSetAgentUrl = async (_url: string) => {
const hasError = await checkErrorAgentUrl(_url);
if (hasError) return;

await configService.setAgentUrl(_url);
setAgentUrl(_url);
Expand All @@ -80,13 +66,34 @@ export function Config(props: any): JSX.Element {
}
};

const handleSetBootUrl = async (_url: string) => {
if (_url) {
if (!isValidUrl(_url)) {
setBootUrlError(validUrlMsg);
return;
}

await configService.setBootUrl(_url);
setBootUrl(_url);
setBootUrlError("");
} else {
await configService.setBootUrl("");
setBootUrl("");
setBootUrlError("");
}
props.afterBootUrlUpdate();
};

const handleSetVendorUrl = async () => {
let hasError = checkErrorVendorUrl();
try {
const resp = await (await fetch(vendorUrl)).json();
if (resp?.agentUrl) {
await handleSetAgentUrl(resp?.agentUrl);
}
if (resp?.bootUrl) {
await handleSetBootUrl(resp?.bootUrl);
}
await configService.setVendorData(resp);
if (resp?.icon) {
await setActionIcon(resp?.icon);
Expand All @@ -108,8 +115,10 @@ export function Config(props: any): JSX.Element {
};

const handleBack = async () => {
const hasError = await checkErrorAgentUrl(agentUrl);
if (hasError) return;
if (!agentUrl || !isValidUrl(agentUrl)) {
setAgentUrlError(validUrlMsg);
return;
}
props.handleBack();
};

Expand Down Expand Up @@ -142,6 +151,17 @@ export function Config(props: any): JSX.Element {
onBlur={() => handleSetAgentUrl(agentUrl)}
/>
</Box>
<Box paddingX={3}>
<Input
id="boot_url"
label={`${formatMessage({ id: "config.bootUrl.label" })} *`}
error={bootUrlError}
placeholder={formatMessage({ id: "config.bootUrl.placeholder" })}
value={bootUrl}
onChange={(e) => setBootUrl(e.target.value)}
onBlur={() => handleSetBootUrl(bootUrl)}
/>
</Box>
<Box paddingX={3}>
<Dropdown
label={formatMessage({ id: "config.language.label" })}
Expand Down
28 changes: 11 additions & 17 deletions src/screens/permission/permission.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,6 @@ export function Permission({
}
};

const checkErrorAgentUrl = async (_url: string) => {
const urlObject = isValidUrl(_url);
if (!_url || !urlObject) {
return true;
}
if (urlObject && urlObject?.origin) {
try {
const bootUrl = getBootUrl(urlObject.origin);
await (await fetch(`${bootUrl}/health`)).json();
} catch (error) {
return true;
}
}
};

const removePostPermissionFlags = async () => {
await configService.setWebRequestedPermission(
WEB_APP_PERMS.SET_VENDOR_URL,
Expand All @@ -67,9 +52,14 @@ export function Permission({
subtype: "unset-action-icon",
});
};

const handleSetBootUrl = async (_url: string) => {
if (!isValidUrl(_url)) return;
await configService.setBootUrl(_url);
};

const handleSetAgentUrl = async (_url: string) => {
const hasError = await checkErrorAgentUrl(_url);
if (hasError) return;
if (!_url || !isValidUrl(_url)) return;

await configService.setAgentUrl(_url);
await configService.setHasOnboarded(true);
Expand All @@ -92,6 +82,10 @@ export function Permission({
await handleSetAgentUrl(resp?.agentUrl);
}

if(resp?.bootUrl) {
await handleSetBootUrl(resp?.bootUrl);
}

await configService.setVendorData(resp);
if (resp?.icon) {
await setActionIcon(resp?.icon);
Expand Down
11 changes: 6 additions & 5 deletions src/screens/signin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ interface ISignin {

export function Signin(props: ISignin): JSX.Element {
const { formatMessage } = useIntl();
const [hasOnboarded, setHasOnboarded] = useState(false);
const [hasAgentAndBootUrls, setHasAgentAndBootUrls] = useState(false);

const checkIfOnboarded = async () => {
const _hasOnboarded = await configService.getHasOnboarded();
setHasOnboarded(_hasOnboarded);
const response = await configService.getAgentAndVendorInfo();
setHasAgentAndBootUrls(response.agentUrl && response.bootUrl);
};

useEffect(() => {
Expand All @@ -53,6 +53,7 @@ export function Signin(props: ISignin): JSX.Element {
props.setShowConfig(false);
checkIfOnboarded();
}}
afterBootUrlUpdate={checkIfOnboarded}
afterSetUrl={props?.afterSetUrl}
/>
) : (
Expand All @@ -63,8 +64,8 @@ export function Signin(props: ISignin): JSX.Element {
logo={props.logo}
/>
)}
<Box fontSize={0} position="absolute" width="100%" bottom={2}>
{hasOnboarded ? (
<Box fontSize={0} padding={2} bottom={2}>
{hasAgentAndBootUrls ? (
<Box textAlign="center">
<NewButton onClick={props.handleSignup} $hoverUnderline>
{formatMessage({ id: "account.onboard.cta" })}
Expand Down

0 comments on commit d436cb2

Please sign in to comment.