Skip to content

Commit

Permalink
single timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelchia committed Jun 18, 2024
1 parent 89093a7 commit 0c7c5da
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions packages/jupyter-ai/src/components/pending-messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function PendingMessages(
return null;
}

const [timestamps, setTimestamps] = useState<Record<string, string>>({});
const [timestamp, setTimestamp] = useState<string>('');
const lastMessage = props.messages[props.messages.length - 1];
const agentMessage: AiService.AgentChatMessage = {
type: 'agent',
Expand All @@ -57,28 +57,17 @@ export function PendingMessages(
reply_to: '',
persona: lastMessage.persona
};
/**
* 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;
useEffect(() => {
// timestamp format copied from ChatMessage
const newTimestamp = new Date(agentMessage.time * 1000).toLocaleTimeString(
[],
{
hour: 'numeric',
minute: '2-digit'
}
}
if (timestampAdded) {
setTimestamps(newTimestamps);
}
);
setTimestamp(newTimestamp);
}, [agentMessage]);

return (
Expand All @@ -93,7 +82,7 @@ export function PendingMessages(
<Box sx={{ padding: 4 }}>
<ChatMessageHeader
message={agentMessage}
timestamp={timestamps[agentMessage.id]}
timestamp={timestamp}
sx={{ marginBottom: 4 }}
/>
<Box
Expand Down

0 comments on commit 0c7c5da

Please sign in to comment.