Timeline.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React from 'react'
  15. import { observer } from 'mobx-react'
  16. import styled from 'styled-components'
  17. import type { Execution } from '../../../../@types/Execution'
  18. import Arrow from '../../../ui/Arrow/Arrow'
  19. import StatusIcon from '../../../ui/StatusComponents/StatusIcon/StatusIcon'
  20. import { ThemePalette, ThemeProps } from '../../../Theme'
  21. import DateUtils from '../../../../utils/DateUtils'
  22. const ITEM_GAP = 96
  23. const ArrowStyled = styled(Arrow)<any>`
  24. opacity: ${props => (props.forceShow ? 1 : 0)};
  25. position: absolute;
  26. top: 0;
  27. transition: all ${ThemeProps.animations.swift};
  28. ${props => (props.orientation === 'left' ? 'left: -19px;' : '')}
  29. ${props => (props.orientation === 'right' ? 'right: -19px;' : '')}
  30. `
  31. const Wrapper = styled.div<any>`
  32. position: relative;
  33. height: 30px;
  34. user-select: none;
  35. &:hover ${ArrowStyled} {
  36. opacity: 1;
  37. }
  38. `
  39. const MainLine = styled.div<any>`
  40. width: 100%;
  41. padding-top: 7px;
  42. display: flex;
  43. `
  44. const ProgressLine = styled.div<any>`
  45. border-bottom: 2px solid ${ThemePalette.primary};
  46. transition: all ${ThemeProps.animations.swift};
  47. `
  48. const EndLine = styled.div<any>`
  49. border-bottom: 2px solid ${ThemePalette.grayscale[2]};
  50. transition: all ${ThemeProps.animations.swift};
  51. `
  52. const ItemsWrapper = styled.div<any>`
  53. overflow: hidden;
  54. position: absolute;
  55. top: 0;
  56. left: 0;
  57. right: 0;
  58. `
  59. const Items = styled.div<any>`
  60. display: flex;
  61. `
  62. const Item = styled.div<any>`
  63. display: flex;
  64. flex-direction: column;
  65. align-items: center;
  66. margin-right: ${ITEM_GAP}px;
  67. cursor: pointer;
  68. min-width: 75px;
  69. max-width: 75px;
  70. `
  71. const ItemLabel = styled.div<any>`
  72. font-size: 12px;
  73. color: ${ThemePalette.grayscale[4]};
  74. margin-top: 2px;
  75. ${props => (props.selected ? `color: ${ThemePalette.black};` : '')}
  76. ${props => (props.selected ? `font-weight: ${ThemeProps.fontWeights.medium};` : '')}
  77. `
  78. type Props = {
  79. items?: Execution[] | null,
  80. selectedItem?: Execution | null,
  81. onPreviousClick?: () => void,
  82. onNextClick?: () => void,
  83. onItemClick?: (item: Execution) => void,
  84. }
  85. @observer
  86. class Timeline extends React.Component<Props> {
  87. itemsRef: HTMLElement | null | undefined
  88. progressLineRef: HTMLElement | null | undefined
  89. wrapperRef: HTMLElement | null | undefined
  90. itemRef: HTMLElement | null | undefined
  91. endLineRef: HTMLElement | null | undefined
  92. componentDidMount() {
  93. this.moveToSelectedItem()
  94. if (!this.itemsRef) {
  95. return
  96. }
  97. this.itemsRef.style.transition = `all ${ThemeProps.animations.swift}`
  98. }
  99. componentDidUpdate() {
  100. if (this.itemsRef && !this.itemsRef.style.transition) {
  101. this.itemsRef.style.transition = `all ${ThemeProps.animations.swift}`
  102. }
  103. this.moveToSelectedItem()
  104. }
  105. moveToSelectedItem() {
  106. if (!this.progressLineRef || !this.endLineRef || !this.props.items || !this.wrapperRef) {
  107. return
  108. }
  109. const selectedItem = this.props.selectedItem
  110. if (!this.itemRef || !selectedItem || !this.itemsRef) {
  111. this.progressLineRef.style.width = '0'
  112. this.endLineRef.style.width = '100%'
  113. return
  114. }
  115. const itemIndex = this.props.items.findIndex(i => i.id === selectedItem.id)
  116. const halfWidth = this.wrapperRef.offsetWidth / 2
  117. const itemGap = this.itemRef.offsetWidth + ITEM_GAP
  118. const itemHalfWidth = this.itemRef.offsetWidth / 2
  119. const offset = (halfWidth - (itemGap * itemIndex)) - itemHalfWidth
  120. this.itemsRef.style.marginLeft = `${offset}px`
  121. const lastItemPos = (itemGap * (this.props.items.length - 1)) + offset + itemHalfWidth
  122. this.progressLineRef.style.width = `${lastItemPos}px`
  123. this.endLineRef.style.width = `${Math.max(this.wrapperRef.offsetWidth - lastItemPos, 0)}px`
  124. }
  125. renderMainLine() {
  126. return (
  127. <MainLine>
  128. <ProgressLine ref={(line: HTMLElement | null | undefined) => {
  129. this.progressLineRef = line
  130. }}
  131. />
  132. <EndLine ref={(line: HTMLElement | null | undefined) => { this.endLineRef = line }} />
  133. </MainLine>
  134. )
  135. }
  136. renderItems() {
  137. if (!this.props.items || !this.props.items.length) {
  138. return null
  139. }
  140. return (
  141. <ItemsWrapper>
  142. <Items ref={(items: HTMLElement | null | undefined) => { this.itemsRef = items }}>
  143. {this.props.items.map(item => (
  144. <Item
  145. key={item.id}
  146. ref={(ref: HTMLElement | null | undefined) => { this.itemRef = ref }}
  147. onClick={() => { if (this.props.onItemClick) this.props.onItemClick(item) }}
  148. data-test-id={`timeline-item-${item.id}`}
  149. >
  150. <StatusIcon status={item.status} useBackground />
  151. <ItemLabel selected={this.props.selectedItem && this.props.selectedItem.id === item.id} data-test-id={`timeline-label-${item.id}`}>
  152. {DateUtils.getLocalTime(item.created_at).format('DD MMM YYYY')}
  153. </ItemLabel>
  154. </Item>
  155. ))}
  156. </Items>
  157. </ItemsWrapper>
  158. )
  159. }
  160. render() {
  161. return (
  162. <Wrapper ref={(w: HTMLElement | null | undefined) => { this.wrapperRef = w }}>
  163. <ArrowStyled
  164. orientation="left"
  165. forceShow={!this.props.items || !this.props.items.length}
  166. primary={Boolean(this.props.items && this.props.items.length)}
  167. onClick={this.props.onPreviousClick}
  168. data-test-id="timeline-previous"
  169. />
  170. {this.renderMainLine()}
  171. {this.renderItems()}
  172. <ArrowStyled
  173. orientation="right"
  174. forceShow={!this.props.items || !this.props.items.length}
  175. onClick={this.props.onNextClick}
  176. data-test-id="timeline-next"
  177. />
  178. </Wrapper>
  179. )
  180. }
  181. }
  182. export default Timeline