-
Notifications
You must be signed in to change notification settings - Fork 120
UI: Implement toast component #4093
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,74 @@ | ||
| .toast { | ||
| position: fixed; | ||
| display: block; | ||
| bottom: calc(var(--item-height-large) + var(--space-half)); | ||
| right: 50%; | ||
| transform: translate(50%, 120%); | ||
| max-width: calc(100% - (var(--guide-width) + var(--sidebar-width) + var(--space-default))); | ||
| min-width: 180px; | ||
| min-height: 20px; | ||
| border-radius: 2px; | ||
| padding: var(--space-half); | ||
| box-shadow: 0px 1px 5px rgba(0, 0, 0, 0.2); | ||
| transition: transform ease-out 0.2s; | ||
| z-index: 1; | ||
| -webkit-backdrop-filter: blur(28px); | ||
| backdrop-filter: blur(28px); | ||
| margin: 0; | ||
| } | ||
|
|
||
| .active { | ||
| transform: translate(50%, 0%); | ||
| .toastItem { | ||
| animation: toast-in 360ms ease-out; | ||
| margin: 0; | ||
| } | ||
|
|
||
| .active.shifted { | ||
| right: calc(var(--space-half) + 350px); | ||
| .viewport { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: var(--space-half); | ||
| left: 50%; | ||
| max-width: min(560px, calc(100% - var(--space-default) * 2)); | ||
| pointer-events: none; | ||
| position: fixed; | ||
| top: calc(var(--space-default) + env(safe-area-inset-top, 0)); | ||
| transform: translateX(-50%); | ||
| width: 100%; | ||
| z-index: 4005; | ||
| } | ||
|
|
||
| .info { | ||
| background-color: var(--color-darkblue); | ||
| .viewport > * { | ||
| pointer-events: auto; | ||
| } | ||
|
|
||
| .success { | ||
| background-color: var(--color-success); | ||
| .container { | ||
| align-items: flex-start; | ||
| display: flex; | ||
| width: 100%; | ||
| } | ||
|
|
||
| .warning { | ||
| background-color: var(--color-softred); | ||
| .content { | ||
| margin-right: var(--space-eight); | ||
| width: 100%; | ||
| } | ||
|
|
||
| .toast p { | ||
| margin: 0; | ||
| color: var(--color-alt); | ||
| .icon { | ||
| display: flex; | ||
| margin-right: var(--space-quarter); | ||
| } | ||
|
|
||
| @media (max-width: 768px) { | ||
| .toast { | ||
| position: initial; | ||
| transition: none; | ||
| transform: none; | ||
| } | ||
| } | ||
| .closeButton { | ||
| background-color: transparent; | ||
| border: none; | ||
| margin-left: auto; | ||
| padding: 0; | ||
| } | ||
|
|
||
| .closeButton img { | ||
| height: var(--size-default); | ||
| margin-right: 0; | ||
| width: var(--size-default); | ||
| } | ||
|
|
||
| @keyframes toast-in { | ||
| from { | ||
| transform: translateY(-8px); | ||
| } | ||
| to { | ||
| transform: translateY(0); | ||
| } | ||
| } | ||
|
|
||
| @media (prefers-reduced-motion: reduce) { | ||
| .toastItem, | ||
| .closing { | ||
| animation: none; | ||
| transition: none; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { useCallback, useEffect, useRef } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { getTransactionList, TAccount } from '@/api/account'; | ||
| import { syncdone } from '@/api/accountsync'; | ||
| import { syncNewTxs } from '@/api/transactions'; | ||
| import { notifyUser } from '@/api/system'; | ||
| import { ArrowFloorDownBlue } from '@/components/icon'; | ||
| import { useToast } from '@/contexts/toast-context'; | ||
|
|
||
| type TIncomingTransactionNotifierProps = { | ||
| activeAccounts: TAccount[]; | ||
| }; | ||
|
|
||
| export const IncomingTransactionNotifier = ({ activeAccounts }: TIncomingTransactionNotifierProps) => { | ||
| const { t } = useTranslation(); | ||
| const { showToast } = useToast(); | ||
| const shownIncomingTxIDsRef = useRef<Record<string, boolean>>({}); | ||
| const initializedAccountsRef = useRef<Record<string, boolean>>({}); | ||
| const seedingAccountsRef = useRef<Record<string, boolean>>({}); | ||
|
|
||
| const markIncomingAsSeen = useCallback((accountCode: string, txID: string) => { | ||
| shownIncomingTxIDsRef.current[`${accountCode}:${txID}`] = true; | ||
| }, []); | ||
|
|
||
| const showIncomingTxToast = useCallback((amount: string, unit: string) => { | ||
| showToast({ | ||
| icon: <ArrowFloorDownBlue />, | ||
| message: t('notification.incomingTxToast', { | ||
| amount, | ||
| unit, | ||
| }), | ||
| type: 'info', | ||
| }); | ||
| }, [showToast, t]); | ||
|
|
||
| const seedIncomingTransactionsForAccount = useCallback(async (account: TAccount) => { | ||
| if (initializedAccountsRef.current[account.code] || seedingAccountsRef.current[account.code]) { | ||
| return; | ||
| } | ||
|
|
||
| seedingAccountsRef.current[account.code] = true; | ||
| try { | ||
| const transactions = await getTransactionList(account.code); | ||
| if (!transactions.success) { | ||
| return; | ||
| } | ||
| transactions.list | ||
| .filter(tx => tx.type === 'receive') | ||
| .forEach(tx => markIncomingAsSeen(account.code, tx.internalID)); | ||
| initializedAccountsRef.current[account.code] = true; | ||
| } finally { | ||
| seedingAccountsRef.current[account.code] = false; | ||
| } | ||
| }, [markIncomingAsSeen]); | ||
|
|
||
| // Seed known incoming transactions for active accounts so we only toast truly new ones. | ||
| useEffect(() => { | ||
| void Promise.all(activeAccounts.map(account => seedIncomingTransactionsForAccount(account))); | ||
| }, [activeAccounts, seedIncomingTransactionsForAccount]); | ||
|
|
||
| useEffect(() => { | ||
| return syncNewTxs((meta) => { | ||
| notifyUser(t('notification.newTxs', { | ||
| count: meta.count, | ||
| accountName: meta.accountName, | ||
| })); | ||
| }); | ||
| }, [t]); | ||
|
|
||
| const detectAndToastIncomingForAccount = useCallback(async (account: TAccount) => { | ||
| if (!initializedAccountsRef.current[account.code]) { | ||
| return; | ||
| } | ||
|
|
||
| const transactions = await getTransactionList(account.code); | ||
| if (!transactions.success) { | ||
| return; | ||
| } | ||
|
|
||
| // New transactions are sorted to the front. Check only the latest window. | ||
| const recentTransactions = transactions.list.slice(0, 30); | ||
| recentTransactions | ||
| .filter(tx => tx.type === 'receive') | ||
| .reverse() | ||
| .forEach((tx) => { | ||
| const txKey = `${account.code}:${tx.internalID}`; | ||
| if (shownIncomingTxIDsRef.current[txKey]) { | ||
| return; | ||
| } | ||
| markIncomingAsSeen(account.code, tx.internalID); | ||
| showIncomingTxToast(tx.amount.amount, tx.amount.unit); | ||
| }); | ||
| }, [markIncomingAsSeen, showIncomingTxToast]); | ||
|
|
||
| useEffect(() => { | ||
| const unsubscribers = activeAccounts.map((account) => { | ||
| return syncdone(account.code, () => { | ||
| void (async () => { | ||
| await seedIncomingTransactionsForAccount(account); | ||
| await detectAndToastIncomingForAccount(account); | ||
| })(); | ||
| }); | ||
| }); | ||
| return () => { | ||
| unsubscribers.forEach(unsubscribe => unsubscribe()); | ||
| }; | ||
| }, [activeAccounts, detectAndToastIncomingForAccount, seedIncomingTransactionsForAccount]); | ||
|
|
||
| return null; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { ReactNode } from 'react'; | ||
| import { CloseXDark, CloseXWhite } from '@/components/icon'; | ||
| import { Message } from '@/components/message/message'; | ||
| import { useDarkmode } from '@/hooks/darkmode'; | ||
| import { TMessageTypes } from '@/utils/types'; | ||
| import style from './Toast.module.css'; | ||
|
|
||
| type TToastProps = { | ||
| type?: TMessageTypes; | ||
| // Deprecated prop kept for compatibility with the existing callsites. | ||
| theme?: TMessageTypes; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the old Toast is only used in backups.tsx. Please remove the deprecated theme prop here and update backups.tsx to use the new toast with type prop. |
||
| icon?: ReactNode; | ||
| className?: string; | ||
| onClose?: () => void; | ||
| children: ReactNode; | ||
| }; | ||
|
|
||
| export const Toast = ({ type, theme, icon, className = '', onClose, children }: TToastProps) => { | ||
| const resolvedType = type || theme || 'info'; | ||
| const { isDarkMode } = useDarkmode(); | ||
| const iconWithSpacing = icon ? <span className={style.icon}>{icon}</span> : undefined; | ||
| return ( | ||
| <Message icon={iconWithSpacing} type={resolvedType} className={`${style.toast || ''} ${className || ''}`.trim()}> | ||
| <div className={style.container}> | ||
| <div className={style.content}>{children}</div> | ||
| <button | ||
| aria-label="Close toast" | ||
| className={style.closeButton} | ||
| hidden={!onClose} | ||
| onClick={onClose} | ||
| type="button"> | ||
| {isDarkMode ? <CloseXWhite /> : <CloseXDark />} | ||
| </button> | ||
| </div> | ||
| </Message> | ||
| ); | ||
| }; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move this to components/toast/toast.tsx |
||
Uh oh!
There was an error while loading. Please reload this page.