dashboardCard.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import {useState} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import Feature from 'sentry/components/acl/feature';
  5. import {ActivityAvatar} from 'sentry/components/activity/item/avatar';
  6. import {Button} from 'sentry/components/button';
  7. import Card from 'sentry/components/card';
  8. import InteractionStateLayer from 'sentry/components/interactionStateLayer';
  9. import type {LinkProps} from 'sentry/components/links/link';
  10. import Link from 'sentry/components/links/link';
  11. import {IconStar} from 'sentry/icons';
  12. import {t} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import type {User} from 'sentry/types/user';
  15. interface Props {
  16. detail: React.ReactNode;
  17. onFavorite: (isFavorited: boolean) => void;
  18. renderWidgets: () => React.ReactNode;
  19. title: string;
  20. to: LinkProps['to'];
  21. createdBy?: User;
  22. dateStatus?: React.ReactNode;
  23. isFavorited?: boolean;
  24. onEventClick?: () => void;
  25. renderContextMenu?: () => React.ReactNode;
  26. }
  27. function DashboardCard({
  28. title,
  29. detail,
  30. createdBy,
  31. renderWidgets,
  32. dateStatus,
  33. to,
  34. onEventClick,
  35. renderContextMenu,
  36. isFavorited = false,
  37. onFavorite,
  38. }: Props) {
  39. const [favorited, setFavorited] = useState<boolean>(isFavorited);
  40. function onClick() {
  41. onEventClick?.();
  42. }
  43. // Fetch the theme to set the `InteractionStateLayer` color. Otherwise it will
  44. // use the `currentColor` of the `Link`, which is blue, and not correct
  45. const theme = useTheme();
  46. return (
  47. <CardWithoutMargin>
  48. <CardLink
  49. data-test-id={`card-${title}`}
  50. onClick={onClick}
  51. to={to}
  52. aria-label={title}
  53. >
  54. <InteractionStateLayer as="div" color={theme.textColor} />
  55. <CardHeader>
  56. <CardContent>
  57. <Title>{title}</Title>
  58. <Detail>{detail}</Detail>
  59. </CardContent>
  60. <AvatarWrapper>
  61. {createdBy ? (
  62. <ActivityAvatar type="user" user={createdBy} size={34} />
  63. ) : (
  64. <ActivityAvatar type="system" size={34} />
  65. )}
  66. </AvatarWrapper>
  67. </CardHeader>
  68. <CardBody>{renderWidgets()}</CardBody>
  69. <CardFooter>
  70. <DateSelected>
  71. {dateStatus ? (
  72. <DateStatus>
  73. {t('Created')} {dateStatus}
  74. </DateStatus>
  75. ) : (
  76. <DateStatus />
  77. )}
  78. </DateSelected>
  79. </CardFooter>
  80. </CardLink>
  81. <ContextMenuWrapper>
  82. <Feature features="dashboards-favourite">
  83. <StyledButton
  84. icon={
  85. <IconStar
  86. isSolid={favorited}
  87. color={favorited ? 'yellow300' : 'gray300'}
  88. size="sm"
  89. aria-label={favorited ? t('UnFavorite') : t('Favorite')}
  90. />
  91. }
  92. size="zero"
  93. borderless
  94. aria-label={t('Dashboards Favorite')}
  95. onClick={async () => {
  96. try {
  97. setFavorited(!favorited);
  98. await onFavorite(!favorited);
  99. } catch (error) {
  100. // If the api call fails, revert the state
  101. setFavorited(favorited);
  102. }
  103. }}
  104. />
  105. </Feature>
  106. {renderContextMenu?.()}
  107. </ContextMenuWrapper>
  108. </CardWithoutMargin>
  109. );
  110. }
  111. const AvatarWrapper = styled('span')`
  112. border: 3px solid ${p => p.theme.border};
  113. border-radius: 50%;
  114. height: min-content;
  115. `;
  116. const CardContent = styled('div')`
  117. flex-grow: 1;
  118. overflow: hidden;
  119. margin-right: ${space(1)};
  120. `;
  121. const CardWithoutMargin = styled(Card)`
  122. margin: 0;
  123. `;
  124. const Title = styled('div')`
  125. ${p => p.theme.text.cardTitle};
  126. color: ${p => p.theme.headingColor};
  127. ${p => p.theme.overflowEllipsis};
  128. font-weight: ${p => p.theme.fontWeightNormal};
  129. `;
  130. const CardLink = styled(Link)`
  131. position: relative;
  132. display: flex;
  133. flex-direction: column;
  134. color: ${p => p.theme.textColor};
  135. &:focus,
  136. &:hover {
  137. color: ${p => p.theme.textColor};
  138. ${Title} {
  139. text-decoration: underline;
  140. }
  141. }
  142. `;
  143. const CardHeader = styled('div')`
  144. display: flex;
  145. padding: ${space(1.5)} ${space(2)};
  146. `;
  147. const Detail = styled('div')`
  148. font-family: ${p => p.theme.text.familyMono};
  149. font-size: ${p => p.theme.fontSizeSmall};
  150. color: ${p => p.theme.gray300};
  151. ${p => p.theme.overflowEllipsis};
  152. line-height: 1.5;
  153. `;
  154. const CardBody = styled('div')`
  155. background: ${p => p.theme.gray100};
  156. padding: ${space(1.5)} ${space(2)};
  157. max-height: 150px;
  158. min-height: 150px;
  159. overflow: hidden;
  160. border-bottom: 1px solid ${p => p.theme.gray100};
  161. `;
  162. const CardFooter = styled('div')`
  163. display: flex;
  164. justify-content: space-between;
  165. align-items: center;
  166. padding: ${space(1)} ${space(2)};
  167. height: 42px;
  168. `;
  169. const DateSelected = styled('div')`
  170. font-size: ${p => p.theme.fontSizeSmall};
  171. display: grid;
  172. grid-column-gap: ${space(1)};
  173. color: ${p => p.theme.textColor};
  174. ${p => p.theme.overflowEllipsis};
  175. `;
  176. const DateStatus = styled('span')`
  177. color: ${p => p.theme.subText};
  178. padding-left: ${space(1)};
  179. `;
  180. const ContextMenuWrapper = styled('div')`
  181. position: absolute;
  182. right: ${space(2)};
  183. bottom: ${space(1)};
  184. display: flex;
  185. `;
  186. const StyledButton = styled(Button)`
  187. margin-right: -10px;
  188. padding: 5px;
  189. `;
  190. export default DashboardCard;