contentCard.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. export type ContentCardTemplate = 'minimal' | 'story' | 'focus'
  2. export type ContentCardCtaType = 'detail' | 'photo' | 'audio' | 'quiz'
  3. export interface ContentCardQuizConfig {
  4. bonusScore: number
  5. countdownSeconds: number
  6. minValue: number
  7. maxValue: number
  8. allowSubtraction: boolean
  9. }
  10. export interface ContentCardCtaConfig {
  11. type: ContentCardCtaType
  12. label: string
  13. quiz: ContentCardQuizConfig | null
  14. }
  15. export interface ContentCardCtaConfigOverride {
  16. type?: ContentCardCtaType
  17. label?: string
  18. bonusScore?: number
  19. countdownSeconds?: number
  20. minValue?: number
  21. maxValue?: number
  22. allowSubtraction?: boolean
  23. }
  24. export interface ContentCardActionViewModel {
  25. key: string
  26. type: ContentCardCtaType
  27. label: string
  28. }
  29. export const DEFAULT_CONTENT_CARD_QUIZ_CONFIG: ContentCardQuizConfig = {
  30. bonusScore: 1,
  31. countdownSeconds: 10,
  32. minValue: 10,
  33. maxValue: 999,
  34. allowSubtraction: true,
  35. }
  36. export function buildDefaultContentCardCtaLabel(type: ContentCardCtaType): string {
  37. if (type === 'detail') {
  38. return '查看详情'
  39. }
  40. if (type === 'photo') {
  41. return '拍照打卡'
  42. }
  43. if (type === 'audio') {
  44. return '语音留言'
  45. }
  46. return '答题加分'
  47. }
  48. export function buildDefaultContentCardQuizConfig(
  49. override?: ContentCardCtaConfigOverride | null,
  50. ): ContentCardQuizConfig {
  51. const minValue = Number(override && override.minValue)
  52. const maxValue = Number(override && override.maxValue)
  53. return {
  54. bonusScore: Number.isFinite(Number(override && override.bonusScore))
  55. ? Math.max(1, Math.round(Number(override && override.bonusScore)))
  56. : DEFAULT_CONTENT_CARD_QUIZ_CONFIG.bonusScore,
  57. countdownSeconds: Number.isFinite(Number(override && override.countdownSeconds))
  58. ? Math.max(5, Math.round(Number(override && override.countdownSeconds)))
  59. : DEFAULT_CONTENT_CARD_QUIZ_CONFIG.countdownSeconds,
  60. minValue: Number.isFinite(minValue)
  61. ? Math.max(10, Math.round(minValue))
  62. : DEFAULT_CONTENT_CARD_QUIZ_CONFIG.minValue,
  63. maxValue: Number.isFinite(maxValue)
  64. ? Math.max(
  65. Number.isFinite(minValue) ? Math.max(10, Math.round(minValue)) : DEFAULT_CONTENT_CARD_QUIZ_CONFIG.minValue,
  66. Math.round(maxValue),
  67. )
  68. : DEFAULT_CONTENT_CARD_QUIZ_CONFIG.maxValue,
  69. allowSubtraction: override && typeof override.allowSubtraction === 'boolean'
  70. ? override.allowSubtraction
  71. : DEFAULT_CONTENT_CARD_QUIZ_CONFIG.allowSubtraction,
  72. }
  73. }
  74. export function resolveContentCardCtaConfig(
  75. override: ContentCardCtaConfigOverride | null | undefined,
  76. ): ContentCardCtaConfig | null {
  77. const type = override && override.type
  78. if (type !== 'detail' && type !== 'photo' && type !== 'audio' && type !== 'quiz') {
  79. return null
  80. }
  81. return {
  82. type,
  83. label: override && override.label ? override.label : buildDefaultContentCardCtaLabel(type),
  84. quiz: type === 'quiz' ? buildDefaultContentCardQuizConfig(override) : null,
  85. }
  86. }