Files
web/src/components/composites/purchase/long/center.tsx

165 lines
5.2 KiB
TypeScript
Raw Normal View History

'use client'
import {FormField} from '@/components/ui/form'
import {RadioGroup} from '@/components/ui/radio-group'
import FormOption from '@/components/composites/purchase/option'
import {Schema} from '@/components/composites/purchase/long/form'
2026-04-18 15:47:20 +08:00
import {useEffect} from 'react'
import {useFormContext, useWatch} from 'react-hook-form'
2025-06-09 19:04:25 +08:00
import {Card} from '@/components/ui/card'
2026-04-18 14:30:30 +08:00
import {BillingMethodField} from '../shared/billing-method-field'
import {FeatureList} from '../shared/feature-list'
import {NumberStepperField} from '../shared/number-stepper-field'
2026-04-18 15:47:20 +08:00
import {getAvailablePurchaseExpires, getAvailablePurchaseLives, getPurchaseSkuPrice, hasPurchaseSku, PurchaseSkuData} from '../shared/sku'
2026-04-18 15:47:20 +08:00
export default function Center({skuData}: {
skuData: PurchaseSkuData
}) {
2026-04-18 15:47:20 +08:00
const {setValue} = useFormContext<Schema>()
2026-04-18 14:30:30 +08:00
const type = useWatch<Schema>({name: 'type'}) as Schema['type']
2026-04-18 15:47:20 +08:00
const live = useWatch<Schema>({name: 'live'}) as Schema['live']
2026-04-18 14:30:30 +08:00
const expire = useWatch<Schema>({name: 'expire'}) as Schema['expire']
2026-04-18 15:47:20 +08:00
const {modeList, priceMap} = skuData
const liveList = type === '1'
? getAvailablePurchaseLives(skuData, {mode: type, expire})
: getAvailablePurchaseLives(skuData, {mode: type})
const expireList = type === '1'
? getAvailablePurchaseExpires(skuData, {mode: type, live})
: []
useEffect(() => {
const nextType = modeList.includes(type) ? type : modeList[0]
if (!nextType) {
return
}
if (nextType !== type) {
setValue('type', nextType)
return
}
const nextLiveList = nextType === '1'
? getAvailablePurchaseLives(skuData, {mode: nextType, expire})
: getAvailablePurchaseLives(skuData, {mode: nextType})
const nextLive = nextLiveList.includes(live) ? live : nextLiveList[0]
if (nextLive && nextLive !== live) {
setValue('live', nextLive)
return
}
if (nextType === '2') {
if (expire !== '0') {
setValue('expire', '0')
}
return
}
const nextExpireList = getAvailablePurchaseExpires(skuData, {mode: nextType, live: nextLive})
if (!nextExpireList.includes(expire) && nextExpireList[0]) {
setValue('expire', nextExpireList[0])
}
}, [expire, live, modeList, setValue, skuData, type])
return (
2025-06-09 19:04:25 +08:00
<Card className="flex-auto p-6 flex flex-col gap-6 relative">
2026-04-18 15:47:20 +08:00
<BillingMethodField modeList={modeList} timeDailyLimit={100}/>
{/* IP 时效 */}
2026-04-18 14:30:30 +08:00
<FormField<Schema, 'live'>
className="space-y-4"
name="live"
label="IP 时效">
{({id, field}) => (
<RadioGroup
id={id}
value={field.value}
2026-04-18 15:47:20 +08:00
onValueChange={(value) => {
field.onChange(value)
if (type !== '1') {
return
}
const nextExpireList = getAvailablePurchaseExpires(skuData, {mode: type, live: value})
if (!nextExpireList.includes(expire) && nextExpireList[0]) {
setValue('expire', nextExpireList[0])
}
}}
2026-04-18 14:30:30 +08:00
className="grid grid-cols-[repeat(auto-fill,minmax(120px,1fr))] gap-4">
{liveList.map((live) => {
2026-04-18 15:47:20 +08:00
const priceExpire = type === '1' && !hasPurchaseSku(skuData, {mode: type, live, expire})
? getAvailablePurchaseExpires(skuData, {mode: type, live})[0] || '0'
: String(expire)
2026-04-18 14:30:30 +08:00
const price = getPurchaseSkuPrice(priceMap, {
mode: type,
live,
2026-04-18 15:47:20 +08:00
expire: priceExpire,
2026-04-18 14:30:30 +08:00
})
return (
2026-04-18 14:30:30 +08:00
<FormOption
key={live}
id={`${id}-${live}`}
value={live}
label={`${Number(live) / 60} 小时`}
description={price && `${price}/IP`}
compare={field.value}
/>
)
2026-04-18 14:30:30 +08:00
})}
</RadioGroup>
)}
</FormField>
{/* 套餐时效 */}
{type === '1' && (
<FormField className="space-y-4" name="expire" label="套餐时效">
{({id, field}) => (
2026-04-18 15:47:20 +08:00
<RadioGroup
id={id}
value={field.value}
onValueChange={(value) => {
field.onChange(value)
const nextLiveList = getAvailablePurchaseLives(skuData, {mode: type, expire: value})
if (!nextLiveList.includes(live) && nextLiveList[0]) {
setValue('live', nextLiveList[0])
}
}}
className="flex gap-4 flex-wrap">
2026-04-18 14:30:30 +08:00
{expireList.map(day => (
<FormOption
key={day}
id={`${id}-${day}`}
value={day}
label={`${day}`}
compare={field.value}
/>
))}
</RadioGroup>
)}
</FormField>
)}
2026-04-18 14:30:30 +08:00
{/* 每日提取上限/购买数量 */}
{type === '1' ? (
<NumberStepperField
name="daily_limit"
label="每日提取上限"
min={100}
step={100}
/>
) : (
<NumberStepperField
name="quota"
label="IP 购买数量"
min={500}
step={100}
/>
)}
2026-04-18 14:30:30 +08:00
<FeatureList/>
</Card>
)
}