Files
auto-trade/features/dashboard/components/StatusHeader.tsx

220 lines
8.8 KiB
TypeScript
Raw Normal View History

2026-02-12 14:20:07 +09:00
import Link from "next/link";
import { Activity, RefreshCcw, Settings2, Wifi } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card";
import type { DashboardBalanceSummary } from "@/features/dashboard/types/dashboard.types";
import {
formatCurrency,
2026-02-13 12:17:35 +09:00
formatSignedCurrency,
formatSignedPercent,
2026-02-12 14:20:07 +09:00
getChangeToneClass,
} from "@/features/dashboard/utils/dashboard-format";
import { cn } from "@/lib/utils";
interface StatusHeaderProps {
summary: DashboardBalanceSummary | null;
isKisRestConnected: boolean;
isWebSocketReady: boolean;
2026-02-13 12:17:35 +09:00
isRealtimePending: boolean;
isProfileVerified: boolean;
verifiedAccountNo: string | null;
2026-02-12 14:20:07 +09:00
isRefreshing: boolean;
lastUpdatedAt: string | null;
onRefresh: () => void;
}
/**
* @description (///) .
* @see features/dashboard/components/DashboardContainer.tsx .
*/
export function StatusHeader({
summary,
isKisRestConnected,
isWebSocketReady,
2026-02-13 12:17:35 +09:00
isRealtimePending,
isProfileVerified,
verifiedAccountNo,
2026-02-12 14:20:07 +09:00
isRefreshing,
lastUpdatedAt,
onRefresh,
}: StatusHeaderProps) {
const toneClass = getChangeToneClass(summary?.totalProfitLoss ?? 0);
const updatedLabel = lastUpdatedAt
? new Date(lastUpdatedAt).toLocaleTimeString("ko-KR", {
hour12: false,
})
: "--:--:--";
2026-02-13 12:17:35 +09:00
const hasApiTotalAmount =
Boolean(summary) && (summary?.apiReportedTotalAmount ?? 0) > 0;
const hasApiNetAssetAmount =
Boolean(summary) && (summary?.apiReportedNetAssetAmount ?? 0) > 0;
const isApiTotalAmountDifferent =
Boolean(summary) &&
Math.abs(
(summary?.apiReportedTotalAmount ?? 0) - (summary?.totalAmount ?? 0),
) >= 1;
const realtimeStatusLabel = isWebSocketReady
? isRealtimePending
? "수신 대기중"
: "연결됨"
: "미연결";
2026-02-12 14:20:07 +09:00
return (
<Card className="relative overflow-hidden border-brand-200 shadow-sm dark:border-brand-800/50">
{/* ========== BACKGROUND DECORATION ========== */}
<div className="pointer-events-none absolute inset-x-0 top-0 h-20 bg-linear-to-r from-brand-100/70 via-brand-50/50 to-transparent dark:from-brand-900/35 dark:via-brand-900/20" />
<CardContent className="relative grid gap-3 p-4 md:grid-cols-[1fr_1fr_1fr_auto]">
{/* ========== TOTAL ASSET ========== */}
<div className="rounded-xl border border-border/70 bg-background/90 px-4 py-3">
2026-02-13 12:17:35 +09:00
<p className="text-xs font-medium text-muted-foreground"> ( )</p>
2026-02-12 14:20:07 +09:00
<p className="mt-1 text-xl font-semibold tracking-tight">
{summary ? `${formatCurrency(summary.totalAmount)}` : "-"}
</p>
<p className="mt-1 text-xs text-muted-foreground">
2026-02-13 12:17:35 +09:00
() {summary ? `${formatCurrency(summary.cashBalance)}` : "-"}
2026-02-12 14:20:07 +09:00
</p>
<p className="mt-1 text-xs text-muted-foreground">
2026-02-13 12:17:35 +09:00
{" "}
{summary ? `${formatCurrency(summary.evaluationAmount)}` : "-"}
</p>
2026-02-13 12:17:35 +09:00
<p className="mt-1 text-xs text-muted-foreground">
(KIS){" "}
{summary ? `${formatCurrency(summary.totalDepositAmount)}` : "-"}
</p>
<p className="mt-1 text-[11px] text-muted-foreground/80">
.
</p>
<p className="mt-1 text-xs text-muted-foreground">
( ){" "}
{summary ? `${formatCurrency(summary.netAssetAmount)}` : "-"}
</p>
{hasApiTotalAmount && isApiTotalAmountDifferent ? (
<p className="mt-1 text-xs text-muted-foreground">
KIS {formatCurrency(summary?.apiReportedTotalAmount ?? 0)}
</p>
) : null}
{hasApiNetAssetAmount ? (
<p className="mt-1 text-xs text-muted-foreground">
KIS {formatCurrency(summary?.apiReportedNetAssetAmount ?? 0)}
</p>
) : null}
2026-02-12 14:20:07 +09:00
</div>
{/* ========== PROFIT/LOSS ========== */}
<div className="rounded-xl border border-border/70 bg-background/90 px-4 py-3">
<p className="text-xs font-medium text-muted-foreground"> </p>
2026-02-13 12:17:35 +09:00
<p
className={cn(
"mt-1 text-xl font-semibold tracking-tight",
toneClass,
)}
>
{summary ? `${formatSignedCurrency(summary.totalProfitLoss)}` : "-"}
2026-02-12 14:20:07 +09:00
</p>
<p className={cn("mt-1 text-xs font-medium", toneClass)}>
2026-02-13 12:17:35 +09:00
{summary ? formatSignedPercent(summary.totalProfitRate) : "-"}
2026-02-12 14:20:07 +09:00
</p>
<p className="mt-1 text-xs text-muted-foreground">
2026-02-13 12:17:35 +09:00
{" "}
{summary ? `${formatCurrency(summary.evaluationAmount)}` : "-"}
</p>
<p className="mt-1 text-xs text-muted-foreground">
2026-02-13 12:17:35 +09:00
{" "}
{summary ? `${formatCurrency(summary.purchaseAmount)}` : "-"}
</p>
2026-02-12 14:20:07 +09:00
</div>
{/* ========== CONNECTION STATUS ========== */}
<div className="rounded-xl border border-border/70 bg-background/90 px-4 py-3">
<p className="text-xs font-medium text-muted-foreground"> </p>
2026-02-12 14:20:07 +09:00
<div className="mt-2 flex flex-wrap items-center gap-2 text-xs font-medium">
<span
className={cn(
"inline-flex items-center gap-1 rounded-full px-2 py-1",
isKisRestConnected
? "bg-emerald-500/10 text-emerald-600 dark:text-emerald-400"
: "bg-red-500/10 text-red-600 dark:text-red-400",
)}
>
<Wifi className="h-3.5 w-3.5" />
{isKisRestConnected ? "연결됨" : "연결 끊김"}
2026-02-12 14:20:07 +09:00
</span>
<span
className={cn(
"inline-flex items-center gap-1 rounded-full px-2 py-1",
isWebSocketReady
2026-02-13 12:17:35 +09:00
? isRealtimePending
? "bg-amber-500/10 text-amber-700 dark:text-amber-400"
: "bg-emerald-500/10 text-emerald-600 dark:text-emerald-400"
2026-02-12 14:20:07 +09:00
: "bg-muted text-muted-foreground",
)}
>
<Activity className="h-3.5 w-3.5" />
2026-02-13 12:17:35 +09:00
{realtimeStatusLabel}
</span>
<span
className={cn(
"inline-flex items-center gap-1 rounded-full px-2 py-1",
isProfileVerified
? "bg-emerald-500/10 text-emerald-600 dark:text-emerald-400"
: "bg-amber-500/10 text-amber-700 dark:text-amber-400",
)}
>
<Activity className="h-3.5 w-3.5" />
{isProfileVerified ? "완료" : "미완료"}
2026-02-12 14:20:07 +09:00
</span>
</div>
<p className="mt-2 text-xs text-muted-foreground">
{updatedLabel}
</p>
<p className="mt-1 text-xs text-muted-foreground">
{maskAccountNo(verifiedAccountNo)}
</p>
<p className="mt-1 text-xs text-muted-foreground">
{summary ? `${formatCurrency(summary.loanAmount)}` : "-"}
2026-02-12 14:20:07 +09:00
</p>
</div>
{/* ========== QUICK ACTIONS ========== */}
2026-02-13 12:17:35 +09:00
<div className="flex flex-col gap-2 sm:flex-row md:flex-col md:items-stretch md:justify-center">
2026-02-12 14:20:07 +09:00
<Button
type="button"
variant="outline"
onClick={onRefresh}
disabled={isRefreshing}
className="w-full border-brand-200 text-brand-700 hover:bg-brand-50 dark:border-brand-700 dark:text-brand-300 dark:hover:bg-brand-900/30"
>
<RefreshCcw
2026-02-13 12:17:35 +09:00
className={cn("h-4 w-4 mr-2", isRefreshing ? "animate-spin" : "")}
2026-02-12 14:20:07 +09:00
/>
2026-02-12 14:20:07 +09:00
</Button>
<Button
asChild
2026-02-13 12:17:35 +09:00
className="w-full bg-brand-600 text-white hover:bg-brand-700 dark:bg-brand-600 dark:text-white dark:hover:bg-brand-500"
2026-02-12 14:20:07 +09:00
>
<Link href="/settings">
2026-02-13 12:17:35 +09:00
<Settings2 className="h-4 w-4 mr-2" />
2026-02-12 14:20:07 +09:00
</Link>
</Button>
</div>
</CardContent>
</Card>
);
}
/**
* @description .
* @param value (8-2)
* @returns
* @see features/dashboard/components/StatusHeader.tsx
*/
function maskAccountNo(value: string | null) {
if (!value) return "-";
const digits = value.replace(/\D/g, "");
if (digits.length !== 10) return "********";
return "********-**";
}