trading-app/src/app/(app)/reel/page.tsx
David 234efda586 Fix trade proposals, speed, Trade Republic setup and stock UI
- Buy by EUR amount (fractional shares, like Trade Republic): 200 EUR can
  now reach every large cap; risk engine sizes in whole euros
- No-AI screener (pullback in uptrend) feeds training cards and AI candidates
- Auto-update on app open via after(); "find ideas now" button (3/day)
- New Actions tab: stock list, stock page with chart, news, AI opinion,
  sandbox buy by amount or shares
- "Mon compte" tab: declare deposits, cash, holdings and PEA plan
  (no broker credentials, no PEA invented by default)
- Yahoo data cached and deduplicated, pages stream per section
- Tests: fractional sizing, screener, no-AI idea flow

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 19:05:20 +01:00

128 lines
5.1 KiB
TypeScript

import { Suspense } from "react";
import { connection } from "next/server";
import { eq } from "drizzle-orm";
import { AccountForm, AddHolding, CashForms, PeaForm, RealPositions } from "@/components/account-forms";
import { SettingsLink } from "@/components/nav";
import { ClosedList } from "@/components/positions";
import { Badge, Card, CardSkeleton, Delta, Page, Stat } from "@/components/ui";
import { getDb, schema } from "@/lib/db";
import { eur, signedEur } from "@/lib/format";
import { getClosedPositions, getPortfolioState } from "@/lib/portfolio";
import { defaultPeaPlan, getSettings } from "@/lib/settings";
import { getPea } from "@/lib/stats";
import { formatDateFr, parisDate } from "@/lib/time";
async function AccountContent() {
await connection();
const db = await getDb();
const settings = await getSettings();
const [state, closed, flows] = await Promise.all([
getPortfolioState("real"),
getClosedPositions("real"),
db.select().from(schema.cashFlows).where(eq(schema.cashFlows.portfolioId, "real")),
]);
const deposited = flows.filter((f) => f.kind === "deposit" || f.kind === "withdrawal").reduce((s, f) => s + f.amount, 0);
const toPea = Math.max(0, -flows.filter((f) => f.kind === "to_pea").reduce((s, f) => s + f.amount, 0));
const realActive = Boolean(settings.realUnlockedAt) && !settings.realLockedReason;
const configured = Boolean(settings.trAccountOpenedAt);
const gain = state.equity + toPea - deposited;
return (
<>
<Card>
<p className="text-sm leading-relaxed text-muted">
🔒 Trade Republic n&apos;offre pas de connexion pour les applications : l&apos;app ne se branche jamais à ton compte et ne te demandera jamais tes identifiants.
Tu lui indiques ce que tu as, elle fait les calculs et te dit quoi faire.
</p>
</Card>
<Card title="1 · Mon compte-titres" tone={configured ? undefined : "warn"} action={configured ? <Badge tone="accent">configuré</Badge> : <Badge tone="warn">à faire</Badge>}>
{configured && <p className="mb-3 text-xs text-muted">Ouvert le {formatDateFr(settings.trAccountOpenedAt!)}.</p>}
<AccountForm
openedAt={settings.trAccountOpenedAt}
monthly={settings.realMonthlyDeposit}
day={settings.realDepositDay}
auto={Boolean(settings.realDepositsStart)}
firstTime={!configured && deposited === 0}
/>
</Card>
<Card title="Ma cagnotte réelle" action={realActive ? <Badge tone="accent">Mode réel actif</Badge> : <Badge tone="warn">Simulation d&apos;abord</Badge>}>
<div className="flex items-end justify-between">
<p className="num text-3xl font-semibold">{eur(state.equity)}</p>
{deposited > 0 && <Delta value={gain}>{signedEur(gain)}</Delta>}
</div>
<div className="mt-3 grid grid-cols-3 gap-2 text-sm">
<Stat label="liquidités">{eur(state.cash, 0)}</Stat>
<Stat label="versés">{eur(deposited, 0)}</Stat>
<Stat label="partis au PEA">{eur(toPea, 0)}</Stat>
</div>
{!realActive && (
<p className="mt-3 text-xs leading-relaxed text-muted">
Tant que l&apos;IA n&apos;a pas prouvé en simulation qu&apos;elle bat le S&amp;P 500, elle ne te propose pas de trade réel : ton argent attend sur le compte. Tu peux quand même
enregistrer ici ce que tu fais de ton côté.
</p>
)}
<div className="mt-4">
<CashForms cash={state.cash} />
</div>
</Card>
<Card title="2 · Mes actions chez Trade Republic">
<div className="flex flex-col gap-3">
<RealPositions positions={state.positions} />
<AddHolding />
</div>
</Card>
<Suspense fallback={<CardSkeleton title="3 · Mon PEA" height="h-40" />}>
<PeaSection />
</Suspense>
{closed.length > 0 && (
<Card title="Historique réel">
<ClosedList positions={closed} />
</Card>
)}
</>
);
}
async function PeaSection() {
await connection();
const settings = await getSettings();
const plan = settings.peaPlan ?? defaultPeaPlan(parisDate());
const pea = await getPea(settings);
return (
<section id="pea">
<Card title="3 · Mon PEA (cœur)">
{pea && (
<div className="mb-4 grid grid-cols-3 gap-2 text-sm">
<Stat label="valeur">{eur(pea.value, 0)}</Stat>
<Stat label="versés">{eur(pea.invested, 0)}</Stat>
<Stat label="parts">{pea.shares.toLocaleString("fr-FR", { maximumFractionDigits: 3 })}</Stat>
</div>
)}
<PeaForm
has={Boolean(settings.peaPlan)}
symbol={plan.etfSymbol}
amount={plan.monthlyAmount}
day={plan.dayOfMonth}
start={plan.startDate}
calibration={plan.calibration?.shares ?? null}
/>
</Card>
</section>
);
}
export default function AccountPage() {
return (
<Page title="Mon compte" subtitle="Ton compte Trade Republic : compte-titres (satellite) et PEA (cœur)." action={<SettingsLink />}>
<Suspense fallback={<CardSkeleton height="h-64" />}>
<AccountContent />
</Suspense>
</Page>
);
}