Files
frontend/src/features/instrument-details/ui/InstrumentChart/InstrumentChart.vm.ts
T
ksv741 b40066838c
Frontend verify / commits (pull_request) Successful in 35s
Frontend verify / security (pull_request) Successful in 10s
Frontend verify / verify (pull_request) Successful in 7m20s
refactor!: support RUB only (WealthLedger/main#55)
Adopt RUB-only API contract; remove currency cards, selectors,
screener currency filter and multi-currency branches.

BREAKING CHANGE: requires the RUB-only backend contract.
2026-11-11 08:22:44 +03:00

78 lines
1.7 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"
? 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")
: "₽",
},
);
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 };