From 771ddd4d399952642a0d4e8709d55127e06b944e Mon Sep 17 00:00:00 2001 From: Grant Date: Tue, 4 Jun 2024 14:49:58 -0600 Subject: [PATCH] move VITE_MATRIX_HOST & VITE_ELEMENT_HOST to ClientConfig (fixes #41) --- packages/client/src/components/App.tsx | 51 +++++++++++-------- .../src/components/Chat/OpenChatButton.tsx | 8 ++- .../src/components/Profile/UserCard.tsx | 21 +++++--- packages/client/src/contexts/ChatContext.tsx | 8 +-- packages/lib/src/net.ts | 11 ++++ packages/server/src/lib/SocketServer.ts | 5 ++ packages/server/src/types.ts | 3 ++ 7 files changed, 70 insertions(+), 37 deletions(-) diff --git a/packages/client/src/components/App.tsx b/packages/client/src/components/App.tsx index 05daaba..340031d 100644 --- a/packages/client/src/components/App.tsx +++ b/packages/client/src/components/App.tsx @@ -27,32 +27,21 @@ const DynamicallyLoadChat = () => { // get access to context data const AppInner = () => { - return ( - <> -
- - + const { config } = useAppContext(); - {/* */} - - - - - - - - - - ); -}; - -const App = () => { useEffect(() => { // detect auth callback for chat, regardless of it being loaded // callback token expires quickly, so we should exchange it as quick as possible (async () => { const params = new URLSearchParams(window.location.search); if (params.has("loginToken")) { + if (!config) { + console.warn( + "[App] loginToken parsing is delayed because config is not available" + ); + return; + } + // login button opens a new tab that redirects here // if we're that tab, we should try to close this tab when we're done // should work because this tab is opened by JS @@ -66,7 +55,7 @@ const App = () => { window.history.replaceState({}, "", "/"); const loginReq = await fetch( - `https://${import.meta.env.VITE_MATRIX_HOST}/_matrix/client/v3/login`, + `https://${config.chat.matrix_homeserver}/_matrix/client/v3/login`, { method: "POST", headers: { @@ -137,8 +126,28 @@ const App = () => { } } })(); - }, []); + }, [config]); + return ( + <> +
+ + + + {/* */} + + + + + + + + + + ); +}; + +const App = () => { return ( diff --git a/packages/client/src/components/Chat/OpenChatButton.tsx b/packages/client/src/components/Chat/OpenChatButton.tsx index 18b21c0..057d060 100644 --- a/packages/client/src/components/Chat/OpenChatButton.tsx +++ b/packages/client/src/components/Chat/OpenChatButton.tsx @@ -1,7 +1,9 @@ import { Badge, Button, Link } from "@nextui-org/react"; import { useChatContext } from "../../contexts/ChatContext"; +import { useAppContext } from "../../contexts/AppContext"; const OpenChatButton = () => { + const { config } = useAppContext(); const { notificationCount } = useChatContext(); return ( @@ -11,11 +13,7 @@ const OpenChatButton = () => { color="danger" size="sm" > - diff --git a/packages/client/src/components/Profile/UserCard.tsx b/packages/client/src/components/Profile/UserCard.tsx index 58a0ace..51a525d 100644 --- a/packages/client/src/components/Profile/UserCard.tsx +++ b/packages/client/src/components/Profile/UserCard.tsx @@ -1,8 +1,10 @@ import { faMessage, faWarning, faX } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { Button, Link, Spinner } from "@nextui-org/react"; +import { ClientConfig } from "@sc07-canvas/lib/src/net"; import { MouseEvent, useEffect, useState } from "react"; import { toast } from "react-toastify"; +import { useAppContext } from "../../contexts/AppContext"; interface IUser { sub: string; @@ -13,11 +15,8 @@ interface IUser { isModerator: boolean; } -const MATRIX_HOST = import.meta.env.VITE_MATRIX_HOST!; // eg aftermath.gg -const ELEMENT_HOST = import.meta.env.VITE_ELEMENT_HOST!; // eg https://chat.fediverse.events - -const getMatrixLink = (user: IUser) => { - return `${ELEMENT_HOST}/#/user/@${user.sub.replace("@", "=40")}:${MATRIX_HOST}`; +const getMatrixLink = (user: IUser, config: ClientConfig) => { + return `${config.chat.element_host}/#/user/@${user.sub.replace("@", "=40")}:${config.chat.matrix_homeserver}`; }; /** @@ -26,15 +25,21 @@ const getMatrixLink = (user: IUser) => { * @returns */ export const UserCard = ({ user }: { user: IUser }) => { + const { config } = useAppContext(); const [messageStatus, setMessageStatus] = useState< "loading" | "no_account" | "has_account" | "error" >("loading"); useEffect(() => { + if (!config) { + console.warn("[UserCard] config is not available yet"); + return; + } + setMessageStatus("loading"); fetch( - `https://${MATRIX_HOST}/_matrix/client/v3/profile/${encodeURIComponent(`@${user.sub.replace("@", "=40")}:${MATRIX_HOST}`)}` + `https://${config.chat.matrix_homeserver}/_matrix/client/v3/profile/${encodeURIComponent(`@${user.sub.replace("@", "=40")}:${config.chat.matrix_homeserver}`)}` ) .then((req) => { if (req.status === 200) { @@ -53,7 +58,7 @@ export const UserCard = ({ user }: { user: IUser }) => { "Error while getting Matrix account details for " + user.sub ); }); - }, [user]); + }, [user, config]); const handleMatrixClick = (e: MouseEvent) => { if (messageStatus === "no_account") { @@ -79,7 +84,7 @@ export const UserCard = ({ user }: { user: IUser }) => {