-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Connect delegate modal to keplr balance
- Loading branch information
Showing
41 changed files
with
1,671 additions
and
25 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...src/components/Delegations/ErrorModal.tsx → ...nts/Delegations/components/ErrorModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../components/Delegations/ModalListItem.tsx → .../Delegations/components/ModalListItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const config = { | ||
IS_DEV_MODE: process.env.NODE_ENV === 'development', | ||
LOG_TAURI_OPERATIONS: process.env.NODE_ENV === 'development', | ||
}; |
170 changes: 170 additions & 0 deletions
170
explorer/src/components/Delegations/context/accounts.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import React, { createContext, Dispatch, SetStateAction, useContext, useEffect, useMemo, useState } from 'react'; | ||
import { AccountEntry } from '@nymproject/types'; | ||
import { addAccount as addAccountRequest, renameAccount, showMnemonicForAccount } from '../requests'; | ||
import { useSnackbar } from 'notistack'; | ||
import { AppContext } from './main'; | ||
|
||
type TAccounts = { | ||
accounts?: AccountEntry[]; | ||
selectedAccount?: AccountEntry; | ||
accountToEdit?: AccountEntry; | ||
dialogToDisplay?: TAccountsDialog; | ||
isLoading: boolean; | ||
error?: string; | ||
accountMnemonic: TAccountMnemonic; | ||
setError: Dispatch<SetStateAction<string | undefined>>; | ||
setAccountMnemonic: Dispatch<SetStateAction<TAccountMnemonic>>; | ||
handleAddAccount: (data: { accountName: string; mnemonic: string; password: string }) => void; | ||
setDialogToDisplay: (dialog?: TAccountsDialog) => void; | ||
handleSelectAccount: (data: { accountName: string; password: string }) => Promise<boolean>; | ||
handleAccountToEdit: (accountId: string | undefined) => void; | ||
handleEditAccount: ({ | ||
account, | ||
newAccountName, | ||
password, | ||
}: { | ||
account: AccountEntry; | ||
newAccountName: string; | ||
password: string; | ||
}) => Promise<void>; | ||
handleImportAccount: (account: AccountEntry) => void; | ||
handleGetAccountMnemonic: (data: { password: string; accountName: string }) => void; | ||
}; | ||
|
||
export type TAccountsDialog = 'Accounts' | 'Add' | 'Edit' | 'Import' | 'Mnemonic'; | ||
export type TAccountMnemonic = { value?: string; accountName?: string }; | ||
|
||
export const AccountsContext = createContext({} as TAccounts); | ||
|
||
export const AccountsProvider: FCWithChildren = ({ children }) => { | ||
const [accounts, setAccounts] = useState<AccountEntry[]>([]); | ||
const [selectedAccount, setSelectedAccount] = useState<AccountEntry>(); | ||
const [accountToEdit, setAccountToEdit] = useState<AccountEntry>(); | ||
const [dialogToDisplay, setDialogToDisplay] = useState<TAccountsDialog>(); | ||
const [accountMnemonic, setAccountMnemonic] = useState<TAccountMnemonic>({ | ||
value: undefined, | ||
accountName: undefined, | ||
}); | ||
const [error, setError] = useState<string>(); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const { onAccountChange, storedAccounts } = useContext(AppContext); | ||
const { enqueueSnackbar } = useSnackbar(); | ||
|
||
const handleAddAccount = async ({ | ||
accountName, | ||
mnemonic, | ||
password, | ||
}: { | ||
accountName: string; | ||
mnemonic: string; | ||
password: string; | ||
}) => { | ||
setIsLoading(true); | ||
try { | ||
const newAccount = await addAccountRequest({ | ||
accountName, | ||
mnemonic, | ||
password, | ||
}); | ||
setAccounts((accs) => [...accs, newAccount]); | ||
enqueueSnackbar('New account created', { variant: 'success' }); | ||
} catch (e) { | ||
setError(`Error adding account: ${e}`); | ||
throw new Error(); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
const handleEditAccount = async ({ | ||
account, | ||
newAccountName, | ||
password, | ||
}: { | ||
account: AccountEntry; | ||
newAccountName: string; | ||
password: string; | ||
}) => { | ||
setIsLoading(true); | ||
try { | ||
await renameAccount({ accountName: account.id, newAccountName, password }); | ||
setAccounts((accs) => | ||
accs?.map((acc) => (acc.address === account.address ? { ...acc, id: newAccountName } : acc)), | ||
); | ||
if (selectedAccount?.id === account.id) { | ||
setSelectedAccount({ ...selectedAccount, id: newAccountName }); | ||
} | ||
setDialogToDisplay('Accounts'); | ||
} catch (e) { | ||
throw new Error(`Error editing account: ${e}`); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
const handleImportAccount = (account: AccountEntry) => setAccounts((accs) => [...(accs ? [...accs] : []), account]); | ||
|
||
const handleAccountToEdit = (accountName: string | undefined) => | ||
setAccountToEdit(accounts?.find((acc) => acc.id === accountName)); | ||
|
||
const handleSelectAccount = async ({ accountName, password }: { accountName: string; password: string }) => { | ||
try { | ||
await onAccountChange({ accountId: accountName, password }); | ||
const match = accounts?.find((acc) => acc.id === accountName); | ||
setSelectedAccount(match); | ||
return true; | ||
} catch (e) { | ||
setError('Error switching account. Please check your password'); | ||
return false; | ||
} | ||
}; | ||
|
||
const handleGetAccountMnemonic = async ({ password, accountName }: { password: string; accountName: string }) => { | ||
try { | ||
setIsLoading(true); | ||
const mnemonic = await showMnemonicForAccount({ password, accountName }); | ||
setAccountMnemonic({ value: mnemonic, accountName }); | ||
} catch (e) { | ||
setError(e as string); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (storedAccounts) { | ||
setAccounts(storedAccounts); | ||
} | ||
|
||
if (storedAccounts && !selectedAccount) { | ||
setSelectedAccount(storedAccounts[0]); | ||
} | ||
}, [storedAccounts]); | ||
|
||
return ( | ||
<AccountsContext.Provider | ||
value={useMemo( | ||
() => ({ | ||
error, | ||
setError, | ||
accounts, | ||
selectedAccount, | ||
accountToEdit, | ||
dialogToDisplay, | ||
accountMnemonic, | ||
setDialogToDisplay, | ||
setAccountMnemonic, | ||
isLoading, | ||
handleAddAccount, | ||
handleEditAccount, | ||
handleAccountToEdit, | ||
handleSelectAccount, | ||
handleImportAccount, | ||
handleGetAccountMnemonic, | ||
}), | ||
[accounts, selectedAccount, accountToEdit, dialogToDisplay, isLoading, error, accountMnemonic], | ||
)} | ||
> | ||
{children} | ||
</AccountsContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; | ||
import { sign } from '../requests'; | ||
// import { Console } from 'src/utils/console'; | ||
// import { AppContext } from './main'; | ||
|
||
export type TBuyContext = { | ||
loading: boolean; | ||
error?: string; | ||
signMessage: (message: string) => Promise<string | undefined>; | ||
refresh: () => Promise<void>; | ||
}; | ||
|
||
export const BuyContext = createContext<TBuyContext>({ | ||
loading: false, | ||
signMessage: async () => '', | ||
refresh: async () => undefined, | ||
}); | ||
|
||
export const BuyContextProvider: FCWithChildren = ({ children }): JSX.Element => { | ||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState<string>(); | ||
|
||
const refresh = useCallback(async () => { | ||
setError(undefined); | ||
}, []); | ||
|
||
useEffect(() => { | ||
refresh(); | ||
}, [refresh]); | ||
|
||
const signMessage = async (message: string) => { | ||
let signature; | ||
setLoading(true); | ||
try { | ||
signature = await sign(message); | ||
} catch (e: any) { | ||
// Console.log(`Sign message operation failed: ${e}`); | ||
console.log('`Sign message operation failed: ${e}` :>> ', `Sign message operation failed: ${e}`); | ||
setError(`Sign message operation failed: ${e}`); | ||
} finally { | ||
setLoading(false); | ||
} | ||
return signature; | ||
}; | ||
|
||
const memoizedValue = useMemo( | ||
() => ({ | ||
loading, | ||
error, | ||
refresh, | ||
signMessage, | ||
}), | ||
[loading, error], | ||
); | ||
|
||
return <BuyContext.Provider value={memoizedValue}>{children}</BuyContext.Provider>; | ||
}; | ||
|
||
export const useBuyContext = () => useContext<TBuyContext>(BuyContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './main'; | ||
export * from './accounts'; | ||
export * from './buy'; |
Oops, something went wrong.