fieldHelpers.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { BlockFieldConfig } from '../types'
  2. /**
  3. * Common field configuration helpers to reduce duplication
  4. */
  5. // Helper for simple fields that fallback from enrichedData to data
  6. export const createField = (
  7. id: string,
  8. label: string,
  9. enrichedKey?: string,
  10. dataKey?: string,
  11. requiresEnrichedData = false
  12. ): BlockFieldConfig => ({
  13. id,
  14. label,
  15. getValue: (data, enrichedData) => {
  16. const eKey = enrichedKey || id
  17. const dKey = dataKey || id
  18. return enrichedData?.[eKey] || data?.[dKey]
  19. },
  20. requiresEnrichedData,
  21. })
  22. // Helper for truncated text fields
  23. export const createTruncatedField = (
  24. id: string,
  25. label: string,
  26. maxLength: number,
  27. enrichedKey?: string,
  28. dataKey?: string,
  29. requiresEnrichedData = false
  30. ): BlockFieldConfig => ({
  31. id,
  32. label,
  33. getValue: (data, enrichedData) => {
  34. const eKey = enrichedKey || id
  35. const dKey = dataKey || id
  36. const value = enrichedData?.[eKey] || data?.[dKey]
  37. if (!value || typeof value !== 'string') return value
  38. return value.length > maxLength ? `${value.substring(0, maxLength)}...` : value
  39. },
  40. requiresEnrichedData,
  41. })
  42. // Helper for ID fields that show truncated version
  43. export const createIdField = (
  44. id: string,
  45. label: string,
  46. truncateLength = 8,
  47. enrichedKey?: string,
  48. dataKey?: string,
  49. requiresEnrichedData = false
  50. ): BlockFieldConfig => ({
  51. id,
  52. label,
  53. getValue: (data, enrichedData) => {
  54. const eKey = enrichedKey || id
  55. const dKey = dataKey || id
  56. const value = enrichedData?.[eKey] || data?.[dKey]
  57. if (!value) return null
  58. const stringValue = String(value)
  59. return truncateLength ? `${stringValue.substring(0, truncateLength)}...` : stringValue
  60. },
  61. requiresEnrichedData,
  62. })
  63. // Helper for time/duration fields
  64. export const createTimeField = (
  65. id: string,
  66. label: string,
  67. unit = 'ms',
  68. enrichedKey?: string,
  69. dataKey?: string,
  70. requiresEnrichedData = false
  71. ): BlockFieldConfig => ({
  72. id,
  73. label,
  74. getValue: (data, enrichedData) => {
  75. const eKey = enrichedKey || id
  76. const dKey = dataKey || id
  77. const time = enrichedData?.[eKey] || data?.[dKey]
  78. return time ? `${time}${unit}` : null
  79. },
  80. requiresEnrichedData,
  81. })
  82. // Helper for status fields with fallback logic
  83. export const createStatusField = (
  84. id: string = 'status',
  85. label: string = 'Status'
  86. ): BlockFieldConfig => ({
  87. id,
  88. label,
  89. getValue: (data, enrichedData) => enrichedData?.status || data?.status,
  90. })
  91. // Helper for path/filename extraction
  92. export const createFileNameField = (
  93. id: string,
  94. label: string,
  95. pathKey = 'path',
  96. requiresEnrichedData = false
  97. ): BlockFieldConfig => ({
  98. id,
  99. label,
  100. getValue: (data, enrichedData) => {
  101. const path = enrichedData?.[pathKey] || data?.[pathKey]
  102. if (!path) return null
  103. // Remove query parameters and hash, get last segment
  104. const cleanPath = path.split('?')[0].split('#')[0]
  105. const segments = cleanPath.split('/')
  106. return segments[segments.length - 1] || null
  107. },
  108. requiresEnrichedData,
  109. })
  110. // Helper for conditional fields based on error status
  111. export const createConditionalField = (
  112. id: string,
  113. label: string,
  114. getValue: (data: any, enrichedData?: any) => any,
  115. errorValue: string = 'Unavailable',
  116. deletedValue: string = 'Object deleted',
  117. requiresEnrichedData = false
  118. ): BlockFieldConfig => ({
  119. id,
  120. label,
  121. getValue: (data, enrichedData) => {
  122. const status = enrichedData?.status || data?.status
  123. const numStatus = Number(status)
  124. if (status === 404 || status === '404') {
  125. return deletedValue
  126. } else if (numStatus >= 400) {
  127. return errorValue
  128. }
  129. return getValue(data, enrichedData)
  130. },
  131. requiresEnrichedData,
  132. })