querycard.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import * as React from 'react';
  2. import styled from '@emotion/styled';
  3. import ActivityAvatar from 'sentry/components/activity/item/avatar';
  4. import Card from 'sentry/components/card';
  5. import ErrorBoundary from 'sentry/components/errorBoundary';
  6. import Link from 'sentry/components/links/link';
  7. import {t} from 'sentry/locale';
  8. import overflowEllipsis from 'sentry/styles/overflowEllipsis';
  9. import space from 'sentry/styles/space';
  10. import {User} from 'sentry/types';
  11. import {callIfFunction} from 'sentry/utils/callIfFunction';
  12. type Props = {
  13. title?: string;
  14. subtitle?: string;
  15. queryDetail?: string;
  16. to: object;
  17. createdBy?: User | undefined;
  18. dateStatus?: React.ReactNode;
  19. onEventClick?: () => void;
  20. renderGraph: () => React.ReactNode;
  21. renderContextMenu?: () => React.ReactNode;
  22. };
  23. class QueryCard extends React.PureComponent<Props> {
  24. handleClick = () => {
  25. const {onEventClick} = this.props;
  26. callIfFunction(onEventClick);
  27. };
  28. render() {
  29. const {
  30. title,
  31. subtitle,
  32. queryDetail,
  33. renderContextMenu,
  34. renderGraph,
  35. createdBy,
  36. dateStatus,
  37. } = this.props;
  38. return (
  39. <Link data-test-id={`card-${title}`} onClick={this.handleClick} to={this.props.to}>
  40. <StyledQueryCard interactive>
  41. <QueryCardHeader>
  42. <QueryCardContent>
  43. <QueryTitle>{title}</QueryTitle>
  44. <QueryDetail>{queryDetail}</QueryDetail>
  45. </QueryCardContent>
  46. <AvatarWrapper>
  47. {createdBy ? (
  48. <ActivityAvatar type="user" user={createdBy} size={34} />
  49. ) : (
  50. <ActivityAvatar type="system" size={34} />
  51. )}
  52. </AvatarWrapper>
  53. </QueryCardHeader>
  54. <QueryCardBody>
  55. <StyledErrorBoundary mini>{renderGraph()}</StyledErrorBoundary>
  56. </QueryCardBody>
  57. <QueryCardFooter>
  58. <DateSelected>
  59. {subtitle}
  60. {dateStatus ? (
  61. <DateStatus>
  62. {t('Edited')} {dateStatus}
  63. </DateStatus>
  64. ) : null}
  65. </DateSelected>
  66. {renderContextMenu && renderContextMenu()}
  67. </QueryCardFooter>
  68. </StyledQueryCard>
  69. </Link>
  70. );
  71. }
  72. }
  73. const AvatarWrapper = styled('span')`
  74. border: 3px solid ${p => p.theme.border};
  75. border-radius: 50%;
  76. height: min-content;
  77. `;
  78. const QueryCardContent = styled('div')`
  79. flex-grow: 1;
  80. overflow: hidden;
  81. margin-right: ${space(1)};
  82. `;
  83. const StyledQueryCard = styled(Card)`
  84. justify-content: space-between;
  85. height: 100%;
  86. &:focus,
  87. &:hover {
  88. top: -1px;
  89. }
  90. `;
  91. const QueryCardHeader = styled('div')`
  92. display: flex;
  93. padding: ${space(1.5)} ${space(2)};
  94. `;
  95. const QueryTitle = styled('div')`
  96. ${p => p.theme.text.cardTitle};
  97. color: ${p => p.theme.headingColor};
  98. ${overflowEllipsis};
  99. `;
  100. const QueryDetail = styled('div')`
  101. font-family: ${p => p.theme.text.familyMono};
  102. font-size: ${p => p.theme.fontSizeSmall};
  103. color: ${p => p.theme.gray300};
  104. line-height: 1.5;
  105. ${overflowEllipsis};
  106. `;
  107. const QueryCardBody = styled('div')`
  108. background: ${p => p.theme.backgroundSecondary};
  109. max-height: 150px;
  110. height: 100%;
  111. overflow: hidden;
  112. `;
  113. const QueryCardFooter = styled('div')`
  114. display: flex;
  115. justify-content: space-between;
  116. align-items: center;
  117. padding: ${space(1)} ${space(2)};
  118. `;
  119. const DateSelected = styled('div')`
  120. font-size: ${p => p.theme.fontSizeSmall};
  121. display: grid;
  122. grid-column-gap: ${space(1)};
  123. ${overflowEllipsis};
  124. color: ${p => p.theme.textColor};
  125. `;
  126. const DateStatus = styled('span')`
  127. color: ${p => p.theme.purple300};
  128. padding-left: ${space(1)};
  129. `;
  130. const StyledErrorBoundary = styled(ErrorBoundary)`
  131. margin-bottom: 100px;
  132. `;
  133. export default QueryCard;