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

67 lines
2 KiB
React
Raw Normal View History

// @flow
2020-09-30 22:46:20 +02:00
import React from 'react';
import classnames from 'classnames';
import MarkdownPreview from 'component/common/markdown-preview';
import ClaimTags from 'component/claimTags';
2020-09-30 22:46:20 +02:00
import Button from 'component/button';
type Props = {
uri: string,
claim: StreamClaim,
metadata: StreamMetadata,
user: ?any,
tags: any,
};
2020-09-30 22:46:20 +02:00
function FileDescription(props: Props) {
const { uri, claim, metadata, tags } = props;
const [expanded, setExpanded] = React.useState(false);
const [hasOverflow, setHasOverflow] = React.useState(false);
const [hasCalculatedOverflow, setHasCalculatedOverflow] = React.useState(false);
const descriptionRef = React.useRef();
2020-09-30 22:46:20 +02:00
React.useEffect(() => {
if (descriptionRef && descriptionRef.current) {
const element = descriptionRef.current;
const isOverflowing = element.scrollHeight > element.clientHeight;
setHasOverflow(isOverflowing);
setHasCalculatedOverflow(true);
}
2020-09-30 22:46:20 +02:00
}, [descriptionRef]);
2020-09-30 22:46:20 +02:00
if (!claim || !metadata) {
return <span className="empty">{__('Empty claim or metadata info.')}</span>;
}
2020-09-30 22:46:20 +02:00
const { description } = metadata;
if (!description && !(tags && tags.length)) return null;
return (
<div>
<div
ref={descriptionRef}
className={classnames({
'media__info-text--contracted': !expanded,
'media__info-text--expanded': expanded,
'media__info-text--fade': hasOverflow && !expanded,
})}
2020-09-30 22:46:20 +02:00
>
<MarkdownPreview className="markdown-preview--description" content={description} />
<ClaimTags uri={uri} type="large" />
</div>
{hasCalculatedOverflow && hasOverflow && (
<div className="media__info-expand">
{expanded ? (
<Button button="link" label={__('Less')} onClick={() => setExpanded(!expanded)} />
) : (
<Button button="link" label={__('More')} onClick={() => setExpanded(!expanded)} />
)}
</div>
)}
</div>
);
}
export default FileDescription;