Skip to content

Commit

Permalink
Move sending of messages to top level.
Browse files Browse the repository at this point in the history
Refactor sharing actions UI.
  • Loading branch information
michaelclapham committed Mar 31, 2024
1 parent 1389376 commit d9c6f29
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 193 deletions.
16 changes: 15 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ export const App: React.FC = () => {
setChangeToRoute("/index.html");
};

const onShare = (msg: SessionMessage) => {
let msgWithSender: SessionMessage = {
...msg,
senderId: wsClient.getId() ?? "",
senderName: wsClient.getName(),
}
let serverMsg: ServerTypes.BroadcastToSessionMsg = {
type: "BroadcastToSession",
payload: msgWithSender
};
wsClient.sendMessage(serverMsg);
}

return (
<IonApp>
<IonReactRouter>
Expand All @@ -107,10 +120,11 @@ export const App: React.FC = () => {
<Route path="/session">
<SessionPage
sessionMessages={sessionMessages}
wsClient={wsClient}
userIsSessionOwner={wsClient.getId() === sessionOwnerId}
sessionId={sessionId}
clientMap={clientMap}
sessionOwnerId={sessionOwnerId}
onShare={onShare}
onLeaveSession={onLeaveSession}
></SessionPage>
</Route>
Expand Down
4 changes: 2 additions & 2 deletions src/feature/history/HistoryCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const HistoryCard: React.FC<HistoryCardProps> = ({messages}) => {
</IonCardHeader>
<IonCardContent>
{ messages.length === 0 ? <NoMessages/> :
messages.map(msg => <Message msg={msg} />) }
messages.map(msg => <Message key={msg.uuid ?? "" + Math.random()} msg={msg} />) }
</IonCardContent>
</IonCard>;
};
Expand All @@ -24,7 +24,7 @@ type MessageProps = {

const Message: React.FC<MessageProps> = ({msg}) => {
return <IonItem>
<b>{msg.senderName}: </b>
<b>{msg.senderName}:</b>
{msg.text}
</IonItem>
}
Expand Down
45 changes: 0 additions & 45 deletions src/feature/session-actions/OpenWebsiteModal.tsx

This file was deleted.

11 changes: 0 additions & 11 deletions src/feature/session-actions/SessionActionModal.ts

This file was deleted.

107 changes: 0 additions & 107 deletions src/feature/session-actions/SessionActionsList.tsx

This file was deleted.

6 changes: 4 additions & 2 deletions src/feature/session/SessionMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ export const sessionMessageTypes = ["TEXT_MESSAGE", "FILE", "OPEN_WEBSITE"] as c
export type SessionMessageType = typeof sessionMessageTypes[number];

export interface SessionMessage {
uuid?: string;
type?: SessionMessageType;
senderId: string;
senderName: string;
senderId?: string;
senderName?: string;
text?: string;
}

Expand All @@ -20,6 +21,7 @@ export function mapSessionMsg(serverMsg: ServerTypes.BroadcastFromSessionMsg): S
sessionMsgType = undefined;
}
return {
uuid: ("" + Math.random() + "-" + Math.random()).replace(".", "0"),
type: sessionMsgType,
senderId: serverMsg.senderId,
senderName: serverMsg.payload["senderName"] ?? "",
Expand Down
9 changes: 5 additions & 4 deletions src/feature/session/SessionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
IonTitle,
IonToolbar,
} from "@ionic/react";
import { WSClient } from "../../WSClient";
import { ClientsCard } from "../clients/ClientsCard";
import { HistoryCard } from "../history/HistoryCard";
import { ShareCard } from "../share/ShareCard";
Expand All @@ -19,7 +18,8 @@ export interface SessionPageProps {
sessionId: string | undefined;
sessionOwnerId: string | undefined;
clientMap: Record<string, ServerTypes.Client>;
wsClient: WSClient;
userIsSessionOwner: boolean;
onShare: (msg: SessionMessage) => void;
onLeaveSession: () => any;
}

Expand All @@ -28,7 +28,8 @@ export const SessionPage: React.FC<SessionPageProps> = ({
sessionId,
sessionOwnerId,
clientMap,
wsClient,
userIsSessionOwner,
onShare,
onLeaveSession,
}) => {
return (
Expand All @@ -49,7 +50,7 @@ export const SessionPage: React.FC<SessionPageProps> = ({
<HistoryCard
messages={sessionMessages}
></HistoryCard>
<ShareCard wsClient={wsClient} sessionOwnerId={sessionOwnerId} />
<ShareCard onShare={onShare} userIsSessionOwner={userIsSessionOwner}/>
<IonButton onClick={onLeaveSession}>Leave Session with id: {sessionId}</IonButton>
</IonContent>
</IonPage>
Expand Down
49 changes: 28 additions & 21 deletions src/feature/share/ShareCard.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import { IonCard, IonCardHeader, IonCardTitle, IonCardContent } from "@ionic/react";
import React from "react";
import { SessionActionsList } from "../session-actions/SessionActionsList";
import { WSClient } from "../../WSClient";
import { IonCard, IonCardHeader, IonCardTitle, IonCardContent, IonButton, IonModal } from "@ionic/react";
import React, { useState } from "react";
import { SessionMessage, SessionMessageType } from "../session/SessionMessage";
import { ShareModal } from "./share-actions/ShareModal";

export type ShareCardProps = {
wsClient: WSClient;
sessionOwnerId?: string;
onShare: (msg: SessionMessage) => void;
userIsSessionOwner?: boolean;
}

export const ShareCard: React.FC<ShareCardProps> = ({wsClient, sessionOwnerId}) => {
return <IonCard>
<IonCardHeader>
<IonCardTitle>Share</IonCardTitle>
</IonCardHeader>
<IonCardContent>
<SessionActionsList
wsClient={wsClient}
userIsSessionOwner={
sessionOwnerId === wsClient.getId()
}
></SessionActionsList>
</IonCardContent>
</IonCard>;
};
export const ShareCard: React.FC<ShareCardProps> = ({onShare, userIsSessionOwner}) => {
const [shareModalTypeOpen, setShareModalTypeOpen] = useState<SessionMessageType | null>(null);

return <IonCard>
<IonCardHeader>
<IonCardTitle>Share</IonCardTitle>
</IonCardHeader>
<IonCardContent>
{/* Send text message */}
<IonButton
onClick={() => setShareModalTypeOpen("TEXT_MESSAGE")}
>Send Message</IonButton>
</IonCardContent>
<IonModal isOpen={shareModalTypeOpen != null} onDidDismiss={() => setShareModalTypeOpen(null)}>
<ShareModal
type={shareModalTypeOpen}
onShare={onShare}
closeModal={() => setShareModalTypeOpen(null)}
/>
</IonModal>
</IonCard>;
};
34 changes: 34 additions & 0 deletions src/feature/share/share-actions/SendMessageModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { IonButton, IonInput } from "@ionic/react";
import React, { useState } from "react";
import { ShareModalProps } from "./ShareModal";
import { SessionMessage } from "../../session/SessionMessage";

export const SendMessageModal: React.FC<ShareModalProps> = ({
onShare,
closeModal,
}) => {

const [text, setText] = useState("");

const sendClick = () => {
let sessionMsg: SessionMessage = {
type: "TEXT_MESSAGE",
text: text,
};
onShare(sessionMsg);
closeModal();
};

return (
<div style={{display: "flex", flexDirection: "column"}}>
<h1>Send message</h1>
<p>Send message to all clients in this session.</p>
<IonInput
value={text}
placeholder="Enter message to send"
onIonChange={(e) => setText(e.detail.value!)}
></IonInput>
<IonButton onClick={() => sendClick()}>Send</IonButton>
</div>
);
}
20 changes: 20 additions & 0 deletions src/feature/share/share-actions/ShareModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from "react";
import { SessionMessage, SessionMessageType } from "../../session/SessionMessage";
import { SendMessageModal } from "./SendMessageModal";

export type ShareModalProps = {
type: SessionMessageType | null;
onShare: (msg: SessionMessage) => void;
closeModal: () => any;
};

export const ShareModal: React.FC<ShareModalProps> = ({type, onShare, closeModal}) => {
switch(type) {
case "TEXT_MESSAGE": return <SendMessageModal
type="TEXT_MESSAGE"
onShare={onShare}
closeModal={closeModal}
/>
default: return null;
}
}

0 comments on commit d9c6f29

Please sign in to comment.