This repository was archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchat-messages.tsx
144 lines (132 loc) · 3.57 KB
/
chat-messages.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { Avatar, Box, Typography } from '@mui/material';
import type { SxProps, Theme } from '@mui/material';
import React, { useState, useEffect } from 'react';
import { RendermimeMarkdown } from './rendermime-markdown';
import { ChatService } from '../services';
type ChatMessagesProps = {
rmRegistry: IRenderMimeRegistry;
messages: ChatService.IChatMessage[];
};
export type ChatMessageHeaderProps = ChatService.IUser & {
timestamp: string;
sx?: SxProps<Theme>;
};
export function ChatMessageHeader(props: ChatMessageHeaderProps): JSX.Element {
const sharedStyles: SxProps<Theme> = {
height: '24px',
width: '24px'
};
const bgcolor = props.color;
const avatar = props.avatar_url ? (
<Avatar
sx={{
...sharedStyles,
...(bgcolor && { bgcolor })
}}
src={props.avatar_url}
></Avatar>
) : props.initials ? (
<Avatar
sx={{
...sharedStyles,
...(bgcolor && { bgcolor })
}}
>
<Typography
sx={{
fontSize: 'var(--jp-ui-font-size1)',
color: 'var(--jp-ui-inverse-font-color1)'
}}
>
{props.initials}
</Typography>
</Avatar>
) : null;
const name =
props.display_name ?? props.name ?? (props.username || 'User undefined');
return (
<Box
sx={{
display: 'flex',
alignItems: 'center',
'& > :not(:last-child)': {
marginRight: 3
},
...props.sx
}}
>
{avatar}
<Box
sx={{
display: 'flex',
flexGrow: 1,
flexWrap: 'wrap',
justifyContent: 'space-between',
alignItems: 'center'
}}
>
<Typography sx={{ fontWeight: 700, color: 'var(--jp-ui-font-color1)' }}>
{name}
</Typography>
<Typography
sx={{
fontSize: '0.8em',
color: 'var(--jp-ui-font-color2)',
fontWeight: 300
}}
>
{props.timestamp}
</Typography>
</Box>
</Box>
);
}
export function ChatMessages(props: ChatMessagesProps): JSX.Element {
const [timestamps, setTimestamps] = useState<Record<string, string>>({});
/**
* Effect: update cached timestamp strings upon receiving a new message.
*/
useEffect(() => {
const newTimestamps: Record<string, string> = { ...timestamps };
let timestampAdded = false;
for (const message of props.messages) {
if (!(message.id in newTimestamps)) {
// Use the browser's default locale
newTimestamps[message.id] = new Date(message.time * 1000) // Convert message time to milliseconds
.toLocaleTimeString([], {
hour: 'numeric', // Avoid leading zero for hours; we don't want "03:15 PM"
minute: '2-digit'
});
timestampAdded = true;
}
}
if (timestampAdded) {
setTimestamps(newTimestamps);
}
}, [props.messages]);
return (
<Box
sx={{
'& > :not(:last-child)': {
borderBottom: '1px solid var(--jp-border-color2)'
}
}}
>
{props.messages.map((message, i) => (
// extra div needed to ensure each bubble is on a new line
<Box key={i} sx={{ padding: 4 }}>
<ChatMessageHeader
{...message.sender}
timestamp={timestamps[message.id]}
sx={{ marginBottom: 3 }}
/>
<RendermimeMarkdown
rmRegistry={props.rmRegistry}
markdownStr={message.body}
/>
</Box>
))}
</Box>
);
}