lbry-desktop/ui/component/comment/view.jsx

88 lines
2.4 KiB
React
Raw Normal View History

2019-06-27 01:59:27 +02:00
// @flow
2019-10-23 09:04:40 +02:00
import React, { useEffect } from 'react';
import { isEmpty } from 'util/object';
2019-06-27 01:59:27 +02:00
import relativeDate from 'tiny-relative-date';
2019-07-21 22:46:30 +02:00
import Button from 'component/button';
import Expandable from 'component/expandable';
2019-10-13 06:04:16 +02:00
import MarkdownPreview from 'component/common/markdown-preview';
2019-10-23 09:04:40 +02:00
import ChannelThumbnail from 'component/channelThumbnail';
2019-06-27 01:59:27 +02:00
type Props = {
author: string,
authorUri: string,
2019-06-27 01:59:27 +02:00
message: string,
timePosted: number,
2019-10-23 09:04:40 +02:00
claim: ?Claim,
pending?: boolean,
resolveUri: string => void,
isResolvingUri: boolean,
channelIsBlocked: boolean,
2019-06-27 01:59:27 +02:00
};
const LENGTH_TO_COLLAPSE = 300;
2019-06-27 01:59:27 +02:00
function Comment(props: Props) {
2019-10-23 09:04:40 +02:00
const {
author,
authorUri,
timePosted,
message,
pending,
claim,
isResolvingUri,
resolveUri,
channelIsBlocked,
} = props;
// to debounce subsequent requests
const shouldFetch =
claim === undefined || (claim !== null && claim.value_type === 'channel' && isEmpty(claim.meta) && !pending);
2019-12-04 19:07:40 +01:00
2019-10-23 09:04:40 +02:00
useEffect(() => {
// If author was extracted from the URI, then it must be valid.
if (authorUri && author && !isResolvingUri && shouldFetch) {
resolveUri(authorUri);
}
}, [isResolvingUri, shouldFetch, author, authorUri, resolveUri]);
2019-12-04 19:07:40 +01:00
2019-06-27 01:59:27 +02:00
return (
<li className="comment">
2019-10-24 19:24:53 +02:00
<div className="comment__author-thumbnail">
2019-12-04 19:07:40 +01:00
{authorUri ? <ChannelThumbnail uri={authorUri} obscure={channelIsBlocked} small /> : <ChannelThumbnail small />}
2019-06-27 01:59:27 +02:00
</div>
2019-10-24 19:24:53 +02:00
<div className="comment__body_container">
<span className="comment__meta">
2019-12-04 19:07:40 +01:00
{!author ? (
<span className="comment__author">{__('Anonymous')}</span>
) : (
<Button
className="button--uri-indicator truncated-text comment__author"
navigate={authorUri}
label={author}
/>
)}
2019-10-24 19:24:53 +02:00
<time className="comment__time" dateTime={timePosted}>
{relativeDate(timePosted)}
</time>
</span>
<div>
{message.length >= LENGTH_TO_COLLAPSE ? (
2019-12-04 19:07:40 +01:00
<div className="comment__message">
<Expandable>
<MarkdownPreview content={message} />
</Expandable>
</div>
) : (
2019-12-04 19:07:40 +01:00
<div className="comment__message">
<MarkdownPreview content={message} />
</div>
)}
2019-10-24 19:24:53 +02:00
</div>
</div>
2019-06-27 01:59:27 +02:00
</li>
);
}
export default Comment;