feat: impl accounts and channels

This commit is contained in:
2026-09-06 18:17:42 +08:00
parent aa9999a940
commit 1ccee1414c
70 changed files with 9879 additions and 31 deletions
+316
View File
@@ -0,0 +1,316 @@
"use client"
import * as React from "react"
import { CircleAlertIcon, Loader2Icon } from "lucide-react"
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import {
createAccountAction,
updateAccountAction,
type AccountInput,
type AccountWithChannels,
} from "@/lib/actions/account"
type AccountType = "BANK" | "E_WALLET" | "CASH"
const accountLabels = {
BANK: "银行账户",
E_WALLET: "电子钱包",
CASH: "现金",
} as const
const balanceLabels = {
ASSET: "资产类",
LIABILITY: "负债类",
EQUITY: "权益类",
} as const
export function AccountDialog({
open,
onOpenChange,
account,
onSuccess,
}: {
open: boolean
onOpenChange: (open: boolean) => void
account?: AccountWithChannels | null
onSuccess: () => void
}) {
const editing = Boolean(account)
const [accountType, setAccountType] = React.useState<AccountType>(
account?.accountType || "BANK"
)
const [balanceType, setBalanceType] = React.useState<
"ASSET" | "LIABILITY" | "EQUITY"
>(account?.balanceType || "ASSET")
const [name, setName] = React.useState(account?.name || "")
const [primaryCurrency, setPrimaryCurrency] = React.useState(
account?.primaryCurrency || ""
)
const [supportedCurrencies, setSupportedCurrencies] = React.useState(
account?.supportedCurrencies?.join(", ") || ""
)
const [remark, setRemark] = React.useState(account?.remark || "")
const [issuerName, setIssuerName] = React.useState(account?.issuerName || "")
const [accountNumber, setAccountNumber] = React.useState(
account?.accountNumber || ""
)
const [platform, setPlatform] = React.useState(account?.platform || "")
const [accountId, setAccountId] = React.useState(account?.accountId || "")
const [location, setLocation] = React.useState(account?.location || "")
const [isPending, setIsPending] = React.useState(false)
const [errorMessage, setErrorMessage] = React.useState<string | null>(null)
const submit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault()
setErrorMessage(null)
if (!name.trim()) {
setErrorMessage("请输入账户名称")
return
}
const currencies = supportedCurrencies
.split(/[,\s]+/)
.map((item) => item.trim().toUpperCase())
.filter(Boolean)
const payload: AccountInput = {
name: name.trim(),
accountType,
balanceType,
primaryCurrency: primaryCurrency.trim().toUpperCase() || null,
supportedCurrencies: currencies.length ? currencies : null,
remark: remark.trim() || null,
issuerName: accountType === "BANK" ? issuerName.trim() || null : null,
accountNumber:
accountType === "BANK" ? accountNumber.trim() || null : null,
platform: accountType === "E_WALLET" ? platform.trim() || null : null,
accountId: accountType === "E_WALLET" ? accountId.trim() || null : null,
location: accountType === "CASH" ? location.trim() || null : null,
}
setIsPending(true)
try {
const result =
editing && account
? await updateAccountAction(account.id, payload)
: await createAccountAction(payload)
if (!result.success) {
setErrorMessage(result.error || "保存失败,请检查输入")
return
}
onOpenChange(false)
onSuccess()
} catch {
setErrorMessage("网络异常,提交失败")
} finally {
setIsPending(false)
}
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-lg">
<DialogHeader>
<DialogTitle>{editing ? "编辑资金账户" : "新建资金账户"}</DialogTitle>
<DialogDescription>
</DialogDescription>
</DialogHeader>
<form id="account-form" onSubmit={submit}>
<FieldGroup>
{errorMessage && (
<Alert variant="destructive">
<CircleAlertIcon />
<AlertTitle></AlertTitle>
<AlertDescription>{errorMessage}</AlertDescription>
</Alert>
)}
<FieldGroup className="grid gap-3 sm:grid-cols-2">
<Field>
<FieldLabel htmlFor="accountType"></FieldLabel>
<Select
value={accountType}
onValueChange={(value) =>
value && setAccountType(value as AccountType)
}
>
<SelectTrigger id="accountType">
<SelectValue>{accountLabels[accountType]}</SelectValue>
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectItem value="BANK"></SelectItem>
<SelectItem value="E_WALLET"></SelectItem>
<SelectItem value="CASH"></SelectItem>
</SelectGroup>
</SelectContent>
</Select>
</Field>
<Field>
<FieldLabel htmlFor="balanceType"></FieldLabel>
<Select
value={balanceType}
onValueChange={(value) =>
value && setBalanceType(value as typeof balanceType)
}
>
<SelectTrigger id="balanceType">
<SelectValue>{balanceLabels[balanceType]}</SelectValue>
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectItem value="ASSET"></SelectItem>
<SelectItem value="LIABILITY"></SelectItem>
<SelectItem value="EQUITY"></SelectItem>
</SelectGroup>
</SelectContent>
</Select>
</Field>
</FieldGroup>
<FieldGroup>
<Field>
<FieldLabel htmlFor="accountName"></FieldLabel>
<Input
id="accountName"
value={name}
onChange={(event) => setName(event.target.value)}
placeholder="例如:招商银行个人消费卡账户"
disabled={isPending}
required
/>
</Field>
<FieldGroup className="grid gap-3 sm:grid-cols-2">
<Field>
<FieldLabel htmlFor="primaryCurrency"></FieldLabel>
<Input
id="primaryCurrency"
value={primaryCurrency}
onChange={(event) => setPrimaryCurrency(event.target.value)}
placeholder="例如:HKD"
disabled={isPending}
/>
</Field>
<Field>
<FieldLabel htmlFor="supportedCurrencies">
</FieldLabel>
<Input
id="supportedCurrencies"
value={supportedCurrencies}
onChange={(event) =>
setSupportedCurrencies(event.target.value)
}
placeholder="用逗号分隔"
disabled={isPending}
/>
</Field>
</FieldGroup>
</FieldGroup>
<FieldGroup className="grid gap-3 sm:grid-cols-2">
{accountType === "BANK" && (
<>
<Field>
<FieldLabel htmlFor="issuerName"></FieldLabel>
<Input
id="issuerName"
value={issuerName}
onChange={(event) => setIssuerName(event.target.value)}
placeholder="例如:汇丰银行"
disabled={isPending}
/>
</Field>
<Field>
<FieldLabel htmlFor="accountNumber">
</FieldLabel>
<Input
id="accountNumber"
value={accountNumber}
onChange={(event) => setAccountNumber(event.target.value)}
placeholder="可填写完整账号或尾号"
disabled={isPending}
/>
</Field>
</>
)}
{accountType === "E_WALLET" && (
<>
<Field>
<FieldLabel htmlFor="platform"></FieldLabel>
<Input
id="platform"
value={platform}
onChange={(event) => setPlatform(event.target.value)}
placeholder="例如:支付宝"
disabled={isPending}
/>
</Field>
<Field>
<FieldLabel htmlFor="accountId"></FieldLabel>
<Input
id="accountId"
value={accountId}
onChange={(event) => setAccountId(event.target.value)}
placeholder="手机号、邮箱或会员号"
disabled={isPending}
/>
</Field>
</>
)}
{accountType === "CASH" && (
<Field>
<FieldLabel htmlFor="location"></FieldLabel>
<Input
id="location"
value={location}
onChange={(event) => setLocation(event.target.value)}
placeholder="例如:随身钱包"
disabled={isPending}
/>
</Field>
)}
</FieldGroup>
<Field>
<FieldLabel htmlFor="remark"></FieldLabel>
<Input
id="remark"
value={remark}
onChange={(event) => setRemark(event.target.value)}
placeholder="可选"
disabled={isPending}
/>
</Field>
</FieldGroup>
</form>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => onOpenChange(false)}
disabled={isPending}
>
</Button>
<Button form="account-form" type="submit" disabled={isPending}>
{isPending && (
<Loader2Icon data-icon="inline-start" className="animate-spin" />
)}
{editing ? "保存修改" : "创建账户"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}