Files
react-test/docs/data/joy/getting-started/templates/messages/components/MessagesPane.tsx
how2ice 005cf56baf
Some checks failed
No response / noResponse (push) Has been cancelled
CI / Continuous releases (push) Has been cancelled
CI / test-dev (macos-latest) (push) Has been cancelled
CI / test-dev (ubuntu-latest) (push) Has been cancelled
CI / test-dev (windows-latest) (push) Has been cancelled
Maintenance / main (push) Has been cancelled
Scorecards supply-chain security / Scorecards analysis (push) Has been cancelled
CodeQL / Analyze (push) Has been cancelled
init project
2025-12-12 14:26:25 +09:00

87 lines
2.5 KiB
TypeScript

import * as React from 'react';
import Box from '@mui/joy/Box';
import Sheet from '@mui/joy/Sheet';
import Stack from '@mui/joy/Stack';
import AvatarWithStatus from './AvatarWithStatus';
import ChatBubble from './ChatBubble';
import MessageInput from './MessageInput';
import MessagesPaneHeader from './MessagesPaneHeader';
import { ChatProps, MessageProps } from '../types';
type MessagesPaneProps = {
chat: ChatProps;
};
export default function MessagesPane(props: MessagesPaneProps) {
const { chat } = props;
const [chatMessages, setChatMessages] = React.useState(chat.messages);
const [textAreaValue, setTextAreaValue] = React.useState('');
React.useEffect(() => {
setChatMessages(chat.messages);
}, [chat.messages]);
return (
<Sheet
sx={{
height: { xs: 'calc(100dvh - var(--Header-height))', md: '100dvh' },
display: 'flex',
flexDirection: 'column',
backgroundColor: 'background.level1',
}}
>
<MessagesPaneHeader sender={chat.sender} />
<Box
sx={{
display: 'flex',
flex: 1,
minHeight: 0,
px: 2,
py: 3,
overflowY: 'scroll',
flexDirection: 'column-reverse',
}}
>
<Stack spacing={2} sx={{ justifyContent: 'flex-end' }}>
{chatMessages.map((message: MessageProps, index: number) => {
const isYou = message.sender === 'You';
return (
<Stack
key={index}
direction="row"
spacing={2}
sx={{ flexDirection: isYou ? 'row-reverse' : 'row' }}
>
{message.sender !== 'You' && (
<AvatarWithStatus
online={message.sender.online}
src={message.sender.avatar}
/>
)}
<ChatBubble variant={isYou ? 'sent' : 'received'} {...message} />
</Stack>
);
})}
</Stack>
</Box>
<MessageInput
textAreaValue={textAreaValue}
setTextAreaValue={setTextAreaValue}
onSubmit={() => {
const newId = chatMessages.length + 1;
const newIdString = newId.toString();
setChatMessages([
...chatMessages,
{
id: newIdString,
sender: 'You',
content: textAreaValue,
timestamp: 'Just now',
},
]);
}}
/>
</Sheet>
);
}