formSource.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import {Component} from 'react';
  2. // eslint-disable-next-line no-restricted-imports
  3. import {withRouter, WithRouterProps} from 'react-router';
  4. import {loadSearchMap} from 'sentry/actionCreators/formSearch';
  5. import FormSearchStore, {FormSearchField} from 'sentry/stores/formSearchStore';
  6. import {createFuzzySearch, Fuse} from 'sentry/utils/fuzzySearch';
  7. import replaceRouterParams from 'sentry/utils/replaceRouterParams';
  8. import {ChildProps, Result, ResultItem} from './types';
  9. import {strGetFn} from './utils';
  10. type Props = WithRouterProps<{orgId: string}> & {
  11. children: (props: ChildProps) => React.ReactElement;
  12. /**
  13. * search term
  14. */
  15. query: string;
  16. /**
  17. * List of form fields to search
  18. */
  19. searchMap?: null | FormSearchField[];
  20. /**
  21. * fusejs options.
  22. */
  23. searchOptions?: Fuse.IFuseOptions<FormSearchField>;
  24. };
  25. type State = {
  26. fuzzy: null | Fuse<FormSearchField>;
  27. };
  28. class FormSource extends Component<Props, State> {
  29. static defaultProps = {
  30. searchOptions: {},
  31. };
  32. state: State = {
  33. fuzzy: null,
  34. };
  35. componentDidMount() {
  36. this.createSearch(this.props.searchMap);
  37. }
  38. componentDidUpdate(prevProps: Props) {
  39. if (this.props.searchMap !== prevProps.searchMap) {
  40. this.createSearch(this.props.searchMap);
  41. }
  42. }
  43. async createSearch(searchMap: Props['searchMap']) {
  44. this.setState({
  45. fuzzy: await createFuzzySearch(searchMap || [], {
  46. ...this.props.searchOptions,
  47. keys: ['title', 'description'],
  48. getFn: strGetFn,
  49. }),
  50. });
  51. }
  52. render() {
  53. const {searchMap, query, params, children} = this.props;
  54. const {fuzzy} = this.state;
  55. const results =
  56. fuzzy?.search(query).map<Result>(value => {
  57. const {item, ...rest} = value;
  58. return {
  59. item: {
  60. ...item,
  61. sourceType: 'field',
  62. resultType: 'field',
  63. to: `${replaceRouterParams(item.route, params)}#${encodeURIComponent(
  64. item.field.name
  65. )}`,
  66. } as ResultItem,
  67. ...rest,
  68. };
  69. }) ?? [];
  70. return children({
  71. isLoading: searchMap === null,
  72. results,
  73. });
  74. }
  75. }
  76. type ContainerProps = Omit<Props, 'searchMap'>;
  77. type ContainerState = Pick<Props, 'searchMap'>;
  78. class FormSourceContainer extends Component<ContainerProps, ContainerState> {
  79. state = {
  80. searchMap: FormSearchStore.get(),
  81. };
  82. componentDidMount() {
  83. // Loads form fields
  84. loadSearchMap();
  85. }
  86. componentWillUnmount() {
  87. this.unsubscribe();
  88. }
  89. unsubscribe = FormSearchStore.listen(
  90. (searchMap: ContainerState['searchMap']) => this.setState({searchMap}),
  91. undefined
  92. );
  93. render() {
  94. return <FormSource searchMap={this.state.searchMap} {...this.props} />;
  95. }
  96. }
  97. export default withRouter(FormSourceContainer);