section-registry.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { parseCurrencyDisplayType } from '@/lib/currency'
  2. import type { BillingSettings } from '../types'
  3. import { createSectionRegistry } from '../utils/section-registry'
  4. import { CheckinSettingsSection } from '../general/checkin-settings-section'
  5. import { PricingSection } from '../general/pricing-section'
  6. import { QuotaSettingsSection } from '../general/quota-settings-section'
  7. import { PaymentSettingsSection } from '../integrations/payment-settings-section'
  8. import { RatioSettingsCard } from '../models/ratio-settings-card'
  9. const getModelDefaults = (settings: BillingSettings) => ({
  10. ModelPrice: settings.ModelPrice,
  11. ModelRatio: settings.ModelRatio,
  12. CacheRatio: settings.CacheRatio,
  13. CreateCacheRatio: settings.CreateCacheRatio,
  14. CompletionRatio: settings.CompletionRatio,
  15. ImageRatio: settings.ImageRatio,
  16. AudioRatio: settings.AudioRatio,
  17. AudioCompletionRatio: settings.AudioCompletionRatio,
  18. ExposeRatioEnabled: settings.ExposeRatioEnabled,
  19. BillingMode: settings['billing_setting.billing_mode'],
  20. BillingExpr: settings['billing_setting.billing_expr'],
  21. })
  22. const getGroupDefaults = (settings: BillingSettings) => ({
  23. TopupGroupRatio: settings.TopupGroupRatio,
  24. GroupRatio: settings.GroupRatio,
  25. UserUsableGroups: settings.UserUsableGroups,
  26. GroupGroupRatio: settings.GroupGroupRatio,
  27. AutoGroups: settings.AutoGroups,
  28. DefaultUseAutoGroup: settings.DefaultUseAutoGroup,
  29. GroupSpecialUsableGroup:
  30. settings['group_ratio_setting.group_special_usable_group'],
  31. })
  32. const BILLING_SECTIONS = [
  33. {
  34. id: 'quota',
  35. titleKey: 'Quota Settings',
  36. descriptionKey: 'Configure user quota allocation and rewards',
  37. build: (settings: BillingSettings) => (
  38. <QuotaSettingsSection
  39. defaultValues={{
  40. QuotaForNewUser: settings.QuotaForNewUser,
  41. PreConsumedQuota: settings.PreConsumedQuota,
  42. QuotaForInviter: settings.QuotaForInviter,
  43. QuotaForInvitee: settings.QuotaForInvitee,
  44. TopUpLink: settings.TopUpLink,
  45. general_setting: {
  46. docs_link: settings['general_setting.docs_link'],
  47. },
  48. quota_setting: {
  49. enable_free_model_pre_consume:
  50. settings['quota_setting.enable_free_model_pre_consume'],
  51. },
  52. }}
  53. />
  54. ),
  55. },
  56. {
  57. id: 'currency',
  58. titleKey: 'Currency & Display',
  59. descriptionKey: 'Configure currency conversion and quota display options',
  60. build: (settings: BillingSettings) => (
  61. <PricingSection
  62. defaultValues={{
  63. QuotaPerUnit: settings.QuotaPerUnit,
  64. USDExchangeRate: settings.USDExchangeRate,
  65. DisplayInCurrencyEnabled: settings.DisplayInCurrencyEnabled,
  66. DisplayTokenStatEnabled: settings.DisplayTokenStatEnabled,
  67. general_setting: {
  68. quota_display_type: parseCurrencyDisplayType(
  69. settings['general_setting.quota_display_type']
  70. ),
  71. custom_currency_symbol:
  72. settings['general_setting.custom_currency_symbol'] ?? '¤',
  73. custom_currency_exchange_rate:
  74. settings['general_setting.custom_currency_exchange_rate'] ?? 1,
  75. },
  76. }}
  77. />
  78. ),
  79. },
  80. {
  81. id: 'model-pricing',
  82. titleKey: 'Model Pricing',
  83. descriptionKey: 'Configure model pricing ratios and tool prices',
  84. build: (settings: BillingSettings) => (
  85. <RatioSettingsCard
  86. titleKey='Model Pricing'
  87. descriptionKey='Configure model pricing ratios and tool prices'
  88. modelDefaults={getModelDefaults(settings)}
  89. groupDefaults={getGroupDefaults(settings)}
  90. toolPricesDefault={settings['tool_price_setting.prices']}
  91. visibleTabs={['models', 'tool-prices', 'upstream-sync']}
  92. />
  93. ),
  94. },
  95. {
  96. id: 'group-pricing',
  97. titleKey: 'Group Pricing',
  98. descriptionKey: 'Configure group ratios and group-specific pricing rules',
  99. build: (settings: BillingSettings) => (
  100. <RatioSettingsCard
  101. titleKey='Group Pricing'
  102. descriptionKey='Configure group ratios and group-specific pricing rules'
  103. modelDefaults={getModelDefaults(settings)}
  104. groupDefaults={getGroupDefaults(settings)}
  105. toolPricesDefault={settings['tool_price_setting.prices']}
  106. visibleTabs={['groups']}
  107. />
  108. ),
  109. },
  110. {
  111. id: 'payment',
  112. titleKey: 'Payment Gateway',
  113. descriptionKey: 'Configure payment gateway integrations',
  114. build: (settings: BillingSettings) => (
  115. <PaymentSettingsSection
  116. defaultValues={{
  117. PayAddress: settings.PayAddress,
  118. EpayId: settings.EpayId,
  119. EpayKey: settings.EpayKey,
  120. Price: settings.Price,
  121. MinTopUp: settings.MinTopUp,
  122. CustomCallbackAddress: settings.CustomCallbackAddress,
  123. PayMethods: settings.PayMethods,
  124. AmountOptions: settings['payment_setting.amount_options'],
  125. AmountDiscount: settings['payment_setting.amount_discount'],
  126. StripeApiSecret: settings.StripeApiSecret,
  127. StripeWebhookSecret: settings.StripeWebhookSecret,
  128. StripePriceId: settings.StripePriceId,
  129. StripeUnitPrice: settings.StripeUnitPrice,
  130. StripeMinTopUp: settings.StripeMinTopUp,
  131. StripePromotionCodesEnabled: settings.StripePromotionCodesEnabled,
  132. CreemApiKey: settings.CreemApiKey,
  133. CreemWebhookSecret: settings.CreemWebhookSecret,
  134. CreemTestMode: settings.CreemTestMode,
  135. CreemProducts: settings.CreemProducts,
  136. }}
  137. waffoDefaultValues={{
  138. WaffoEnabled: settings.WaffoEnabled ?? false,
  139. WaffoApiKey: settings.WaffoApiKey ?? '',
  140. WaffoPrivateKey: settings.WaffoPrivateKey ?? '',
  141. WaffoPublicCert: settings.WaffoPublicCert ?? '',
  142. WaffoSandboxPublicCert: settings.WaffoSandboxPublicCert ?? '',
  143. WaffoSandboxApiKey: settings.WaffoSandboxApiKey ?? '',
  144. WaffoSandboxPrivateKey: settings.WaffoSandboxPrivateKey ?? '',
  145. WaffoSandbox: settings.WaffoSandbox ?? false,
  146. WaffoMerchantId: settings.WaffoMerchantId ?? '',
  147. WaffoCurrency: settings.WaffoCurrency ?? 'USD',
  148. WaffoUnitPrice: settings.WaffoUnitPrice ?? 1,
  149. WaffoMinTopUp: settings.WaffoMinTopUp ?? 1,
  150. WaffoNotifyUrl: settings.WaffoNotifyUrl ?? '',
  151. WaffoReturnUrl: settings.WaffoReturnUrl ?? '',
  152. WaffoPayMethods: settings.WaffoPayMethods ?? '[]',
  153. }}
  154. waffoPancakeDefaultValues={{
  155. WaffoPancakeEnabled: settings.WaffoPancakeEnabled ?? false,
  156. WaffoPancakeSandbox: settings.WaffoPancakeSandbox ?? false,
  157. WaffoPancakeMerchantID: settings.WaffoPancakeMerchantID ?? '',
  158. WaffoPancakePrivateKey: settings.WaffoPancakePrivateKey ?? '',
  159. WaffoPancakeWebhookPublicKey:
  160. settings.WaffoPancakeWebhookPublicKey ?? '',
  161. WaffoPancakeWebhookTestKey:
  162. settings.WaffoPancakeWebhookTestKey ?? '',
  163. WaffoPancakeStoreID: settings.WaffoPancakeStoreID ?? '',
  164. WaffoPancakeProductID: settings.WaffoPancakeProductID ?? '',
  165. WaffoPancakeReturnURL: settings.WaffoPancakeReturnURL ?? '',
  166. WaffoPancakeCurrency: settings.WaffoPancakeCurrency ?? 'USD',
  167. WaffoPancakeUnitPrice: settings.WaffoPancakeUnitPrice ?? 1,
  168. WaffoPancakeMinTopUp: settings.WaffoPancakeMinTopUp ?? 1,
  169. }}
  170. />
  171. ),
  172. },
  173. {
  174. id: 'checkin',
  175. titleKey: 'Check-in Rewards',
  176. descriptionKey: 'Configure daily check-in rewards for users',
  177. build: (settings: BillingSettings) => (
  178. <CheckinSettingsSection
  179. defaultValues={{
  180. enabled: settings['checkin_setting.enabled'],
  181. minQuota: settings['checkin_setting.min_quota'],
  182. maxQuota: settings['checkin_setting.max_quota'],
  183. }}
  184. />
  185. ),
  186. },
  187. ] as const
  188. export type BillingSectionId = (typeof BILLING_SECTIONS)[number]['id']
  189. const billingRegistry = createSectionRegistry<BillingSectionId, BillingSettings>(
  190. {
  191. sections: BILLING_SECTIONS,
  192. defaultSection: 'quota',
  193. basePath: '/system-settings/billing',
  194. urlStyle: 'path',
  195. }
  196. )
  197. export const BILLING_SECTION_IDS = billingRegistry.sectionIds
  198. export const BILLING_DEFAULT_SECTION = billingRegistry.defaultSection
  199. export const getBillingSectionNavItems = billingRegistry.getSectionNavItems
  200. export const getBillingSectionContent = billingRegistry.getSectionContent