feat: портфели в разделе «Криптовалюта»

Криптовалюту теперь можно разделять на отдельные портфели (например,
долгосрочные инвестиции, накопления на недвижимость, спекулятивные
монеты) вместо одного общего списка — по тому же принципу, что уже
есть у счетов (Account): у портфеля есть имя, цветная метка и
архивация, каждая монета обязательно принадлежит одному портфелю,
один и тот же токен можно держать в нескольких портфелях сразу.
This commit is contained in:
ZProger
2026-09-04 01:39:18 +03:00
parent c2f59c8773
commit aefd6cc035
21 changed files with 933 additions and 50 deletions

View File

@@ -3,28 +3,34 @@ import { ApiError } from "@/api/client";
import { searchCryptoCoins } from "@/api/crypto";
import { Button } from "@/components/ui/Button";
import { Dialog } from "@/components/ui/Dialog";
import { Input, Label } from "@/components/ui/Input";
import { useCreateCryptoHolding } from "@/hooks/useCrypto";
import { Input, Label, Select } from "@/components/ui/Input";
import { useCreateCryptoHolding, useCryptoPortfolios } from "@/hooks/useCrypto";
import { useTranslation } from "@/lib/i18n";
import type { CryptoSearchResult } from "@/types";
interface CryptoAddModalProps {
open: boolean;
onClose: () => void;
// The portfolio tab active on the Crypto page when "+" was clicked — pre-
// selects that portfolio here instead of always defaulting to the first
// one. Null (the "All" tab) falls back to the first portfolio in the list.
defaultPortfolioId?: number | null;
}
/** Two phases in one dialog: search CoinGecko for a coin, then pick one and
* enter how much of it is held. No separate multi-step wizard — unlike CSV
* import, there's nothing here worth a dedicated page for. */
export function CryptoAddModal({ open, onClose }: CryptoAddModalProps) {
export function CryptoAddModal({ open, onClose, defaultPortfolioId }: CryptoAddModalProps) {
const { t } = useTranslation();
const createHolding = useCreateCryptoHolding();
const { data: portfolios } = useCryptoPortfolios();
const [query, setQuery] = useState("");
const [results, setResults] = useState<CryptoSearchResult[]>([]);
const [searching, setSearching] = useState(false);
const [searchError, setSearchError] = useState<string | null>(null);
const [selected, setSelected] = useState<CryptoSearchResult | null>(null);
const [portfolioId, setPortfolioId] = useState<number | null>(null);
const [quantity, setQuantity] = useState("");
const [pricePerUnit, setPricePerUnit] = useState("");
const [date, setDate] = useState(() => new Date().toISOString().slice(0, 10));
@@ -42,6 +48,18 @@ export function CryptoAddModal({ open, onClose }: CryptoAddModalProps) {
setSaveError(null);
}, [open]);
// Re-picked every time the dialog opens or the portfolio list loads —
// covers both "opened from a specific tab" and "opened from All before
// any portfolio existed yet, then the default one got created".
useEffect(() => {
if (!open) return;
if (defaultPortfolioId != null) {
setPortfolioId(defaultPortfolioId);
} else if (portfolios && portfolios.length > 0) {
setPortfolioId((current) => (current !== null && portfolios.some((p) => p.id === current) ? current : portfolios[0].id));
}
}, [open, defaultPortfolioId, portfolios]);
useEffect(() => {
if (!open || selected) return;
const trimmed = query.trim();
@@ -73,6 +91,7 @@ export function CryptoAddModal({ open, onClose }: CryptoAddModalProps) {
setSaveError(null);
try {
await createHolding.mutateAsync({
portfolio_id: portfolioId,
coingecko_id: selected.coingecko_id,
symbol: selected.symbol,
name: selected.name,
@@ -145,6 +164,23 @@ export function CryptoAddModal({ open, onClose }: CryptoAddModalProps) {
</span>
</div>
{portfolios && portfolios.length > 1 && (
<div>
<Label htmlFor="crypto-portfolio">{t("crypto.portfolio.form.nameLabel")}</Label>
<Select
id="crypto-portfolio"
value={portfolioId ?? ""}
onChange={(event) => setPortfolioId(Number(event.target.value))}
>
{portfolios.map((portfolio) => (
<option key={portfolio.id} value={portfolio.id}>
{portfolio.name}
</option>
))}
</Select>
</div>
)}
<div className="grid grid-cols-2 gap-3">
<div>
<Label htmlFor="crypto-quantity">{t("crypto.form.quantityLabel")}</Label>

View File

@@ -1,7 +1,7 @@
import { Bitcoin, Plus, Trash2 } from "lucide-react";
import { formatCurrency, maskAmount } from "@/lib/format";
import { useTranslation } from "@/lib/i18n";
import type { CryptoHolding } from "@/types";
import type { CryptoHolding, CryptoPortfolio } from "@/types";
interface CryptoHoldingsTableProps {
items: CryptoHolding[];
@@ -9,6 +9,11 @@ interface CryptoHoldingsTableProps {
onTrade: (holding: CryptoHolding) => void;
onViewHistory: (holding: CryptoHolding) => void;
onDelete: (holding: CryptoHolding) => void;
// Only passed while viewing the "All" tab — renders each row's portfolio
// as a small colored badge under the coin name so it's still clear which
// portfolio it belongs to. Omitted while a single portfolio is selected,
// since every row would carry the same badge.
portfoliosById?: Map<number, CryptoPortfolio>;
}
function PercentCell({ value }: { value: string | null }) {
@@ -23,7 +28,14 @@ function PercentCell({ value }: { value: string | null }) {
);
}
export function CryptoHoldingsTable({ items, hidden, onTrade, onViewHistory, onDelete }: CryptoHoldingsTableProps) {
export function CryptoHoldingsTable({
items,
hidden,
onTrade,
onViewHistory,
onDelete,
portfoliosById,
}: CryptoHoldingsTableProps) {
const { t } = useTranslation();
if (items.length === 0) {
@@ -75,7 +87,20 @@ export function CryptoHoldingsTable({ items, hidden, onTrade, onViewHistory, onD
)}
<span className="min-w-0">
<span className="block truncate text-sm font-medium text-text-primary">{holding.name}</span>
<span className="block text-xs text-text-muted">{holding.symbol}</span>
<span className="flex items-center gap-1.5 text-xs text-text-muted">
{holding.symbol}
{portfoliosById && (
<span className="flex items-center gap-1 truncate">
<span
className="h-1.5 w-1.5 shrink-0 rounded-full"
style={{
backgroundColor: portfoliosById.get(holding.portfolio_id)?.color ?? "var(--text-muted)",
}}
/>
<span className="truncate">{portfoliosById.get(holding.portfolio_id)?.name}</span>
</span>
)}
</span>
</span>
</div>
</td>

View File

@@ -0,0 +1,124 @@
import { type FormEvent, useEffect, useState } from "react";
import { Button } from "@/components/ui/Button";
import { Dialog } from "@/components/ui/Dialog";
import { Input, Label } from "@/components/ui/Input";
import { useCreateCryptoPortfolio, useDeleteCryptoPortfolio, useUpdateCryptoPortfolio } from "@/hooks/useCrypto";
import { useTranslation } from "@/lib/i18n";
import type { CryptoPortfolio } from "@/types";
interface CryptoPortfolioFormModalProps {
open: boolean;
onClose: () => void;
portfolio?: CryptoPortfolio | null;
// Called after a successful delete — lets CryptoPage fall back to the
// "All" tab when the portfolio it was showing just disappeared.
onDeleted?: () => void;
}
/** Create/rename a portfolio, plus (when editing) archive and delete —
* folded into one dialog rather than a separate management page, since a
* portfolio is a lightweight grouping with only a name to edit. */
export function CryptoPortfolioFormModal({ open, onClose, portfolio, onDeleted }: CryptoPortfolioFormModalProps) {
const { t } = useTranslation();
const createPortfolio = useCreateCryptoPortfolio();
const updatePortfolio = useUpdateCryptoPortfolio();
const deletePortfolio = useDeleteCryptoPortfolio();
const [name, setName] = useState("");
const [saveError, setSaveError] = useState<string | null>(null);
const [deleteError, setDeleteError] = useState<string | null>(null);
useEffect(() => {
if (!open) return;
setName(portfolio?.name ?? "");
setSaveError(null);
setDeleteError(null);
}, [open, portfolio]);
const isSaving = createPortfolio.isPending || updatePortfolio.isPending;
async function handleSubmit(event: FormEvent) {
event.preventDefault();
setSaveError(null);
try {
if (portfolio) {
await updatePortfolio.mutateAsync({ id: portfolio.id, input: { name } });
} else {
await createPortfolio.mutateAsync({ name });
}
onClose();
} catch {
setSaveError(t("crypto.portfolio.form.saveError"));
}
}
function handleToggleArchived() {
if (!portfolio) return;
updatePortfolio.mutate({ id: portfolio.id, input: { is_archived: !portfolio.is_archived } });
}
async function handleDelete() {
if (!portfolio) return;
if (!window.confirm(t("crypto.portfolio.confirmDelete", { name: portfolio.name }))) return;
setDeleteError(null);
try {
await deletePortfolio.mutateAsync(portfolio.id);
onDeleted?.();
onClose();
} catch {
// 400 when the portfolio still has coins in it — the only realistic
// failure mode here (see services/crypto_service.py's delete_portfolio).
setDeleteError(t("crypto.portfolio.deleteError"));
}
}
return (
<Dialog
open={open}
onClose={onClose}
title={portfolio ? t("crypto.portfolio.form.editTitle") : t("crypto.portfolio.form.newTitle")}
>
<form onSubmit={handleSubmit} className="space-y-3">
<div>
<Label htmlFor="portfolio-name">{t("crypto.portfolio.form.nameLabel")}</Label>
<Input
id="portfolio-name"
autoFocus
required
maxLength={100}
placeholder={t("crypto.portfolio.form.namePlaceholder")}
value={name}
onChange={(event) => setName(event.target.value)}
/>
</div>
{saveError && <p className="text-sm text-danger">{saveError}</p>}
<div className="flex justify-end gap-2 pt-2">
<Button type="button" variant="ghost" onClick={onClose}>
{t("common.cancel")}
</Button>
<Button type="submit" disabled={isSaving}>
{isSaving ? t("common.saving") : t("common.save")}
</Button>
</div>
{portfolio && (
<div className="flex items-center justify-between border-t border-border pt-3">
<button
type="button"
onClick={handleToggleArchived}
className="text-xs font-medium text-text-muted hover:text-text-primary"
>
{portfolio.is_archived ? t("crypto.portfolio.unarchiveLabel") : t("crypto.portfolio.archiveLabel")}
</button>
<button type="button" onClick={handleDelete} className="text-xs font-medium text-danger hover:underline">
{t("common.delete")}
</button>
</div>
)}
{deleteError && <p className="text-sm text-danger">{deleteError}</p>}
</form>
</Dialog>
);
}

View File

@@ -0,0 +1,78 @@
import { Pencil, Plus } from "lucide-react";
import { cn } from "@/lib/utils";
import { useTranslation } from "@/lib/i18n";
import type { CryptoPortfolio } from "@/types";
interface CryptoPortfolioTabsProps {
portfolios: CryptoPortfolio[];
activeId: number | null;
onChange: (id: number | null) => void;
onAdd: () => void;
onEdit: (portfolio: CryptoPortfolio) => void;
}
/** Horizontally-scrolling pill row (no wrap) so a long list of portfolios
* stays usable on a phone screen instead of pushing the rest of the tab
* down. "All" plus one pill per portfolio, a pencil to edit whichever one
* is active, and a dashed "+" pill to create another. */
export function CryptoPortfolioTabs({ portfolios, activeId, onChange, onAdd, onEdit }: CryptoPortfolioTabsProps) {
const { t } = useTranslation();
const active = portfolios.find((p) => p.id === activeId) ?? null;
return (
<div className="flex items-center gap-1.5 overflow-x-auto pb-1">
<button
type="button"
onClick={() => onChange(null)}
className={cn(
"shrink-0 rounded-full border px-3 py-1.5 text-xs font-medium transition-colors",
activeId === null
? "border-series-1 bg-surface-2 text-text-primary"
: "border-border text-text-muted hover:text-text-primary"
)}
>
{t("crypto.portfolio.all")}
</button>
{portfolios.map((portfolio) => {
const isActive = portfolio.id === activeId;
return (
<button
key={portfolio.id}
type="button"
onClick={() => onChange(portfolio.id)}
className={cn(
"flex shrink-0 items-center gap-1.5 rounded-full border px-3 py-1.5 text-xs font-medium transition-colors",
isActive
? "border-series-1 bg-surface-2 text-text-primary"
: "border-border text-text-muted hover:text-text-primary"
)}
>
<span
className="h-2 w-2 shrink-0 rounded-full"
style={{ backgroundColor: portfolio.color ?? "var(--text-muted)" }}
/>
{portfolio.name}
</button>
);
})}
{active && (
<button
type="button"
aria-label={t("common.edit")}
onClick={() => onEdit(active)}
className="shrink-0 rounded-full p-1.5 text-text-muted hover:bg-surface-2 hover:text-text-primary"
>
<Pencil size={13} />
</button>
)}
<button
type="button"
aria-label={t("crypto.portfolio.addLabel")}
onClick={onAdd}
className="shrink-0 rounded-full border border-dashed border-border p-1.5 text-text-muted hover:border-series-1 hover:text-text-primary"
>
<Plus size={13} />
</button>
</div>
);
}