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