From 57c06bc0fed86b12145fe68732e8036ba3558845 Mon Sep 17 00:00:00 2001 From: Filipe Medeiros Date: Sun, 28 Nov 2021 05:15:19 +0000 Subject: [PATCH] ... --- components/Balance.tsx | 8 +++++++- components/RecentTransactions.tsx | 19 ++++++------------- lib/context/preferencesContext.tsx | 6 +++++- lib/hooks/useDarkMode.ts | 6 ++++-- lib/hooks/useListenToConfirmations.ts | 16 +++++++++++++--- lib/hooks/useReceiveNano.ts | 2 +- lib/xno/fetchAccountHistory.ts | 2 +- lib/xno/fetchAccountInfo.ts | 2 +- lib/xno/fetchAccountReceivable.ts | 2 +- lib/xno/fetchBlocksInfo.ts | 2 +- lib/xno/rawToNanoDisplay.ts | 9 +++++++++ lib/xno/receiveNano.ts | 2 +- lib/xno/sendNano.ts | 2 +- 13 files changed, 51 insertions(+), 27 deletions(-) create mode 100644 lib/xno/rawToNanoDisplay.ts diff --git a/components/Balance.tsx b/components/Balance.tsx index aac0a42..e6ff6ca 100644 --- a/components/Balance.tsx +++ b/components/Balance.tsx @@ -6,6 +6,7 @@ import { useCurrentAccount } from '../lib/context/accountContext' import { usePreferences } from '../lib/context/preferencesContext' import { ShowCurrencyPreference } from '../lib/db/types' import useXnoPrice from '../lib/hooks/useXnoPrice' +import rawToNanoDisplay from '../lib/xno/rawToNanoDisplay' export interface Props { className?: string @@ -37,6 +38,7 @@ const Balance: FC = ({ className }) => { account !== undefined const xnoBalance = tools.convert(account?.balance ?? '0', 'RAW', 'NANO') + const xnoBalanceDisplay = rawToNanoDisplay(account?.balance ?? '0') return (
= ({ className }) => { showXnoBalance ? 'font-medium dark:font-semibold' : 'font-semibold' )} > - {showXnoBalance ? <> {Number(xnoBalance).toFixed(2)} : 'NO'} + {showXnoBalance ? ( + <> {xnoBalanceDisplay === 'small' ? '<0.01' : xnoBalanceDisplay} + ) : ( + 'NO' + )} {showFiatBalance && ( diff --git a/components/RecentTransactions.tsx b/components/RecentTransactions.tsx index 8e93fd1..98a7350 100644 --- a/components/RecentTransactions.tsx +++ b/components/RecentTransactions.tsx @@ -1,8 +1,8 @@ import { ClockIcon } from '@heroicons/react/outline' import { DownloadIcon, UploadIcon } from '@heroicons/react/solid' import clsx from 'clsx' -import { tools } from 'nanocurrency-web' -import { FC, useCallback, useState } from 'react' +import { useCallback, useState } from 'react' +import type { FC } from 'react' import { useCurrentAccount } from '../lib/context/accountContext' import useAccountHistory from '../lib/hooks/useAccountHistory' @@ -10,12 +10,7 @@ import useAccountReceivable from '../lib/hooks/useAccountReceivable' import useListenToConfirmations from '../lib/hooks/useListenToConfirmations' import useReceiveNano from '../lib/hooks/useReceiveNano' import { ConfirmationMessage } from '../lib/types' - -const rawToNanoDisplay = (raw: string) => - Number(tools.convert(raw, 'RAW', 'NANO').slice(0, 20)) - .toFixed(2) - .replace(/^0\.00/, 'small') - .replace('.00', '') +import rawToNanoDisplay from '../lib/xno/rawToNanoDisplay' export interface Props { className?: string @@ -65,8 +60,6 @@ const RecentTransactions: FC = ({ className }) => { useListenToConfirmations(onConfirmation) - console.log(listenedReceivables) - return (
{hasReceivable && ( @@ -95,7 +88,7 @@ const RecentTransactions: FC = ({ className }) => { Ӿ{' '} {rawToNanoDisplay(message.amount) === 'small' ? ( - '<.01' + '<0.01' ) : rawToNanoDisplay(message.amount).startsWith('0.') ? ( <> 0 @@ -135,7 +128,7 @@ const RecentTransactions: FC = ({ className }) => { Ӿ{' '} {rawToNanoDisplay(receivable.amount) === 'small' ? ( - '<.01' + '<0.01' ) : rawToNanoDisplay(receivable.amount).startsWith('0.') ? ( <> 0 @@ -187,7 +180,7 @@ const RecentTransactions: FC = ({ className }) => { Ӿ{' '} {rawToNanoDisplay(txn.amount) === 'small' ? ( - '<.01' + '<0.01' ) : rawToNanoDisplay(txn.amount).startsWith('0.') ? ( <> 0 diff --git a/lib/context/preferencesContext.tsx b/lib/context/preferencesContext.tsx index 99694d9..eca5ad7 100644 --- a/lib/context/preferencesContext.tsx +++ b/lib/context/preferencesContext.tsx @@ -54,7 +54,7 @@ export const PreferencesProvider: FC = ({ children }) => { fetchPreferencesFromIdb() }, []) - const setDarkMode = useDarkMode() + const { setDarkMode, isDarkMode } = useDarkMode() const setPreference = useCallback(

( @@ -70,6 +70,10 @@ export const PreferencesProvider: FC = ({ children }) => { [setDarkMode] ) + useEffect(() => { + setPreference('darkMode', isDarkMode) + }, [isDarkMode, setPreference]) + return ( {children} diff --git a/lib/hooks/useDarkMode.ts b/lib/hooks/useDarkMode.ts index 33f55ea..f0690f6 100644 --- a/lib/hooks/useDarkMode.ts +++ b/lib/hooks/useDarkMode.ts @@ -1,10 +1,12 @@ -import { useCallback, useEffect } from 'react' +import { useCallback, useEffect, useState } from 'react' import { getPreference } from '../db/preferences' const useDarkMode = () => { + const [isDarkMode, setIsDarkMode] = useState(true) const setDarkMode = useCallback(async (darkMode: boolean) => { const htmlClasses = document.querySelector('html')?.classList + setIsDarkMode(darkMode) if (darkMode) htmlClasses?.add('dark') else htmlClasses?.remove('dark') }, []) @@ -19,7 +21,7 @@ const useDarkMode = () => { darkModeOnStarup() }, [setDarkMode]) - return setDarkMode + return { setDarkMode, isDarkMode } } export default useDarkMode diff --git a/lib/hooks/useListenToConfirmations.ts b/lib/hooks/useListenToConfirmations.ts index ab2aa0c..d34967f 100644 --- a/lib/hooks/useListenToConfirmations.ts +++ b/lib/hooks/useListenToConfirmations.ts @@ -15,8 +15,14 @@ const useListenToConfirmations = ( const wsRef = useRef() useEffect(() => { - wsRef.current = new WebSocket('wss://node.somenano.com/websocket') - return () => wsRef.current?.close() + wsRef.current = new WebSocket('wss://socket.nanos.cc/') + return () => { + if ( + wsRef.current?.readyState !== WebSocket.CLOSING && + wsRef.current?.readyState !== WebSocket.CLOSED + ) + wsRef.current?.close() + } }, []) useEffect(() => { @@ -36,7 +42,11 @@ const useListenToConfirmations = ( ) wsRef.current!.addEventListener('message', ({ data }) => { const parsed = JSON.parse(data) as ConfirmationMessage - if (parsed.topic !== 'confirmation') return + if ( + parsed.topic !== 'confirmation' || + parsed.message.block.subtype !== 'send' + ) + return onConfirmation(parsed) }) diff --git a/lib/hooks/useReceiveNano.ts b/lib/hooks/useReceiveNano.ts index cb0704a..56820c0 100644 --- a/lib/hooks/useReceiveNano.ts +++ b/lib/hooks/useReceiveNano.ts @@ -39,7 +39,7 @@ const useReceiveNano = () => { ) await consumePrecomputedWork(account.address) - const work = await computeWorkAsync(processResponse.hash, { send: false }) + const work = await computeWorkAsync(processResponse.hash, { send: true }) setAccount({ ...account, frontier: processResponse.hash, diff --git a/lib/xno/fetchAccountHistory.ts b/lib/xno/fetchAccountHistory.ts index 95725eb..cfc09d6 100644 --- a/lib/xno/fetchAccountHistory.ts +++ b/lib/xno/fetchAccountHistory.ts @@ -2,7 +2,7 @@ import fetcher from '../fetcher' import { AccountHistoryResponse } from '../types' const fetchAccountHistory = (address: string, count = 20, head = undefined) => - fetcher('https://mynano.ninja/api/node', { + fetcher('https://proxy.powernode.cc/proxy', { method: 'POST', body: { action: 'account_history', diff --git a/lib/xno/fetchAccountInfo.ts b/lib/xno/fetchAccountInfo.ts index 6e21f82..d8b6006 100644 --- a/lib/xno/fetchAccountInfo.ts +++ b/lib/xno/fetchAccountInfo.ts @@ -2,7 +2,7 @@ import fetcher from '../fetcher' import { AccountInfoResponse } from '../types' const fetchAccountInfo = (address: string) => - fetcher('https://mynano.ninja/api/node', { + fetcher('https://proxy.powernode.cc/proxy', { method: 'POST', body: { action: 'account_info', diff --git a/lib/xno/fetchAccountReceivable.ts b/lib/xno/fetchAccountReceivable.ts index 8264670..f01c2d0 100644 --- a/lib/xno/fetchAccountReceivable.ts +++ b/lib/xno/fetchAccountReceivable.ts @@ -6,7 +6,7 @@ const _fetchAccountReceivable = ( count = 20, version22 = false ) => - fetcher('https://mynano.ninja/api/node', { + fetcher('https://proxy.powernode.cc/proxy', { method: 'POST', body: { action: version22 ? 'accounts_pending' : 'accounts_receivable', diff --git a/lib/xno/fetchBlocksInfo.ts b/lib/xno/fetchBlocksInfo.ts index fde3a53..55b9742 100644 --- a/lib/xno/fetchBlocksInfo.ts +++ b/lib/xno/fetchBlocksInfo.ts @@ -2,7 +2,7 @@ import fetcher from '../fetcher' import type { BlocksInfoResponse } from '../types' const fetchBlocksInfo = (hashes: string[]) => - fetcher('https://mynano.ninja/api/node', { + fetcher('https://proxy.powernode.cc/proxy', { method: 'POST', body: { action: 'blocks_info', diff --git a/lib/xno/rawToNanoDisplay.ts b/lib/xno/rawToNanoDisplay.ts new file mode 100644 index 0000000..38ea5ce --- /dev/null +++ b/lib/xno/rawToNanoDisplay.ts @@ -0,0 +1,9 @@ +import { tools } from 'nanocurrency-web' + +const rawToNanoDisplay = (raw: string) => + Number(tools.convert(raw, 'RAW', 'NANO').slice(0, 20)) + .toFixed(2) + .replace(/^0\.00/, 'small') + .replace('.00', '') + +export default rawToNanoDisplay diff --git a/lib/xno/receiveNano.ts b/lib/xno/receiveNano.ts index d23b808..103c73c 100644 --- a/lib/xno/receiveNano.ts +++ b/lib/xno/receiveNano.ts @@ -19,7 +19,7 @@ const sendNano = async ( const signedBlock = block.receive(blockData, privateKey) const processResponse = await fetcher( - 'https://mynano.ninja/api/node', + 'https://proxy.powernode.cc/proxy', { method: 'POST', body: { diff --git a/lib/xno/sendNano.ts b/lib/xno/sendNano.ts index eecd9a8..8aaffca 100644 --- a/lib/xno/sendNano.ts +++ b/lib/xno/sendNano.ts @@ -18,7 +18,7 @@ const sendNano = async ( const signedBlock = block.send(blockData, privateKey) const processResponse = await fetcher( - 'https://mynano.ninja/api/node', + 'https://proxy.powernode.cc/proxy', { method: 'POST', body: {