Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Chat logs after conversation ends #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ html, body {
body {
background: var(--background);
color: var(--foreground);
}
}

fieldset {
display: contents;
}
14 changes: 11 additions & 3 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import WaitingForChat from "@/components/sections/WaitingForChat";
import { useChatSocket } from "@/hooks/useChatSocket";

export default function Home() {

const {
user,
partner,
partnerGone,
messages,
isConnected,
isWaiting,
Expand All @@ -33,11 +34,18 @@ export default function Home() {
if (!isWaiting) {
if (!partner) { // if user hasn't allowed to find matches
content = <StartChatting name={user} onConnect={findPartner} />;

if (partnerGone) { // if the partner left, show the chat history
content = <Chat partner={""} partnerGone={partnerGone}
onMessage={sendMessage} messages={messages}
onStop={disconnect} onReconnect={findPartner}
readIndex={readIndex} readMessage={readMessage} />;
}
} else { // if matches found
content = <Chat partner={partner}
content = <Chat partner={partner} partnerGone={partnerGone}
onMessage={sendMessage} messages={messages}
onStop={disconnect} onReconnect={findPartner}
readIndex={readIndex} readMessage={readMessage}/>;
readIndex={readIndex} readMessage={readMessage} />;
}
} else { // if user is waiting for a partner
content = <WaitingForChat />;
Expand Down
14 changes: 12 additions & 2 deletions src/components/sections/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ChangeEvent, FormEvent, useCallback, useEffect, useRef, useState } from

type ChatProps = {
partner: string,
partnerGone: boolean,
messages: Message[],
onMessage: (message: string|null, image: string|null) => void,
onStop: () => void,
Expand All @@ -18,7 +19,7 @@ type ChatProps = {
readMessage: (messageId: number) => void,
}

export default function Chat({ partner, onMessage, messages, onStop, onReconnect, readIndex, readMessage }: ChatProps) {
export default function Chat({ partner, partnerGone, onMessage, messages, onStop, onReconnect, readIndex, readMessage }: ChatProps) {

const message = useRef<HTMLInputElement>(null);
const fileinput = useRef<HTMLInputElement>(null);
Expand Down Expand Up @@ -143,10 +144,17 @@ export default function Chat({ partner, onMessage, messages, onStop, onReconnect

<header className="fixed top-0 left-0 w-full bg-background font-display">
<div className="w-[1080px] max-w-full p-4 m-auto flex justify-between items-end">
{ partnerGone ?
<div className="">
<h4 className="text-[0.8em]">You are no longer connected to anyone</h4>
<button onClick={onRefresh}>Reconnect</button>
</div>
:
<div className="">
<h4 className="text-[0.8em]">You&apos;re connected to</h4>
<h2>{partner}</h2>
</div>
}
<div className="flex gap-2">
<button onClick={onRefresh}><RefreshIcon/></button>
<button onClick={onStop}><ExitIcon/></button>
Expand All @@ -155,6 +163,7 @@ export default function Chat({ partner, onMessage, messages, onStop, onReconnect
</header>

<form onSubmit={onSubmit} className="fixed bg-background bottom-[1em] border-[1px] border-foreground p-2 w-[1080px] max-w-[95vw] flex rounded-[50px]">
<fieldset disabled={partnerGone}>
{ attachment.length > 0 && <div className="absolute -top-24 border-[1px] border-foreground rounded-lg">
<button type="button" onClick={clearAttachment} className="bg-foreground font-[800] font-mono text-background text-[0.8em] w-5 h-5 flex justify-center items-center rounded-[50%] absolute -top-2 -right-2">
<CloseIcon width="18" fill="background"/>
Expand All @@ -168,8 +177,9 @@ export default function Chat({ partner, onMessage, messages, onStop, onReconnect
<input accept="image/*" onChange={onFileChange} ref={fileinput} type="file" className="hidden"/>
<AttachmentIcon width="20"/>
</button>
<input ref={message} autoFocus className="bg-inherit flex-1 outline-none text-base" type="text" placeholder="Send a message"/>
<input ref={message} disabled={partnerGone} autoFocus className="bg-inherit flex-1 outline-none text-base" type="text" placeholder="Send a message"/>
<button className="text-sm px-4 rounded-md" type="submit">Send</button>
</fieldset>
</form>

</section>
Expand Down
5 changes: 5 additions & 0 deletions src/hooks/useChatSocket.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const useChatSocket = () => {
const [socket, setSocket] = useState<Socket<ServerToClientEvents, ClientToServerEvents> | null>(null);
const [user, setUser] = useState<string | null>(null);
const [partner, setPartner] = useState<string | null>(null);
const [partnerGone, setPartnerGone] = useState(false);
const [isConnected, setIsConnected] = useState(false);
const [isWaiting, setIsWaiting] = useState(false);
const [messages, setMessages] = useState<Message[]>([]);
Expand All @@ -29,6 +30,7 @@ export const useChatSocket = () => {
setIsConnected(false);
setUser(null);
setPartner(null);
setPartnerGone(false);
setIsWaiting(false);
console.log('Disconnected from server');
});
Expand All @@ -40,6 +42,7 @@ export const useChatSocket = () => {

newSocket.on(ServerEvents.MATCHED, ({ partnerName }) => {
setPartner(partnerName);
setPartnerGone(false);
setIsWaiting(false);
console.log("Matched with a Partner");
setMessages([]);
Expand All @@ -55,6 +58,7 @@ export const useChatSocket = () => {

newSocket.on(ServerEvents.PARTNER_DISCONNECTED, () => {
setPartner(null);
setPartnerGone(true);
})

return () => {
Expand Down Expand Up @@ -95,6 +99,7 @@ export const useChatSocket = () => {
return {
user,
partner,
partnerGone,
messages,
isConnected,
isWaiting,
Expand Down