import React, { useCallback, useContext, useEffect, useRef, useState } from "react"; import { Spacer } from "../common/Spacer"; import { CustomButton, TextItalic, TextP, TextSpan } from "../App-styles"; import { Box, Button, ButtonBase, 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 HelpIcon from '@mui/icons-material/Help'; import { CustomizedSnackbars } from "../components/Snackbar/Snackbar"; import { set } from "lodash"; import { cleanUrl, isUsingLocal } from "../background"; import { GlobalContext } from "../App"; const manifestData = { version: "0.4.0", }; export const NotAuthenticated = ({ getRootProps, getInputProps, setExtstate, apiKey, setApiKey, globalApiKey, handleSetGlobalApikey, }) => { 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 { showTutorial } = useContext(GlobalContext); const importedApiKeyRef = useRef(null); const currentNodeRef = useRef(null); const hasLocalNodeRef = 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 if (file) { const reader = new FileReader(); reader.onload = (e) => { const text = e.target.result; // Get the file content 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); return true } return false } catch (error) { return false } }, []); useEffect(() => { checkIfUserHasLocalNode(); }, []); useEffect(() => { window .sendMessage("getCustomNodesFromStorage") .then((response) => { if (response) { setCustomNodes(response || []); window.electronAPI.setAllowedDomains(response?.map((node)=> node.url)) } }) .catch((error) => { console.error( "Failed to get custom nodes from storage:", error.message || "An error occurred" ); }); }, []); useEffect(() => { importedApiKeyRef.current = importedApiKey; }, [importedApiKey]); useEffect(() => { currentNodeRef.current = currentNode; }, [currentNode]); useEffect(() => { hasLocalNodeRef.current = hasLocalNode; }, [hasLocalNode]); const validateApiKey = useCallback(async (key, fromStartUp) => { try { if (!currentNodeRef.current) return; const stillHasLocal = await checkIfUserHasLocalNode() const isLocalKey = cleanUrl(key?.url) === "127.0.0.1:12391"; if (isLocalKey && !stillHasLocal && !fromStartUp) { throw new Error("Please turn on your local node"); } 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: currentNodeRef.current?.url, }; } else if (currentNodeRef.current) { payload = currentNodeRef.current; } 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(); if (data === "true") { window .sendMessage("setApiKey", payload) .then((response) => { if (response) { handleSetGlobalApikey(payload); setIsValidApiKey(true); setUseLocalNode(true); if (!fromStartUp) { setApiKey(payload); } } }) .catch((error) => { console.error( "Failed to set API key:", error.message || "An error occurred" ); }); } else { setIsValidApiKey(false); setUseLocalNode(false); setInfoSnack({ type: "error", message: "Select a valid apikey", }); setOpenSnack(true); } } catch (error) { setIsValidApiKey(false); setUseLocalNode(false); if(fromStartUp){ setCurrentNode({ url: "http://127.0.0.1:12391", }); window .sendMessage("setApiKey", null) .then((response) => { if (response) { setApiKey(null); handleSetGlobalApikey(null); } }) .catch((error) => { console.error( "Failed to set API key:", error.message || "An error occurred" ); }); return } setInfoSnack({ type: "error", message: error?.message || "Select a valid apikey", }); setOpenSnack(true); console.error("Error validating API key:", error); } }, []); useEffect(() => { if (apiKey) { validateApiKey(apiKey, true); } }, [apiKey]); const addCustomNode = () => { setMode("add-node"); }; const saveCustomNodes = (myNodes) => { let nodes = [...(myNodes || [])]; if (customNodeToSaveIndex !== null) { nodes.splice(customNodeToSaveIndex, 1, { url, apikey: customApikey, }); } else if (url && customApikey) { nodes.push({ url, apikey: customApikey, }); } setCustomNodes(nodes); window.electronAPI.setAllowedDomains(nodes?.map((node)=> node.url)) setCustomNodeToSaveIndex(null); if (!nodes) return; window .sendMessage("setCustomNodes", nodes) .then((response) => { if (response) { setMode("list"); setUrl("http://"); setCustomApiKey(""); // add alert if needed } }) .catch((error) => { console.error( "Failed to set custom nodes:", error.message || "An error occurred" ); }); }; return ( <>
WELCOME TO YOUR

QORTAL WALLET
setExtstate('wallets')}> {/* */} Wallets {/* */} { setExtstate("create-wallet"); }} > Create account {"Using node: "} {currentNode?.url} <> <> { if (event.target.checked) { validateApiKey(currentNode); } else { setCurrentNode({ url: "http://127.0.0.1:12391", }); setUseLocalNode(false); window .sendMessage("setApiKey", null) .then((response) => { if (response) { setApiKey(null); handleSetGlobalApikey(null); } }) .catch((error) => { console.error( "Failed to set API key:", error.message || "An error occurred" ); }); } }} disabled={false} defaultChecked /> } label={`Use ${isLocal ? "Local" : "Custom"} Node`} /> {currentNode?.url === "http://127.0.0.1:12391" && ( <> {`api key : ${importedApiKey}`} )} Build version: {manifestData?.version} {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" && ( <> )} )} { showTutorial('create-account', true) }} sx={{ position: 'fixed', bottom: '25px', right: '25px' }}> ); };