-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace legacy toast with new notification toast (#1370)
- Loading branch information
1 parent
92554fc
commit 7a8db34
Showing
13 changed files
with
251 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { useHover } from '@react-aria/interactions'; | ||
import { mergeProps } from '@react-aria/utils'; | ||
import { ReactNode } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { CheckCircle, XCircle } from '@/assets/icons'; | ||
import { CTA, Divider, Flex, FlexProps, LoadingSpinner, P } from '@/component-library'; | ||
import { useCountdown } from '@/utils/hooks/use-countdown'; | ||
|
||
import { StyledProgressBar, StyledWrapper } from './NotificationToast.styles'; | ||
|
||
type NotificationToastVariant = 'success' | 'error' | 'loading'; | ||
|
||
const loadingSpinner = <LoadingSpinner thickness={2} diameter={24} variant='indeterminate' />; | ||
|
||
const getIcon = (variant: NotificationToastVariant) => | ||
({ | ||
loading: loadingSpinner, | ||
success: <CheckCircle color='success' />, | ||
error: <XCircle color='error' /> | ||
}[variant]); | ||
|
||
type Props = { | ||
title?: ReactNode; | ||
variant?: NotificationToastVariant; | ||
description?: string; | ||
timeout?: number; | ||
action?: ReactNode; | ||
onDismiss?: () => void; | ||
}; | ||
|
||
type InheritAttrs = Omit<FlexProps, keyof Props>; | ||
|
||
type NotificationToastProps = Props & InheritAttrs; | ||
|
||
const NotificationToast = ({ | ||
variant = 'success', | ||
title, | ||
timeout = 8000, | ||
description, | ||
onDismiss, | ||
action, | ||
...props | ||
}: NotificationToastProps): JSX.Element => { | ||
const { t } = useTranslation(); | ||
|
||
const showCountdown = variant === 'success' || variant === 'error'; | ||
|
||
const { value: countdown, start, stop } = useCountdown({ | ||
timeout, | ||
disabled: !showCountdown, | ||
onEndCountdown: onDismiss | ||
}); | ||
|
||
const { hoverProps } = useHover({ | ||
onHoverStart: stop, | ||
onHoverEnd: start, | ||
isDisabled: !showCountdown | ||
}); | ||
|
||
const icon = getIcon(variant); | ||
|
||
return ( | ||
<StyledWrapper direction='column' {...mergeProps(props, hoverProps)}> | ||
<Flex gap='spacing3'> | ||
<Flex elementType='span' flex={0} alignItems={description ? 'flex-start' : 'center'}> | ||
{icon} | ||
</Flex> | ||
<Flex direction='column' gap='spacing1' marginY='spacing1'> | ||
<P weight='bold' size='s'> | ||
{title} | ||
</P> | ||
{description && ( | ||
<P rows={2} size='xs'> | ||
{description} | ||
</P> | ||
)} | ||
</Flex> | ||
</Flex> | ||
{showCountdown && ( | ||
<StyledProgressBar | ||
aria-label='notification timer' | ||
value={showCountdown ? countdown : 0} | ||
color={variant === 'error' ? 'red' : 'default'} | ||
/> | ||
)} | ||
<Flex gap='spacing2' marginTop='spacing4'> | ||
{action && ( | ||
<> | ||
{action} | ||
<Divider orientation='vertical' color='default' /> | ||
</> | ||
)} | ||
<CTA size='small' fullWidth variant='text' onPress={onDismiss}> | ||
{t('dismiss')} | ||
</CTA> | ||
</Flex> | ||
</StyledWrapper> | ||
); | ||
}; | ||
|
||
export { NotificationToast }; | ||
export type { NotificationToastProps, NotificationToastVariant }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { TFunction } from 'i18next'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useDispatch } from 'react-redux'; | ||
|
||
import { updateTransactionModal } from '@/common/actions/general.actions'; | ||
import { CTA, CTALink } from '@/component-library'; | ||
import { TransactionStatus } from '@/utils/hooks/transaction/types'; | ||
|
||
import { NotificationToast, NotificationToastProps, NotificationToastVariant } from './NotificationToast'; | ||
|
||
const getData = (t: TFunction, variant: TransactionStatus) => | ||
(({ | ||
[TransactionStatus.CONFIRM]: { | ||
title: t('transaction.confirm_transaction'), | ||
status: 'loading' | ||
}, | ||
[TransactionStatus.SUBMITTING]: { | ||
title: t('transaction.transaction_processing'), | ||
status: 'loading' | ||
}, | ||
[TransactionStatus.SUCCESS]: { | ||
title: t('transaction.transaction_successful'), | ||
status: 'success' | ||
}, | ||
[TransactionStatus.ERROR]: { | ||
title: t('transaction.transaction_failed'), | ||
status: 'error' | ||
} | ||
} as Record<TransactionStatus, { title: string; status: NotificationToastVariant }>)[variant]); | ||
|
||
type Props = { | ||
variant?: TransactionStatus; | ||
url?: string; | ||
errorMessage?: string; | ||
}; | ||
|
||
type InheritAttrs = Omit<NotificationToastProps, keyof Props>; | ||
|
||
type TransactionToastProps = Props & InheritAttrs; | ||
|
||
const TransactionToast = ({ | ||
variant = TransactionStatus.SUCCESS, | ||
url, | ||
description, | ||
errorMessage, | ||
onDismiss, | ||
...props | ||
}: TransactionToastProps): JSX.Element => { | ||
const { t } = useTranslation(); | ||
const dispatch = useDispatch(); | ||
|
||
const handleViewDetails = () => { | ||
dispatch(updateTransactionModal(true, { variant: TransactionStatus.ERROR, description, errorMessage })); | ||
onDismiss?.(); | ||
}; | ||
|
||
const { title, status } = getData(t, variant); | ||
|
||
const action = url ? ( | ||
<CTALink size='small' fullWidth external to={url} variant='text'> | ||
{t('view_subscan')} | ||
</CTALink> | ||
) : ( | ||
errorMessage && ( | ||
<CTA size='small' fullWidth variant='text' onPress={handleViewDetails}> | ||
View Details | ||
</CTA> | ||
) | ||
); | ||
|
||
return ( | ||
<NotificationToast | ||
variant={status} | ||
action={action} | ||
title={title} | ||
description={description} | ||
onDismiss={onDismiss} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export { TransactionToast }; | ||
export type { TransactionToastProps }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type { NotificationToastProps } from './NotificationToast'; | ||
export { NotificationToast } from './NotificationToast'; | ||
export type { TransactionToastProps } from './TransactionToast'; | ||
export { TransactionToast } from './TransactionToast'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
7a8db34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
interbtc-ui-kintsugi-testnet – ./
interbtc-ui-kintsugi-testnet-git-master-interlay.vercel.app
interbtc-ui.vercel.app
kintnet.interlay.io
interbtc-ui-kintsugi-testnet-interlay.vercel.app
7a8db34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
interbtc-ui-interlay-testnet – ./
interbtc-ui-interlay-testnet-interlay.vercel.app
testnet.interlay.io
interbtc-ui-interlay-testnet.vercel.app
interbtc-ui-interlay-testnet-git-master-interlay.vercel.app