返回 AiToEarn
materialUseCount.ts
根目录 / project / aitoearn-web / src / components / draft-box / utils / materialUseCount.ts
1 import type { PromotionMaterial } from '@/api/materials/material.types'
2 import type { PlatType } from '@/app/config/platConfig'
3 import { getPlatformInfoSync } from '@/store/platformMetadata'
4
5 type TranslateFn = (key: string, options?: Record<string, unknown>) => string
6
7 export const UNLIMITED_PLATFORM_MAX_USE_COUNT = 999999
8
9 function getMaterialUseCountPlatforms(material: PromotionMaterial): PlatType[] {
10 const platformSet = new Set<PlatType>()
11
12 material.accountTypes?.forEach(type => platformSet.add(type as PlatType))
13 Object.keys(material.useCountByAccountType ?? {}).forEach(type => platformSet.add(type as PlatType))
14 Object.keys(material.maxUseCountByAccountType ?? {}).forEach(type => platformSet.add(type as PlatType))
15
16 return [...platformSet]
17 }
18
19 export function getMaterialUseCountLabels(
20 material: PromotionMaterial,
21 t: TranslateFn,
22 options?: { showZeroTotal?: boolean },
23 ) {
24 const useCountByAccountType = material.useCountByAccountType ?? {}
25 const maxUseCountByAccountType = material.maxUseCountByAccountType ?? {}
26 const hasAccountTypeUseCount = Object.keys(useCountByAccountType).length > 0
27 const hasAccountTypeMaxUseCount = Object.keys(maxUseCountByAccountType).length > 0
28
29 if (!hasAccountTypeUseCount && !hasAccountTypeMaxUseCount) {
30 if (material.useCount != null && (options?.showZeroTotal || material.useCount > 0))
31 return [t('material.useCount', { count: material.useCount })]
32
33 return []
34 }
35
36 return getMaterialUseCountPlatforms(material)
37 .map((platform) => {
38 const platformName = getPlatformInfoSync(platform)?.name ?? platform
39 const usedCount = useCountByAccountType[platform] ?? 0
40 const maxUseCount = maxUseCountByAccountType[platform]
41
42 if (maxUseCount === UNLIMITED_PLATFORM_MAX_USE_COUNT) {
43 return usedCount > 0
44 ? t('material.platformUseCount', {
45 platform: platformName,
46 count: usedCount,
47 })
48 : null
49 }
50
51 if (typeof maxUseCount === 'number' && maxUseCount > 0) {
52 return t('material.platformUseLimit', {
53 platform: platformName,
54 used: usedCount,
55 max: maxUseCount,
56 })
57 }
58
59 if (usedCount > 0) {
60 return t('material.platformUseCount', {
61 platform: platformName,
62 count: usedCount,
63 })
64 }
65
66 return null
67 })
68 .filter((label): label is string => !!label)
69 }
70
70 lines TYPESCRIPT