2022-11-10 11:28:34 +01:00
|
|
|
import React, { useContext, useState } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { debounce } from 'lodash';
|
|
|
|
import { I18nContext } from '../../../../contexts/i18n';
|
|
|
|
import Box from '../../../ui/box';
|
2023-06-27 00:50:08 +02:00
|
|
|
import { Text } from '../../../component-library';
|
2022-11-10 11:28:34 +01:00
|
|
|
import {
|
2023-06-27 00:50:08 +02:00
|
|
|
Display,
|
|
|
|
FlexDirection,
|
2023-02-02 21:15:26 +01:00
|
|
|
AlignItems,
|
|
|
|
JustifyContent,
|
|
|
|
Color,
|
|
|
|
BackgroundColor,
|
|
|
|
BorderColor,
|
|
|
|
BorderRadius,
|
|
|
|
TextColor,
|
2023-06-27 00:50:08 +02:00
|
|
|
FontWeight,
|
2022-11-10 11:28:34 +01:00
|
|
|
} from '../../../../helpers/constants/design-system';
|
|
|
|
import SignatureRequestData from '../signature-request-data';
|
|
|
|
|
|
|
|
export default function SignatureRequestMessage({
|
|
|
|
data,
|
|
|
|
onMessageScrolled,
|
|
|
|
setMessageRootRef,
|
|
|
|
messageRootRef,
|
|
|
|
messageIsScrollable,
|
2023-01-12 15:52:02 +01:00
|
|
|
primaryType,
|
2022-11-10 11:28:34 +01:00
|
|
|
}) {
|
|
|
|
const t = useContext(I18nContext);
|
|
|
|
const [messageIsScrolled, setMessageIsScrolled] = useState(false);
|
|
|
|
const setMessageIsScrolledAtBottom = () => {
|
|
|
|
if (!messageRootRef || messageIsScrolled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { scrollTop, offsetHeight, scrollHeight } = messageRootRef;
|
|
|
|
const isAtBottom = Math.round(scrollTop) + offsetHeight >= scrollHeight;
|
|
|
|
|
|
|
|
if (isAtBottom) {
|
|
|
|
setMessageIsScrolled(true);
|
|
|
|
onMessageScrolled();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Box
|
2023-06-27 00:50:08 +02:00
|
|
|
display={Display.Flex}
|
|
|
|
flexDirection={FlexDirection.Column}
|
2022-11-10 11:28:34 +01:00
|
|
|
onScroll={debounce(setMessageIsScrolledAtBottom, 25)}
|
|
|
|
className="signature-request-message"
|
|
|
|
>
|
|
|
|
{messageIsScrollable ? (
|
|
|
|
<Box
|
2023-06-27 00:50:08 +02:00
|
|
|
display={Display.Flex}
|
2023-02-02 21:15:26 +01:00
|
|
|
alignItems={AlignItems.center}
|
|
|
|
justifyContent={JustifyContent.center}
|
|
|
|
borderColor={BorderColor.borderDefault}
|
|
|
|
backgroundColor={BackgroundColor.backgroundDefault}
|
|
|
|
color={Color.iconDefault}
|
2022-11-10 11:28:34 +01:00
|
|
|
onClick={() => {
|
|
|
|
setMessageIsScrolled(true);
|
|
|
|
onMessageScrolled();
|
|
|
|
messageRootRef?.scrollTo(0, messageRootRef?.scrollHeight);
|
|
|
|
}}
|
|
|
|
className="signature-request-message__scroll-button"
|
|
|
|
data-testid="signature-request-scroll-button"
|
|
|
|
>
|
|
|
|
<i className="fa fa-arrow-down" aria-label={t('scrollDown')} />
|
|
|
|
</Box>
|
|
|
|
) : null}
|
|
|
|
<Box
|
2023-02-02 21:15:26 +01:00
|
|
|
backgroundColor={BackgroundColor.backgroundDefault}
|
2022-11-10 11:28:34 +01:00
|
|
|
paddingBottom={3}
|
|
|
|
paddingTop={3}
|
|
|
|
paddingRight={3}
|
|
|
|
margin={2}
|
2023-02-02 21:15:26 +01:00
|
|
|
borderRadius={BorderRadius.XL}
|
|
|
|
borderColor={BorderColor.borderMuted}
|
2022-11-10 11:28:34 +01:00
|
|
|
className="signature-request-message__root"
|
|
|
|
ref={setMessageRootRef}
|
|
|
|
>
|
2023-06-27 00:50:08 +02:00
|
|
|
<Text
|
|
|
|
fontWeight={FontWeight.Bold}
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.textDefault}
|
2022-11-10 11:28:34 +01:00
|
|
|
marginLeft={4}
|
|
|
|
>
|
2023-01-12 15:52:02 +01:00
|
|
|
{primaryType}
|
2023-06-27 00:50:08 +02:00
|
|
|
</Text>
|
2023-02-27 17:32:51 +01:00
|
|
|
<SignatureRequestData data={data.value} />
|
2022-11-10 11:28:34 +01:00
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
SignatureRequestMessage.propTypes = {
|
|
|
|
data: PropTypes.object.isRequired,
|
|
|
|
onMessageScrolled: PropTypes.func,
|
|
|
|
setMessageRootRef: PropTypes.func,
|
|
|
|
messageRootRef: PropTypes.object,
|
|
|
|
messageIsScrollable: PropTypes.bool,
|
2023-01-12 15:52:02 +01:00
|
|
|
primaryType: PropTypes.string,
|
2022-11-10 11:28:34 +01:00
|
|
|
};
|