searchResult.tsx 3.9 KB

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