feat: group all-account rows by instrument and day (WealthLedger/main#47) (#126)
This commit was merged in pull request #126.
This commit is contained in:
@@ -1,5 +1,13 @@
|
||||
import { formatShare, type Portfolio } from "@/entities/portfolio";
|
||||
import { formatDecimal, formatMoney, instrumentLogoUrl } from "@/shared/lib";
|
||||
import {
|
||||
type Decimal,
|
||||
decimalFromString,
|
||||
decimalToString,
|
||||
formatDecimal,
|
||||
formatMoney,
|
||||
instrumentLogoUrl,
|
||||
sumDecimals,
|
||||
} from "@/shared/lib";
|
||||
|
||||
type AccountPagePosition = Portfolio["positions"][number] & {
|
||||
instrumentTo?: string;
|
||||
@@ -23,8 +31,6 @@ type AccountPagePositionSection = {
|
||||
|
||||
type AccountPageReturnTone = "negative" | "positive";
|
||||
|
||||
type Decimal = { scale: number; value: bigint };
|
||||
|
||||
type Money = NonNullable<Portfolio["positions"][number]["absoluteReturn"]>;
|
||||
|
||||
type PositionSectionKind = "bonds"
|
||||
@@ -32,38 +38,11 @@ type PositionSectionKind = "bonds"
|
||||
| "stocks";
|
||||
|
||||
const RADIX = 10n;
|
||||
const DECIMAL_PARTS = 2;
|
||||
const PERCENT_SCALE = 6;
|
||||
const DISPLAY_PERCENT_SCALE = 2;
|
||||
const PERCENT_FACTOR = 100n;
|
||||
const ROUNDING_FACTOR = 2n;
|
||||
|
||||
function decimalFromString(value: string): Decimal {
|
||||
const [whole = "0", fraction = ""] = value.split(".", DECIMAL_PARTS);
|
||||
|
||||
return { scale: fraction.length, value: BigInt(`${whole}${fraction}`) };
|
||||
}
|
||||
|
||||
function decimalToString(decimal: Decimal): string {
|
||||
const sign = decimal.value < 0n ? "-" : "";
|
||||
|
||||
const digits = (decimal.value < 0n ? -decimal.value : decimal.value)
|
||||
.toString()
|
||||
.padStart(decimal.scale + 1, "0");
|
||||
|
||||
const whole = decimal.scale
|
||||
? digits.slice(0, -decimal.scale)
|
||||
: digits;
|
||||
|
||||
let fraction = decimal.scale ? digits.slice(-decimal.scale) : "";
|
||||
|
||||
while (fraction.endsWith("0")) fraction = fraction.slice(0, -1);
|
||||
|
||||
const suffix = fraction ? `.${fraction}` : "";
|
||||
|
||||
return `${sign}${whole}${suffix}`;
|
||||
}
|
||||
|
||||
function formatPositionReturnPercent(value: string): string {
|
||||
const decimal = decimalFromString(value);
|
||||
let rounded = decimal.value;
|
||||
@@ -167,18 +146,6 @@ function returnPercent(
|
||||
return decimalToString({ scale: PERCENT_SCALE, value: rounded });
|
||||
}
|
||||
|
||||
function sumDecimals(values: Decimal[]): Decimal {
|
||||
const scale = Math.max(...values.map(value => value.scale));
|
||||
|
||||
const sum = values.reduce(
|
||||
(total, value) => total
|
||||
+ value.value * RADIX ** BigInt(scale - value.scale),
|
||||
0n,
|
||||
);
|
||||
|
||||
return { scale, value: sum };
|
||||
}
|
||||
|
||||
function toAccountPagePositionSections(
|
||||
portfolio: Portfolio,
|
||||
): AccountPagePositionSection[] {
|
||||
|
||||
@@ -33,6 +33,7 @@ type AccountPageRecentOperations
|
||||
stale?: boolean;
|
||||
state: "loaded" | "partial";
|
||||
to: string;
|
||||
totalVisible?: boolean;
|
||||
}
|
||||
| {
|
||||
state: "empty"
|
||||
@@ -84,6 +85,7 @@ function toRecentEvents(
|
||||
items: events.data?.events.slice(0, RECENT_EVENTS_LIMIT) ?? [],
|
||||
state: "partial",
|
||||
to,
|
||||
totalVisible: false,
|
||||
};
|
||||
if (!events.data && (events.isError || events.meta?.lastAttempt.state === "failed"))
|
||||
return { state: "error", to };
|
||||
@@ -94,6 +96,7 @@ function toRecentEvents(
|
||||
items,
|
||||
state: "loaded",
|
||||
to,
|
||||
totalVisible: events.meta?.completeness === "complete",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -109,7 +112,12 @@ function toRecentOperations(
|
||||
)
|
||||
return { state: "loading", to };
|
||||
if (operations.meta?.completeness === "incomplete")
|
||||
return { items: operations.data?.items ?? [], state: "partial", to };
|
||||
return {
|
||||
items: operations.data?.items ?? [],
|
||||
state: "partial",
|
||||
to,
|
||||
totalVisible: false,
|
||||
};
|
||||
if (
|
||||
!operations.data
|
||||
&& (operations.isError || operations.meta?.lastAttempt.state === "failed")
|
||||
@@ -125,6 +133,7 @@ function toRecentOperations(
|
||||
stale: operations.meta?.freshness === "stale",
|
||||
state: "loaded",
|
||||
to,
|
||||
totalVisible: operations.meta?.completeness === "complete",
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user