2023-08-02 14:29:41 +00:00
|
|
|
import {
|
|
|
|
ChartType,
|
|
|
|
Overlay,
|
|
|
|
Study,
|
|
|
|
chartTypeLabels,
|
|
|
|
overlayLabels,
|
|
|
|
studyLabels,
|
|
|
|
} from 'pennant';
|
2023-12-13 01:33:41 +00:00
|
|
|
import { Trans } from 'react-i18next';
|
2023-08-02 14:29:41 +00:00
|
|
|
import {
|
2023-09-02 00:00:11 +00:00
|
|
|
TradingButton,
|
|
|
|
TradingDropdown,
|
|
|
|
TradingDropdownCheckboxItem,
|
|
|
|
TradingDropdownContent,
|
|
|
|
TradingDropdownItemIndicator,
|
|
|
|
TradingDropdownRadioGroup,
|
|
|
|
TradingDropdownRadioItem,
|
|
|
|
TradingDropdownTrigger,
|
2023-08-02 14:29:41 +00:00
|
|
|
Icon,
|
|
|
|
} from '@vegaprotocol/ui-toolkit';
|
2024-01-24 22:34:11 +00:00
|
|
|
import { type Interval } from '@vegaprotocol/types';
|
2023-12-13 01:33:41 +00:00
|
|
|
import { useEnvironment } from '@vegaprotocol/environment';
|
|
|
|
import { ALLOWED_TRADINGVIEW_HOSTNAMES } from '@vegaprotocol/trading-view';
|
|
|
|
import { IconNames, type IconName } from '@blueprintjs/icons';
|
|
|
|
import { useChartSettings } from './use-chart-settings';
|
|
|
|
import { useT } from '../../lib/use-t';
|
2024-01-24 22:34:11 +00:00
|
|
|
import { SUPPORTED_INTERVALS } from './constants';
|
2023-08-02 14:29:41 +00:00
|
|
|
|
|
|
|
const chartTypeIcon = new Map<ChartType, IconName>([
|
|
|
|
[ChartType.AREA, IconNames.TIMELINE_AREA_CHART],
|
|
|
|
[ChartType.CANDLE, IconNames.WATERFALL_CHART],
|
|
|
|
[ChartType.LINE, IconNames.TIMELINE_LINE_CHART],
|
|
|
|
[ChartType.OHLC, IconNames.WATERFALL_CHART],
|
|
|
|
]);
|
|
|
|
|
2023-12-13 01:33:41 +00:00
|
|
|
export const ChartMenu = () => {
|
|
|
|
const { CHARTING_LIBRARY_PATH } = useEnvironment();
|
2023-08-02 14:29:41 +00:00
|
|
|
const {
|
2023-12-13 01:33:41 +00:00
|
|
|
chartlib,
|
2023-08-02 14:29:41 +00:00
|
|
|
interval,
|
|
|
|
chartType,
|
|
|
|
studies,
|
|
|
|
overlays,
|
2023-12-13 01:33:41 +00:00
|
|
|
setChartlib,
|
2023-08-02 14:29:41 +00:00
|
|
|
setInterval,
|
|
|
|
setType,
|
|
|
|
setStudies,
|
|
|
|
setOverlays,
|
2023-12-13 01:33:41 +00:00
|
|
|
} = useChartSettings();
|
2023-11-15 05:10:06 +00:00
|
|
|
const t = useT();
|
2023-12-13 01:33:41 +00:00
|
|
|
|
2023-08-02 14:29:41 +00:00
|
|
|
const contentAlign = 'end';
|
2023-12-13 01:33:41 +00:00
|
|
|
const triggerClasses = 'text-xs';
|
2023-09-02 00:00:11 +00:00
|
|
|
const triggerButtonProps = { size: 'extra-small' } as const;
|
2023-08-02 14:29:41 +00:00
|
|
|
|
2023-12-13 01:33:41 +00:00
|
|
|
const isPennant = chartlib === 'pennant';
|
|
|
|
const commonMenuItems = (
|
|
|
|
<TradingButton
|
|
|
|
onClick={() => {
|
|
|
|
setChartlib(isPennant ? 'tradingview' : 'pennant');
|
|
|
|
}}
|
|
|
|
size="extra-small"
|
2024-01-24 10:21:33 +00:00
|
|
|
testId="chartlib-toggle-button"
|
2023-12-13 01:33:41 +00:00
|
|
|
>
|
|
|
|
{isPennant ? 'TradingView' : t('Vega chart')}
|
|
|
|
</TradingButton>
|
|
|
|
);
|
|
|
|
|
|
|
|
const pennantMenuItems = (
|
2023-08-02 14:29:41 +00:00
|
|
|
<>
|
2023-09-02 00:00:11 +00:00
|
|
|
<TradingDropdown
|
2023-08-02 14:29:41 +00:00
|
|
|
trigger={
|
2023-09-02 00:00:11 +00:00
|
|
|
<TradingDropdownTrigger className={triggerClasses}>
|
|
|
|
<TradingButton {...triggerButtonProps}>
|
2023-11-15 05:10:06 +00:00
|
|
|
{t('Interval: {{interval}}', {
|
2023-12-13 01:33:41 +00:00
|
|
|
interval: t(interval),
|
2023-11-15 05:10:06 +00:00
|
|
|
})}
|
2023-09-02 00:00:11 +00:00
|
|
|
</TradingButton>
|
|
|
|
</TradingDropdownTrigger>
|
2023-08-02 14:29:41 +00:00
|
|
|
}
|
|
|
|
>
|
2023-09-02 00:00:11 +00:00
|
|
|
<TradingDropdownContent align={contentAlign}>
|
|
|
|
<TradingDropdownRadioGroup
|
2023-08-02 14:29:41 +00:00
|
|
|
value={interval}
|
|
|
|
onValueChange={(value) => {
|
|
|
|
setInterval(value as Interval);
|
|
|
|
}}
|
|
|
|
>
|
2024-01-24 22:34:11 +00:00
|
|
|
{SUPPORTED_INTERVALS.map((timeInterval) => (
|
2023-09-02 00:00:11 +00:00
|
|
|
<TradingDropdownRadioItem
|
2023-08-02 14:29:41 +00:00
|
|
|
key={timeInterval}
|
|
|
|
inset
|
|
|
|
value={timeInterval}
|
|
|
|
>
|
2023-12-13 01:33:41 +00:00
|
|
|
{t(timeInterval)}
|
2023-09-02 00:00:11 +00:00
|
|
|
<TradingDropdownItemIndicator />
|
|
|
|
</TradingDropdownRadioItem>
|
2023-08-02 14:29:41 +00:00
|
|
|
))}
|
2023-09-02 00:00:11 +00:00
|
|
|
</TradingDropdownRadioGroup>
|
|
|
|
</TradingDropdownContent>
|
|
|
|
</TradingDropdown>
|
|
|
|
<TradingDropdown
|
2023-08-02 14:29:41 +00:00
|
|
|
trigger={
|
2023-09-02 00:00:11 +00:00
|
|
|
<TradingDropdownTrigger className={triggerClasses}>
|
|
|
|
<TradingButton {...triggerButtonProps}>
|
|
|
|
<Icon name={chartTypeIcon.get(chartType) as IconName} />
|
|
|
|
</TradingButton>
|
|
|
|
</TradingDropdownTrigger>
|
2023-08-02 14:29:41 +00:00
|
|
|
}
|
|
|
|
>
|
2023-09-02 00:00:11 +00:00
|
|
|
<TradingDropdownContent align={contentAlign}>
|
|
|
|
<TradingDropdownRadioGroup
|
2023-08-02 14:29:41 +00:00
|
|
|
value={chartType}
|
|
|
|
onValueChange={(value) => {
|
|
|
|
setType(value as ChartType);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{Object.values(ChartType).map((type) => (
|
2023-09-02 00:00:11 +00:00
|
|
|
<TradingDropdownRadioItem key={type} inset value={type}>
|
2023-08-02 14:29:41 +00:00
|
|
|
{chartTypeLabels[type]}
|
2023-09-02 00:00:11 +00:00
|
|
|
<TradingDropdownItemIndicator />
|
|
|
|
</TradingDropdownRadioItem>
|
2023-08-02 14:29:41 +00:00
|
|
|
))}
|
2023-09-02 00:00:11 +00:00
|
|
|
</TradingDropdownRadioGroup>
|
|
|
|
</TradingDropdownContent>
|
|
|
|
</TradingDropdown>
|
|
|
|
<TradingDropdown
|
2023-08-02 14:29:41 +00:00
|
|
|
trigger={
|
2023-09-02 00:00:11 +00:00
|
|
|
<TradingDropdownTrigger className={triggerClasses}>
|
|
|
|
<TradingButton {...triggerButtonProps}>
|
2023-10-27 13:21:14 +00:00
|
|
|
{t('Indicators')}
|
2023-09-02 00:00:11 +00:00
|
|
|
</TradingButton>
|
|
|
|
</TradingDropdownTrigger>
|
2023-08-02 14:29:41 +00:00
|
|
|
}
|
|
|
|
>
|
2023-09-02 00:00:11 +00:00
|
|
|
<TradingDropdownContent align={contentAlign}>
|
2023-08-02 14:29:41 +00:00
|
|
|
{Object.values(Overlay).map((overlay) => (
|
2023-09-02 00:00:11 +00:00
|
|
|
<TradingDropdownCheckboxItem
|
2023-08-02 14:29:41 +00:00
|
|
|
key={overlay}
|
|
|
|
checked={overlays.includes(overlay)}
|
|
|
|
onCheckedChange={() => {
|
|
|
|
const newOverlays = [...overlays];
|
|
|
|
const index = overlays.findIndex((item) => item === overlay);
|
|
|
|
|
|
|
|
index !== -1
|
|
|
|
? newOverlays.splice(index, 1)
|
|
|
|
: newOverlays.push(overlay);
|
|
|
|
|
|
|
|
setOverlays(newOverlays);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{overlayLabels[overlay]}
|
2023-09-02 00:00:11 +00:00
|
|
|
<TradingDropdownItemIndicator />
|
|
|
|
</TradingDropdownCheckboxItem>
|
2023-08-02 14:29:41 +00:00
|
|
|
))}
|
|
|
|
{Object.values(Study).map((study) => (
|
2023-09-02 00:00:11 +00:00
|
|
|
<TradingDropdownCheckboxItem
|
2023-08-02 14:29:41 +00:00
|
|
|
key={study}
|
|
|
|
checked={studies.includes(study)}
|
|
|
|
onCheckedChange={() => {
|
|
|
|
const newStudies = [...studies];
|
|
|
|
const index = studies.findIndex((item) => item === study);
|
|
|
|
|
|
|
|
index !== -1
|
|
|
|
? newStudies.splice(index, 1)
|
|
|
|
: newStudies.push(study);
|
|
|
|
|
|
|
|
setStudies(newStudies);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{studyLabels[study]}
|
2023-09-02 00:00:11 +00:00
|
|
|
<TradingDropdownItemIndicator />
|
|
|
|
</TradingDropdownCheckboxItem>
|
2023-08-02 14:29:41 +00:00
|
|
|
))}
|
2023-09-02 00:00:11 +00:00
|
|
|
</TradingDropdownContent>
|
|
|
|
</TradingDropdown>
|
2023-08-02 14:29:41 +00:00
|
|
|
</>
|
|
|
|
);
|
2023-12-13 01:33:41 +00:00
|
|
|
|
|
|
|
const tradingViewMenuItems = (
|
|
|
|
<p className="text-xs mr-2 whitespace-nowrap">
|
|
|
|
<Trans
|
|
|
|
i18nKey="Chart by <0>TradingView</0>"
|
|
|
|
components={[
|
|
|
|
// eslint-disable-next-line
|
|
|
|
<a
|
|
|
|
className="underline"
|
|
|
|
target="_blank"
|
|
|
|
href="https://www.tradingview.com"
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</p>
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!ALLOWED_TRADINGVIEW_HOSTNAMES.includes(window.location.hostname)) {
|
|
|
|
return pennantMenuItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CHARTING_LIBRARY_PATH) {
|
|
|
|
return pennantMenuItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (chartlib) {
|
|
|
|
case 'tradingview': {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{tradingViewMenuItems}
|
|
|
|
{commonMenuItems}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case 'pennant': {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{pennantMenuItems}
|
|
|
|
{commonMenuItems}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw new Error('invalid chart lib');
|
|
|
|
}
|
|
|
|
}
|
2023-08-02 14:29:41 +00:00
|
|
|
};
|