Skip to content

Commit

Permalink
feat(wip): transaction manager v2
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Sep 18, 2024
1 parent bcd6140 commit 00085a5
Show file tree
Hide file tree
Showing 150 changed files with 13,729 additions and 62 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@
{
"selector": "typeLike",
"format": ["PascalCase"]
},
{
"selector": "typeParameter",
"format": ["PascalCase", "camelCase"]
}
],
"@typescript-eslint/no-redeclare": "off",
"radix": "off",
"consistent-return": "off",
"jsx-a11y/anchor-is-valid": "off",
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/knip.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Knip

on: [push]
on: []

jobs:
knip:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pages-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
NEXT_PUBLIC_INTERCOM_ID: re9q5yti

on: [push]
on: []
jobs:
yalc_check:
runs-on: ubuntu-latest
Expand Down
35 changes: 33 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Test

on: [push]
on: []

env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
Expand Down Expand Up @@ -97,7 +97,38 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
shard: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
shard:
[
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
]
steps:
- uses: actions/checkout@v3

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"i18next-browser-languagedetector": "^6.1.5",
"i18next-http-backend": "^1.4.1",
"idb-keyval": "^6.2.1",
"immer": "^9.0.15",
"immer": "^9.0.21",
"intl-segmenter-polyfill": "^0.4.4",
"iso-639-1": "^2.1.15",
"lodash": "^4.17.21",
Expand All @@ -97,7 +97,8 @@
"ts-pattern": "^4.2.2",
"use-immer": "^0.7.0",
"viem": "2.19.4",
"wagmi": "2.12.4"
"wagmi": "2.12.4",
"zustand": "5.0.0-rc.2"
},
"peerDependencies": {
"react": "*",
Expand Down
30 changes: 29 additions & 1 deletion pnpm-lock.yaml

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

137 changes: 137 additions & 0 deletions src/components/Notifications2.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { usePreviousDistinct } from 'react-use'
import styled, { css } from 'styled-components'

import { Button, Toast } from '@ensdomains/thorin'

import { useTransactionStore } from '@app/transaction-flow/new/TransactionStore'
import type { LastTransactionChange } from '@app/transaction/types'
import { useBreakpoint } from '@app/utils/BreakpointProvider'
import { getChainName } from '@app/utils/getChainName'
import { wagmiConfig } from '@app/utils/query/wagmi'
import { makeEtherscanLink } from '@app/utils/utils'

const ButtonContainer = styled.div(
({ theme }) => css`
display: flex;
flex-direction: row;
align-items: center;
justify-content: stretch;
gap: ${theme.space['2']};
`,
)

type SuccessOrRevertedTransaction = Extract<
LastTransactionChange,
{ status: 'success' | 'reverted' }
>

const Notification = ({
transaction,
onClose,
open,
}: {
transaction: SuccessOrRevertedTransaction | null
onClose: () => void
open: boolean
}) => {
const { t } = useTranslation()
const breakpoints = useBreakpoint()
const getResumable = useTransactionStore((s) => s.flow.getResumable)
const resumeFlow = useTransactionStore((s) => s.flow.resume)

const resumable = transaction && getResumable(transaction.flowKey)
const chainName = transaction && getChainName(wagmiConfig, { chainId: transaction.chainId })

const button = (() => {
if (!transaction) return null
if (!resumable)
return (
<a
target="_blank"
href={makeEtherscanLink(transaction.currentHash, chainName!)}
rel="noreferrer"
>
<Button size="small" colorStyle="accentSecondary">
{t('transaction.viewEtherscan')}
</Button>
</a>
)

return (
<ButtonContainer>
<a
target="_blank"
href={makeEtherscanLink(transaction.currentHash, chainName!)}
rel="noreferrer"
>
<Button size="small" colorStyle="accentSecondary">
{t('transaction.viewEtherscan')}
</Button>
</a>
<Button
size="small"
data-testid="notification-continue-button"
onClick={() => resumeFlow(transaction.flowKey)}
>
{t('action.continue')}
</Button>
</ButtonContainer>
)
})()

const toastProps = transaction
? {
title: t(`transaction.status.${transaction.status}.notifyTitle`),
description: t(`transaction.status.${transaction.status}.notifyMessage`, {
action: t(`transaction.description.${transaction.action}`),
}),
children: button,
}
: {
title: '',
}

return (
<Toast
onClose={onClose}
open={open}
variant={breakpoints.sm ? 'desktop' : 'touch'}
{...toastProps}
/>
)
}

export const Notifications = () => {
const [open, setOpen] = useState(false)
const [transactionQueue, setTransactionQueue] = useState<SuccessOrRevertedTransaction[]>([])
const lastTransaction = useTransactionStore((s) => {
const tx = s.transaction.getLastTransactionChange()
if (!tx) return null
if (tx.status !== 'success' && tx.status !== 'reverted') return null
return tx
})

const prevLastTransaction = usePreviousDistinct(lastTransaction)

if (lastTransaction && prevLastTransaction !== lastTransaction) {
setTransactionQueue((q) => [...q, lastTransaction])
}

const currentTransaction = transactionQueue[0] ?? null

return (
<Notification
onClose={() => {
setOpen(false)
setTimeout(
() => setTransactionQueue((prev) => [...prev.filter((x) => x !== currentTransaction)]),
300,
)
}}
open={open}
transaction={currentTransaction}
/>
)
}
17 changes: 8 additions & 9 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { createGlobalStyle, keyframes, ThemeProvider } from 'styled-components'

import { ThorinGlobalStyles, lightTheme as thorinLightTheme } from '@ensdomains/thorin'

import { Notifications } from '@app/components/Notifications'
import { Notifications } from '@app/components/Notifications2'
import { TestnetWarning } from '@app/components/TestnetWarning'
import { TransactionStoreProvider } from '@app/hooks/transactions/TransactionStoreContext'
import { Basic } from '@app/layouts/Basic'
import { TransactionFlowProvider } from '@app/transaction-flow/TransactionFlowProvider'
import { TransactionDialogManager } from '@app/transaction/components/TransactionDialogManager'
import { setupAnalytics } from '@app/utils/analytics'
import { BreakpointProvider } from '@app/utils/BreakpointProvider'
import { QueryProviders } from '@app/utils/query/providers'
Expand Down Expand Up @@ -152,13 +152,12 @@ function MyApp({ Component, pageProps }: AppPropsWithLayout) {
<GlobalStyle />
<ThorinGlobalStyles />
<SyncProvider>
<TransactionFlowProvider>
<SyncDroppedTransaction>
<Notifications />
<TestnetWarning />
<Basic>{getLayout(<Component {...pageProps} />)}</Basic>
</SyncDroppedTransaction>
</TransactionFlowProvider>
<SyncDroppedTransaction>
<Notifications />
<TransactionDialogManager />
<TestnetWarning />
<Basic>{getLayout(<Component {...pageProps} />)}</Basic>
</SyncDroppedTransaction>
</SyncProvider>
</IntercomProvider>
</BreakpointProvider>
Expand Down
30 changes: 15 additions & 15 deletions src/transaction-flow/transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,37 +65,37 @@ export const transactions = {
export type Transaction = typeof transactions
export type TransactionName = keyof Transaction

export type TransactionParameters<T extends TransactionName> = Parameters<
Transaction[T]['transaction']
export type TransactionParameters<name extends TransactionName> = Parameters<
Transaction[name]['transaction']
>[0]

export type TransactionData<T extends TransactionName> = TransactionParameters<T>['data']
export type TransactionData<name extends TransactionName> = TransactionParameters<name>['data']

export type TransactionReturnType<T extends TransactionName> = ReturnType<
Transaction[T]['transaction']
export type TransactionReturnType<name extends TransactionName> = ReturnType<
Transaction[name]['transaction']
>

export const createTransactionItem = <T extends TransactionName>(
name: T,
data: TransactionData<T>,
export const createTransactionItem = <name extends TransactionName>(
name: name,
data: TransactionData<name>,
) => ({
name,
data,
})

export const createTransactionRequest = <TName extends TransactionName>({
export const createTransactionRequest = <name extends TransactionName>({
name,
...rest
}: { name: TName } & TransactionParameters<TName>): TransactionReturnType<TName> => {
}: { name: name } & TransactionParameters<name>): TransactionReturnType<name> => {
// i think this has to be any :(
return transactions[name].transaction({ ...rest } as any) as TransactionReturnType<TName>
return transactions[name].transaction({ ...rest } as any) as TransactionReturnType<name>
}

export type TransactionItem<TName extends TransactionName = TransactionName> = {
name: TName
data: TransactionData<TName>
export type TransactionItem<name extends TransactionName = TransactionName> = {
name: name
data: TransactionData<name>
}

export type TransactionItemUnion = {
[TName in TransactionName]: TransactionItem<TName>
[name in TransactionName]: TransactionItem<name>
}[TransactionName]
Loading

0 comments on commit 00085a5

Please sign in to comment.