searchResult.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {Component, Fragment} from 'react';
  2. import {withRouter, WithRouterProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import IdBadge from 'sentry/components/idBadge';
  5. import {IconInput, IconLink, IconSettings} from 'sentry/icons';
  6. import PluginIcon from 'sentry/plugins/components/pluginIcon';
  7. import space from 'sentry/styles/space';
  8. import highlightFuseMatches from 'sentry/utils/highlightFuseMatches';
  9. import {Result} from './sources/types';
  10. type Props = WithRouterProps<{orgId: string}> & {
  11. highlighted: boolean;
  12. item: Result['item'];
  13. matches: Result['matches'];
  14. };
  15. class SearchResult extends Component<Props> {
  16. renderContent() {
  17. const {highlighted, item, matches, params} = this.props;
  18. const {sourceType, model, extra} = item;
  19. let {title, description} = item;
  20. if (matches) {
  21. // TODO(ts) Type this better.
  22. const HighlightedMarker = (p: any) => (
  23. <HighlightMarker highlighted={highlighted} {...p} />
  24. );
  25. const matchedTitle = matches && matches.find(({key}) => key === 'title');
  26. const matchedDescription =
  27. matches && matches.find(({key}) => key === 'description');
  28. title = matchedTitle
  29. ? highlightFuseMatches(matchedTitle, HighlightedMarker)
  30. : title;
  31. description = matchedDescription
  32. ? highlightFuseMatches(matchedDescription, HighlightedMarker)
  33. : description;
  34. }
  35. if (['organization', 'member', 'project', 'team'].includes(sourceType)) {
  36. const DescriptionNode = (
  37. <BadgeDetail highlighted={highlighted}>{description}</BadgeDetail>
  38. );
  39. const badgeProps = {
  40. displayName: title,
  41. displayEmail: DescriptionNode,
  42. description: DescriptionNode,
  43. useLink: false,
  44. orgId: params.orgId,
  45. avatarSize: 32,
  46. [sourceType]: model,
  47. };
  48. return <IdBadge {...badgeProps} />;
  49. }
  50. return (
  51. <Fragment>
  52. <div>
  53. <SearchTitle>{title}</SearchTitle>
  54. </div>
  55. {description && <SearchDetail>{description}</SearchDetail>}
  56. {extra && <ExtraDetail>{extra}</ExtraDetail>}
  57. </Fragment>
  58. );
  59. }
  60. renderResultType() {
  61. const {item} = this.props;
  62. const {resultType, model} = item;
  63. const isSettings = resultType === 'settings';
  64. const isField = resultType === 'field';
  65. const isRoute = resultType === 'route';
  66. const isIntegration = resultType === 'integration';
  67. if (isSettings) {
  68. return <IconSettings />;
  69. }
  70. if (isField) {
  71. return <IconInput />;
  72. }
  73. if (isRoute) {
  74. return <IconLink />;
  75. }
  76. if (isIntegration) {
  77. return <StyledPluginIcon pluginId={model.slug} />;
  78. }
  79. return null;
  80. }
  81. render() {
  82. return (
  83. <Wrapper>
  84. <Content>{this.renderContent()}</Content>
  85. <div>{this.renderResultType()}</div>
  86. </Wrapper>
  87. );
  88. }
  89. }
  90. export default withRouter(SearchResult);
  91. // This is for tests
  92. const SearchTitle = styled('span')``;
  93. const SearchDetail = styled('div')`
  94. font-size: 0.8em;
  95. line-height: 1.3;
  96. margin-top: 4px;
  97. opacity: 0.8;
  98. `;
  99. const ExtraDetail = styled('div')`
  100. font-size: ${p => p.theme.fontSizeSmall};
  101. color: ${p => p.theme.gray300};
  102. margin-top: ${space(0.5)};
  103. `;
  104. const BadgeDetail = styled('div')<{highlighted: boolean}>`
  105. line-height: 1.3;
  106. color: ${p => (p.highlighted ? p.theme.purple300 : null)};
  107. `;
  108. const Wrapper = styled('div')`
  109. display: flex;
  110. justify-content: space-between;
  111. align-items: center;
  112. `;
  113. const Content = styled('div')`
  114. display: flex;
  115. flex-direction: column;
  116. `;
  117. const StyledPluginIcon = styled(PluginIcon)`
  118. flex-shrink: 0;
  119. `;
  120. const HighlightMarker = styled('mark')`
  121. padding: 0;
  122. background: transparent;
  123. font-weight: bold;
  124. color: ${p => p.theme.active};
  125. `;