refactor: enforce frontend view model boundaries
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { formatChartValue, formatDecimal, formatMoney } from "@/shared/lib";
|
||||
|
||||
import type { InstrumentChartVm } from "../../model/types";
|
||||
|
||||
type InstrumentChartProps = { priceUnit?: "percent"; vm: InstrumentChartVm };
|
||||
|
||||
function useInstrumentChartVm(props: InstrumentChartProps) {
|
||||
const { vm } = props;
|
||||
const { t } = useTranslation();
|
||||
|
||||
const priceRows = vm.priceRows.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 = ["month", "halfYear", "year", "all"].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
|
||||
= vm.averageLine === null
|
||||
? []
|
||||
: [
|
||||
{
|
||||
color: "orange.7",
|
||||
ifOverflow: "extendDomain" as const,
|
||||
label: t("stock.averageLine"),
|
||||
y: vm.averageLine,
|
||||
},
|
||||
];
|
||||
|
||||
return {
|
||||
chartLabel,
|
||||
formatChartValue,
|
||||
periodOptions,
|
||||
priceRows,
|
||||
referenceLines,
|
||||
t,
|
||||
vm,
|
||||
};
|
||||
}
|
||||
|
||||
export { useInstrumentChartVm };
|
||||
Reference in New Issue
Block a user