import { useQueryClient } from "@tanstack/react-query"; import { useEffect, useState } from "react"; import { useDebug } from "../../hooks/useDebug"; import { usePersistence } from "../../hooks/usePersistence"; import { usePortForwarding } from "../../hooks/usePortForwarding"; import { CodexSdk } from "../../proxy"; import { ErrorCircleIcon } from "../ErrorCircleIcon/ErrorCircleIcon"; import { SuccessCheckIcon } from "../SuccessCheckIcon/SuccessCheckIcon"; import { WarningIcon } from "../WarningIcon/WarningIcon"; import { HealthCheckIcon } from "./HealthCheckIcon"; import { Input } from "@codex-storage/marketplace-ui-components"; import { classnames } from "../../utils/classnames"; import { DebugUtils } from "../../utils/debug"; import { RefreshIcon } from "../RefreshIcon/RefreshIcon"; import "./HealthChecks.css"; type Props = { online: boolean; onStepValid: (valid: boolean) => void; }; const throwOnError = false; const defaultPort = 8070; export function HealthChecks({ online, onStepValid }: Props) { const codex = useDebug(throwOnError); const portForwarding = usePortForwarding(codex.data); const persistence = usePersistence(codex.isSuccess); const [isAddressInvalid, setIsAddressInvalid] = useState(false); const [isPortInvalid, setIsPortInvalid] = useState(false); const [address, setAddress] = useState( CodexSdk.url().split(":")[0] + ":" + CodexSdk.url().split(":")[1] ); const [port, setPort] = useState( parseInt(CodexSdk.url().split(":")[2] || "80", 10) ); const queryClient = useQueryClient(); useEffect(() => { if (codex.isSuccess) { persistence.refetch(); portForwarding.refetch().then(({ isError }) => onStepValid(isError)); } else { onStepValid(false); } }, [ persistence.refetch, onStepValid, portForwarding.refetch, codex.isSuccess, ]); const onAddressChange = (e: React.FormEvent) => { const element = e.currentTarget; const parts = e.currentTarget.value.split(":"); setIsAddressInvalid(!element.checkValidity()); if (parts.length > 2) { const [protocol, addr, port] = parts; setAddress(protocol + ":" + addr); const p = parseInt(port, 10); if (!isNaN(p)) { setPort(p); } } else { setAddress(parts.join(":")); } }; const onPortChange = (e: React.FormEvent) => { const element = e.currentTarget; const value = element.value; setIsPortInvalid(!element.checkValidity()); setPort(parseInt(value, 10)); }; const onSave = () => { if (isAddressInvalid || isPortInvalid) { return; } CodexSdk.updateURL(address + ":" + port) .then(() => queryClient.invalidateQueries()) .then(() => codex.refetch()); }; let forwardingPortValue = defaultPort; if (codex.isSuccess && codex.data) { const port = DebugUtils.getTcpPort(codex.data); if (!port.error) { forwardingPortValue = port.data; } } return (
{isAddressInvalid ? ( ) : ( )}

  • Port forwarding should be {forwardingPortValue} for TCP and 8090 by default for UDP.
    • Health Check
    • {online ? ( ) : ( )} Internet connection
    • {portForwarding.enabled ? ( ) : ( )} Port forwarding
    • {codex.isSuccess ? ( ) : ( )} Codex connection
    • {persistence.enabled ? ( ) : ( )} Marketplace
    ); }