Skip to content

Commit

Permalink
Merge pull request #152 from HunnySajid/feat/resolve-booturp
Browse files Browse the repository at this point in the history
Feat/resolve booturl
  • Loading branch information
HunnySajid committed Mar 27, 2024
2 parents 0d5e9c4 + d436cb2 commit e3c2a88
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 69 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
4 changes: 2 additions & 2 deletions src/pages/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ console.log("Background script loaded");

chrome.runtime.onStartup.addListener(function () {
(async () => {
const vendorData = await configService.getData();
const vendorData = await configService.getVendorData();
if (vendorData?.icon) {
setActionIcon(vendorData?.icon);
}
Expand Down Expand Up @@ -95,7 +95,7 @@ chrome.runtime.onMessage.addListener(function (
message.type === "vendor-info" &&
message.subtype === "get-vendor-data"
) {
const vendorData = await configService.getData();
const vendorData = await configService.getVendorData();
sendResponse({ data: { vendorData } });
}

Expand Down
39 changes: 27 additions & 12 deletions src/pages/background/services/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const CONFIG_ENUMS = {
VENDOR_DATA: "vendor-data",
VENDOR_LANG: "vendor-lang",
AGENT_URL: "agent-url",
BOOT_URL: "boot-url",
HAS_ONBOARDED: "has-onboarded",
WEB_APP_PERMISSION: "WEB_APP_PERMISSION",
};
Expand All @@ -15,33 +16,33 @@ export const WEB_APP_PERMS = {
};

const Config = () => {
const getUrl = async (): Promise<string> => {
const getVendorUrl = async (): Promise<string> => {
return (await browserStorageService.getValue(
CONFIG_ENUMS.VENDOR_URL
)) as string;
};

const removeUrl = async () => {
const removeVendorUrl = async () => {
await browserStorageService.removeKey(CONFIG_ENUMS.VENDOR_URL);
};

const setUrl = async (token: string) => {
const setVendorUrl = async (token: string) => {
await browserStorageService.setValue(CONFIG_ENUMS.VENDOR_URL, token);
};

const getData = async (): Promise<any> => {
const getVendorData = async (): Promise<any> => {
const _vendor = await browserStorageService.getValue(
CONFIG_ENUMS.VENDOR_DATA
);

return _vendor ?? defaultVendor;
};

const removeData = async () => {
const removeVendorData = async () => {
await browserStorageService.removeKey(CONFIG_ENUMS.VENDOR_DATA);
};

const setData = async (data: any) => {
const setVendorData = async (data: any) => {
await browserStorageService.setValue(CONFIG_ENUMS.VENDOR_DATA, data);
};

Expand All @@ -61,6 +62,16 @@ const Config = () => {
)) as string;
};

const setBootUrl = async (token: string) => {
await browserStorageService.setValue(CONFIG_ENUMS.BOOT_URL, token);
};

const getBootUrl = async (): Promise<string> => {
return (await browserStorageService.getValue(
CONFIG_ENUMS.BOOT_URL
)) as string;
};

const setAgentUrl = async (token: string) => {
await browserStorageService.setValue(CONFIG_ENUMS.AGENT_URL, token);
};
Expand All @@ -81,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 All @@ -111,16 +124,18 @@ const Config = () => {
};

return {
setUrl,
removeUrl,
getUrl,
getData,
removeData,
setData,
setVendorUrl,
removeVendorUrl,
getVendorUrl,
getVendorData,
removeVendorData,
setVendorData,
getLanguage,
setLanguage,
getAgentUrl,
setAgentUrl,
setBootUrl,
getBootUrl,
getHasOnboarded,
setHasOnboarded,
getAgentAndVendorInfo,
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
72 changes: 46 additions & 26 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,14 +66,35 @@ 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);
}
await configService.setData(resp);
if (resp?.bootUrl) {
await handleSetBootUrl(resp?.bootUrl);
}
await configService.setVendorData(resp);
if (resp?.icon) {
await setActionIcon(resp?.icon);
}
Expand All @@ -96,7 +103,7 @@ export function Config(props: any): JSX.Element {
hasError = true;
}
if (!hasError) {
await configService.setUrl(vendorUrl);
await configService.setVendorUrl(vendorUrl);
props.afterSetUrl();
}
};
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
Loading

0 comments on commit e3c2a88

Please sign in to comment.