refactor: simplify auth and instrument pages
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
export { section, type SectionState } from "./model/section";
|
||||
export { useInstrumentParameters } from "./model/useInstrumentParameters";
|
||||
export { InstrumentChart } from "./ui/InstrumentChart/InstrumentChart.view";
|
||||
export { InstrumentEvents } from "./ui/InstrumentEvents.view";
|
||||
export { InstrumentHeader } from "./ui/InstrumentHeader/InstrumentHeader.view";
|
||||
export { InstrumentPageState } from "./ui/InstrumentPageState.view";
|
||||
export { InstrumentPosition } from "./ui/InstrumentPosition.view";
|
||||
export { InstrumentSection } from "./ui/InstrumentSection.view";
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { useSearchParams } from "react-router";
|
||||
|
||||
import type { OperationCategory } from "@/entities/broker-operation";
|
||||
|
||||
const instrumentPeriods = ["month", "halfYear", "year", "all"] as const;
|
||||
|
||||
function useInstrumentParameters(incomeFilter: "coupons" | "dividends") {
|
||||
const [parameters, setParameters] = useSearchParams();
|
||||
|
||||
const period = instrumentPeriods.find(
|
||||
value => value === parameters.get("period"),
|
||||
) ?? "month";
|
||||
|
||||
const filters: { categories: OperationCategory[]; value: string }[] = [
|
||||
{ categories: [], value: "all" },
|
||||
{ categories: ["trade"], value: "trades" },
|
||||
{ categories: ["income"], value: incomeFilter },
|
||||
{ categories: ["tax", "fee", "transfer", "other"], value: "other" },
|
||||
];
|
||||
|
||||
const { categories, value: filter } = filters.find(
|
||||
item => item.value === parameters.get("events"),
|
||||
) ?? filters[0];
|
||||
|
||||
const setParameter = (key: string, value: string) => {
|
||||
const next = new URLSearchParams(parameters);
|
||||
next.set(key, value);
|
||||
setParameters(next, { replace: true });
|
||||
};
|
||||
|
||||
return {
|
||||
categories,
|
||||
filter,
|
||||
period,
|
||||
search: parameters.size > 0 ? `?${parameters.toString()}` : "",
|
||||
setFilter: (value: string) => setParameter("events", value),
|
||||
setPeriod: (value: string) => setParameter("period", value),
|
||||
};
|
||||
}
|
||||
|
||||
export { instrumentPeriods, useInstrumentParameters };
|
||||
@@ -4,6 +4,8 @@ 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();
|
||||
@@ -34,7 +36,7 @@ function useInstrumentChartVm(props: InstrumentChartProps) {
|
||||
return { ...candle, price };
|
||||
});
|
||||
|
||||
const periodOptions = ["month", "halfYear", "year", "all"].map(value => ({
|
||||
const periodOptions = instrumentPeriods.map(value => ({
|
||||
label: t(`stock.periods.${value}`),
|
||||
value,
|
||||
}));
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
.header {
|
||||
padding: var(--mantine-spacing-lg);
|
||||
border-radius: var(--mantine-radius-lg);
|
||||
min-width: 0;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.identity {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--mantine-spacing-md);
|
||||
min-width: 0;
|
||||
}
|
||||
@media (max-width: 48em) {
|
||||
.header {
|
||||
padding: var(--mantine-spacing-md);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import type { FC, PropsWithChildren, ReactNode } from "react";
|
||||
|
||||
import { Avatar, Button, Group, Paper, Text, Title } from "@mantine/core";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import classes from "./InstrumentHeader.module.css";
|
||||
|
||||
type InstrumentHeaderProps = PropsWithChildren<{
|
||||
logoSrc: null | string;
|
||||
name: string;
|
||||
onRefresh: () => void;
|
||||
refreshing: boolean;
|
||||
subtitle: ReactNode;
|
||||
}>;
|
||||
|
||||
const InstrumentHeader: FC<InstrumentHeaderProps> = (props) => {
|
||||
const { children, logoSrc, name, onRefresh, refreshing, subtitle } = props;
|
||||
const { t } = useTranslation();
|
||||
|
||||
return (
|
||||
<Paper className={classes.header} component="header" withBorder>
|
||||
<Group align="start" justify="space-between">
|
||||
<div className={classes.identity}>
|
||||
<Avatar alt="" key={logoSrc} radius="md" size={56} src={logoSrc}>
|
||||
{name[0]}
|
||||
</Avatar>
|
||||
|
||||
<div>
|
||||
<Title order={1} size="h2">{name}</Title>
|
||||
|
||||
<Text c="dimmed" size="sm">{subtitle}</Text>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Button disabled={refreshing} onClick={onRefresh} variant="subtle">
|
||||
{t("investment.refresh")}
|
||||
</Button>
|
||||
</Group>
|
||||
|
||||
{children}
|
||||
</Paper>
|
||||
);
|
||||
};
|
||||
|
||||
export { InstrumentHeader };
|
||||
@@ -0,0 +1,66 @@
|
||||
import type { FC } from "react";
|
||||
|
||||
import { Anchor, Button, Skeleton, Stack, Text, Title } from "@mantine/core";
|
||||
import { LoadingSkeleton } from "@wealthledger/ui";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Link } from "react-router";
|
||||
|
||||
import { ApiErrorCode, isApiErrorCode } from "@/shared/api";
|
||||
|
||||
type InstrumentPageStateProps = {
|
||||
code?: string;
|
||||
kind: "bond" | "stock";
|
||||
loading: boolean;
|
||||
retry: () => void;
|
||||
};
|
||||
|
||||
const InstrumentPageState: FC<InstrumentPageStateProps> = (props) => {
|
||||
const { code, kind, loading, retry } = props;
|
||||
const { t } = useTranslation();
|
||||
if (loading) return (
|
||||
<LoadingSkeleton label={t(`${kind}.loading`)}>
|
||||
<Stack>
|
||||
<Skeleton h={110} />
|
||||
|
||||
<Skeleton h={350} />
|
||||
|
||||
<Skeleton h={170} />
|
||||
</Stack>
|
||||
</LoadingSkeleton>
|
||||
);
|
||||
|
||||
const ambiguousCode = kind === "stock"
|
||||
? ApiErrorCode.StockTickerAmbiguous
|
||||
: ApiErrorCode.BondTickerAmbiguous;
|
||||
|
||||
let message = t(`${kind}.unavailableText`);
|
||||
if (isApiErrorCode(code, ambiguousCode))
|
||||
message = t(`${kind}.ambiguousTicker`);
|
||||
if (
|
||||
isApiErrorCode(code, ApiErrorCode.TbankNotConnected)
|
||||
|| isApiErrorCode(code, ApiErrorCode.InvalidTbankToken)
|
||||
)
|
||||
message = t(`${kind}.connect`);
|
||||
|
||||
return (
|
||||
<Stack role="alert">
|
||||
<Title order={1}>{t(`${kind}.unavailable`)}</Title>
|
||||
|
||||
<Text>{message}</Text>
|
||||
|
||||
<Anchor component={Link} to={`/${kind}s`}>
|
||||
{t(`${kind}.backToSearch`)}
|
||||
</Anchor>
|
||||
|
||||
<Anchor component={Link} to="/settings">
|
||||
{t("accounts.openSettings")}
|
||||
</Anchor>
|
||||
|
||||
<Button onClick={retry} variant="default">
|
||||
{t("accounts.retry")}
|
||||
</Button>
|
||||
</Stack>
|
||||
);
|
||||
};
|
||||
|
||||
export { InstrumentPageState };
|
||||
Reference in New Issue
Block a user