2022-09-20 19:00:07 +02:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
2023-04-03 18:04:30 +02:00
|
|
|
import { useSelector } from 'react-redux';
|
2022-09-20 19:00:07 +02:00
|
|
|
import Preloader from '../../../ui/icon/preloader/preloader-icon.component';
|
2023-03-16 01:00:54 +01:00
|
|
|
import { Text } from '../../../component-library';
|
2022-09-20 19:00:07 +02:00
|
|
|
import {
|
2023-02-02 21:15:26 +01:00
|
|
|
AlignItems,
|
2022-09-20 19:00:07 +02:00
|
|
|
FLEX_DIRECTION,
|
2023-02-02 21:15:26 +01:00
|
|
|
JustifyContent,
|
2023-05-23 21:29:14 +02:00
|
|
|
TextAlign,
|
2023-02-02 21:15:26 +01:00
|
|
|
TextColor,
|
2023-03-16 01:00:54 +01:00
|
|
|
TextVariant,
|
2022-09-20 19:00:07 +02:00
|
|
|
} from '../../../../helpers/constants/design-system';
|
|
|
|
import { useI18nContext } from '../../../../hooks/useI18nContext';
|
2023-04-25 16:32:51 +02:00
|
|
|
import { useTransactionInsightSnap } from '../../../../hooks/snaps/useTransactionInsightSnap';
|
2022-09-20 19:00:07 +02:00
|
|
|
import Box from '../../../ui/box/box';
|
2023-04-25 16:32:51 +02:00
|
|
|
import { SnapUIRenderer } from '../../snaps/snap-ui-renderer';
|
|
|
|
import { SnapDelineator } from '../../snaps/snap-delineator';
|
2023-05-03 10:53:58 +02:00
|
|
|
import { DelineatorType } from '../../../../helpers/constants/snaps';
|
2023-04-03 18:04:30 +02:00
|
|
|
import { getSnapName } from '../../../../helpers/utils/util';
|
2023-04-25 16:32:51 +02:00
|
|
|
import { Copyable } from '../../snaps/copyable';
|
2023-04-03 18:04:30 +02:00
|
|
|
import { getTargetSubjectMetadata } from '../../../../selectors';
|
2022-09-20 19:00:07 +02:00
|
|
|
|
2022-12-01 14:38:56 +01:00
|
|
|
export const SnapInsight = ({ transaction, origin, chainId, selectedSnap }) => {
|
2022-09-20 19:00:07 +02:00
|
|
|
const t = useI18nContext();
|
2022-11-09 14:20:57 +01:00
|
|
|
const {
|
|
|
|
data: response,
|
|
|
|
error,
|
|
|
|
loading,
|
|
|
|
} = useTransactionInsightSnap({
|
2022-09-20 19:00:07 +02:00
|
|
|
transaction,
|
|
|
|
chainId,
|
2022-12-01 14:38:56 +01:00
|
|
|
origin,
|
2022-09-20 19:00:07 +02:00
|
|
|
snapId: selectedSnap.id,
|
|
|
|
});
|
|
|
|
|
2023-04-03 18:04:30 +02:00
|
|
|
const targetSubjectMetadata = useSelector((state) =>
|
|
|
|
getTargetSubjectMetadata(state, selectedSnap.id),
|
|
|
|
);
|
|
|
|
|
|
|
|
const snapName = getSnapName(selectedSnap.id, targetSubjectMetadata);
|
|
|
|
|
2022-12-20 11:44:49 +01:00
|
|
|
const data = response?.content;
|
2022-09-20 19:00:07 +02:00
|
|
|
|
2022-11-09 14:20:57 +01:00
|
|
|
const hasNoData =
|
|
|
|
!error && (loading || !data || (data && Object.keys(data).length === 0));
|
2022-09-20 19:00:07 +02:00
|
|
|
return (
|
|
|
|
<Box
|
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
|
|
height="full"
|
|
|
|
marginTop={hasNoData && 12}
|
|
|
|
marginBottom={hasNoData && 12}
|
2023-02-02 21:15:26 +01:00
|
|
|
alignItems={hasNoData && AlignItems.center}
|
|
|
|
justifyContent={hasNoData && JustifyContent.center}
|
2023-05-23 21:29:14 +02:00
|
|
|
textAlign={hasNoData && TextAlign.Center}
|
2022-09-20 19:00:07 +02:00
|
|
|
className="snap-insight"
|
|
|
|
>
|
2022-11-09 14:20:57 +01:00
|
|
|
{!loading && !error && (
|
2022-09-20 19:00:07 +02:00
|
|
|
<Box
|
|
|
|
height="full"
|
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
|
|
className="snap-insight__container"
|
|
|
|
>
|
2022-11-09 14:20:57 +01:00
|
|
|
{data && Object.keys(data).length > 0 ? (
|
2023-04-03 18:04:30 +02:00
|
|
|
<SnapUIRenderer
|
|
|
|
snapId={selectedSnap.id}
|
|
|
|
data={data}
|
|
|
|
delineatorType={DelineatorType.Insights}
|
|
|
|
/>
|
2022-09-20 19:00:07 +02:00
|
|
|
) : (
|
2023-03-16 01:00:54 +01:00
|
|
|
<Text
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.textAlternative}
|
2023-03-16 01:00:54 +01:00
|
|
|
variant={TextVariant.bodySm}
|
|
|
|
as="h6"
|
2023-02-02 21:15:26 +01:00
|
|
|
>
|
2022-09-20 19:00:07 +02:00
|
|
|
{t('snapsNoInsight')}
|
2023-03-16 01:00:54 +01:00
|
|
|
</Text>
|
2022-09-20 19:00:07 +02:00
|
|
|
)}
|
|
|
|
</Box>
|
2022-11-09 14:20:57 +01:00
|
|
|
)}
|
|
|
|
|
|
|
|
{!loading && error && (
|
2023-04-03 18:04:30 +02:00
|
|
|
<Box padding={4} className="snap-insight__container__error">
|
|
|
|
<SnapDelineator snapName={snapName} type={DelineatorType.Error}>
|
|
|
|
<Text variant={TextVariant.bodySm} marginBottom={4}>
|
|
|
|
{t('snapsUIError', [<b key="0">{snapName}</b>])}
|
|
|
|
</Text>
|
|
|
|
<Copyable text={error.message} />
|
|
|
|
</SnapDelineator>
|
2022-11-09 14:20:57 +01:00
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{loading && (
|
2022-09-20 19:00:07 +02:00
|
|
|
<>
|
|
|
|
<Preloader size={40} />
|
2023-03-16 01:00:54 +01:00
|
|
|
<Text
|
2022-09-20 19:00:07 +02:00
|
|
|
marginTop={3}
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.textAlternative}
|
2023-03-16 01:00:54 +01:00
|
|
|
variant={TextVariant.bodySm}
|
|
|
|
as="h6"
|
2022-09-20 19:00:07 +02:00
|
|
|
>
|
|
|
|
{t('snapsInsightLoading')}
|
2023-03-16 01:00:54 +01:00
|
|
|
</Text>
|
2022-09-20 19:00:07 +02:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
SnapInsight.propTypes = {
|
|
|
|
/*
|
|
|
|
* The transaction data object
|
|
|
|
*/
|
|
|
|
transaction: PropTypes.object,
|
|
|
|
/*
|
|
|
|
* CAIP2 Chain ID
|
|
|
|
*/
|
|
|
|
chainId: PropTypes.string,
|
2022-12-01 14:38:56 +01:00
|
|
|
/*
|
|
|
|
* The origin of the transaction
|
|
|
|
*/
|
|
|
|
origin: PropTypes.string,
|
2022-09-20 19:00:07 +02:00
|
|
|
/*
|
|
|
|
* The insight snap selected
|
|
|
|
*/
|
|
|
|
selectedSnap: PropTypes.object,
|
|
|
|
};
|