Candle/depth chart switcher (#284)

This commit is contained in:
John Walley 2022-04-25 18:34:07 +01:00 committed by GitHub
parent 2cdf349641
commit ba19b5acca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 51 additions and 121 deletions

View File

@ -1,6 +1,9 @@
import merge from 'lodash/merge'; import merge from 'lodash/merge';
import type { PartialDeep } from 'type-fest'; import type { PartialDeep } from 'type-fest';
import type { Candles, Candles_market_candles } from '@vegaprotocol/chart'; import type {
Candles,
Candles_market_candles,
} from '@vegaprotocol/candles-chart';
export const generateCandles = (override?: PartialDeep<Candles>): Candles => { export const generateCandles = (override?: PartialDeep<Candles>): Candles => {
const candles: Candles_market_candles[] = [ const candles: Candles_market_candles[] = [

View File

@ -3,7 +3,7 @@ import type { PartialDeep } from 'type-fest';
import type { import type {
Chart, Chart,
Chart_market_data_priceMonitoringBounds, Chart_market_data_priceMonitoringBounds,
} from '@vegaprotocol/chart'; } from '@vegaprotocol/candles-chart';
export const generateChart = (override?: PartialDeep<Chart>): Chart => { export const generateChart = (override?: PartialDeep<Chart>): Chart => {
const priceMonitoringBound: Chart_market_data_priceMonitoringBounds = { const priceMonitoringBound: Chart_market_data_priceMonitoringBounds = {

View File

@ -1,38 +0,0 @@
import { Button } from '@vegaprotocol/ui-toolkit';
interface ButtonRadioProps<T> {
name: string;
options: Array<{ value: T; text: string }>;
currentOption: T | null;
onSelect: (option: T) => void;
}
export const ButtonRadio = <T extends string>({
name,
options,
currentOption,
onSelect,
}: ButtonRadioProps<T>) => {
return (
<div className="flex gap-8">
{options.map((option) => {
const isSelected = option.value === currentOption;
return (
<Button
onClick={() => onSelect(option.value)}
className="flex-1"
variant={isSelected ? 'accent' : undefined}
data-testid={
isSelected
? `${name}-${option.value}-selected`
: `${name}-${option.value}`
}
key={option.value}
>
{option.text}
</Button>
);
})}
</div>
);
};

View File

@ -1,39 +0,0 @@
import { ButtonRadio } from './button-radio';
import { DepthChartContainer } from '@vegaprotocol/depth-chart';
import { TradingChartContainer } from '@vegaprotocol/chart';
import { useState } from 'react';
type ChartType = 'depth' | 'trading';
interface ChartContainerProps {
marketId: string;
}
export const ChartContainer = ({ marketId }: ChartContainerProps) => {
const [chartType, setChartType] = useState<ChartType>('trading');
return (
<div className="px-4 py-8 flex flex-col h-full">
<div className="mb-4">
<ButtonRadio
name="chart-type"
options={[
{ text: 'Trading view', value: 'trading' },
{ text: 'Depth', value: 'depth' },
]}
currentOption={chartType}
onSelect={(value) => {
setChartType(value);
}}
/>
</div>
<div className="flex-1">
{chartType === 'trading' ? (
<TradingChartContainer marketId={marketId} />
) : (
<DepthChartContainer marketId={marketId} />
)}
</div>
</div>
);
};

View File

@ -1 +0,0 @@
export { ChartContainer } from './chart-container';

View File

@ -5,13 +5,14 @@ import { useState } from 'react';
import { GridTab, GridTabs } from './grid-tabs'; import { GridTab, GridTabs } from './grid-tabs';
import { DealTicketContainer } from '@vegaprotocol/deal-ticket'; import { DealTicketContainer } from '@vegaprotocol/deal-ticket';
import { OrderListContainer } from '@vegaprotocol/order-list'; import { OrderListContainer } from '@vegaprotocol/order-list';
import { ChartContainer } from '../../components/chart-container';
import { TradesContainer } from '@vegaprotocol/trades'; import { TradesContainer } from '@vegaprotocol/trades';
import { Splash } from '@vegaprotocol/ui-toolkit'; import { Splash } from '@vegaprotocol/ui-toolkit';
import { PositionsContainer } from '@vegaprotocol/positions'; import { PositionsContainer } from '@vegaprotocol/positions';
import type { Market_market } from './__generated__/Market'; import type { Market_market } from './__generated__/Market';
import { t } from '@vegaprotocol/react-helpers'; import { t } from '@vegaprotocol/react-helpers';
import { AccountsContainer } from '@vegaprotocol/accounts'; import { AccountsContainer } from '@vegaprotocol/accounts';
import { DepthChartContainer } from '@vegaprotocol/depth-chart';
import { CandlesChartContainer } from '@vegaprotocol/candles-chart';
const Orderbook = () => ( const Orderbook = () => (
<Splash> <Splash>
@ -20,7 +21,8 @@ const Orderbook = () => (
); );
const TradingViews = { const TradingViews = {
Chart: ChartContainer, Candles: CandlesChartContainer,
Depth: DepthChartContainer,
Ticket: DealTicketContainer, Ticket: DealTicketContainer,
Orderbook: Orderbook, Orderbook: Orderbook,
Orders: OrderListContainer, Orders: OrderListContainer,
@ -50,7 +52,14 @@ export const TradeGrid = ({ market }: TradeGridProps) => {
</h1> </h1>
</header> </header>
<TradeGridChild className="col-start-1 col-end-2"> <TradeGridChild className="col-start-1 col-end-2">
<TradingViews.Chart marketId={market.id} /> <GridTabs group="chart">
<GridTab id="candles" name={t('Candles')}>
<TradingViews.Candles marketId={market.id} />
</GridTab>
<GridTab id="depth" name={t('Depth')}>
<TradingViews.Depth marketId={market.id} />
</GridTab>
</GridTabs>
</TradeGridChild> </TradeGridChild>
<TradeGridChild className="row-start-1 row-end-3"> <TradeGridChild className="row-start-1 row-end-3">
<TradingViews.Ticket marketId={market.id} /> <TradingViews.Ticket marketId={market.id} />
@ -107,7 +116,7 @@ interface TradePanelsProps {
} }
export const TradePanels = ({ market }: TradePanelsProps) => { export const TradePanels = ({ market }: TradePanelsProps) => {
const [view, setView] = useState<TradingView>('Chart'); const [view, setView] = useState<TradingView>('Candles');
const renderView = () => { const renderView = () => {
const Component = TradingViews[view]; const Component = TradingViews[view];

View File

@ -0,0 +1,7 @@
# candles-chart
This library was generated with [Nx](https://nx.dev).
## Running unit tests
Run `nx test candles-chart` to execute the unit tests via [Jest](https://jestjs.io).

View File

@ -1,10 +1,10 @@
module.exports = { module.exports = {
displayName: 'chart', displayName: 'candles-chart',
preset: '../../jest.preset.js', preset: '../../jest.preset.js',
transform: { transform: {
'^.+\\.[tj]sx?$': 'babel-jest', '^.+\\.[tj]sx?$': 'babel-jest',
}, },
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/chart', coverageDirectory: '../../coverage/libs/candles-chart',
setupFiles: ['jest-canvas-mock'], setupFiles: ['jest-canvas-mock'],
}; };

View File

@ -0,0 +1,4 @@
{
"name": "@vegaprotocol/candles-chart",
"version": "0.0.1"
}

View File

@ -1,6 +1,6 @@
{ {
"root": "libs/chart", "root": "libs/candles-chart",
"sourceRoot": "libs/chart/src", "sourceRoot": "libs/candles-chart/src",
"projectType": "library", "projectType": "library",
"tags": [], "tags": [],
"targets": { "targets": {
@ -8,16 +8,16 @@
"executor": "@nrwl/web:rollup", "executor": "@nrwl/web:rollup",
"outputs": ["{options.outputPath}"], "outputs": ["{options.outputPath}"],
"options": { "options": {
"outputPath": "dist/libs/chart", "outputPath": "dist/libs/candles-chart",
"tsConfig": "libs/chart/tsconfig.lib.json", "tsConfig": "libs/candles-chart/tsconfig.lib.json",
"project": "libs/chart/package.json", "project": "libs/candles-chart/package.json",
"entryFile": "libs/chart/src/index.ts", "entryFile": "libs/candles-chart/src/index.ts",
"external": ["react/jsx-runtime"], "external": ["react/jsx-runtime"],
"rollupConfig": "@nrwl/react/plugins/bundle-rollup", "rollupConfig": "@nrwl/react/plugins/bundle-rollup",
"compiler": "babel", "compiler": "babel",
"assets": [ "assets": [
{ {
"glob": "libs/chart/README.md", "glob": "libs/candles-chart/README.md",
"input": ".", "input": ".",
"output": "." "output": "."
} }
@ -28,14 +28,14 @@
"executor": "@nrwl/linter:eslint", "executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"], "outputs": ["{options.outputFile}"],
"options": { "options": {
"lintFilePatterns": ["libs/chart/**/*.{ts,tsx,js,jsx}"] "lintFilePatterns": ["libs/candles-chart/**/*.{ts,tsx,js,jsx}"]
} }
}, },
"test": { "test": {
"executor": "@nrwl/jest:jest", "executor": "@nrwl/jest:jest",
"outputs": ["coverage/libs/chart"], "outputs": ["coverage/libs/candles-chart"],
"options": { "options": {
"jestConfig": "libs/chart/jest.config.js", "jestConfig": "libs/candles-chart/jest.config.js",
"passWithNoTests": true "passWithNoTests": true
} }
} }

View File

@ -1,4 +1,4 @@
export * from './lib/trading-chart'; export * from './lib/candles-chart';
export * from './lib/__generated__/Candles'; export * from './lib/__generated__/Candles';
export * from './lib/__generated__/CandleFields'; export * from './lib/__generated__/CandleFields';
export * from './lib/__generated__/CandlesSub'; export * from './lib/__generated__/CandlesSub';

View File

@ -1,4 +1,4 @@
import { TradingChartContainer } from './trading-chart'; import { CandlesChartContainer } from './candles-chart';
import { render } from '@testing-library/react'; import { render } from '@testing-library/react';
import { MockedProvider } from '@apollo/client/testing'; import { MockedProvider } from '@apollo/client/testing';
import { VegaWalletContext } from '@vegaprotocol/wallet'; import { VegaWalletContext } from '@vegaprotocol/wallet';
@ -8,7 +8,7 @@ describe('TradingChart', () => {
const { baseElement } = render( const { baseElement } = render(
<MockedProvider> <MockedProvider>
<VegaWalletContext.Provider value={{} as never}> <VegaWalletContext.Provider value={{} as never}>
<TradingChartContainer marketId={'market-id'} /> <CandlesChartContainer marketId={'market-id'} />
</VegaWalletContext.Provider> </VegaWalletContext.Provider>
</MockedProvider> </MockedProvider>
); );

View File

@ -1,19 +1,19 @@
import 'pennant/dist/style.css'; import 'pennant/dist/style.css';
import { Chart as TradingChart, Interval } from 'pennant'; import { Chart, Interval } from 'pennant';
import { VegaDataSource } from './data-source'; import { VegaDataSource } from './data-source';
import { useApolloClient } from '@apollo/client'; import { useApolloClient } from '@apollo/client';
import { useContext, useMemo } from 'react'; import { useContext, useMemo } from 'react';
import { useVegaWallet } from '@vegaprotocol/wallet'; import { useVegaWallet } from '@vegaprotocol/wallet';
import { ThemeContext } from '@vegaprotocol/react-helpers'; import { ThemeContext } from '@vegaprotocol/react-helpers';
export type TradingChartContainerProps = { export type CandlesChartContainerProps = {
marketId: string; marketId: string;
}; };
export const TradingChartContainer = ({ export const CandlesChartContainer = ({
marketId, marketId,
}: TradingChartContainerProps) => { }: CandlesChartContainerProps) => {
const client = useApolloClient(); const client = useApolloClient();
const { keypair } = useVegaWallet(); const { keypair } = useVegaWallet();
const theme = useContext(ThemeContext); const theme = useContext(ThemeContext);
@ -23,10 +23,6 @@ export const TradingChartContainer = ({
}, [client, marketId, keypair]); }, [client, marketId, keypair]);
return ( return (
<TradingChart <Chart dataSource={dataSource} interval={Interval.I15M} theme={theme} />
dataSource={dataSource}
interval={Interval.I15M}
theme={theme}
/>
); );
}; };

View File

@ -1,7 +0,0 @@
# chart
This library was generated with [Nx](https://nx.dev).
## Running unit tests
Run `nx test chart` to execute the unit tests via [Jest](https://jestjs.io).

View File

@ -1,4 +0,0 @@
{
"name": "@vegaprotocol/chart",
"version": "0.0.1"
}

View File

@ -16,7 +16,7 @@
"baseUrl": ".", "baseUrl": ".",
"paths": { "paths": {
"@vegaprotocol/accounts": ["libs/accounts/src/index.ts"], "@vegaprotocol/accounts": ["libs/accounts/src/index.ts"],
"@vegaprotocol/chart": ["libs/chart/src/index.ts"], "@vegaprotocol/candles-chart": ["libs/candles-chart/src/index.ts"],
"@vegaprotocol/cypress": ["libs/cypress/src/index.ts"], "@vegaprotocol/cypress": ["libs/cypress/src/index.ts"],
"@vegaprotocol/deal-ticket": ["libs/deal-ticket/src/index.ts"], "@vegaprotocol/deal-ticket": ["libs/deal-ticket/src/index.ts"],
"@vegaprotocol/deposits": ["libs/deposits/src/index.ts"], "@vegaprotocol/deposits": ["libs/deposits/src/index.ts"],

View File

@ -2,7 +2,7 @@
"version": 2, "version": 2,
"projects": { "projects": {
"accounts": "libs/accounts", "accounts": "libs/accounts",
"chart": "libs/chart", "candles-chart": "libs/candles-chart",
"cypress": "libs/cypress", "cypress": "libs/cypress",
"deal-ticket": "libs/deal-ticket", "deal-ticket": "libs/deal-ticket",
"deposits": "libs/deposits", "deposits": "libs/deposits",