mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
[FLASK] Catch and display errors in snaps insight (#16416)
* snaps insight error catching and error state * revert confirm-transaction-base changes * display empty state if data is null * add loading state * reset errors and loading state on call * update hasNoData * update length check in component
This commit is contained in:
parent
5e746dcc2f
commit
d154cc78e4
@ -3273,6 +3273,10 @@
|
|||||||
"snaps": {
|
"snaps": {
|
||||||
"message": "Snaps"
|
"message": "Snaps"
|
||||||
},
|
},
|
||||||
|
"snapsInsightError": {
|
||||||
|
"message": "An error occured with $1: $2",
|
||||||
|
"description": "This is shown when the insight snap throws an error. $1 is the snap name, $2 is the error message."
|
||||||
|
},
|
||||||
"snapsInsightLoading": {
|
"snapsInsightLoading": {
|
||||||
"message": "Loading transaction insight..."
|
"message": "Loading transaction insight..."
|
||||||
},
|
},
|
||||||
|
@ -15,10 +15,15 @@ import { useI18nContext } from '../../../../hooks/useI18nContext';
|
|||||||
import { useTransactionInsightSnap } from '../../../../hooks/flask/useTransactionInsightSnap';
|
import { useTransactionInsightSnap } from '../../../../hooks/flask/useTransactionInsightSnap';
|
||||||
import SnapContentFooter from '../../flask/snap-content-footer/snap-content-footer';
|
import SnapContentFooter from '../../flask/snap-content-footer/snap-content-footer';
|
||||||
import Box from '../../../ui/box/box';
|
import Box from '../../../ui/box/box';
|
||||||
|
import ActionableMessage from '../../../ui/actionable-message/actionable-message';
|
||||||
|
|
||||||
export const SnapInsight = ({ transaction, chainId, selectedSnap }) => {
|
export const SnapInsight = ({ transaction, chainId, selectedSnap }) => {
|
||||||
const t = useI18nContext();
|
const t = useI18nContext();
|
||||||
const response = useTransactionInsightSnap({
|
const {
|
||||||
|
data: response,
|
||||||
|
error,
|
||||||
|
loading,
|
||||||
|
} = useTransactionInsightSnap({
|
||||||
transaction,
|
transaction,
|
||||||
chainId,
|
chainId,
|
||||||
snapId: selectedSnap.id,
|
snapId: selectedSnap.id,
|
||||||
@ -26,8 +31,8 @@ export const SnapInsight = ({ transaction, chainId, selectedSnap }) => {
|
|||||||
|
|
||||||
const data = response?.insights;
|
const data = response?.insights;
|
||||||
|
|
||||||
const hasNoData = !data || !Object.keys(data).length;
|
const hasNoData =
|
||||||
|
!error && (loading || !data || (data && Object.keys(data).length === 0));
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
flexDirection={FLEX_DIRECTION.COLUMN}
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
||||||
@ -39,13 +44,13 @@ export const SnapInsight = ({ transaction, chainId, selectedSnap }) => {
|
|||||||
textAlign={hasNoData && TEXT_ALIGN.CENTER}
|
textAlign={hasNoData && TEXT_ALIGN.CENTER}
|
||||||
className="snap-insight"
|
className="snap-insight"
|
||||||
>
|
>
|
||||||
{data ? (
|
{!loading && !error && (
|
||||||
<Box
|
<Box
|
||||||
height="full"
|
height="full"
|
||||||
flexDirection={FLEX_DIRECTION.COLUMN}
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
||||||
className="snap-insight__container"
|
className="snap-insight__container"
|
||||||
>
|
>
|
||||||
{Object.keys(data).length ? (
|
{data && Object.keys(data).length > 0 ? (
|
||||||
<>
|
<>
|
||||||
<Box
|
<Box
|
||||||
flexDirection={FLEX_DIRECTION.COLUMN}
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
||||||
@ -94,7 +99,29 @@ export const SnapInsight = ({ transaction, chainId, selectedSnap }) => {
|
|||||||
</Typography>
|
</Typography>
|
||||||
)}
|
)}
|
||||||
</Box>
|
</Box>
|
||||||
) : (
|
)}
|
||||||
|
|
||||||
|
{!loading && error && (
|
||||||
|
<Box
|
||||||
|
paddingTop={0}
|
||||||
|
paddingRight={6}
|
||||||
|
paddingBottom={3}
|
||||||
|
paddingLeft={6}
|
||||||
|
className="snap-insight__container__error"
|
||||||
|
>
|
||||||
|
<ActionableMessage
|
||||||
|
message={t('snapsInsightError', [
|
||||||
|
selectedSnap.manifest.proposedName,
|
||||||
|
error.message,
|
||||||
|
])}
|
||||||
|
type="danger"
|
||||||
|
useIcon
|
||||||
|
iconFillColor="var(--color-error-default)"
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{loading && (
|
||||||
<>
|
<>
|
||||||
<Preloader size={40} />
|
<Preloader size={40} />
|
||||||
<Typography
|
<Typography
|
||||||
|
@ -12,26 +12,38 @@ export function useTransactionInsightSnap({ transaction, chainId, snapId }) {
|
|||||||
'This snap does not have the transaction insight endowment.',
|
'This snap does not have the transaction insight endowment.',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
const [data, setData] = useState(undefined);
|
const [data, setData] = useState(undefined);
|
||||||
|
const [error, setError] = useState(undefined);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function fetchInsight() {
|
async function fetchInsight() {
|
||||||
const d = await handleSnapRequest({
|
try {
|
||||||
snapId,
|
setError(undefined);
|
||||||
origin: '',
|
setLoading(true);
|
||||||
handler: 'onTransaction',
|
|
||||||
request: {
|
const d = await handleSnapRequest({
|
||||||
jsonrpc: '2.0',
|
snapId,
|
||||||
method: ' ',
|
origin: '',
|
||||||
params: { transaction, chainId },
|
handler: 'onTransaction',
|
||||||
},
|
request: {
|
||||||
});
|
jsonrpc: '2.0',
|
||||||
setData(d);
|
method: ' ',
|
||||||
|
params: { transaction, chainId },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
setData(d);
|
||||||
|
} catch (err) {
|
||||||
|
setError(err);
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (transaction) {
|
if (transaction) {
|
||||||
fetchInsight();
|
fetchInsight();
|
||||||
}
|
}
|
||||||
}, [snapId, transaction, chainId]);
|
}, [snapId, transaction, chainId]);
|
||||||
|
|
||||||
return data;
|
return { data, error, loading };
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user