querycard.tsx 3.4 KB

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