94 lines
1.9 KiB
TypeScript
Raw Normal View History

2021-04-19 13:33:23 +10:00
import {
2021-04-19 14:54:39 +10:00
Box,
2021-04-19 13:33:23 +10:00
Grid,
List,
ListItem,
2021-04-19 14:57:38 +10:00
ListItemText
2021-04-19 13:33:23 +10:00
} from '@material-ui/core';
2021-04-14 15:13:55 +10:00
import React from 'react';
2021-04-22 10:58:56 +10:00
import { ChatMessage } from 'waku-chat/chat_message';
2021-04-19 13:33:23 +10:00
import MessageInput from './MessageInput';
2021-04-19 11:34:13 +10:00
import Send from './Send';
2021-04-14 15:13:55 +10:00
interface Props {
2021-04-22 10:58:56 +10:00
lines: ChatMessage[],
2021-04-14 15:13:55 +10:00
}
interface State {
2021-04-19 12:55:33 +10:00
messageToSend: string
2021-04-14 15:13:55 +10:00
}
2021-04-19 11:34:13 +10:00
2021-04-16 11:32:00 +10:00
export default class Room extends React.Component<Props, State> {
2021-04-19 12:55:33 +10:00
constructor(props: Props) {
super(props);
this.state = { messageToSend: '' };
}
2021-04-19 13:33:23 +10:00
messageHandler(msg: string) {
this.setState({ messageToSend: msg });
}
2021-04-14 15:13:55 +10:00
render() {
return (
2021-04-19 14:54:39 +10:00
<Grid container spacing={2}>
<Grid item xs={12}>
2021-04-19 16:19:32 +10:00
<Box height={800} maxHeight={800}
2021-04-19 14:54:39 +10:00
style={{ flex: 1, maxHeight: '100%', overflow: 'scroll' }}>
2021-04-19 14:57:38 +10:00
<Lines messages={this.props.lines} />
2021-04-19 14:54:39 +10:00
</Box>
</Grid>
<Grid item xs={12}>
2021-04-19 14:57:38 +10:00
<Grid container spacing={2} direction='row' alignItems='center'>
2021-04-19 14:54:39 +10:00
<Grid item xs={11}>
<MessageInput messageHandler={this.messageHandler.bind(this)} />
</Grid>
<Grid item xs={1}>
<Send message={this.state.messageToSend} />
</Grid>
2021-04-19 13:33:23 +10:00
</Grid>
</Grid>
2021-04-19 14:54:39 +10:00
</Grid>
2021-04-14 15:13:55 +10:00
);
}
2021-04-19 14:57:38 +10:00
}
2021-04-14 15:13:55 +10:00
2021-04-19 14:57:38 +10:00
interface LinesProps {
2021-04-22 10:58:56 +10:00
messages: ChatMessage[]
2021-04-19 14:57:38 +10:00
}
2021-04-19 14:54:39 +10:00
2021-04-19 14:57:38 +10:00
const Lines = (props: LinesProps) => {
const renderedLines = [];
2021-04-14 15:13:55 +10:00
2021-04-22 10:58:56 +10:00
for (const i in props.messages) {
2021-04-19 14:57:38 +10:00
renderedLines.push(<ListItem>
2021-04-22 10:58:56 +10:00
<ListItemText key={"chat-message-" + i}
primary={printMessage(props.messages[i])}
2021-04-19 14:57:38 +10:00
/>
</ListItem>);
2021-04-14 15:13:55 +10:00
}
2021-04-19 14:57:38 +10:00
return (
<List dense={true}>
{renderedLines}
</List>
);
};
2021-04-22 10:58:56 +10:00
// TODO: Make it a proper component
function printMessage(chatMsg: ChatMessage) {
const timestamp = chatMsg.timestamp.toLocaleString([], {
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: '2-digit',
hour12: false
});
return `<${timestamp}> ${chatMsg.nick}: ${chatMsg.message}`;
}