Skip to content

Commit

Permalink
Merge pull request #31 from CityOfZion/CU-86dteamh6
Browse files Browse the repository at this point in the history
CU-86dteamh6 - Neon3 - In migration import contacts
  • Loading branch information
yumiuehara authored May 8, 2024
2 parents a0d4cf9 + f22bfca commit 9b889c8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
14 changes: 10 additions & 4 deletions src/renderer/src/hooks/useBackupOrMigrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import zod from 'zod'

import { useModalNavigate } from './useModalRouter'

export type TMigrateAccountSchema = zod.infer<typeof migrateWalletsSchema>
export type TMigrateWalletsSchema = zod.infer<typeof migrateWalletsSchema>
export type TMigrateSchema = zod.infer<typeof migrateSchema>

type TFile = {
path: string
content: string | TMigrateAccountSchema[]
content: string | TMigrateSchema
}

const migrateWalletsSchema = zod.object({
Expand All @@ -24,6 +25,11 @@ const migrateWalletsSchema = zod.object({
extra: zod.any(),
})

const migrateSchema = zod.object({
accounts: zod.array(migrateWalletsSchema),
contacts: zod.array(zod.object({ name: zod.string(), addresses: zod.array(zod.string()) })),
})

export const useBackupOrMigrate = () => {
const { t } = useTranslation('hooks', { keyPrefix: 'useBackupOrMigrate' })
const [file, setFile] = useState<TFile>()
Expand All @@ -48,7 +54,7 @@ export const useBackupOrMigrate = () => {

try {
const parsedContent = JSON.parse(fileContent)
const validatedContent = await migrateWalletsSchema.array().parseAsync(parsedContent)
const validatedContent = await migrateSchema.parseAsync(parsedContent)
ToastHelper.success({ message: t('neon2MigrateFileDetected') })
setFile({ path: filePath, content: validatedContent })
setHasError(false)
Expand All @@ -70,7 +76,7 @@ export const useBackupOrMigrate = () => {
}

navigate('/app/settings/security/migrate-accounts')
modalNavigate('migrate-accounts-step-3', { state: { content: file.content as TMigrateAccountSchema[] } })
modalNavigate('migrate-accounts-step-3', { state: { content: file.content } })
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { Button } from '@renderer/components/Button'
import { Checkbox } from '@renderer/components/Checkbox'
import { Separator } from '@renderer/components/Separator'
import { useAccountUtils } from '@renderer/hooks/useAccountSelector'
import { TMigrateAccountSchema } from '@renderer/hooks/useBackupOrMigrate'
import { TMigrateSchema, TMigrateWalletsSchema } from '@renderer/hooks/useBackupOrMigrate'
import { useModalNavigate, useModalState } from '@renderer/hooks/useModalRouter'
import { MigrateAccountsModalLayout } from '@renderer/layouts/MigrateAccountsModalLayout'

type TState = {
content: TMigrateAccountSchema[]
content: TMigrateSchema
}

export const MigrateAccountsStep3Modal = () => {
Expand All @@ -19,9 +19,9 @@ export const MigrateAccountsStep3Modal = () => {
const { modalNavigateWrapper } = useModalNavigate()
const { doesAccountExist } = useAccountUtils()

const [selectedAccountsToMigrate, setSelectedAccountsToMigrate] = useState<TMigrateAccountSchema[]>([])
const [selectedAccountsToMigrate, setSelectedAccountsToMigrate] = useState<TMigrateWalletsSchema[]>([])

const handleSelect = (wallet: TMigrateAccountSchema) => {
const handleSelect = (wallet: TMigrateWalletsSchema) => {
setSelectedAccountsToMigrate(prev => {
const index = prev.findIndex(prevWallet => prevWallet.address === wallet.address)

Expand All @@ -34,7 +34,7 @@ export const MigrateAccountsStep3Modal = () => {
}

const handleSelectAll = () => {
const filteredContent = content.filter(wallet => !doesAccountExist(wallet.address))
const filteredContent = content.accounts.filter(wallet => !doesAccountExist(wallet.address))
setSelectedAccountsToMigrate(filteredContent)
}

Expand All @@ -49,7 +49,7 @@ export const MigrateAccountsStep3Modal = () => {
</div>

<div className="w-full flex-grow flex flex-col overflow-y-auto min-h-0 mt-1 pr-2">
{content.map((wallet, index) => {
{content.accounts.map((wallet, index) => {
const isAccountExist = doesAccountExist(wallet.address)

return (
Expand All @@ -73,22 +73,22 @@ export const MigrateAccountsStep3Modal = () => {
/>
</div>

{index < content.length - 1 && <Separator />}
{index < content.accounts.length - 1 && <Separator />}
</Fragment>
)
})}
</div>

<span className="text-center text-blue my-3.5">
{t('selectedQuantity', { selected: selectedAccountsToMigrate.length, total: content.length })}
{t('selectedQuantity', { selected: selectedAccountsToMigrate.length, total: content.accounts.length })}
</span>

<Button
label={t('buttonLabel')}
flat
className="px-16"
disabled={selectedAccountsToMigrate.length <= 0}
onClick={modalNavigateWrapper('migrate-accounts-step-4', { state: { selectedAccountsToMigrate } })}
onClick={modalNavigateWrapper('migrate-accounts-step-4', { state: { selectedAccountsToMigrate, content } })}
/>
</MigrateAccountsModalLayout>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ import { Fragment } from 'react'
import { useTranslation } from 'react-i18next'
import { MdLooks4 } from 'react-icons/md'
import { TbPackageImport } from 'react-icons/tb'
import { useDispatch } from 'react-redux'
import { TContactAddress } from '@renderer/@types/store'
import { Button } from '@renderer/components/Button'
import { Separator } from '@renderer/components/Separator'
import { ToastHelper } from '@renderer/helpers/ToastHelper'
import { UtilsHelper } from '@renderer/helpers/UtilsHelper'
import { useActions } from '@renderer/hooks/useActions'
import { TMigrateAccountSchema } from '@renderer/hooks/useBackupOrMigrate'
import { TMigrateSchema, TMigrateWalletsSchema } from '@renderer/hooks/useBackupOrMigrate'
import { useBlockchainActions } from '@renderer/hooks/useBlockchainActions'
import { useModalNavigate, useModalState } from '@renderer/hooks/useModalRouter'
import { MigrateAccountsModalLayout } from '@renderer/layouts/MigrateAccountsModalLayout'
import { bsAggregator } from '@renderer/libs/blockchainService'
import { contactReducerActions } from '@renderer/store/reducers/ContactReducer'

import { DecryptAccountPasswordContainer, TMigrateDecryptedAccountSchema } from './DecryptAccountPasswordContainer'
import { SuccessContent } from './SuccessContent'

type TState = {
selectedAccountsToMigrate: TMigrateAccountSchema[]
selectedAccountsToMigrate: TMigrateWalletsSchema[]
content: TMigrateSchema
}

type TActionData = {
Expand All @@ -25,9 +31,10 @@ type TActionData = {
export const MigrateAccountsStep4Modal = () => {
const { t } = useTranslation('modals', { keyPrefix: 'migrateWallets' })
const { t: commonT } = useTranslation('common', { keyPrefix: 'wallet' })
const { selectedAccountsToMigrate } = useModalState<TState>()
const { selectedAccountsToMigrate, content } = useModalState<TState>()
const { createWallet, importAccounts } = useBlockchainActions()
const { modalNavigate } = useModalNavigate()
const dispatch = useDispatch()

const { actionData, actionState, setData, handleAct } = useActions<TActionData>({
decryptedAccounts: [],
Expand Down Expand Up @@ -55,6 +62,27 @@ export const MigrateAccountsStep4Modal = () => {
})),
})

content.contacts.map(contact => {
const contactAddresses: TContactAddress[] = []

contact.addresses.forEach(address => {
const blockchain = bsAggregator.getBlockchainNameByAddress(address)
if (!blockchain) return

contactAddresses.push({ address, blockchain })
})

if (!contactAddresses.length) return

dispatch(
contactReducerActions.saveContact({
name: contact.name,
id: UtilsHelper.uuid(),
addresses: contactAddresses,
})
)
})

modalNavigate(-3)
modalNavigate('success', {
state: {
Expand Down

0 comments on commit 9b889c8

Please sign in to comment.