1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 12:29:06 +01:00
metamask-extension/ui/hooks/flask/useTransactionInsightSnap.js
Guillaume Roux d154cc78e4
[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
2022-11-09 14:20:57 +01:00

50 lines
1.3 KiB
JavaScript

import { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { handleSnapRequest } from '../../store/actions';
import { getPermissionSubjects } from '../../selectors';
const INSIGHT_PERMISSION = 'endowment:transaction-insight';
export function useTransactionInsightSnap({ transaction, chainId, snapId }) {
const subjects = useSelector(getPermissionSubjects);
if (!subjects[snapId]?.permissions[INSIGHT_PERMISSION]) {
throw new Error(
'This snap does not have the transaction insight endowment.',
);
}
const [loading, setLoading] = useState(true);
const [data, setData] = useState(undefined);
const [error, setError] = useState(undefined);
useEffect(() => {
async function fetchInsight() {
try {
setError(undefined);
setLoading(true);
const d = await handleSnapRequest({
snapId,
origin: '',
handler: 'onTransaction',
request: {
jsonrpc: '2.0',
method: ' ',
params: { transaction, chainId },
},
});
setData(d);
} catch (err) {
setError(err);
} finally {
setLoading(false);
}
}
if (transaction) {
fetchInsight();
}
}, [snapId, transaction, chainId]);
return { data, error, loading };
}