feat(accounts): enrich brokerage account list
This commit is contained in:
@@ -1,5 +1,17 @@
|
||||
import type { BrokerageAccount } from "../api/useBrokerageAccounts";
|
||||
|
||||
type Money = NonNullable<
|
||||
BrokerageAccount["portfolioSummary"]
|
||||
>["totalsByCurrency"][number];
|
||||
|
||||
type BrokerageAccountCard = {
|
||||
account: BrokerageAccount;
|
||||
summary:
|
||||
| { state: "unavailable" }
|
||||
| { state: "empty" }
|
||||
| { state: "available"; totals: Money[]; remainingCurrencyCount: number };
|
||||
};
|
||||
|
||||
function accountTypeKey(type: BrokerageAccount["type"]) {
|
||||
return `accounts.types.${type}` as const;
|
||||
}
|
||||
@@ -10,4 +22,31 @@ function errorCode(error: unknown): string | undefined {
|
||||
: undefined;
|
||||
}
|
||||
|
||||
export { accountTypeKey, errorCode };
|
||||
function brokerageAccountCards(
|
||||
accounts: BrokerageAccount[],
|
||||
): BrokerageAccountCard[] {
|
||||
return accounts.map((account) => {
|
||||
const summary = account.portfolioSummary;
|
||||
if (!summary) return { account, summary: { state: "unavailable" } };
|
||||
if (!summary.totalsByCurrency.length)
|
||||
return { account, summary: { state: "empty" } };
|
||||
return {
|
||||
account,
|
||||
summary: {
|
||||
state: "available",
|
||||
totals: summary.totalsByCurrency.slice(0, 2),
|
||||
remainingCurrencyCount: Math.max(
|
||||
summary.totalsByCurrency.length - 2,
|
||||
0,
|
||||
),
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export {
|
||||
accountTypeKey,
|
||||
type BrokerageAccountCard,
|
||||
brokerageAccountCards,
|
||||
errorCode,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user