From fb678a57213eb01422ccd9c5d8bacd32bedc4043 Mon Sep 17 00:00:00 2001 From: Michael Clapham Date: Sun, 31 Mar 2024 12:52:07 +0200 Subject: [PATCH] Make session page a function component --- src/feature/session/SessionPage.tsx | 102 +++++++++++----------------- 1 file changed, 41 insertions(+), 61 deletions(-) diff --git a/src/feature/session/SessionPage.tsx b/src/feature/session/SessionPage.tsx index b7da16c..1e7562a 100644 --- a/src/feature/session/SessionPage.tsx +++ b/src/feature/session/SessionPage.tsx @@ -1,12 +1,7 @@ -import React from "react"; +import React, { useEffect, useState } from "react"; import { ServerTypes } from "../../ServerTypes"; import { IonButton, - IonCard, - IonCardContent, - IonCardHeader, - IonCardSubtitle, - IonCardTitle, IonContent, IonHeader, IonPage, @@ -15,7 +10,6 @@ import { } from "@ionic/react"; import { WSClient } from "../../WSClient"; import { SessionMessage } from "./SessionMessage"; -import { SessionActionsList } from "../session-actions/SessionActionsList"; import { ClientsCard } from "../clients/ClientsCard"; import { HistoryCard } from "../history/HistoryCard"; import { ShareCard } from "../share/ShareCard"; @@ -28,68 +22,31 @@ export interface SessionPageProps { onLeaveSession: () => any; } -export interface SessionPageState { - urlInput: string; - sessionMessages: SessionMessage[]; -} - -export class SessionPage extends React.Component< - SessionPageProps, - SessionPageState -> { - state = { - urlInput: "", - sessionMessages: [], - } as SessionPageState; - - constructor(props: SessionPageProps) { - super(props); - props.wsClient.addMessageHandler("session_page", this.onWebsocketMessage); - } +export const SessionPage: React.FC = ({ + sessionId, + sessionOwnerId, + clientMap, + wsClient, + onLeaveSession, +}) => { + const [sessionMessages, setSessionMessages] = useState([]); - render() { - return ( - - - - QR Sync - Session - - - - { - // TODO: Implement opening add client modal from session page - console.log("add client from clients card to be implemented") - }} - > - {/* TODO: Show received messages / content in history card */} - - - - Leave Session - - - - ); - } + useEffect(() => { + wsClient.addMessageHandler('session_page', onWebsocketMessage); + }, [wsClient]); - onWebsocketMessage = (msg: ServerTypes.Msg) => { + const onWebsocketMessage = (msg: ServerTypes.Msg) => { if (msg.type === "BroadcastFromSession") { let payload: SessionMessage = msg.payload; if (payload.type && payload.text) { let sessionMsgs: SessionMessage[] = []; - if (this.state.sessionMessages) { - sessionMsgs = this.state.sessionMessages; + if (sessionMessages) { + sessionMsgs = sessionMessages; } sessionMsgs.push(payload); - this.setState({ sessionMessages: sessionMsgs }); + setSessionMessages(sessionMsgs); } - if (msg.senderId !== this.props.wsClient.getId()) { + if (msg.senderId !== wsClient.getId()) { if (payload.type === "OPEN_WEBSITE") { console.log("Opening url ", payload.text); window.open(payload.text, "_blank"); @@ -97,4 +54,27 @@ export class SessionPage extends React.Component< } } }; -} + + return ( + + + + QR Sync - Session + + + + { + /* TODO: Implement opening add client modal */ + }} + /> + {/* TODO: Show received messages in history card */} + + + Leave Session with id: {sessionId} + + + ); +};