From cd6e906dc61f6849ae9b23acf606dfddadc8bf4a Mon Sep 17 00:00:00 2001 From: Art Date: Thu, 3 Aug 2023 13:14:35 +0200 Subject: [PATCH] feat(trading): view as button in sidebar (#4470) --- .../components/sidebar/sidebar.spec.tsx | 82 ++++++++++++++----- apps/trading/components/sidebar/sidebar.tsx | 49 ++++++++--- apps/trading/pages/dialogs-container.tsx | 3 +- .../icon/vega-icons/svg-icons/icon-eye.tsx | 8 ++ .../icon/vega-icons/vega-icon-record.ts | 3 + .../notification-banner.tsx | 2 +- .../trading-button/trading-button.tsx | 3 +- .../src/components/viewing-as-user/index.tsx | 10 ++- libs/wallet/src/connect-dialog/index.ts | 1 + .../src/connect-dialog/view-as-dialog.tsx | 40 +++++++++ .../connect-dialog/view-connector-form.tsx | 25 ++++-- 11 files changed, 178 insertions(+), 48 deletions(-) create mode 100644 libs/ui-toolkit/src/components/icon/vega-icons/svg-icons/icon-eye.tsx create mode 100644 libs/wallet/src/connect-dialog/view-as-dialog.tsx diff --git a/apps/trading/components/sidebar/sidebar.spec.tsx b/apps/trading/components/sidebar/sidebar.spec.tsx index bf673133b..f9fe36511 100644 --- a/apps/trading/components/sidebar/sidebar.spec.tsx +++ b/apps/trading/components/sidebar/sidebar.spec.tsx @@ -1,7 +1,15 @@ import { act, render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; -import { Sidebar, SidebarContent, ViewType, useSidebar } from './sidebar'; +import { + Sidebar, + SidebarButton, + SidebarContent, + ViewType, + useSidebar, +} from './sidebar'; import { MemoryRouter, Route, Routes } from 'react-router-dom'; +import { VegaIconNames } from '@vegaprotocol/ui-toolkit'; +import { VegaWalletProvider } from '@vegaprotocol/wallet'; jest.mock('../node-health', () => ({ NodeHealthContainer: () => , @@ -28,11 +36,14 @@ describe('Sidebar', () => { 'does not render ticket and info', (path) => { render( - - - + + + + + ); + expect(screen.getByTestId(ViewType.ViewAs)).toBeInTheDocument(); expect(screen.getByTestId(ViewType.Settings)).toBeInTheDocument(); expect(screen.getByTestId(ViewType.Transfer)).toBeInTheDocument(); expect(screen.getByTestId(ViewType.Deposit)).toBeInTheDocument(); @@ -47,11 +58,14 @@ describe('Sidebar', () => { it('renders ticket and info on market pages', () => { render( - - - + + + + + ); + expect(screen.getByTestId(ViewType.ViewAs)).toBeInTheDocument(); expect(screen.getByTestId(ViewType.Settings)).toBeInTheDocument(); expect(screen.getByTestId(ViewType.Transfer)).toBeInTheDocument(); expect(screen.getByTestId(ViewType.Deposit)).toBeInTheDocument(); @@ -65,9 +79,11 @@ describe('Sidebar', () => { it('renders selected state', async () => { render( - - - + + + + + ); const settingsButton = screen.getByTestId(ViewType.Settings); @@ -91,11 +107,13 @@ describe('Sidebar', () => { describe('SidebarContent', () => { it('renders the correct content', () => { const { container } = render( - - - } /> - - + + + + } /> + + + ); expect(container).toBeEmptyDOMElement(); @@ -115,11 +133,13 @@ describe('SidebarContent', () => { it('closes sidebar if market id is required but not present', () => { const { container } = render( - - - } /> - - + + + + } /> + + + ); act(() => { @@ -141,3 +161,25 @@ describe('SidebarContent', () => { expect(container).toBeEmptyDOMElement(); }); }); + +describe('SidebarButton', () => { + it.each([ViewType.Info, ViewType.Deposit, ViewType.ViewAs])( + 'runs given callback regardless of requested view (%s)', + async (view) => { + const onClick = jest.fn(); + render( + + ); + + const btn = screen.getByTestId(view); + await userEvent.click(btn); + + expect(onClick).toBeCalled(); + } + ); +}); diff --git a/apps/trading/components/sidebar/sidebar.tsx b/apps/trading/components/sidebar/sidebar.tsx index 861e499cb..5c8c3a8a8 100644 --- a/apps/trading/components/sidebar/sidebar.tsx +++ b/apps/trading/components/sidebar/sidebar.tsx @@ -16,6 +16,7 @@ import { WithdrawContainer } from '../withdraw-container'; import { Routes as AppRoutes } from '../../pages/client-router'; import { persist } from 'zustand/middleware'; import { GetStarted } from '../welcome-dialog'; +import { useVegaWallet, useViewAsDialog } from '@vegaprotocol/wallet'; const STORAGE_KEY = 'vega_sidebar_store'; @@ -26,6 +27,7 @@ export enum ViewType { Withdraw = 'Withdraw', Transfer = 'Transfer', Settings = 'Settings', + ViewAs = 'ViewAs', } type SidebarView = @@ -53,6 +55,8 @@ type SidebarView = export const Sidebar = () => { const navClasses = 'flex lg:flex-col items-center gap-2 lg:gap-4 p-1'; + const setViewAsDialogOpen = useViewAsDialog((state) => state.setOpen); + const { pubKeys } = useVegaWallet(); return (