-
Notifications
You must be signed in to change notification settings - Fork 47
add new wallet #696
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
add new wallet #696
Changes from 12 commits
8fb8af9
c7e574a
070f633
d8882b2
d09c91f
d06e764
3a077e8
51b576c
57625b8
6f87870
b41e11d
61a859d
567d80a
481b13d
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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@status-im/wallet": patch | ||
| "wallet": patch | ||
| --- | ||
|
|
||
| add new wallet |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| import { useMutation } from '@tanstack/react-query' | ||
| import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
|
|
||
| import { useAPI } from '../providers/api-client' | ||
|
|
||
| export const useImportWallet = () => { | ||
| const api = useAPI() | ||
| const queryClient = useQueryClient() | ||
|
|
||
| const { mutate, mutateAsync, ...result } = useMutation({ | ||
| mutationKey: ['import-wallet'], | ||
|
|
@@ -20,6 +21,9 @@ export const useImportWallet = () => { | |
| name: 'Imported Wallet', | ||
| }) | ||
| }, | ||
| onSuccess: () => { | ||
| queryClient.invalidateQueries({ queryKey: ['wallets'] }) | ||
|
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. Shouldn't have distinct key from "create"?
Contributor
Author
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. No, it's invalidating query which has queryKey
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. Doesn't |
||
| }, | ||
| }) | ||
|
|
||
| return { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { useEffect, useState } from 'react' | ||
|
|
||
| export const usePinExtension = () => { | ||
| const [isPinExtension, setIsPinExtension] = useState<boolean>(false) | ||
|
|
||
| const handleClose = async () => { | ||
| setIsPinExtension(false) | ||
| await chrome.storage.local.set({ pinExtension: true }) | ||
| } | ||
|
|
||
| useEffect(() => { | ||
| async function checkSettings() { | ||
| const storage = await chrome.storage.local.get(['pinExtension']) | ||
| if (storage.pinExtension) { | ||
| setIsPinExtension(false) | ||
| return | ||
| } | ||
|
|
||
| const settings = await chrome.action.getUserSettings() | ||
| setIsPinExtension(!settings.isOnToolbar) | ||
| } | ||
|
|
||
| checkSettings() | ||
| }, []) | ||
|
|
||
| return { isPinExtension, handleClose } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { createContext, useContext, useEffect, useState } from 'react' | ||
|
|
||
| import { useQuery } from '@tanstack/react-query' | ||
|
|
||
| import { apiClient } from './api-client' | ||
|
|
||
| import type { KeyStore } from '@trustwallet/wallet-core' | ||
|
|
||
| type Wallet = KeyStore.Wallet | ||
|
|
||
| type WalletContext = { | ||
| currentWallet: Wallet | null | ||
| wallets: Wallet[] | ||
| isLoading: boolean | ||
| hasWallets: boolean | ||
| setCurrentWallet: (id: Wallet['id']) => void | ||
| } | ||
|
|
||
| const WalletContext = createContext<WalletContext | undefined>(undefined) | ||
|
|
||
| export function useWallet() { | ||
| const context = useContext(WalletContext) | ||
| if (!context) { | ||
| throw new Error('useWallet must be used within WalletProvider') | ||
| } | ||
| return context | ||
| } | ||
|
|
||
| export function WalletProvider({ children }: { children: React.ReactNode }) { | ||
| const [selectedWalletId, setSelectedWalletId] = useState<string | null>(null) | ||
|
|
||
| const { data: wallets = [], isLoading } = useQuery({ | ||
| queryKey: ['wallets'], | ||
| queryFn: () => apiClient.wallet.all.query(), | ||
| staleTime: 5 * 60 * 1000, // 5 minutes | ||
| }) | ||
|
|
||
| const hasWallets = wallets.length > 0 | ||
|
|
||
| const currentWallet = useMemo(() => { | ||
| if (!hasWallets) return null | ||
|
|
||
| if (selectedWalletId) { | ||
| const selectedWallet = wallets.find( | ||
| wallet => wallet.id === selectedWalletId, | ||
| ) | ||
| if (selectedWallet) return selectedWallet | ||
| } | ||
|
|
||
| return wallets[0] || null | ||
| }, [hasWallets, selectedWalletId, wallets]) | ||
|
|
||
| useEffect(() => { | ||
| if (hasWallets && !selectedWalletId && wallets[0]) { | ||
| setSelectedWalletId(wallets[0].id) | ||
| } | ||
| }, [hasWallets, selectedWalletId, wallets]) | ||
|
|
||
| const setCurrentWallet = (id: string) => { | ||
| setSelectedWalletId(id) | ||
| } | ||
|
|
||
| const contextValue: WalletContext = { | ||
| currentWallet, | ||
| wallets, | ||
| isLoading, | ||
| hasWallets, | ||
| setCurrentWallet, | ||
| } | ||
|
|
||
| return ( | ||
| <WalletContext.Provider value={contextValue}> | ||
| {children} | ||
| </WalletContext.Provider> | ||
| ) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,21 @@ | ||
| import { createFileRoute, Outlet } from '@tanstack/react-router' | ||
| import { createFileRoute, Outlet, redirect } from '@tanstack/react-router' | ||
|
|
||
| import { apiClient } from '../../providers/api-client' | ||
|
|
||
| export const Route = createFileRoute('/onboarding')({ | ||
| component: RouteComponent, | ||
| beforeLoad: () => { | ||
| // TODO: check if user is already onboarded | ||
| // throw redirect({ to: '/' }) | ||
| beforeLoad: async () => { | ||
felicio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const wallets = await apiClient.wallet.all.query() | ||
| if (wallets && wallets.length > 0) { | ||
| throw redirect({ to: '/portfolio' }) | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| function RouteComponent() { | ||
| return <Outlet /> | ||
| return ( | ||
| <div className="m-auto min-h-[650px] w-full max-w-[440px] rounded-[24px] border border-neutral-5 p-5 shadow-2"> | ||
| <Outlet /> | ||
| </div> | ||
| ) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.