Timeline.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 { observer } from "mobx-react";
  15. import React from "react";
  16. import styled from "styled-components";
  17. import { ThemePalette, ThemeProps } from "@src/components/Theme";
  18. import Arrow from "@src/components/ui/Arrow";
  19. import StatusIcon from "@src/components/ui/StatusComponents/StatusIcon";
  20. import DateUtils from "@src/utils/DateUtils";
  21. import type { Execution } from "@src/@types/Execution";
  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 =>
  77. props.selected ? `font-weight: ${ThemeProps.fontWeights.medium};` : ""}
  78. `;
  79. type Props = {
  80. items?: Execution[] | null;
  81. selectedItem?: Execution | null;
  82. hasOlderItems?: boolean;
  83. onPreviousClick?: () => void;
  84. onNextClick?: () => void;
  85. onItemClick?: (item: Execution) => void;
  86. };
  87. @observer
  88. class Timeline extends React.Component<Props> {
  89. itemsRef: HTMLElement | null | undefined;
  90. progressLineRef: HTMLElement | null | undefined;
  91. wrapperRef: HTMLElement | null | undefined;
  92. itemRef: HTMLElement | null | undefined;
  93. endLineRef: HTMLElement | null | undefined;
  94. componentDidMount() {
  95. this.moveToSelectedItem();
  96. if (!this.itemsRef) {
  97. return;
  98. }
  99. this.itemsRef.style.transition = `all ${ThemeProps.animations.swift}`;
  100. }
  101. componentDidUpdate() {
  102. if (this.itemsRef && !this.itemsRef.style.transition) {
  103. this.itemsRef.style.transition = `all ${ThemeProps.animations.swift}`;
  104. }
  105. this.moveToSelectedItem();
  106. }
  107. moveToSelectedItem() {
  108. if (
  109. !this.progressLineRef ||
  110. !this.endLineRef ||
  111. !this.props.items ||
  112. !this.wrapperRef
  113. ) {
  114. return;
  115. }
  116. const selectedItem = this.props.selectedItem;
  117. if (!this.itemRef || !selectedItem || !this.itemsRef) {
  118. this.progressLineRef.style.width = "0";
  119. this.endLineRef.style.width = "100%";
  120. return;
  121. }
  122. const itemIndex = this.props.items.findIndex(i => i.id === selectedItem.id);
  123. const halfWidth = this.wrapperRef.offsetWidth / 2;
  124. const itemGap = this.itemRef.offsetWidth + ITEM_GAP;
  125. const itemHalfWidth = this.itemRef.offsetWidth / 2;
  126. const offset = halfWidth - itemGap * itemIndex - itemHalfWidth;
  127. this.itemsRef.style.marginLeft = `${offset}px`;
  128. const lastItemPos =
  129. itemGap * (this.props.items.length - 1) + offset + itemHalfWidth;
  130. this.progressLineRef.style.width = `${lastItemPos}px`;
  131. this.endLineRef.style.width = `${Math.max(
  132. this.wrapperRef.offsetWidth - lastItemPos,
  133. 0,
  134. )}px`;
  135. }
  136. renderMainLine() {
  137. return (
  138. <MainLine>
  139. <ProgressLine
  140. ref={(line: HTMLElement | null | undefined) => {
  141. this.progressLineRef = line;
  142. }}
  143. />
  144. <EndLine
  145. ref={(line: HTMLElement | null | undefined) => {
  146. this.endLineRef = line;
  147. }}
  148. />
  149. </MainLine>
  150. );
  151. }
  152. renderItems() {
  153. if (!this.props.items || !this.props.items.length) {
  154. return null;
  155. }
  156. return (
  157. <ItemsWrapper>
  158. <Items
  159. ref={(items: HTMLElement | null | undefined) => {
  160. this.itemsRef = items;
  161. }}
  162. >
  163. {this.props.items.map(item => (
  164. <Item
  165. key={item.id}
  166. ref={(ref: HTMLElement | null | undefined) => {
  167. this.itemRef = ref;
  168. }}
  169. onClick={() => {
  170. if (this.props.onItemClick) this.props.onItemClick(item);
  171. }}
  172. >
  173. <StatusIcon status={item.status} useBackground />
  174. <ItemLabel
  175. selected={
  176. this.props.selectedItem &&
  177. this.props.selectedItem.id === item.id
  178. }
  179. >
  180. {DateUtils.getLocalDate(item.created_at).toFormat(
  181. "dd LLL yyyy",
  182. )}
  183. </ItemLabel>
  184. </Item>
  185. ))}
  186. </Items>
  187. </ItemsWrapper>
  188. );
  189. }
  190. render() {
  191. return (
  192. <Wrapper
  193. ref={(w: HTMLElement | null | undefined) => {
  194. this.wrapperRef = w;
  195. }}
  196. >
  197. <ArrowStyled
  198. orientation="left"
  199. forceShow={
  200. !this.props.items ||
  201. !this.props.items.length ||
  202. this.props.hasOlderItems
  203. }
  204. primary={Boolean(this.props.items && this.props.items.length)}
  205. onClick={this.props.onPreviousClick}
  206. />
  207. {this.renderMainLine()}
  208. {this.renderItems()}
  209. <ArrowStyled
  210. orientation="right"
  211. forceShow={!this.props.items || !this.props.items.length}
  212. onClick={this.props.onNextClick}
  213. />
  214. </Wrapper>
  215. );
  216. }
  217. }
  218. export default Timeline;