79 lines
1.8 KiB
TypeScript
79 lines
1.8 KiB
TypeScript
import { useTranslation } from "react-i18next";
|
|
|
|
import { formatDate, formatDecimal, formatMoney } from "@/shared/lib";
|
|
|
|
import type { InstrumentChartProps } from "../../model/types";
|
|
|
|
import { instrumentPeriods } from "../../model/useInstrumentParameters";
|
|
|
|
function useInstrumentChartVm(props: InstrumentChartProps) {
|
|
const { vm } = props;
|
|
const { t } = useTranslation();
|
|
|
|
const chartData = vm.candles?.candles.map(candle => ({
|
|
...candle,
|
|
date: formatDate(candle.time),
|
|
price: Number(candle.close),
|
|
})) ?? [];
|
|
|
|
const average = vm.position?.averagePurchasePrice;
|
|
|
|
const averageLine = average && props.priceUnit !== "percent"
|
|
&& average.currency === vm.candles?.currency
|
|
? Number(average.value)
|
|
: null;
|
|
|
|
const priceRows = chartData.toReversed().map((candle) => {
|
|
const currency = vm.candles?.currency;
|
|
const value = candle.close;
|
|
|
|
const amount = currency ? { currency, value } : undefined;
|
|
|
|
const price = props.priceUnit === "percent"
|
|
? `${formatDecimal(value)} %`
|
|
: formatMoney(amount);
|
|
|
|
return { ...candle, price };
|
|
});
|
|
|
|
const periodOptions = instrumentPeriods.map(value => ({
|
|
label: t(`stock.periods.${value}`),
|
|
value,
|
|
}));
|
|
|
|
const chartLabel = t(
|
|
props.priceUnit === "percent" ? "bond.chartLabel" : "stock.chartLabel",
|
|
{
|
|
currency:
|
|
props.priceUnit === "percent"
|
|
? t("bond.percentOfNominal")
|
|
: (vm.candles?.currency ?? ""),
|
|
},
|
|
);
|
|
|
|
const referenceLines
|
|
= averageLine === null || !Number.isFinite(averageLine)
|
|
? []
|
|
: [
|
|
{
|
|
color: "orange.7",
|
|
ifOverflow: "extendDomain" as const,
|
|
label: t("stock.averageLine"),
|
|
y: averageLine,
|
|
},
|
|
];
|
|
|
|
return {
|
|
chartData,
|
|
chartLabel,
|
|
hasIncompleteCandle: chartData.some(candle => !candle.isComplete),
|
|
periodOptions,
|
|
priceRows,
|
|
referenceLines,
|
|
t,
|
|
vm,
|
|
};
|
|
}
|
|
|
|
export { useInstrumentChartVm };
|