signal-desktop/ts/components/conversation/GroupDescription.tsx

71 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-06-01 17:24:28 -07:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React, { useEffect, useRef, useState } from 'react';
2021-06-01 17:24:28 -07:00
import { Modal } from '../Modal';
import type { LocalizerType } from '../../types/Util';
2021-06-17 12:15:51 -05:00
import { GroupDescriptionText } from '../GroupDescriptionText';
// Emojification can cause the scroll height to be *slightly* larger than the client
// height, so we add a little wiggle room.
const SHOW_READ_MORE_THRESHOLD = 5;
2021-06-01 17:24:28 -07:00
export type PropsType = {
i18n: LocalizerType;
title: string;
text: string;
};
2022-11-17 16:45:19 -08:00
export function GroupDescription({
2021-06-01 17:24:28 -07:00
i18n,
title,
text,
2022-11-17 16:45:19 -08:00
}: PropsType): JSX.Element {
2021-06-01 17:24:28 -07:00
const textRef = useRef<HTMLDivElement | null>(null);
const [hasReadMore, setHasReadMore] = useState(false);
const [showFullDescription, setShowFullDescription] = useState(false);
useEffect(() => {
2021-06-01 17:24:28 -07:00
if (!textRef || !textRef.current) {
return;
}
2021-06-17 12:15:51 -05:00
setHasReadMore(
textRef.current.scrollHeight - SHOW_READ_MORE_THRESHOLD >
textRef.current.clientHeight
);
}, [setHasReadMore, text, textRef]);
2021-06-01 17:24:28 -07:00
return (
<>
{showFullDescription && (
<Modal
2022-09-27 13:24:21 -07:00
modalName="GroupDescription"
2021-06-01 17:24:28 -07:00
hasXButton
i18n={i18n}
onClose={() => setShowFullDescription(false)}
title={title}
>
2021-06-17 12:15:51 -05:00
<GroupDescriptionText text={text} />
2021-06-01 17:24:28 -07:00
</Modal>
)}
<div className="GroupDescription__text" ref={textRef}>
2021-06-17 12:15:51 -05:00
<GroupDescriptionText text={text} />
2021-06-01 17:24:28 -07:00
</div>
{hasReadMore && (
<button
className="GroupDescription__read-more"
onClick={ev => {
ev.preventDefault();
ev.stopPropagation();
setShowFullDescription(true);
}}
type="button"
>
2023-03-29 17:03:25 -07:00
{i18n('icu:GroupDescription__read-more')}
2021-06-01 17:24:28 -07:00
</button>
)}
</>
);
2022-11-17 16:45:19 -08:00
}