Commit 26ea746b authored by tom's avatar tom

fix tx socket alert

parent e74cce21
...@@ -52,11 +52,11 @@ export default function useNewTxsSocket() { ...@@ -52,11 +52,11 @@ export default function useNewTxsSocket() {
}, [ setNum ]); }, [ setNum ]);
const handleSocketClose = React.useCallback(() => { const handleSocketClose = React.useCallback(() => {
setSocketAlert('Connection is lost. Please click here to load new transactions.'); setSocketAlert('Connection is lost. Please reload page.');
}, []); }, []);
const handleSocketError = React.useCallback(() => { const handleSocketError = React.useCallback(() => {
setSocketAlert('An error has occurred while fetching new transactions. Please click here to refresh the page.'); setSocketAlert('An error has occurred while fetching new transactions. Please reload page.');
}, []); }, []);
const channel = useSocketChannel({ const channel = useSocketChannel({
......
...@@ -8,10 +8,10 @@ import { QueryKeys } from 'types/client/queries'; ...@@ -8,10 +8,10 @@ import { QueryKeys } from 'types/client/queries';
import useFetch from 'lib/hooks/useFetch'; import useFetch from 'lib/hooks/useFetch';
import useIsMobile from 'lib/hooks/useIsMobile'; import useIsMobile from 'lib/hooks/useIsMobile';
import link from 'lib/link/link'; import link from 'lib/link/link';
import TxsNewItemNotice from 'ui/txs/TxsNewItemNotice';
import LatestTxsItem from './LatestTxsItem'; import LatestTxsItem from './LatestTxsItem';
import LatestTxsItemSkeleton from './LatestTxsItemSkeleton'; import LatestTxsItemSkeleton from './LatestTxsItemSkeleton';
import LatestTxsNotice from './LatestTxsNotice';
const LatestTransactions = () => { const LatestTransactions = () => {
const isMobile = useIsMobile(); const isMobile = useIsMobile();
...@@ -41,7 +41,7 @@ const LatestTransactions = () => { ...@@ -41,7 +41,7 @@ const LatestTransactions = () => {
const txsUrl = link('txs'); const txsUrl = link('txs');
content = ( content = (
<> <>
<LatestTxsNotice/> <TxsNewItemNotice borderBottomRadius={ 0 } url={ link('txs') }/>
<Box mb={{ base: 3, lg: 6 }}> <Box mb={{ base: 3, lg: 6 }}>
{ data.slice(0, txsCount).map((tx => <LatestTxsItem key={ tx.hash } tx={ tx }/>)) } { data.slice(0, txsCount).map((tx => <LatestTxsItem key={ tx.hash } tx={ tx }/>)) }
</Box> </Box>
......
import { Alert, Text, Link, useColorModeValue, useTheme } from '@chakra-ui/react';
import { transparentize } from '@chakra-ui/theme-tools';
import React from 'react';
import useNewTxsSocket from 'lib/hooks/useNewTxsSocket';
import link from 'lib/link/link';
interface Props {
className?: string;
}
const LatestTxsNotice = ({ className }: Props) => {
const { num, socketAlert } = useNewTxsSocket();
let content;
if (socketAlert) {
content = 'Connection is lost. Please reload page';
} else if (!num) {
content = (
<Text>scanning new transactions...</Text>
);
} else {
const txsUrl = link('txs');
content = (
<>
<Link href={ txsUrl }>{ num } more transaction{ num > 1 ? 's' : '' }</Link>
<Text whiteSpace="pre"> ha{ num > 1 ? 've' : 's' } come in</Text>
</>
);
}
const theme = useTheme();
return (
<Alert
className={ className }
status="warning"
p={ 4 }
fontWeight={ 400 }
bgColor={ useColorModeValue('orange.50', transparentize('orange.200', 0.16)(theme)) }
borderBottomRadius={ 0 }
>
{ content }
</Alert>
);
};
export default LatestTxsNotice;
...@@ -56,7 +56,7 @@ const TxsContent = ({ query, showBlockInfo = true, showSocketInfo = true }: Prop ...@@ -56,7 +56,7 @@ const TxsContent = ({ query, showBlockInfo = true, showSocketInfo = true }: Prop
<Show below="lg" ssr={ false }> <Show below="lg" ssr={ false }>
<Box> <Box>
{ showSocketInfo && ( { showSocketInfo && (
<TxsNewItemNotice> <TxsNewItemNotice url={ window.location.href }>
{ ({ content }) => <Box>{ content }</Box> } { ({ content }) => <Box>{ content }</Box> }
</TxsNewItemNotice> </TxsNewItemNotice>
) } ) }
......
...@@ -23,7 +23,7 @@ test.describe.configure({ mode: 'serial' }); ...@@ -23,7 +23,7 @@ test.describe.configure({ mode: 'serial' });
test('new item in validated txs list', async({ mount, createSocket }) => { test('new item in validated txs list', async({ mount, createSocket }) => {
const component = await mount( const component = await mount(
<TestApp withSocket> <TestApp withSocket>
<TxsNewItemNotice/> <TxsNewItemNotice url="/"/>
</TestApp>, </TestApp>,
{ hooksConfig }, { hooksConfig },
); );
...@@ -35,10 +35,29 @@ test('new item in validated txs list', async({ mount, createSocket }) => { ...@@ -35,10 +35,29 @@ test('new item in validated txs list', async({ mount, createSocket }) => {
await expect(component).toHaveScreenshot(); await expect(component).toHaveScreenshot();
}); });
test.describe('dark mode', () => {
test.use({ colorScheme: 'dark' });
test('default view', async({ mount, createSocket }) => {
const component = await mount(
<TestApp withSocket>
<TxsNewItemNotice url="/"/>
</TestApp>,
{ hooksConfig },
);
const socket = await createSocket();
const channel = await socketServer.joinChannel(socket, 'transactions:new_transaction');
socketServer.sendMessage(socket, channel, 'transaction', { transaction: 1 });
await expect(component).toHaveScreenshot();
});
});
test('2 new items in validated txs list', async({ mount, page, createSocket }) => { test('2 new items in validated txs list', async({ mount, page, createSocket }) => {
const component = await mount( const component = await mount(
<TestApp withSocket> <TestApp withSocket>
<TxsNewItemNotice/> <TxsNewItemNotice url="/"/>
</TestApp>, </TestApp>,
{ hooksConfig }, { hooksConfig },
); );
...@@ -56,7 +75,7 @@ test('2 new items in validated txs list', async({ mount, page, createSocket }) = ...@@ -56,7 +75,7 @@ test('2 new items in validated txs list', async({ mount, page, createSocket }) =
test('connection loss', async({ mount, createSocket }) => { test('connection loss', async({ mount, createSocket }) => {
const component = await mount( const component = await mount(
<TestApp withSocket> <TestApp withSocket>
<TxsNewItemNotice/> <TxsNewItemNotice url="/"/>
</TestApp>, </TestApp>,
{ hooksConfig }, { hooksConfig },
); );
...@@ -71,7 +90,7 @@ test('connection loss', async({ mount, createSocket }) => { ...@@ -71,7 +90,7 @@ test('connection loss', async({ mount, createSocket }) => {
test('fetching', async({ mount, createSocket }) => { test('fetching', async({ mount, createSocket }) => {
const component = await mount( const component = await mount(
<TestApp withSocket> <TestApp withSocket>
<TxsNewItemNotice/> <TxsNewItemNotice url="/"/>
</TestApp>, </TestApp>,
{ hooksConfig }, { hooksConfig },
); );
......
import { Alert, Link, Text, chakra } from '@chakra-ui/react'; import { Alert, Link, Text, chakra, useTheme, useColorModeValue } from '@chakra-ui/react';
import { transparentize } from '@chakra-ui/theme-tools';
import React from 'react'; import React from 'react';
import useNewTxsSocket from 'lib/hooks/useNewTxsSocket'; import useNewTxsSocket from 'lib/hooks/useNewTxsSocket';
...@@ -10,47 +11,44 @@ interface InjectedProps { ...@@ -10,47 +11,44 @@ interface InjectedProps {
interface Props { interface Props {
children?: (props: InjectedProps) => JSX.Element; children?: (props: InjectedProps) => JSX.Element;
className?: string; className?: string;
url: string;
} }
const TxsNewItemNotice = ({ children, className }: Props) => { const TxsNewItemNotice = ({ children, className, url }: Props) => {
const { num, socketAlert } = useNewTxsSocket(); const { num, socketAlert } = useNewTxsSocket();
const theme = useTheme();
const handleClick = React.useCallback(() => { const alertContent = (() => {
window.location.reload();
}, []);
const content = (() => {
if (socketAlert) { if (socketAlert) {
return ( return socketAlert;
<Alert
className={ className }
status="warning"
p={ 4 }
borderRadius={ 0 }
onClick={ handleClick }
cursor="pointer"
>
{ socketAlert }
</Alert>
);
} }
if (!num) { if (!num) {
return ( return 'scanning new transactions...';
<Alert className={ className } status="warning" p={ 4 } fontWeight={ 400 }>
scanning new transactions...
</Alert>
);
} }
return ( return (
<Alert className={ className } status="warning" p={ 4 } fontWeight={ 400 }> <>
<Link onClick={ handleClick }>{ num } more transaction{ num > 1 ? 's' : '' }</Link> <Link href={ url }>{ num } more transaction{ num > 1 ? 's' : '' }</Link>
<Text whiteSpace="pre"> ha{ num > 1 ? 've' : 's' } come in</Text> <Text whiteSpace="pre"> ha{ num > 1 ? 've' : 's' } come in</Text>
</Alert> </>
); );
})(); })();
const content = (
<Alert
className={ className }
status="warning"
px={ 4 }
py="6px"
fontWeight={ 400 }
fontSize="sm"
bgColor={ useColorModeValue('orange.50', transparentize('orange.200', 0.16)(theme)) }
>
{ alertContent }
</Alert>
);
return children ? children({ content }) : content; return children ? children({ content }) : content;
}; };
......
...@@ -51,7 +51,7 @@ const TxsTable = ({ txs, sort, sorting, top, showBlockInfo, showSocketInfo }: Pr ...@@ -51,7 +51,7 @@ const TxsTable = ({ txs, sort, sorting, top, showBlockInfo, showSocketInfo }: Pr
</TheadSticky> </TheadSticky>
<Tbody> <Tbody>
{ showSocketInfo && ( { showSocketInfo && (
<TxsNewItemNotice borderRadius={ 0 }> <TxsNewItemNotice borderRadius={ 0 } url={ window.location.href }>
{ ({ content }) => <Tr><Td colSpan={ 10 } p={ 0 }>{ content }</Td></Tr> } { ({ content }) => <Tr><Td colSpan={ 10 } p={ 0 }>{ content }</Td></Tr> }
</TxsNewItemNotice> </TxsNewItemNotice>
) } ) }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment