import React, { useCallback, useEffect, useRef, useState } from "react"; import { Spacer } from "../common/Spacer"; import { CustomButton, TextItalic, TextP, TextSpan } from "../App-styles"; import { Box, Button, Checkbox, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, Input, Switch, Tooltip, Typography, } from "@mui/material"; import Logo1 from "../assets/svgs/Logo1.svg"; import Logo1Dark from "../assets/svgs/Logo1Dark.svg"; import Info from "../assets/svgs/Info.svg"; import { CustomizedSnackbars } from "../components/Snackbar/Snackbar"; import { set } from "lodash"; import { cleanUrl, isUsingLocal } from "../background"; export const NotAuthenticated = ({ getRootProps, getInputProps, setExtstate, apiKey, setApiKey, globalApiKey, handleSetGlobalApikey, }) => { console.log("apiKey", apiKey); const [isValidApiKey, setIsValidApiKey] = useState(null); const [hasLocalNode, setHasLocalNode] = useState(null); const [useLocalNode, setUseLocalNode] = useState(false); const [openSnack, setOpenSnack] = React.useState(false); const [infoSnack, setInfoSnack] = React.useState(null); const [show, setShow] = React.useState(false); const [mode, setMode] = React.useState("list"); const [customNodes, setCustomNodes] = React.useState(null); const [currentNode, setCurrentNode] = React.useState({ url: "http://127.0.0.1:12391", }); const [importedApiKey, setImportedApiKey] = React.useState(null); //add and edit states const [url, setUrl] = React.useState("http://"); const [customApikey, setCustomApiKey] = React.useState(""); const [customNodeToSaveIndex, setCustomNodeToSaveIndex] = React.useState(null); const importedApiKeyRef = useRef(null) const currentNodeRef = useRef(null) const isLocal = cleanUrl(currentNode?.url) === "127.0.0.1:12391"; const handleFileChangeApiKey = (event) => { const file = event.target.files[0]; // Get the selected file console.log('file', file) if (file) { const reader = new FileReader(); reader.onload = (e) => { const text = e.target.result; // Get the file content console.log('text', text) setImportedApiKey(text); // Store the file content in the state }; reader.readAsText(file); // Read the file as text } }; const checkIfUserHasLocalNode = useCallback(async () => { try { const url = `http://127.0.0.1:12391/admin/status`; const response = await fetch(url, { method: "GET", headers: { "Content-Type": "application/json", }, }); const data = await response.json(); if (data?.height) { setHasLocalNode(true); } } catch (error) {} }, []); useEffect(() => { checkIfUserHasLocalNode(); }, []); useEffect(() => { chrome?.runtime?.sendMessage( { action: "getCustomNodesFromStorage" }, (response) => { if (response) { console.log("response", response); setCustomNodes(response || []); } } ); }, []); useEffect(()=> { importedApiKeyRef.current = importedApiKey }, [importedApiKey]) useEffect(()=> { currentNodeRef.current = currentNode }, [currentNode]) console.log('currentNode', currentNode) const validateApiKey = useCallback(async (key) => { try { console.log('currentNodeRef.current', currentNodeRef.current, key) if(!currentNodeRef.current) return const isLocalKey = cleanUrl(key?.url) === "127.0.0.1:12391"; const isCurrentNodeLocal = cleanUrl(currentNodeRef.current?.url) === "127.0.0.1:12391"; if(isLocalKey && !isCurrentNodeLocal) { setIsValidApiKey(false); setUseLocalNode(false); return } let payload = {}; if (currentNodeRef.current?.url === "http://127.0.0.1:12391") { payload = { apikey: importedApiKeyRef.current || key?.apikey, url: currentNode?.url, }; } else if(currentNodeRef.current) { payload = currentNodeRef.current; } console.log('payload', payload) const url = `${payload?.url}/admin/apikey/test`; const response = await fetch(url, { method: "GET", headers: { accept: "text/plain", "X-API-KEY": payload?.apikey, // Include the API key here }, }); // Assuming the response is in plain text and will be 'true' or 'false' const data = await response.text(); console.log("data", data); if (data === "true") { chrome?.runtime?.sendMessage( { action: "setApiKey", payload }, (response) => { console.log("setApiKey", response); if (response) { handleSetGlobalApikey(payload); setIsValidApiKey(true); setUseLocalNode(true); } } ); } else { setIsValidApiKey(false); setUseLocalNode(false); setInfoSnack({ type: "error", message: "Select a valid apikey", }); setOpenSnack(true); } } catch (error) { setIsValidApiKey(false); setUseLocalNode(false); setInfoSnack({ type: "error", message: "Select a valid apikey", }); console.error("Error validating API key:", error); } }, []); useEffect(() => { if (apiKey) { validateApiKey(apiKey); } }, [apiKey]); const addCustomNode = () => { setMode("add-node"); }; const saveCustomNodes = (myNodes) => { let nodes = [...(myNodes || [])]; console.log("customNodeToSaveIndex", customNodeToSaveIndex); if (customNodeToSaveIndex !== null) { nodes.splice(customNodeToSaveIndex, 1, { url, apikey: customApikey, }); } else if (url && customApikey) { nodes.push({ url, apikey: customApikey, }); } setCustomNodes(nodes); setCustomNodeToSaveIndex(null); if (!nodes) return; chrome?.runtime?.sendMessage( { action: "setCustomNodes", nodes }, (response) => { console.log("setCustomNodes", response); if (response) { setMode("list"); setUrl("http://"); setCustomApiKey(""); // add alert } } ); }; console.log("render customNodes", customNodes, mode); return ( <>
WELCOME TO YOUR

QORTAL WALLET
Authenticate { setExtstate("create-wallet"); }} > Create account {"Using node: "} {apiKey?.url} <> <> { if (event.target.checked) { validateApiKey(currentNode); } else { setCurrentNode({ url: "http://127.0.0.1:12391", }) setUseLocalNode(false) chrome?.runtime?.sendMessage( { action: "setApiKey", payload:null }, (response) => { console.log("setApiKey", response); if (response) { setApiKey(payload); handleSetGlobalApikey(payload); } } ); } }} disabled={false} defaultChecked /> } label={`Use ${isLocal ? 'Local' : 'Custom'} Node`} /> {currentNode?.url === "http://127.0.0.1:12391" && ( <> )} {show && ( {"Custom nodes"} {mode === "list" && ( http://127.0.0.1:12391 {customNodes?.map((node, index) => { return ( {node?.url} ); })} )} {mode === "add-node" && ( { setUrl(e.target.value); }} /> { setCustomApiKey(e.target.value); }} /> )} {mode === "list" && ( <> )} {mode === "list" && ( )} {mode === "add-node" && ( <> )} )} ); };