feat: 添加剩余金额查看功能

This commit is contained in:
2026-01-09 11:51:39 +08:00
parent 0c6e642a33
commit ea63c83cbe
2 changed files with 73 additions and 24 deletions

View File

@@ -1,8 +1,10 @@
"use client" "use client"
import { useState } from "react" import { useCallback, useState } from "react"
import { getRedPacketDrawsByRedPacketId } from "@/app/server/actions/get-red-packet-draws" import { getRedPacketDrawsByRedPacketId } from "@/app/server/actions/get-red-packet-draws"
import { formatAmount } from "@/lib/format-amount" import { formatAmount } from "@/lib/format-amount"
import { CreateRedPacketPayload } from "@/lib/types/red-packet"
import { formatAmountFromInt, parseAmountToInt } from "@/lib/utils"
type Draw = { type Draw = {
id: string id: string
@@ -12,10 +14,14 @@ type Draw = {
} }
export function DrawList({ export function DrawList({
count,
rule,
precision, precision,
redPacketId, redPacketId,
initialDraws, initialDraws,
}: { }: {
count: number
rule: CreateRedPacketPayload['rule']
precision: number precision: number
redPacketId: string redPacketId: string
initialDraws: Draw[] initialDraws: Draw[]
@@ -37,6 +43,39 @@ export function DrawList({
} }
} }
const getBalance = useCallback(() => {
const used = draws.reduce((p, draw) => {
const pInt = parseAmountToInt(p, precision)
const dInt = parseAmountToInt(draw.amount, precision)
return formatAmountFromInt(dInt + pInt, precision)
}, '0.0');
if (rule.type === 'fixed') {
const amount = formatAmountFromInt(
parseAmountToInt(rule.singleAmount, precision) * BigInt(count),
precision
)
return formatAmountFromInt(
parseAmountToInt(amount, precision) - parseAmountToInt(used, precision),
precision,
)
} else if (rule.type === 'luck') {
const amount = formatAmountFromInt(
parseAmountToInt(rule.totalAmount, precision),
precision
)
return formatAmountFromInt(
parseAmountToInt(amount, precision) - parseAmountToInt(used, precision),
precision,
)
} else if (rule.type === 'random') {
return ''
}
return ''
}, [rule, draws])
return ( return (
<section className="rounded-lg border p-3 space-y-2"> <section className="rounded-lg border p-3 space-y-2">
{/* 标题 + 刷新按钮 */} {/* 标题 + 刷新按钮 */}
@@ -54,6 +93,7 @@ export function DrawList({
{draws.length === 0 ? ( {draws.length === 0 ? (
<div className="text-sm text-gray-400"></div> <div className="text-sm text-gray-400"></div>
) : ( ) : (
<>
<ul className="space-y-1"> <ul className="space-y-1">
{draws.map((draw) => ( {draws.map((draw) => (
<li <li
@@ -79,6 +119,13 @@ export function DrawList({
</li> </li>
))} ))}
</ul> </ul>
{getBalance() && <div className="border-t py-1">
<div className="text-sm">
<span></span>
<span>{getBalance()}</span>
</div>
</div>}
</>
)} )}
</section> </section>
) )

View File

@@ -73,6 +73,8 @@ export default async function AdminRedPacketPage({ params }: Props) {
</section> </section>
<DrawList <DrawList
count={redPacket.count}
rule={redPacket.rule}
precision={precision} precision={precision}
redPacketId={redPacketId} redPacketId={redPacketId}
initialDraws={initialDraws} initialDraws={initialDraws}