Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FLASK] Catch and display error in snaps insight #16416

Merged
merged 7 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 33 additions & 6 deletions ui/components/app/confirm-page-container/flask/snap-insight.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,24 @@ import { useI18nContext } from '../../../../hooks/useI18nContext';
import { useTransactionInsightSnap } from '../../../../hooks/flask/useTransactionInsightSnap';
import SnapContentFooter from '../../flask/snap-content-footer/snap-content-footer';
import Box from '../../../ui/box/box';
import ActionableMessage from '../../../ui/actionable-message/actionable-message';

export const SnapInsight = ({ transaction, chainId, selectedSnap }) => {
const t = useI18nContext();
const response = useTransactionInsightSnap({
const {
data: response,
error,
loading,
} = useTransactionInsightSnap({
transaction,
chainId,
snapId: selectedSnap.id,
});

const data = response?.insights;

const hasNoData = !data || !Object.keys(data).length;

const hasNoData =
!error && (loading || !data || (data && Object.keys(data).length === 0));
return (
<Box
flexDirection={FLEX_DIRECTION.COLUMN}
Expand All @@ -39,13 +44,13 @@ export const SnapInsight = ({ transaction, chainId, selectedSnap }) => {
textAlign={hasNoData && TEXT_ALIGN.CENTER}
className="snap-insight"
>
{data ? (
{!loading && !error && (
<Box
height="full"
flexDirection={FLEX_DIRECTION.COLUMN}
className="snap-insight__container"
>
{Object.keys(data).length ? (
{data && Object.keys(data).length > 0 ? (
<>
<Box
flexDirection={FLEX_DIRECTION.COLUMN}
Expand Down Expand Up @@ -94,7 +99,29 @@ export const SnapInsight = ({ transaction, chainId, selectedSnap }) => {
</Typography>
)}
</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} />
<Typography
Expand Down
36 changes: 24 additions & 12 deletions ui/hooks/flask/useTransactionInsightSnap.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,38 @@ export function useTransactionInsightSnap({ transaction, chainId, snapId }) {
'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() {
const d = await handleSnapRequest({
snapId,
origin: '',
handler: 'onTransaction',
request: {
jsonrpc: '2.0',
method: ' ',
params: { transaction, chainId },
},
});
setData(d);
try {
setError(undefined);
setLoading(true);

const d = await handleSnapRequest({
GuillaumeRx marked this conversation as resolved.
Show resolved Hide resolved
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;
return { data, error, loading };
}