formSource.tsx 2.8 KB

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