2021-03-10 19:34:21 +01:00
|
|
|
// @flow
|
2022-01-14 21:24:16 +01:00
|
|
|
import 'scss/component/_livestream-chat.scss';
|
|
|
|
|
2021-04-23 21:59:48 +02:00
|
|
|
import LivestreamComment from 'component/livestreamComment';
|
2022-01-14 21:24:16 +01:00
|
|
|
import React from 'react';
|
2021-03-10 19:34:21 +01:00
|
|
|
|
2022-01-04 16:56:12 +01:00
|
|
|
// 30 sec timestamp refresh timer
|
|
|
|
const UPDATE_TIMESTAMP_MS = 30 * 1000;
|
|
|
|
|
2021-03-10 19:34:21 +01:00
|
|
|
type Props = {
|
2022-01-14 21:24:16 +01:00
|
|
|
commentsToDisplay: Array<Comment>,
|
2021-03-10 19:34:21 +01:00
|
|
|
fetchingComments: boolean,
|
2022-01-14 21:24:16 +01:00
|
|
|
uri: string,
|
2021-12-28 16:51:37 +01:00
|
|
|
};
|
2021-04-23 21:59:48 +02:00
|
|
|
|
|
|
|
export default function LivestreamComments(props: Props) {
|
2022-01-14 21:24:16 +01:00
|
|
|
const { commentsToDisplay, fetchingComments, uri } = props;
|
2021-07-06 22:28:29 +02:00
|
|
|
|
2022-01-04 16:56:12 +01:00
|
|
|
const [forceUpdate, setForceUpdate] = React.useState(0);
|
2022-01-04 16:48:15 +01:00
|
|
|
|
2022-01-04 16:56:12 +01:00
|
|
|
const now = new Date();
|
|
|
|
const shouldRefreshTimestamp =
|
2022-01-14 21:24:16 +01:00
|
|
|
commentsToDisplay &&
|
|
|
|
commentsToDisplay.some((comment) => {
|
2022-01-04 16:56:12 +01:00
|
|
|
const { timestamp } = comment;
|
|
|
|
const timePosted = timestamp * 1000;
|
|
|
|
|
|
|
|
// 1000 * 60 seconds * 60 minutes === less than an hour old
|
|
|
|
return now - timePosted < 1000 * 60 * 60;
|
|
|
|
});
|
2021-08-09 08:26:03 +02:00
|
|
|
|
2022-01-04 16:56:12 +01:00
|
|
|
// Refresh timestamp on timer
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (shouldRefreshTimestamp) {
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
setForceUpdate(Date.now());
|
|
|
|
}, UPDATE_TIMESTAMP_MS);
|
|
|
|
|
|
|
|
return () => clearTimeout(timer);
|
|
|
|
}
|
|
|
|
// forceUpdate will re-activate the timer or else it will only refresh once
|
|
|
|
}, [shouldRefreshTimestamp, forceUpdate]);
|
|
|
|
|
2022-01-14 21:24:16 +01:00
|
|
|
/* top to bottom comment display */
|
|
|
|
if (!fetchingComments && commentsToDisplay && commentsToDisplay.length > 0) {
|
|
|
|
return (
|
|
|
|
<div className="livestream__comments">
|
2022-02-02 13:44:33 +01:00
|
|
|
{commentsToDisplay
|
|
|
|
.slice(0)
|
|
|
|
.reverse()
|
|
|
|
.map((comment) => (
|
|
|
|
<LivestreamComment comment={comment} key={comment.comment_id} uri={uri} forceUpdate={forceUpdate} />
|
|
|
|
))}
|
2021-04-23 21:59:48 +02:00
|
|
|
</div>
|
2022-01-14 21:24:16 +01:00
|
|
|
);
|
|
|
|
}
|
2021-06-18 15:40:52 +02:00
|
|
|
|
2022-01-14 21:24:16 +01:00
|
|
|
return <div className="main--empty" style={{ flex: 1 }} />;
|
2021-03-10 19:34:21 +01:00
|
|
|
}
|