From 1389376b43cad176dbebc499b39b0563ce04ea13 Mon Sep 17 00:00:00 2001 From: Michael Clapham Date: Sun, 31 Mar 2024 15:15:19 +0200 Subject: [PATCH] Show received messages in history card --- src/App.tsx | 12 +++++++-- src/feature/history/HistoryCard.tsx | 27 ++++++++++++++++--- src/feature/session/SessionMessage.ts | 22 +++++++++++++--- src/feature/session/SessionPage.tsx | 37 +++++---------------------- 4 files changed, 60 insertions(+), 38 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 2c4b0ca..6f6c263 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -8,6 +8,7 @@ import { WSClient } from "./WSClient"; import { ServerTypes } from "./ServerTypes"; import { SessionPage } from "./feature/session/SessionPage"; import { NavigateOnStateChange } from "./NavigateOnStateChange"; +import { SessionMessage, mapSessionMsg } from "./feature/session/SessionMessage"; export const App: React.FC = () => { let wsUrl = "wss://qrsync.org/api/v1/ws"; @@ -19,6 +20,8 @@ export const App: React.FC = () => { const [clientMap, setClientMap] = useState< Record >({}); + const [sessionMessages, setSessionMessages] = useState([]); + // State used to navigate to route via a server sent event (not user link click) const [changeToRoute, setChangeToRoute] = useState(); @@ -73,8 +76,12 @@ export const App: React.FC = () => { }; const onBroadcastFromSessionMsg = ( - msg: ServerTypes.BroadcastFromSessionMsg - ) => {}; + serverMsg: ServerTypes.BroadcastFromSessionMsg + ) => { + const newMsg = mapSessionMsg(serverMsg); + const newMsgs = sessionMessages.concat([newMsg]); + setSessionMessages(newMsgs); + }; const onLeaveSession = () => { wsClient.leaveSession(); @@ -99,6 +106,7 @@ export const App: React.FC = () => { = () => { +export type HistoryCardProps = { + messages: SessionMessage[]; +} + +export const HistoryCard: React.FC = ({messages}) => { return History -

Shared content will show up here .

+ { messages.length === 0 ? : + messages.map(msg => ) }
; }; + +type MessageProps = { + msg: SessionMessage; +} + +const Message: React.FC = ({msg}) => { + return + {msg.senderName}: + {msg.text} + +} + +const NoMessages: React.FC = () => { + return
Shared content will show up here.
+} \ No newline at end of file diff --git a/src/feature/session/SessionMessage.ts b/src/feature/session/SessionMessage.ts index 6394390..ff3c137 100644 --- a/src/feature/session/SessionMessage.ts +++ b/src/feature/session/SessionMessage.ts @@ -1,12 +1,28 @@ -export type SessionMessageType = "TEXT_MESSAGE" | "FILE" | "OPEN_WEBSITE"; +import { ServerTypes } from "../../ServerTypes"; + +export const sessionMessageTypes = ["TEXT_MESSAGE", "FILE", "OPEN_WEBSITE"] as const; +export type SessionMessageType = typeof sessionMessageTypes[number]; export interface SessionMessage { - type: SessionMessageType; + type?: SessionMessageType; senderId: string; senderName: string; - text: string; + text?: string; } export interface OpenWebsiteSessionMessage extends SessionMessage { type: "OPEN_WEBSITE" +} + +export function mapSessionMsg(serverMsg: ServerTypes.BroadcastFromSessionMsg): SessionMessage { + let sessionMsgType = serverMsg.payload["type"] as SessionMessageType | undefined; + if (sessionMessageTypes.indexOf(sessionMsgType as SessionMessageType) < 0) { + sessionMsgType = undefined; + } + return { + type: sessionMsgType, + senderId: serverMsg.senderId, + senderName: serverMsg.payload["senderName"] ?? "", + text: serverMsg.payload["text"] ?? "" + }; } \ No newline at end of file diff --git a/src/feature/session/SessionPage.tsx b/src/feature/session/SessionPage.tsx index 1e7562a..c64b89f 100644 --- a/src/feature/session/SessionPage.tsx +++ b/src/feature/session/SessionPage.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from "react"; +import React from "react"; import { ServerTypes } from "../../ServerTypes"; import { IonButton, @@ -9,12 +9,13 @@ import { IonToolbar, } from "@ionic/react"; import { WSClient } from "../../WSClient"; -import { SessionMessage } from "./SessionMessage"; import { ClientsCard } from "../clients/ClientsCard"; import { HistoryCard } from "../history/HistoryCard"; import { ShareCard } from "../share/ShareCard"; +import { SessionMessage } from "./SessionMessage"; export interface SessionPageProps { + sessionMessages: SessionMessage[]; sessionId: string | undefined; sessionOwnerId: string | undefined; clientMap: Record; @@ -23,38 +24,13 @@ export interface SessionPageProps { } export const SessionPage: React.FC = ({ + sessionMessages, sessionId, sessionOwnerId, clientMap, wsClient, onLeaveSession, }) => { - const [sessionMessages, setSessionMessages] = useState([]); - - useEffect(() => { - wsClient.addMessageHandler('session_page', onWebsocketMessage); - }, [wsClient]); - - const onWebsocketMessage = (msg: ServerTypes.Msg) => { - if (msg.type === "BroadcastFromSession") { - let payload: SessionMessage = msg.payload; - if (payload.type && payload.text) { - let sessionMsgs: SessionMessage[] = []; - if (sessionMessages) { - sessionMsgs = sessionMessages; - } - sessionMsgs.push(payload); - setSessionMessages(sessionMsgs); - } - if (msg.senderId !== wsClient.getId()) { - if (payload.type === "OPEN_WEBSITE") { - console.log("Opening url ", payload.text); - window.open(payload.text, "_blank"); - } - } - } - }; - return ( @@ -70,8 +46,9 @@ export const SessionPage: React.FC = ({ /* TODO: Implement opening add client modal */ }} /> - {/* TODO: Show received messages in history card */} - + Leave Session with id: {sessionId}