Skip to content

Commit

Permalink
FS-73 Show message in sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
mic-smith committed Dec 2, 2024
1 parent 01df85f commit 04ed375
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
21 changes: 14 additions & 7 deletions frontend/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect } from 'react';
import styles from './app.module.css';
import { Chat } from './components/chat';
import { Input } from './components/input';
Expand All @@ -15,10 +15,10 @@ export const App = () => {
suggestions,
resetMessages,
initSuggestions,
selectMessage,
selectedMessage,
} = useMessages();

const [showSidebar, setShowSidebar] = useState(false);

useEffect(() => {
initSuggestions();
}, []);
Expand All @@ -27,23 +27,30 @@ export const App = () => {
<>
<NavBar startNewConversation={resetMessages} />
<div className={styles.container}>
{showSidebar && (
{selectedMessage && (
<div className={styles.column}>
<div className={styles.sidepanel}>
<div className={styles.close_container}>
<IconButton
icon={closeIcon}
altText="Close"
onClick={() => setShowSidebar(false)}
onClick={() => selectMessage(null)}
/>
</div>
<p>Sidebar content</p>
<p>id: {selectedMessage.id}</p>
<p>message: {selectedMessage.content}</p>
<p>time: {selectedMessage.time}</p>
</div>
</div>
)}
<div className={styles.column}>
<div className={styles.chatContainer}>
<Chat messages={messages} waiting={waiting} />
<Chat
messages={messages}
waiting={waiting}
selectedMessage={selectedMessage}
selectMessage={selectMessage}
/>
<Input
key={messages?.[0]?.time}
sendMessage={sendMessage}
Expand Down
17 changes: 15 additions & 2 deletions frontend/src/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import {
Message as wsMessage,
} from '../session/websocket-context';
import { Confirmation, ConfirmModal } from './confirm-modal';

export interface ChatProps {
messages: Message[];
waiting: boolean;
selectedMessage: Message | null;
selectMessage: (message: Message | null) => void;
}

const mapWsMessageToConfirmation = (
Expand All @@ -23,7 +26,12 @@ const mapWsMessageToConfirmation = (
return { id: parts[0], requestMessage: parts[1], result: null };
};

export const Chat = ({ messages, waiting }: ChatProps) => {
export const Chat = ({
messages,
waiting,
selectedMessage,
selectMessage,
}: ChatProps) => {
const containerRef = React.useRef<HTMLDivElement>(null);
const { lastMessage, send } = useContext(WebsocketContext);
const [chart, setChart] = useState<string | undefined>(undefined);
Expand Down Expand Up @@ -55,7 +63,12 @@ export const Chat = ({ messages, waiting }: ChatProps) => {
/>
<div ref={containerRef} className={styles.container}>
{messages.map((message, index) => (
<MessageComponent key={index} message={message} />
<MessageComponent
key={index}
message={message}
selectedMessage={selectedMessage}
selectMessage={selectMessage}
/>
))}
{chart && <img src={chart} alt="Generated chart" />}
{waiting && <Waiting />}
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/components/message.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,10 @@
font-size: 16px;
white-space: pre-wrap;
}

.selectMessage {
margin-left: 72px;
margin-bottom: 8px;
position: relative;
top: -4px;
}
23 changes: 22 additions & 1 deletion frontend/src/components/message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import styles from './message.module.css';
import UserIcon from '../icons/account-circle.svg';
import BotIcon from '../icons/logomark.svg';
import ChevronIcon from '../icons/chevron.svg';
import OpenGridIcon from '../icons/open-grid.svg';
import { Button } from './button';

export enum Role {
User = 'User',
Expand All @@ -20,6 +22,8 @@ export interface Message {

export interface MessageProps {
message: Message;
selectedMessage: Message | null;
selectMessage: (message: Message | null) => void;
}

export interface MessageStyle {
Expand All @@ -38,7 +42,11 @@ const roleStyleMap: Record<Role, MessageStyle> = {
},
};

export const MessageComponent = ({ message }: MessageProps) => {
export const MessageComponent = ({
message,
selectMessage,
selectedMessage,
}: MessageProps) => {
const { content, role, reasoning } = message;

const { class: roleClass, icon } = roleStyleMap[role];
Expand All @@ -51,6 +59,19 @@ export const MessageComponent = ({ message }: MessageProps) => {
<img src={icon} className={styles.iconStyle} />
<p className={styles.messageStyle}>{content}</p>
</div>
{role == Role.Bot && false && (
<div className={styles.selectMessage}>
<Button
isOutline
isPressed={message === selectedMessage}
text="Select message"
icon={OpenGridIcon}
onClick={() =>
selectMessage(message === selectedMessage ? null : message)
}
/>
</div>
)}
{role == Role.Bot && reasoning && (
<>
<div
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/useMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ export interface UseMessagesHook {
sendMessage: (message: string) => void;
resetMessages: () => void;
initSuggestions: () => void;
selectMessage: (message: Message | null) => void;
messages: Message[];
suggestions: string[];
waiting: boolean;
selectedMessage: Message | null;
}

export const useMessages = (): UseMessagesHook => {
const [waiting, setWaiting] = useState<boolean>(false);
const [messages, setMessages] = useState<Message[]>([starterMessage]);
const [suggestions, setSuggestions] = useState<string[]>([]);
const [selectedMessage, selectMessage] = useState<Message | null>(null);

const fetchSuggestions = useCallback(async () => {
const newSuggestions = await getSuggestions();
Expand Down Expand Up @@ -90,5 +93,7 @@ export const useMessages = (): UseMessagesHook => {
waiting,
resetMessages,
initSuggestions: fetchSuggestions,
selectedMessage,
selectMessage,
};
};

0 comments on commit 04ed375

Please sign in to comment.