-
Notifications
You must be signed in to change notification settings - Fork 346
Expand file tree
/
Copy pathMetadataViewContainer.tsx
More file actions
273 lines (240 loc) · 9.66 KB
/
MetadataViewContainer.tsx
File metadata and controls
273 lines (240 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import * as React from 'react';
import { useIntl } from 'react-intl';
import {
EnumType,
FloatType,
MetadataFormFieldValue,
MetadataTemplateFieldOption,
RangeType,
} from '@box/metadata-filter';
import {
IconColumnVariant,
MetadataView,
PredefinedFilterName,
type FilterValues,
type MetadataViewProps,
type MetadataFieldType,
type Column,
} from '@box/metadata-view';
import { type Key } from '@react-types/shared';
import cloneDeep from 'lodash/cloneDeep';
import { SortDescriptor } from 'react-aria-components';
import { FIELD_ITEM_NAME } from '../../constants';
import type { Collection } from '../../common/types/core';
import type { MetadataTemplate, MetadataTemplateField } from '../../common/types/metadata';
import messages from '../common/messages';
// Public-friendly version of MetadataFormFieldValue from @box/metadata-filter
// (string[] for enum type, range/float objects stay the same)
type EnumToStringArray<T> = T extends EnumType ? string[] : T;
type ExternalMetadataFormFieldValue = EnumToStringArray<MetadataFormFieldValue>;
export type ExternalFilterValues = Record<
string,
{
options?: FilterValues[string]['options'] | MetadataTemplateFieldOption[];
fieldType: FilterValues[string]['fieldType'] | MetadataFieldType;
value: ExternalMetadataFormFieldValue;
}
>;
type ActionBarProps = Omit<
MetadataViewProps['actionBarProps'],
'initialFilterValues' | 'onFilterSubmit' | 'filterGroups'
> & {
initialFilterValues?: ExternalFilterValues;
onFilterSubmit?: (filterValues: ExternalFilterValues) => void;
};
const ITEM_FILTER_NAME = 'item_name';
/**
* Helper function to trim metadataFieldNamePrefix from column names
* For example: 'metadata.enterprise_1515946.mdViewTemplate1.industry' -> 'industry'
*/
function trimMetadataFieldPrefix(column: string): string {
// Check if the column starts with 'metadata.' and contains at least 2 dots
if (column.startsWith('metadata.') && column.split('.').length >= 3) {
// Split by dots and take everything after the first 3 parts
// metadata.enterprise_1515946.mdViewTemplate1.industry -> industry
const parts = column.split('.');
return parts.slice(3).join('.');
}
return column;
}
function transformInitialFilterValuesToInternal(
publicValues?: ExternalFilterValues,
): Record<string, { value: MetadataFormFieldValue }> | undefined {
if (!publicValues) return undefined;
return Object.entries(publicValues).reduce<Record<string, { value: MetadataFormFieldValue }>>(
(acc, [key, { value }]) => {
acc[key] = Array.isArray(value) ? { value: { enum: value } } : { value };
return acc;
},
{},
);
}
export function convertFilterValuesToExternal(fields: FilterValues): ExternalFilterValues {
return Object.entries(fields).reduce<ExternalFilterValues>((acc, [key, field]) => {
const { value, options, fieldType } = field;
// Transform the value based on its type
const transformedValue: ExternalMetadataFormFieldValue =
'enum' in value && Array.isArray(value.enum)
? value.enum // Convert enum type to string array
: (value as RangeType | FloatType); // Keep range/float objects as-is
acc[key === ITEM_FILTER_NAME ? FIELD_ITEM_NAME : key] = {
options,
fieldType,
value: transformedValue,
};
return acc;
}, {});
}
// Internal helper function for component use
function transformInternalFieldsToPublic(fields: FilterValues): ExternalFilterValues {
return convertFilterValuesToExternal(fields);
}
export interface MetadataViewContainerProps extends Omit<MetadataViewProps, 'items' | 'actionBarProps'> {
actionBarProps?: ActionBarProps;
currentCollection: Collection;
metadataTemplate: MetadataTemplate;
onMetadataFilter: (fields: ExternalFilterValues) => void;
/* Internally controlled onSortChange prop for the MetadataView component. */
onSortChange?: (sortBy: Key, sortDirection: string) => void;
}
const MetadataViewContainer = ({
actionBarProps,
columns,
currentCollection,
metadataTemplate,
onMetadataFilter,
onSortChange: onSortChangeInternal,
tableProps,
...rest
}: MetadataViewContainerProps) => {
const { formatMessage } = useIntl();
const { items = [] } = currentCollection;
const { initialFilterValues: initialFilterValuesProp, onFilterSubmit } = actionBarProps ?? {};
const newColumns = React.useMemo(() => {
let clonedColumns = cloneDeep(columns);
const hasItemNameField = clonedColumns.some((col: Column) => col.id === FIELD_ITEM_NAME);
if (!hasItemNameField) {
clonedColumns = [
{
allowsSorting: true,
id: FIELD_ITEM_NAME,
isItemMetadata: true,
isRowHeader: true,
minWidth: 300,
textValue: formatMessage(messages.name),
type: 'string',
},
...clonedColumns,
];
}
return clonedColumns;
}, [columns, formatMessage]);
const filterGroups = React.useMemo(() => {
const clonedTemplate = cloneDeep(metadataTemplate);
let fields = clonedTemplate?.fields || [];
// Filter fields to only include those that have corresponding columns
const columnIds = newColumns.map(col => col.id);
fields = fields.filter((field: MetadataTemplateField) => {
// For metadata fields, check if the column ID matches the field key
// Column IDs for metadata fields are typically in format: metadata.template.fieldKey
return columnIds.some(columnId => {
const trimmedColumnId = trimMetadataFieldPrefix(columnId);
return trimmedColumnId === field.key;
});
});
// Check if item_name field already exists to avoid duplicates
const hasItemNameField = fields.some((field: MetadataTemplateField) => field.key === ITEM_FILTER_NAME);
if (!hasItemNameField) {
fields = [
{
key: ITEM_FILTER_NAME,
displayName: formatMessage(messages.name),
type: 'string',
shouldRenderChip: true,
},
...fields,
];
}
return [
{
toggleable: true,
filters:
fields?.map(field => {
return {
id: field.key,
name: field.displayName,
fieldType: field.type,
options: field.options?.map(({ key }) => key) || [],
shouldRenderChip: true,
};
}) || [],
},
];
}, [formatMessage, metadataTemplate, newColumns]);
const initialFilterValues = React.useMemo(
() => transformInitialFilterValuesToInternal(initialFilterValuesProp),
[initialFilterValuesProp],
);
const handleFilterSubmit = React.useCallback(
(fields: FilterValues) => {
const transformed = transformInternalFieldsToPublic(fields);
onMetadataFilter(transformed);
if (onFilterSubmit) {
onFilterSubmit(transformed);
}
},
[onFilterSubmit, onMetadataFilter],
);
// Create a wrapper function that calls both. The wrapper function should follow the signature of onSortChange from RAC
const handleSortChange = React.useCallback(
({ column, direction }: SortDescriptor) => {
// Call the internal onSortChange first
// API accepts asc/desc "https://developer.box.com/reference/post-metadata-queries-execute-read/"
if (onSortChangeInternal) {
const trimmedColumn = trimMetadataFieldPrefix(String(column));
onSortChangeInternal(trimmedColumn, direction === 'ascending' ? 'ASC' : 'DESC');
}
const onSortChangeExternal = tableProps?.onSortChange;
// Then call the original customer-provided onSortChange if it exists
// Accepts "ascending" / "descending" (https://react-spectrum.adobe.com/react-aria/Table.html)
if (onSortChangeExternal) {
onSortChangeExternal({
column,
direction,
});
}
},
[onSortChangeInternal, tableProps],
);
const transformedActionBarProps = React.useMemo(() => {
return {
...actionBarProps,
initialFilterValues,
onFilterSubmit: handleFilterSubmit,
filterGroups,
sortDropdownProps: {
onSortChange: handleSortChange,
},
predefinedFilterOptions: {
[PredefinedFilterName.KeywordSearchFilterGroup]: { isDisabled: true },
[PredefinedFilterName.LocationFilterGroup]: { isDisabled: true },
},
};
}, [actionBarProps, initialFilterValues, handleFilterSubmit, handleSortChange, filterGroups]);
// Create new tableProps with our wrapper function
const newTableProps = {
...tableProps,
iconColumnVariant: IconColumnVariant.INLINE,
onSortChange: handleSortChange,
};
return (
<MetadataView
actionBarProps={transformedActionBarProps}
columns={newColumns}
items={items}
tableProps={newTableProps}
{...rest}
/>
);
};
export default MetadataViewContainer;