formSearch.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import type {Field, JsonFormObject} from 'sentry/components/forms/types';
  2. import type {FormSearchField} from 'sentry/stores/formSearchStore';
  3. import FormSearchStore from 'sentry/stores/formSearchStore';
  4. type Params = {
  5. fields: Record<string, Field>;
  6. formGroups: JsonFormObject[];
  7. route: string;
  8. };
  9. /**
  10. * Creates a list of objects to be injected by a search source
  11. *
  12. * @param route The route a form field belongs on
  13. * @param formGroups An array of `FormGroup: {title: string, fields: [Field]}`
  14. * @param fields An object whose key is field name and value is a `Field`
  15. */
  16. const createSearchMap = ({
  17. route,
  18. formGroups,
  19. fields,
  20. ...other
  21. }: Params): FormSearchField[] => {
  22. // There are currently two ways to define forms (TODO(billy): Turn this into one):
  23. // If `formGroups` is defined, then return a flattened list of fields in all formGroups
  24. // Otherwise `fields` is a map of fieldName -> fieldObject -- create a list of fields
  25. const listOfFields = formGroups
  26. ? formGroups.flatMap(formGroup => formGroup.fields)
  27. : Object.keys(fields).map(fieldName => fields[fieldName]);
  28. return listOfFields.map(field => ({
  29. ...other,
  30. route,
  31. title: typeof field !== 'function' ? field.label : undefined,
  32. description: typeof field !== 'function' ? field.help : undefined,
  33. field,
  34. }));
  35. };
  36. export function loadSearchMap() {
  37. // Load all form configuration files via webpack that export a named `route`
  38. // as well as either `fields` or `formGroups`
  39. const context = require.context('../data/forms', true, /\.tsx?$/);
  40. // Get a list of all form fields defined in `../data/forms`
  41. const allFormFields: FormSearchField[] = context.keys().flatMap(key => {
  42. const mod = context(key);
  43. // Since we're dynamically importing an entire directly, there could be malformed modules defined?
  44. // Only look for module that have `route` exported
  45. if (!mod?.route) {
  46. return [];
  47. }
  48. const searchMap = createSearchMap({
  49. // `formGroups` can be a default export or a named export :<
  50. formGroups: mod.default || mod.formGroups,
  51. fields: mod.fields,
  52. route: mod.route,
  53. });
  54. if (searchMap !== null) {
  55. return searchMap;
  56. }
  57. return [];
  58. });
  59. FormSearchStore.loadSearchMap(allFormFields);
  60. }