projectSidebarSection.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {motion, Variants} from 'framer-motion';
  4. import {PlatformIcon} from 'platformicons';
  5. import {PlatformKey} from 'sentry/data/platformCategories';
  6. import platforms from 'sentry/data/platforms';
  7. import {IconCheckmark} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import pulsingIndicatorStyles from 'sentry/styles/pulsingIndicator';
  10. import space from 'sentry/styles/space';
  11. import {Project} from 'sentry/types';
  12. import testableTransition from 'sentry/utils/testableTransition';
  13. type Props = {
  14. activeProject: Project | null;
  15. checkProjectHasFirstEvent: (project: Project) => boolean;
  16. projects: Project[];
  17. selectProject: (newProjectId: string) => void;
  18. // A map from selected platform keys to the projects created by onboarding.
  19. selectedPlatformToProjectIdMap: {[key in PlatformKey]?: string};
  20. };
  21. function ProjectSidebarSection({
  22. projects,
  23. activeProject,
  24. selectProject,
  25. checkProjectHasFirstEvent,
  26. selectedPlatformToProjectIdMap,
  27. }: Props) {
  28. const oneProject = (platformOnCreate: string, projectSlug: string) => {
  29. const project = projects.find(p => p.slug === projectSlug);
  30. const platform = project ? project.platform || 'other' : platformOnCreate;
  31. const platformName = platforms.find(p => p.id === platform)?.name ?? '';
  32. const isActive = !!project && activeProject?.id === project.id;
  33. const errorReceived = !!project && checkProjectHasFirstEvent(project);
  34. return (
  35. <ProjectWrapper
  36. key={projectSlug}
  37. isActive={isActive}
  38. onClick={() => project && selectProject(project.id)}
  39. disabled={!project}
  40. >
  41. <StyledPlatformIcon platform={platform} size={36} />
  42. <MiddleWrapper>
  43. <NameWrapper>{platformName}</NameWrapper>
  44. <SubHeader errorReceived={errorReceived} data-test-id="sidebar-error-indicator">
  45. {!project
  46. ? t('Project Deleted')
  47. : errorReceived
  48. ? t('Error Received')
  49. : t('Waiting for error')}
  50. </SubHeader>
  51. </MiddleWrapper>
  52. {errorReceived ? (
  53. <StyledIconCheckmark isCircled color="green400" />
  54. ) : (
  55. isActive && <WaitingIndicator />
  56. )}
  57. </ProjectWrapper>
  58. );
  59. };
  60. return (
  61. <Fragment>
  62. <Title>{t('Projects to Setup')}</Title>
  63. {Object.entries(selectedPlatformToProjectIdMap).map(
  64. ([platformOnCreate, projectSlug]) => oneProject(platformOnCreate, projectSlug)
  65. )}
  66. </Fragment>
  67. );
  68. }
  69. export default ProjectSidebarSection;
  70. const Title = styled('span')`
  71. font-size: 12px;
  72. font-weight: 600;
  73. text-transform: uppercase;
  74. margin-left: ${space(2)};
  75. `;
  76. const SubHeader = styled('div')<{errorReceived: boolean}>`
  77. color: ${p => (p.errorReceived ? p.theme.successText : p.theme.pink300)};
  78. `;
  79. const StyledPlatformIcon = styled(PlatformIcon)``;
  80. const ProjectWrapper = styled('div')<{disabled: boolean; isActive: boolean}>`
  81. display: flex;
  82. flex-direction: row;
  83. align-items: center;
  84. background-color: ${p => p.isActive && p.theme.gray100};
  85. padding: ${space(2)};
  86. cursor: pointer;
  87. border-radius: 4px;
  88. user-select: none;
  89. ${p =>
  90. p.disabled &&
  91. `
  92. cursor: not-allowed;
  93. ${StyledPlatformIcon} {
  94. filter: grayscale(1);
  95. }
  96. ${SubHeader} {
  97. color: ${p.theme.gray400};
  98. }
  99. ${NameWrapper} {
  100. text-decoration-line: line-through;
  101. }
  102. `}
  103. `;
  104. const indicatorAnimation: Variants = {
  105. initial: {opacity: 0, y: -10},
  106. animate: {opacity: 1, y: 0},
  107. exit: {opacity: 0, y: 10},
  108. };
  109. const WaitingIndicator = styled(motion.div)`
  110. margin: 0 6px;
  111. flex-shrink: 0;
  112. ${pulsingIndicatorStyles};
  113. background-color: ${p => p.theme.pink300};
  114. `;
  115. const StyledIconCheckmark = styled(IconCheckmark)`
  116. flex-shrink: 0;
  117. `;
  118. WaitingIndicator.defaultProps = {
  119. variants: indicatorAnimation,
  120. transition: testableTransition(),
  121. };
  122. const MiddleWrapper = styled('div')`
  123. margin: 0 ${space(1)};
  124. flex-grow: 1;
  125. overflow: hidden;
  126. `;
  127. const NameWrapper = styled('div')`
  128. overflow: hidden;
  129. white-space: nowrap;
  130. text-overflow: ellipsis;
  131. `;