feat: 添加信息和指标组件以增强充电订单和计量信息的展示

This commit is contained in:
2026-03-13 12:11:33 +08:00
parent 83e6ed2412
commit a6621f975c
5 changed files with 266 additions and 138 deletions

View File

@@ -16,10 +16,11 @@ import {
Table, Table,
TextField, TextField,
} from "@heroui/react"; } from "@heroui/react";
import { ArrowLeft, Pencil, PlugConnection, ArrowRotateRight } from "@gravity-ui/icons"; import { ArrowLeft, Pencil, ArrowRotateRight } from "@gravity-ui/icons";
import { api } from "@/lib/api"; import { api } from "@/lib/api";
import { useSession } from "@/lib/auth-client"; import { useSession } from "@/lib/auth-client";
import dayjs from "@/lib/dayjs"; import dayjs from "@/lib/dayjs";
import InfoSection from "@/components/info-section";
import { Plug } from "lucide-react"; import { Plug } from "lucide-react";
// ── Status maps ──────────────────────────────────────────────────────────── // ── Status maps ────────────────────────────────────────────────────────────
@@ -270,8 +271,7 @@ export default function ChargePointDetailPage({ params }: { params: Promise<{ id
<div className="grid gap-4 md:grid-cols-2"> <div className="grid gap-4 md:grid-cols-2">
{/* Device info — admin only */} {/* Device info — admin only */}
{isAdmin && ( {isAdmin && (
<div className="rounded-xl border border-border bg-surface p-4"> <InfoSection title="设备信息">
<h2 className="mb-3 text-sm font-semibold text-foreground"></h2>
<dl className="divide-y divide-border"> <dl className="divide-y divide-border">
{[ {[
{ label: "品牌", value: cp.chargePointVendor }, { label: "品牌", value: cp.chargePointVendor },
@@ -291,13 +291,12 @@ export default function ChargePointDetailPage({ params }: { params: Promise<{ id
</div> </div>
))} ))}
</dl> </dl>
</div> </InfoSection>
)} )}
{/* Operation info — admin only */} {/* Operation info — admin only */}
{isAdmin && ( {isAdmin && (
<div className="rounded-xl border border-border bg-surface p-4"> <InfoSection title="运行配置">
<h2 className="mb-3 text-sm font-semibold text-foreground"></h2>
<dl className="divide-y divide-border"> <dl className="divide-y divide-border">
<div className="flex items-center justify-between gap-4 py-2"> <div className="flex items-center justify-between gap-4 py-2">
<dt className="shrink-0 text-sm text-muted"></dt> <dt className="shrink-0 text-sm text-muted"></dt>
@@ -369,13 +368,12 @@ export default function ChargePointDetailPage({ params }: { params: Promise<{ id
</dd> </dd>
</div> </div>
</dl> </dl>
</div> </InfoSection>
)} )}
{/* Fee info — user only */} {/* Fee info — user only */}
{!isAdmin && ( {!isAdmin && (
<div className="rounded-xl border border-border bg-surface p-4"> <InfoSection title="电价信息">
<h2 className="mb-3 text-sm font-semibold text-foreground"></h2>
<dl className="divide-y divide-border"> <dl className="divide-y divide-border">
<div className="flex items-center justify-between gap-4 py-2"> <div className="flex items-center justify-between gap-4 py-2">
<dt className="shrink-0 text-sm text-muted"></dt> <dt className="shrink-0 text-sm text-muted"></dt>
@@ -404,7 +402,7 @@ export default function ChargePointDetailPage({ params }: { params: Promise<{ id
</dd> </dd>
</div> </div>
</dl> </dl>
</div> </InfoSection>
)} )}
</div> </div>

View File

@@ -2,15 +2,13 @@
import { useState } from "react"; import { useState } from "react";
import { useQuery } from "@tanstack/react-query"; import { useQuery } from "@tanstack/react-query";
import { Button, Card, Spinner } from "@heroui/react"; import { Button, Spinner } from "@heroui/react";
import Link from "next/link"; import Link from "next/link";
import { import {
Thunderbolt, Thunderbolt,
PlugConnection,
CreditCard, CreditCard,
ChartColumn, ChartColumn,
TagDollar, TagDollar,
Person,
ArrowRotateRight, ArrowRotateRight,
TriangleExclamation, TriangleExclamation,
} from "@gravity-ui/icons"; } from "@gravity-ui/icons";
@@ -27,6 +25,7 @@ import {
type ChartDataPoint, type ChartDataPoint,
} from "@/lib/api"; } from "@/lib/api";
import { BanknoteArrowDown, EvCharger, Plug, Users } from "lucide-react"; import { BanknoteArrowDown, EvCharger, Plug, Users } from "lucide-react";
import MetricIndicator from "@/components/metric-indicator";
// ── Helpers ──────────────────────────────────────────────────────────────── // ── Helpers ────────────────────────────────────────────────────────────────
@@ -72,24 +71,20 @@ function StatCard({
}) { }) {
const s = colorStyles[color]; const s = colorStyles[color];
return ( return (
<Card className={`border-t-2 ${s.border}`}> <MetricIndicator
<Card.Content className="flex flex-col gap-3"> title={title}
<div className="flex items-start justify-between gap-2"> value={value}
<p className="text-sm text-muted">{title}</p> color={s.border}
{Icon && ( valueClassName="text-3xl font-bold tabular-nums leading-none text-foreground"
<div className={`flex size-9 shrink-0 items-center justify-center rounded-xl ${s.bg}`}> icon={
<Icon className={`size-4.5 ${s.icon}`} /> Icon ? (
</div> <div className={`flex size-9 shrink-0 items-center justify-center rounded-xl ${s.bg}`}>
)} <Icon className={`size-4.5 ${s.icon}`} />
</div>
<p className="text-3xl font-bold tabular-nums leading-none text-foreground">{value}</p>
{footer && (
<div className="flex flex-wrap items-center gap-x-1.5 gap-y-1 text-xs text-muted">
{footer}
</div> </div>
)} ) : undefined
</Card.Content> }
</Card> footer={footer}
/>
); );
} }

View File

@@ -4,11 +4,35 @@ import { use, useState } from "react";
import Link from "next/link"; import Link from "next/link";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { useQuery } from "@tanstack/react-query"; import { useQuery } from "@tanstack/react-query";
import { Button, Chip, Modal, Spinner } from "@heroui/react"; import { Alert, Button, Chip, Modal, Spinner } from "@heroui/react";
import { ArrowLeft, ArrowRotateRight, TrashBin } from "@gravity-ui/icons"; import { ArrowLeft, ArrowRotateRight, TrashBin } from "@gravity-ui/icons";
import { APIError, api } from "@/lib/api"; import { APIError, api } from "@/lib/api";
import { useSession } from "@/lib/auth-client"; import { useSession } from "@/lib/auth-client";
import dayjs from "@/lib/dayjs"; import dayjs from "@/lib/dayjs";
import InfoSection from "@/components/info-section";
import MetricIndicator from "@/components/metric-indicator";
import { BanknoteArrowUp, Clock, EvCharger } from "lucide-react";
const stopReasonLabelMap: Record<string, string> = {
EmergencyStop: "紧急停止",
EVDisconnected: "车辆断开",
HardReset: "硬重启",
Local: "本地结束",
Other: "其他原因",
PowerLoss: "断电结束",
Reboot: "重启结束",
Remote: "远程结束",
SoftReset: "软重启",
UnlockCommand: "解锁结束",
DeAuthorized: "鉴权拒绝",
};
const idTagRejectLabelMap: Record<string, string> = {
Blocked: "卡片不可用或余额不足",
Expired: "卡片已过期",
Invalid: "卡片无效",
ConcurrentTx: "该卡已有进行中的订单",
};
function formatDuration(start: string, stop: string | null): string { function formatDuration(start: string, stop: string | null): string {
if (!stop) return "进行中"; if (!stop) return "进行中";
@@ -128,6 +152,14 @@ export default function TransactionDetailPage({ params }: { params: Promise<{ id
const amountFen = tx.chargeAmount ?? tx.estimatedCost; const amountFen = tx.chargeAmount ?? tx.estimatedCost;
const isEstimatedEnergy = tx.energyWh == null && tx.liveEnergyWh != null; const isEstimatedEnergy = tx.energyWh == null && tx.liveEnergyWh != null;
const isEstimatedAmount = tx.chargeAmount == null && tx.estimatedCost != null; const isEstimatedAmount = tx.chargeAmount == null && tx.estimatedCost != null;
const isRejected = tx.stopReason === "DeAuthorized";
const stopReasonLabel = tx.stopReason
? (stopReasonLabelMap[tx.stopReason] ?? tx.stopReason)
: "—";
const rejectReason =
tx.idTagStatus && tx.idTagStatus !== "Accepted"
? (idTagRejectLabelMap[tx.idTagStatus] ?? tx.idTagStatus)
: "鉴权失败";
return ( return (
<div className="space-y-6"> <div className="space-y-6">
@@ -142,8 +174,12 @@ export default function TransactionDetailPage({ params }: { params: Promise<{ id
<div className="flex flex-wrap items-start justify-between gap-4"> <div className="flex flex-wrap items-start justify-between gap-4">
<div className="space-y-1.5"> <div className="space-y-1.5">
<div className="flex flex-wrap items-center gap-2"> <div className="flex flex-wrap items-center gap-2">
<h1 className="font-mono text-2xl font-semibold text-foreground">#{tx.id}</h1> <h1 className="font-mono text-2xl font-semibold text-foreground"> #{tx.id}</h1>
{tx.stopTimestamp ? ( {isRejected ? (
<Chip color="danger" size="sm" variant="soft">
</Chip>
) : tx.stopTimestamp ? (
<Chip color="success" size="sm" variant="soft"> <Chip color="success" size="sm" variant="soft">
</Chip> </Chip>
@@ -157,6 +193,11 @@ export default function TransactionDetailPage({ params }: { params: Promise<{ id
</Chip> </Chip>
)} )}
{tx.stopReason && !isRejected && (
<Chip color="default" size="sm" variant="soft">
{stopReasonLabel}
</Chip>
)}
</div> </div>
<p className="text-sm text-muted"> <p className="text-sm text-muted">
{formatDateTime(tx.startTimestamp)} · {tx.stopTimestamp ? "时长 " : ""} {formatDateTime(tx.startTimestamp)} · {tx.stopTimestamp ? "时长 " : ""}
@@ -251,118 +292,156 @@ export default function TransactionDetailPage({ params }: { params: Promise<{ id
</div> </div>
</div> </div>
<Alert status={isRejected ? "danger" : tx.stopTimestamp ? "success" : "accent"}>
<Alert.Indicator />
<Alert.Content>
<Alert.Title>
{isRejected ? "交易已被系统拒绝" : tx.stopTimestamp ? "本次交易已结束" : "充电进行中"}
</Alert.Title>
<Alert.Description>
{isRejected
? rejectReason
: tx.stopTimestamp
? `结束原因:${stopReasonLabel}`
: "充电进行中,实际充电量和费用以结束时系统计算为准。"}
</Alert.Description>
</Alert.Content>
</Alert>
<div className="grid gap-4 md:grid-cols-4"> <div className="grid gap-4 md:grid-cols-4">
<div className="rounded-xl border border-border bg-surface-secondary p-4"> <MetricIndicator
<p className="text-xs text-muted"></p> title="充电量"
<div className="mt-1 inline-flex items-center gap-1.5"> color="border-success"
<p className="text-xl font-semibold text-foreground">{formatEnergy(energyWh)}</p> icon={<EvCharger className="size-5 text-success" />}
{isEstimatedEnergy && ( value={
<span className="rounded bg-warning-soft px-1 py-0.5 text-[10px] font-semibold text-warning leading-none text-nowrap"> <span className="inline-flex items-center gap-1.5">
{formatEnergy(energyWh)}
</span> {isEstimatedEnergy && (
)} <span className="rounded bg-warning-soft px-1 py-0.5 text-[10px] font-semibold text-warning leading-none text-nowrap">
</div>
</div> </span>
<div className="rounded-xl border border-border bg-surface-secondary p-4"> )}
<p className="text-xs text-muted"></p> </span>
<div className="mt-1 inline-flex items-center gap-1.5"> }
<p className="text-xl font-semibold text-foreground">{formatAmount(amountFen)}</p> />
{isEstimatedAmount && ( <MetricIndicator
<span className="rounded bg-warning-soft px-1 py-0.5 text-[10px] font-semibold text-warning leading-none text-nowrap"> title="总费用"
color="border-accent"
</span> icon={<BanknoteArrowUp className="size-5 text-accent" />}
)} value={
</div> <span className="inline-flex items-center gap-1.5">
</div> {formatAmount(amountFen)}
<div className="rounded-xl border border-border bg-surface-secondary p-4"> {isEstimatedAmount && (
<p className="text-xs text-muted"></p> <span className="rounded bg-warning-soft px-1 py-0.5 text-[10px] font-semibold text-warning leading-none text-nowrap">
<p className="mt-1 text-xl font-semibold text-foreground">
{tx.stopTimestamp ? "已完成" : "进行中"} </span>
</p> )}
</div> </span>
<div className="rounded-xl border border-border bg-surface-secondary p-4"> }
<p className="text-xs text-muted"></p> />
<p className="mt-1 text-xl font-semibold text-foreground">{tx.stopReason ?? "—"}</p> <MetricIndicator
</div> title="订单状态"
icon={<Clock className="size-5 text-foreground" />}
color={
isRejected ? "border-danger" : tx.stopTimestamp ? "border-success" : "border-warning"
}
value={isRejected ? "已拒绝" : tx.stopTimestamp ? "已完成" : "进行中"}
/>
<MetricIndicator title="停止原因" value={isRejected ? rejectReason : stopReasonLabel} />
</div> </div>
<div className="grid gap-4 lg:grid-cols-2"> <div className="grid gap-4 lg:grid-cols-2">
<div className="rounded-xl border border-border bg-surface-secondary"> <InfoSection title="交易信息">
<div className="border-b border-border px-5 py-3.5"> <dl className="divide-y divide-border">
<p className="text-sm font-semibold text-foreground"></p> <div className="flex items-center justify-between gap-4 py-2">
</div> <dt className="shrink-0 text-sm text-muted"></dt>
<dl className="grid grid-cols-[120px_1fr] gap-x-4 gap-y-3 px-5 py-4 text-sm"> <dd className="font-mono text-sm text-foreground">#{tx.id}</dd>
<dt className="text-muted"></dt> </div>
<dd className="font-mono text-foreground">#{tx.id}</dd> <div className="flex items-center justify-between gap-4 py-2">
<dt className="shrink-0 text-sm text-muted"></dt>
<dt className="text-muted"></dt> <dd className="font-mono text-sm text-foreground">{tx.idTag}</dd>
<dd className="font-mono text-foreground">{tx.idTag}</dd> </div>
<div className="flex items-center justify-between gap-4 py-2">
<dt className="text-muted"></dt> <dt className="shrink-0 text-sm text-muted"></dt>
<dd className="font-mono text-foreground">{tx.chargePointIdentifier ?? "—"}</dd> <dd className="font-mono text-sm text-foreground">
{tx.chargePointIdentifier ?? "—"}
<dt className="text-muted"></dt> </dd>
<dd className="text-foreground">{tx.connectorNumber ?? "—"}</dd> </div>
<div className="flex items-center justify-between gap-4 py-2">
<dt className="text-muted"></dt> <dt className="shrink-0 text-sm text-muted"></dt>
<dd className="text-foreground">{formatDateTime(tx.startTimestamp)}</dd> <dd className="text-sm text-foreground">{tx.connectorNumber ?? "—"}</dd>
</div>
<dt className="text-muted"></dt> <div className="flex items-center justify-between gap-4 py-2">
<dd className="text-foreground">{formatDateTime(tx.stopTimestamp)}</dd> <dt className="shrink-0 text-sm text-muted"></dt>
<dd className="text-right text-sm text-foreground">
<dt className="text-muted"></dt> {formatDateTime(tx.startTimestamp)}
<dd className="text-foreground"> </dd>
{formatDuration(tx.startTimestamp, tx.stopTimestamp)} </div>
</dd> <div className="flex items-center justify-between gap-4 py-2">
<dt className="shrink-0 text-sm text-muted"></dt>
<dd className="text-right text-sm text-foreground">
{formatDateTime(tx.stopTimestamp)}
</dd>
</div>
<div className="flex items-center justify-between gap-4 py-2">
<dt className="shrink-0 text-sm text-muted"></dt>
<dd className="text-sm text-foreground">
{formatDuration(tx.startTimestamp, tx.stopTimestamp)}
</dd>
</div>
</dl> </dl>
</div> </InfoSection>
<div className="rounded-xl border border-border bg-surface-secondary"> <InfoSection title="计量与费用">
<div className="border-b border-border px-5 py-3.5"> <dl className="divide-y divide-border">
<p className="text-sm font-semibold text-foreground"></p> <div className="flex items-center justify-between gap-4 py-2">
</div> <dt className="shrink-0 text-sm text-muted"></dt>
<dl className="grid grid-cols-[140px_1fr] gap-x-4 gap-y-3 px-5 py-4 text-sm"> <dd className="text-sm text-foreground">
<dt className="text-muted"></dt> {tx.startMeterValue != null ? `${tx.startMeterValue} Wh` : "—"}
<dd className="text-foreground"> </dd>
{tx.startMeterValue != null ? `${tx.startMeterValue} Wh` : "—"} </div>
</dd> <div className="flex items-center justify-between gap-4 py-2">
<dt className="shrink-0 text-sm text-muted"></dt>
<dt className="text-muted"></dt> <dd className="text-sm text-foreground">
<dd className="text-foreground"> {tx.stopMeterValue != null ? `${tx.stopMeterValue} Wh` : "—"}
{tx.stopMeterValue != null ? `${tx.stopMeterValue} Wh` : "—"} </dd>
</dd> </div>
<div className="flex items-center justify-between gap-4 py-2">
<dt className="text-muted"></dt> <dt className="shrink-0 text-sm text-muted"></dt>
<dd className="text-foreground"> <dd className="text-sm text-foreground">
<span className="inline-flex items-center gap-1"> <span className="inline-flex items-center gap-1">
{formatEnergy(energyWh)} {formatEnergy(energyWh)}
{isEstimatedEnergy && ( {isEstimatedEnergy && (
<span className="rounded bg-warning-soft px-1 py-0.5 text-[10px] font-semibold text-warning leading-none text-nowrap"> <span className="rounded bg-warning-soft px-1 py-0.5 text-[10px] font-semibold text-warning leading-none text-nowrap">
</span> </span>
)} )}
</span> </span>
</dd> </dd>
</div>
<dt className="text-muted"></dt> <div className="flex items-center justify-between gap-4 py-2">
<dd className="text-foreground">{formatAmount(tx.electricityFee)}</dd> <dt className="shrink-0 text-sm text-muted"></dt>
<dd className="text-sm text-foreground">{formatAmount(tx.electricityFee)}</dd>
<dt className="text-muted"></dt> </div>
<dd className="text-foreground">{formatAmount(tx.serviceFee)}</dd> <div className="flex items-center justify-between gap-4 py-2">
<dt className="shrink-0 text-sm text-muted"></dt>
<dt className="text-muted"></dt> <dd className="text-sm text-foreground">{formatAmount(tx.serviceFee)}</dd>
<dd className="text-foreground"> </div>
<span className="inline-flex items-center gap-1"> <div className="flex items-center justify-between gap-4 py-2">
{formatAmount(amountFen)} <dt className="shrink-0 text-sm text-muted"></dt>
{isEstimatedAmount && ( <dd className="text-sm text-foreground">
<span className="rounded bg-warning-soft px-1 py-0.5 text-[10px] font-semibold text-warning leading-none text-nowrap"> <span className="inline-flex items-center gap-1">
{formatAmount(amountFen)}
</span> {isEstimatedAmount && (
)} <span className="rounded bg-warning-soft px-1 py-0.5 text-[10px] font-semibold text-warning leading-none text-nowrap">
</span>
</dd> </span>
)}
</span>
</dd>
</div>
</dl> </dl>
</div> </InfoSection>
</div> </div>
</div> </div>
); );

View File

@@ -0,0 +1,15 @@
import type { ReactNode } from "react";
type InfoSectionProps = {
title: string;
children: ReactNode;
};
export default function InfoSection({ title, children }: InfoSectionProps) {
return (
<div className="rounded-xl border border-border bg-surface p-4">
<h2 className="mb-3 text-sm font-semibold text-foreground">{title}</h2>
{children}
</div>
);
}

View File

@@ -0,0 +1,41 @@
import type { ReactNode } from "react";
import { Card } from "@heroui/react";
type MetricIndicatorProps = {
title: string;
value: ReactNode;
hint?: ReactNode;
footer?: ReactNode;
icon?: ReactNode;
color?: string;
valueClassName?: string;
};
export default function MetricIndicator({
title,
value,
hint,
footer,
icon,
color = "border-border",
valueClassName = "text-xl font-semibold text-foreground tabular-nums",
}: MetricIndicatorProps) {
return (
<Card className={`border-t-2 ${color}`}>
<Card.Content className="flex flex-col gap-3">
<div className="flex items-start justify-between gap-2">
<p className="text-xs text-muted">{title}</p>
{icon}
</div>
<p className={valueClassName}>{value}</p>
{footer ? (
<div className="flex flex-wrap items-center gap-x-1.5 gap-y-1 text-xs text-muted">
{footer}
</div>
) : (
hint && <div className="text-xs text-muted">{hint}</div>
)}
</Card.Content>
</Card>
);
}