/* Copyright 2022 Michael Telatynski <7t3chguy@gmail.com> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React, { useContext } from "react"; import BaseTool, { IDevtoolsProps } from "./BaseTool"; import { _t } from "../../../../languageHandler"; import { useAsyncMemo } from "../../../../hooks/useAsyncMemo"; import MatrixClientContext from "../../../../contexts/MatrixClientContext"; import Spinner from "../../elements/Spinner"; import SyntaxHighlight from "../../elements/SyntaxHighlight"; import { MatrixClientPeg } from "../../../../MatrixClientPeg"; const FAILED_TO_LOAD = Symbol("failed-to-load"); interface IServerWellKnown { server: { name: string; version: string; }; } const ServerInfo = ({ onBack }: IDevtoolsProps) => { const cli = useContext(MatrixClientContext); const capabilities = useAsyncMemo(() => cli.getCapabilities(true).catch(() => FAILED_TO_LOAD), [cli]); const clientVersions = useAsyncMemo(() => cli.getVersions().catch(() => FAILED_TO_LOAD), [cli]); const serverVersions = useAsyncMemo(async () => { let baseUrl = cli.getHomeserverUrl(); try { const hsName = MatrixClientPeg.getHomeserverName(); // We don't use the js-sdk Autodiscovery module here as it only support client well-known, not server ones. const response = await fetch(`https://${hsName}/.well-known/matrix/server`); const json = await response.json(); if (json["m.server"]) { baseUrl = `https://${json["m.server"]}`; } } catch (e) { console.warn(e); } try { const response = await fetch(`${baseUrl}/_matrix/federation/v1/version`); return response.json(); } catch (e) { console.warn(e); } return FAILED_TO_LOAD; }, [cli]); let body: JSX.Element; if (!capabilities || !clientVersions || !serverVersions) { body = ; } else { body = <>

{ _t("Capabilities") }

{ capabilities !== FAILED_TO_LOAD ? :
{ _t("Failed to load.") }
}

{ _t("Client Versions") }

{ clientVersions !== FAILED_TO_LOAD ? :
{ _t("Failed to load.") }
}

{ _t("Server Versions") }

{ serverVersions !== FAILED_TO_LOAD ? :
{ _t("Failed to load.") }
} ; } return { body } ; }; export default ServerInfo;