Skip to content

Commit

Permalink
add loading state
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeRx committed Nov 8, 2022
1 parent 854b61f commit 36fcf0c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
40 changes: 23 additions & 17 deletions ui/components/app/confirm-page-container/flask/snap-insight.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ import ActionableMessage from '../../../ui/actionable-message/actionable-message

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

const data = response?.insights;

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

const hasNoData =
!error && (loading || !data || (data && !Object.keys(data).length));
return (
<Box
flexDirection={FLEX_DIRECTION.COLUMN}
Expand All @@ -40,7 +44,7 @@ export const SnapInsight = ({ transaction, chainId, selectedSnap }) => {
textAlign={hasNoData && TEXT_ALIGN.CENTER}
className="snap-insight"
>
{response && !error && (
{!loading && !error && (
<Box
height="full"
flexDirection={FLEX_DIRECTION.COLUMN}
Expand Down Expand Up @@ -96,19 +100,8 @@ export const SnapInsight = ({ transaction, chainId, selectedSnap }) => {
)}
</Box>
)}
{!response && !error && (
<>
<Preloader size={40} />
<Typography
marginTop={3}
color={COLORS.TEXT_ALTERNATIVE}
variant={TYPOGRAPHY.H6}
>
{t('snapsInsightLoading')}
</Typography>
</>
)}
{!response && error && (

{!loading && error && (
<Box
paddingTop={0}
paddingRight={6}
Expand All @@ -127,6 +120,19 @@ export const SnapInsight = ({ transaction, chainId, selectedSnap }) => {
/>
</Box>
)}

{loading && (
<>
<Preloader size={40} />
<Typography
marginTop={3}
color={COLORS.TEXT_ALTERNATIVE}
variant={TYPOGRAPHY.H6}
>
{t('snapsInsightLoading')}
</Typography>
</>
)}
</Box>
);
};
Expand Down
6 changes: 5 additions & 1 deletion ui/hooks/flask/useTransactionInsightSnap.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ 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);

Expand All @@ -31,12 +33,14 @@ export function useTransactionInsightSnap({ transaction, chainId, snapId }) {
setData(d);
} catch (err) {
setError(err);
} finally {
setLoading(false);
}
}
if (transaction) {
fetchInsight();
}
}, [snapId, transaction, chainId]);

return { data, error };
return { data, error, loading };
}

0 comments on commit 36fcf0c

Please sign in to comment.