Skip to content

Commit

Permalink
chore: change opening method for welcome page
Browse files Browse the repository at this point in the history
  • Loading branch information
AricRedemption committed Apr 19, 2024
1 parent ac4cc56 commit 44462f6
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 37 deletions.
8 changes: 8 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ export const goToPage = (path: string, created = false) => {
}
}

export const goToTab = (path: string, created = false) => {
if (IS_DEV || !created) {
router.push(path)
} else {
window.open(`${window.location.href.split('#')[0]}#${path}`, '_blank')
}
}

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
44 changes: 41 additions & 3 deletions src/lib/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { goToPage } from './utils'
import { V3Wallet } from './types'
import useStorage from '@/lib/storage'
import { getCurrentAccountId } from './account'
import { type V3Wallet } from './types'
import { toast } from '@/components/ui/toast'
import { getCurrentAccountId } from './account'

const CURRENT_WALLET_ID = 'currentWalletId'
const V3_WALLETS_STORAGE_KEY = 'wallets_v3'
Expand Down Expand Up @@ -61,7 +61,7 @@ export async function getV3CurrentWallet() {
return wallet
}

export async function getActiveWalletAccount() {
export async function getActiveWalletOnlyAccount() {
const walletId = await getCurrentWalletId()
if (!walletId) {
throw new Error('current wallet id not found')
Expand Down Expand Up @@ -91,6 +91,44 @@ export async function getActiveWalletAccount() {
return wallet
}

export async function getInactiveWallets() {
const currentWalletId = await getCurrentWalletId()
if (!currentWalletId) {
throw new Error('Current wallet id not found.')
}
const wallets = await getV3Wallets()
if (!wallets.length) {
throw new Error('No wallets found. Please create a wallet first.')
}
return wallets.filter((wallet) => wallet.id !== currentWalletId)
}

export async function getActiveWalletOtherAccounts() {
const walletId = await getCurrentWalletId()
if (!walletId) {
throw new Error('current wallet id not found')
}
const wallets = await getV3Wallets()
if (!wallets.length) {
throw new Error('wallets not found')
}
const wallet = wallets.find((wallet) => wallet.id === walletId)
if (!wallet) {
throw new Error('wallet not found')
}

const { accounts } = wallet
if (!accounts || !accounts.length) {
throw new Error('wallet does not have any accounts')
}
const currentAccountId = await getCurrentAccountId()
if (!currentAccountId) {
throw new Error('current account id not found')
}
wallet.accounts = accounts.filter((account) => account.id !== currentAccountId)
return wallet
}

export async function getV3CurrentAccount() {
const walletId = await getCurrentWalletId()
const wallets = await getV3Wallets()
Expand Down
4 changes: 2 additions & 2 deletions src/pages/manage/Index.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script setup lang="ts">
import { ref } from 'vue'
import { goToPage } from '@/lib/utils'
import { EditName } from '@/components'
import { type V3Wallet } from '@/lib/types'
import Avatar from '@/components/Avatar.vue'
import { totalBalance } from '@/lib/balance'
import { toast } from '@/components/ui/toast'
import AddIcon from '@/assets/icons-v3/add.svg'
import { goToTab, goToPage } from '@/lib/utils'
import { WalletsStore } from '@/stores/WalletStore'
import PencilIcon from '@/assets/icons-v3/pencil.svg'
import { FlexBox, Divider, Button } from '@/components'
Expand Down Expand Up @@ -145,7 +145,7 @@ const updataAccountName = (walletId: string, accountId: string, accountName: str
</FlexBox>
<Button
type="primary"
@click="goToPage('/welcome', true)"
@click="goToTab('/welcome', true)"
class="py-6 absolute bottom-6 w-61.5 left-1/2 -translate-x-1/2"
>
Add Wallet
Expand Down
6 changes: 3 additions & 3 deletions src/pages/wallet/Asset.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ import FilterIcon from '@/assets/icons-v3/filter.svg'
import ToggleIcon from '@/assets/icons-v3/toggle.svg'
import ArrowUpIcon from '@/assets/icons-v3/arrow-up.svg'
import SelectorIcon from '@/assets/icons-v3/selector.svg'
import { setV3AddressTypeStorage } from '@/lib/addressType'
import { getTags, BTCAsset, MVCAsset } from '@/data/assets'
import ArrowDownIcon from '@/assets/icons-v3/arrow-down.svg'
import ArrowLeftIcon from '@/assets/icons-v3/arrow-left.svg'
import { AddressType, Chain } from '@metalet/utxo-wallet-service'
import { useChainWalletsStore } from '@/stores/ChainWalletsStore'
import SuccessCheckedIcon from '@/assets/icons-v3/success-checked.svg'
import { useExchangeRatesQuery, CoinCategory } from '@/queries/exchange-rates'
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu'
Expand All @@ -30,9 +33,6 @@ import {
DrawerTitle,
DrawerDescription,
} from '@/components/ui/drawer'
import { setV3AddressTypeStorage } from '@/lib/addressType'
import { AddressType, Chain } from '@metalet/utxo-wallet-service'
import { useChainWalletsStore } from '@/stores/ChainWalletsStore'
const isOpen = ref(false)
const route = useRoute()
Expand Down
8 changes: 4 additions & 4 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import useStorage from './lib/storage'
import { goToPage } from '@/lib/utils'
import { goToPage, goToTab } from '@/lib/utils'
import { IS_DEV } from '@/data/config'
import * as VueRouter from 'vue-router'
import { assetList } from '@/lib/balance'
import { needMigrate } from './lib/migrate'
import Wallet from './pages/wallet/Index.vue'
import { getCurrentAccountId } from './lib/account'
import { getCurrentWalletId, getV3Wallets, hasWallets } from './lib/wallet'
import { needMigrate } from './lib/migrate'
import { getCurrentWalletId, hasWallets } from './lib/wallet'

const storage = useStorage()

Expand Down Expand Up @@ -475,7 +475,7 @@ router.beforeEach(async (to, _, next) => {
if (await hasWallets()) {
next('/manage/wallets')
} else {
goToPage('/welcome', true)
goToTab('/welcome', true)
next('/welcome')
}
} else {
Expand Down
44 changes: 19 additions & 25 deletions src/stores/WalletStore.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import { reactive } from 'vue'
import { getNet } from '@/lib/network'
import { goToPage } from '@/lib/utils'
import { toast } from '@/components/ui/toast'
import { goToPage, goToTab } from '@/lib/utils'
import { getCurrentAccountId } from '@/lib/account'
import { getV3AddressTypeStorage } from '@/lib/addressType'
import { WalletManager } from '@metalet/utxo-wallet-service'
import { Chain, type Net } from '@metalet/utxo-wallet-service'
import { getV3Wallets, getActiveWalletAccount, getCurrentWalletId, hasV3Wallets } from '@/lib/wallet'
import {
hasV3Wallets,
getCurrentWalletId,
getInactiveWallets,
getActiveWalletOnlyAccount,
getActiveWalletOtherAccounts,
} from '@/lib/wallet'

let walletManager: WalletManager | null = null

const initWalletManager = async (): Promise<WalletManager> => {
try {
const activeWallet = await getActiveWalletAccount()
const activeWallet = await getActiveWalletOnlyAccount()
const walletsOptions = [
{
id: activeWallet.id,
mnemonic: activeWallet.mnemonic,
name: activeWallet.name,
mvcTypes: activeWallet.mvcTypes,
accountsOptions: activeWallet.accounts.map(({ id, name, addressIndex }) => ({
id,
name,
addressIndex,
})),
accountsOptions: activeWallet.accounts.map(({ id, name, addressIndex }) => ({ id, name, addressIndex })),
},
]
const network = getNet() as Net
Expand All @@ -35,7 +37,7 @@ const initWalletManager = async (): Promise<WalletManager> => {
if (await hasV3Wallets()) {
goToPage('/manage/wallets')
} else {
goToPage('/welcome', true)
goToTab('/welcome', true)
}
throw err
}
Expand All @@ -54,38 +56,30 @@ const getWalletManager = async (): Promise<WalletManager> => {

const loadOtherAccounts = async () => {
const manager = await getWalletManager()
const wallets = await getV3Wallets()
const activeWallet = await getActiveWalletAccount()
const currentAccountId = await getCurrentAccountId()
if (!activeWallet || !currentAccountId) {
const wallets = await getV3Wallets()
if (wallets.length) {
const activeWallet = await getActiveWalletOtherAccounts()
if (!activeWallet) {
if (await hasV3Wallets()) {
goToPage('/manage/wallets')
} else {
goToPage('/welcome', true)
goToTab('/welcome', true)
}
return
}
const accounts = activeWallet.accounts.filter((account) => account.id !== currentAccountId)
for (let account of accounts) {
for (let account of activeWallet.accounts) {
manager.addAccount(activeWallet.id, {
id: account.id,
name: account.name,
addressIndex: account.addressIndex,
})
}
const otherWallets = wallets.filter((wallet) => wallet.id !== activeWallet.id)
for (let wallet of otherWallets) {
const inactiveWallets = await getInactiveWallets()
for (let wallet of inactiveWallets) {
manager.addWallet({
id: wallet.id,
name: wallet.name,
mnemonic: wallet.mnemonic,
mvcTypes: wallet.mvcTypes,
accountsOptions: wallet.accounts.map((account) => ({
id: account.id,
name: account.name,
addressIndex: account.addressIndex,
})),
accountsOptions: wallet.accounts.map(({ id, name, addressIndex }) => ({ id, name, addressIndex })),
})
}
}
Expand Down

0 comments on commit 44462f6

Please sign in to comment.