Skip to content

Commit

Permalink
Changes messages to use absolute timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonWeill committed May 10, 2023
1 parent 76039cf commit afd6b04
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
1 change: 0 additions & 1 deletion packages/jupyter-ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"@jupyterlab/ui-components": "^3.6.3",
"@mui/icons-material": "^5.11.0",
"@mui/material": "^5.11.0",
"date-fns": "^2.29.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-markdown": "^8.0.6",
Expand Down
19 changes: 10 additions & 9 deletions packages/jupyter-ai/src/components/chat-messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { useState, useEffect } from 'react';

import { Avatar, Box, Typography } from '@mui/material';
import type { SxProps, Theme } from '@mui/material';
import { formatDistanceToNowStrict, fromUnixTime } from 'date-fns';
import ReactMarkdown from 'react-markdown';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
Expand Down Expand Up @@ -106,24 +105,26 @@ export function ChatMessages(props: ChatMessagesProps) {
const [timestamps, setTimestamps] = useState<Record<string, string>>({});

/**
* Effect: update cached timestamp strings upon receiving a new message and
* every 5 seconds after that.
* Effect: update cached timestamp strings upon receiving a new message.
*/
useEffect(() => {
function updateTimestamps() {
const newTimestamps: Record<string, string> = {};
for (const message of props.messages) {
newTimestamps[message.id] =
formatDistanceToNowStrict(fromUnixTime(message.time)) + ' ago';
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'
});
}
}
setTimestamps(newTimestamps);
}

updateTimestamps();
const intervalId = setInterval(updateTimestamps, 5000);
return () => {
clearInterval(intervalId);
};
}, [props.messages]);

return (
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5073,11 +5073,6 @@ data-urls@^2.0.0:
whatwg-mimetype "^2.3.0"
whatwg-url "^8.0.0"

date-fns@^2.29.3:
version "2.29.3"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.29.3.tgz#27402d2fc67eb442b511b70bbdf98e6411cd68a8"
integrity sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==

dateformat@^3.0.0:
version "3.0.3"
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae"
Expand Down

0 comments on commit afd6b04

Please sign in to comment.