2024-02-08 13:24:48 +00:00
|
|
|
import { VegaIcon, VegaIconNames } from '@vegaprotocol/ui-toolkit';
|
|
|
|
import { MarketSelector } from '../market-selector';
|
|
|
|
import {
|
|
|
|
Last24hPriceChange,
|
|
|
|
useMarket,
|
|
|
|
useMarketList,
|
|
|
|
} from '@vegaprotocol/markets';
|
|
|
|
import { useParams } from 'react-router-dom';
|
|
|
|
import * as PopoverPrimitive from '@radix-ui/react-popover';
|
|
|
|
import { useState } from 'react';
|
|
|
|
import { useT } from '../../lib/use-t';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { MarketHeaderStats } from '../../client-pages/market/market-header-stats';
|
|
|
|
import { MarketMarkPrice } from '../market-mark-price';
|
2024-02-26 16:17:08 +00:00
|
|
|
import { MarketBanner } from '../market-banner';
|
2024-02-08 13:24:48 +00:00
|
|
|
/**
|
|
|
|
* This is only rendered for the mobile navigation
|
|
|
|
*/
|
|
|
|
export const MobileMarketHeader = () => {
|
|
|
|
const t = useT();
|
|
|
|
const { marketId } = useParams();
|
|
|
|
const { data } = useMarket(marketId);
|
|
|
|
const [openMarket, setOpenMarket] = useState(false);
|
|
|
|
const [openPrice, setOpenPrice] = useState(false);
|
|
|
|
|
|
|
|
// Ensure that markets are kept cached so opening the list
|
|
|
|
// shows all markets instantly
|
|
|
|
useMarketList();
|
|
|
|
|
|
|
|
if (!marketId) return null;
|
|
|
|
|
|
|
|
return (
|
2024-02-15 15:41:04 +00:00
|
|
|
<div className="pl-3 pr-2 grid grid-cols-2 h-10 bg-vega-clight-700 dark:bg-vega-cdark-700">
|
2024-02-08 13:24:48 +00:00
|
|
|
<FullScreenPopover
|
|
|
|
open={openMarket}
|
|
|
|
onOpenChange={(x) => {
|
|
|
|
setOpenMarket(x);
|
|
|
|
}}
|
|
|
|
trigger={
|
2024-02-15 15:41:04 +00:00
|
|
|
<button
|
|
|
|
data-testid="popover-trigger"
|
|
|
|
className="min-w-0 flex gap-1 items-center"
|
|
|
|
>
|
|
|
|
<h1 className="whitespace-nowrap overflow-hidden text-ellipsis items-center">
|
|
|
|
<span className="">
|
|
|
|
{data
|
|
|
|
? data.tradableInstrument.instrument.code
|
|
|
|
: t('Select market')}
|
|
|
|
</span>
|
|
|
|
</h1>
|
|
|
|
<VegaIcon
|
|
|
|
name={VegaIconNames.CHEVRON_DOWN}
|
|
|
|
size={16}
|
2024-02-08 13:24:48 +00:00
|
|
|
className={classNames(
|
2024-02-15 15:41:04 +00:00
|
|
|
'origin-center transition-transform ease-in-out duration-300 flex',
|
2024-02-08 13:24:48 +00:00
|
|
|
{
|
|
|
|
'rotate-180': openMarket,
|
|
|
|
}
|
|
|
|
)}
|
2024-02-15 15:41:04 +00:00
|
|
|
/>
|
|
|
|
</button>
|
2024-02-08 13:24:48 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
<MarketSelector
|
|
|
|
currentMarketId={marketId}
|
|
|
|
onSelect={() => setOpenMarket(false)}
|
|
|
|
/>
|
|
|
|
</FullScreenPopover>
|
|
|
|
<FullScreenPopover
|
|
|
|
open={openPrice}
|
|
|
|
onOpenChange={(x) => {
|
|
|
|
setOpenPrice(x);
|
|
|
|
}}
|
|
|
|
trigger={
|
2024-02-15 15:41:04 +00:00
|
|
|
<button
|
|
|
|
data-testid="popover-trigger"
|
|
|
|
className="min-w-0 flex gap-2 items-center justify-end"
|
|
|
|
>
|
2024-02-08 13:24:48 +00:00
|
|
|
{data && (
|
|
|
|
<>
|
2024-02-15 15:41:04 +00:00
|
|
|
<span className="min-w-0 flex flex-col items-end gap-0">
|
|
|
|
<span className="text-sm">
|
|
|
|
<MarketMarkPrice
|
|
|
|
marketId={data.id}
|
|
|
|
decimalPlaces={data.decimalPlaces}
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
<span className="text-xs">
|
|
|
|
<Last24hPriceChange
|
|
|
|
marketId={data.id}
|
|
|
|
decimalPlaces={data.decimalPlaces}
|
|
|
|
fallback={<span />} // dont render anything so price is vertically centered
|
|
|
|
/>
|
|
|
|
</span>
|
2024-02-08 13:24:48 +00:00
|
|
|
</span>
|
2024-02-15 15:41:04 +00:00
|
|
|
<VegaIcon
|
|
|
|
name={VegaIconNames.CHEVRON_DOWN}
|
|
|
|
size={16}
|
|
|
|
className={classNames(
|
|
|
|
'min-w-0 transition-transform ease-in-out duration-300',
|
|
|
|
{
|
|
|
|
'rotate-180': openPrice,
|
|
|
|
}
|
|
|
|
)}
|
|
|
|
/>
|
2024-02-08 13:24:48 +00:00
|
|
|
</>
|
|
|
|
)}
|
2024-02-15 15:41:04 +00:00
|
|
|
</button>
|
2024-02-08 13:24:48 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
{data && (
|
2024-02-26 16:17:08 +00:00
|
|
|
<div>
|
|
|
|
<MarketBanner market={data} />
|
|
|
|
<div className="px-3 py-6 text-sm grid grid-cols-2 items-center gap-x-4 gap-y-6">
|
|
|
|
<MarketHeaderStats market={data} />
|
|
|
|
</div>
|
2024-02-08 13:24:48 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</FullScreenPopover>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-02-15 15:41:04 +00:00
|
|
|
interface PopoverProps extends PopoverPrimitive.PopoverProps {
|
2024-02-08 13:24:48 +00:00
|
|
|
trigger: React.ReactNode | string;
|
|
|
|
}
|
|
|
|
|
2024-02-15 15:41:04 +00:00
|
|
|
const FullScreenPopover = ({
|
2024-02-08 13:24:48 +00:00
|
|
|
trigger,
|
|
|
|
children,
|
|
|
|
open,
|
|
|
|
onOpenChange,
|
|
|
|
}: PopoverProps) => {
|
|
|
|
return (
|
|
|
|
<PopoverPrimitive.Root open={open} onOpenChange={onOpenChange}>
|
2024-02-15 15:41:04 +00:00
|
|
|
<PopoverPrimitive.Trigger asChild={true}>
|
2024-02-08 13:24:48 +00:00
|
|
|
{trigger}
|
|
|
|
</PopoverPrimitive.Trigger>
|
|
|
|
<PopoverPrimitive.Portal>
|
|
|
|
<PopoverPrimitive.Content
|
|
|
|
data-testid="popover-content"
|
|
|
|
className="w-screen bg-vega-clight-800 dark:bg-vega-cdark-800 border-y border-default"
|
|
|
|
sideOffset={0}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</PopoverPrimitive.Content>
|
|
|
|
</PopoverPrimitive.Portal>
|
|
|
|
</PopoverPrimitive.Root>
|
|
|
|
);
|
|
|
|
};
|