Skip to content

Commit

Permalink
use context to save state
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistevam committed Jul 25, 2024
1 parent 552754a commit ab98c8d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 162 deletions.
8 changes: 4 additions & 4 deletions ui/components/app/alert-system/alert-modal/alert-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ import { useAlertActionHandler } from '../contexts/alertActionHandler';
import { AlertProvider } from '../alert-provider';
import {
AlertsActionMetrics,
useAlertSystemMetrics,
UseAlertSystemMetricsProps,
} from '../useAlertSystemMetrics';
} from '../useConfirmAlertMetrics';
import { useAlertMetrics } from '../contexts/alertMetricsContext';

export type AlertModalProps = {
/**
Expand Down Expand Up @@ -322,7 +322,7 @@ export function AlertModal({
enableProvider = true,
}: AlertModalProps) {
const { isAlertConfirmed, setAlertConfirmed, alerts } = useAlerts(ownerId);
const { trackAlertMetrics } = useAlertSystemMetrics();
const { trackAlertMetrics } = useAlertMetrics();

const handleClose = useCallback(
(...args) => {
Expand All @@ -348,7 +348,7 @@ export function AlertModal({
alertKey,
action: AlertsActionMetrics.AlertVisualized,
});
}, [ownerId, alertKey]);
}, [alertKey]);

return (
<Modal isOpen onClose={handleClose}>
Expand Down
30 changes: 30 additions & 0 deletions ui/components/app/alert-system/contexts/alertMetricsContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { createContext, useContext, ReactNode } from 'react';
import { ALERTS_NAME_METRICS, UseAlertSystemMetricsProps, useConfirmAlertMetrics } from '../useConfirmAlertMetrics';
import { Alert } from '../../../../ducks/confirm-alerts/confirm-alerts';

const AlertMetricsContext = createContext<{
trackAlertMetrics: (props?: UseAlertSystemMetricsProps) => void;
} | null>(null);

export const AlertMetricsProvider: React.FC<{ children: ReactNode }> = ({ children }) => {

const {trackAlertMetrics} = useConfirmAlertMetrics();

return (
<AlertMetricsContext.Provider value={{ trackAlertMetrics }}>
{children}
</AlertMetricsContext.Provider>
);
};

export const useAlertMetrics = () => {
const context = useContext(AlertMetricsContext);
if (!context) {
throw new Error('useAlertMetrics must be used within an AlertMetricsProvider');
}
return context;
};

function getAlertsName(alerts: Alert[]): string[] {
return alerts.map(alert => ALERTS_NAME_METRICS[alert.key]);
}
143 changes: 0 additions & 143 deletions ui/components/app/alert-system/useAlertSystemMetrics.ts

This file was deleted.

8 changes: 3 additions & 5 deletions ui/components/app/confirm/info/row/alert-row/alert-row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import {
} from '../row';
import { Box } from '../../../../../component-library';
import { MultipleAlertModal } from '../../../../alert-system/multiple-alert-modal';
import {
AlertsActionMetrics,
useAlertSystemMetrics,
} from '../../../../alert-system/useAlertSystemMetrics';
import { AlertsActionMetrics } from '../../../../alert-system/useConfirmAlertMetrics';
import { useAlertMetrics } from '../../../../alert-system/contexts/alertMetricsContext';

export type ConfirmInfoAlertRowProps = ConfirmInfoRowProps & {
alertKey: string;
Expand Down Expand Up @@ -46,7 +44,7 @@ export const ConfirmInfoAlertRow = ({
variant,
...rowProperties
}: ConfirmInfoAlertRowProps) => {
const { trackAlertMetrics } = useAlertSystemMetrics();
const { trackAlertMetrics } = useAlertMetrics();
const { getFieldAlerts } = useAlerts(ownerId);
const fieldAlerts = getFieldAlerts(alertKey);
const hasFieldAlert = fieldAlerts.length > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import React, { ReactElement } from 'react';
import { AlertActionHandlerProvider } from '../../../../../components/app/alert-system/contexts/alertActionHandler';
import useConfirmationAlertActions from '../../../hooks/useConfirmationAlertActions';
import setConfirmationAlerts from '../../../hooks/setConfirmationAlerts';
import { AlertMetricsProvider } from '../../../../../components/app/alert-system/contexts/alertMetricsContext';

const ConfirmAlerts = ({ children }: { children: ReactElement }) => {
const processAction = useConfirmationAlertActions();
setConfirmationAlerts();

return (
<AlertActionHandlerProvider onProcessAction={processAction}>
{children}
</AlertActionHandlerProvider>
<AlertMetricsProvider>
<AlertActionHandlerProvider onProcessAction={processAction}>
{children}
</AlertActionHandlerProvider>
</AlertMetricsProvider>
);
};

Expand Down
10 changes: 6 additions & 4 deletions ui/pages/confirmations/components/confirm/footer/footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
import { confirmSelector } from '../../../selectors';
import { REDESIGN_TRANSACTION_TYPES } from '../../../utils';
import { getConfirmationSender } from '../utils';
import { useUpdateAlertMetrics } from '../../../../../components/app/alert-system/useAlertSystemMetrics';
import { useAlertMetrics } from '../../../../../components/app/alert-system/contexts/alertMetricsContext';

const ConfirmButton = ({
alertOwnerId = '',
Expand Down Expand Up @@ -94,7 +94,7 @@ const ConfirmButton = ({
};

const Footer = () => {
const { updateAlertMetrics } = useUpdateAlertMetrics();
const { trackAlertMetrics } = useAlertMetrics();
const dispatch = useDispatch();
const t = useI18nContext();
const confirm = useSelector(confirmSelector);
Expand All @@ -103,6 +103,8 @@ const Footer = () => {
const { currentConfirmation, isScrollToBottomNeeded } = confirm;
const { from } = getConfirmationSender(currentConfirmation);

trackAlertMetrics();

///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
const { mmiOnSignCallback, mmiSubmitDisabled } = useMMIConfirmations();
///: END:ONLY_INCLUDE_IF
Expand All @@ -118,7 +120,7 @@ const Footer = () => {
if (!currentConfirmation) {
return;
}
updateAlertMetrics();
trackAlertMetrics();

dispatch(
rejectPendingApproval(
Expand All @@ -132,7 +134,7 @@ const Footer = () => {
if (!currentConfirmation) {
return;
}
updateAlertMetrics();
trackAlertMetrics();

const isTransactionConfirmation = REDESIGN_TRANSACTION_TYPES.find(
(type) => type === currentConfirmation?.type,
Expand Down
3 changes: 0 additions & 3 deletions ui/pages/confirmations/hooks/setConfirmationAlerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import {
updateAlerts,
} from '../../../ducks/confirm-alerts/confirm-alerts';
import { currentConfirmationSelector } from '../../../selectors';
import { useUpdateAlertMetrics } from '../../../components/app/alert-system/useAlertSystemMetrics';
import useConfirmationAlerts from './useConfirmationAlerts';

const setConfirmationAlerts = () => {
const dispatch = useDispatch();
const currentConfirmation = useSelector(currentConfirmationSelector);
const alerts = useConfirmationAlerts();
const ownerId = currentConfirmation?.id as string;
const { updateAlertMetrics } = useUpdateAlertMetrics();
updateAlertMetrics();

useEffect(() => {
dispatch(updateAlerts(ownerId, alerts));
Expand Down

0 comments on commit ab98c8d

Please sign in to comment.