Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /* * Copyright 2021 Harness Inc. All rights reserved. * Use of this source code is governed by the PolyForm Shield 1.0.0 license * that can be found in the licenses directory at the root of this repository, also available at * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt. */ import React, { useState, useEffect, useMemo, useRef } from 'react' import { Container, Text } from '@wings-software/uicore' import { Classes } from '@blueprintjs/core' import isUndefined from 'lodash/isUndefined' import moment from 'moment' import classnames from 'classnames' import { TimeBasedShadedRegion, TimeBasedShadedRegionProps } from '../TimeBasedShadedRegion/TimeBasedShadedRegion' import { TimelineBar, TimelineBarProps } from './TimelineBar' import styles from './TimelineView.module.scss' interface RowItem { startTime: string | number | Date endTime?: string | number | Date [x: string]: any } interface RowConfig { name?: string | JSX.Element data: Array<RowItem> className?: string } export interface TimelineViewProps { startTime: string | number | Date endTime: string | number | Date rows: Array<RowConfig> minItemsDistance?: number renderItem(item: RowItem): JSX.Element renderBatch?(items: Array<RowItem>): JSX.Element className?: string labelsWidth?: number loading?: boolean hideTimelineBar?: boolean rowHeight?: number timelineBarProps?: Omit<TimelineBarProps, 'startDate' | 'endDate'> shadedRegionProps?: Omit<TimeBasedShadedRegionProps, 'parentRef'> } export default function TimelineView({ startTime, endTime, rows, minItemsDistance, renderItem, renderBatch, className, loading, labelsWidth = 125, hideTimelineBar = false, rowHeight = 50, timelineBarProps, shadedRegionProps }: TimelineViewProps): JSX.Element { const start = moment(startTime) const end = moment(endTime) const distance = end.diff(start) const ref = useRef<HTMLDivElement>(null) const timelineRowRef = useRef<HTMLDivElement>(null) const [rowWidth, setRowWidth] = useState(0) const leftPosition = (itemStartDate: any) => (100 * itemStartDate.diff(start)) / distance const rightPosition = (itemEndDate: any) => (100 * end.diff(itemEndDate)) / distance const onResize = () => { Eif (ref.current) { const rowElem = ref.current.querySelector(`.${styles.rowItems}`) Eif (rowElem) { setRowWidth(rowElem.getBoundingClientRect().width) } } } useEffect(() => { onResize() window.addEventListener('resize', onResize) return () => window.removeEventListener('resize', onResize) }, []) const rowsInternal = useMemo(() => { return rows.map(row => { let data: Array<any> = row.data.map(item => ({ left: leftPosition(moment(item.startTime)), right: rightPosition(moment(item.endTime || item.startTime)), item, batch: [] })) Iif (!!minItemsDistance && data.length >= 2) { data = calculateInternals(data, rowWidth, minItemsDistance) } return { name: row.name, data } }) }, [rows, rowWidth]) const showLabels = rowsInternal.some(row => !isUndefined(row.name)) return ( <div className={classnames(styles.timelineView, 'timelineView', className)} ref={ref}> {!hideTimelineBar && ( <TimelineBar style={showLabels ? { marginLeft: labelsWidth, marginBottom: 'var(--spacing-small)' } : undefined} className={styles.timelineViewBar} {...timelineBarProps} startDate={startTime} endDate={endTime} /> )} {rowsInternal.map((row, rowIndex) => ( <div key={rowIndex} className={classnames(styles.timelineRow, 'timelineRow')} style={{ height: rowHeight }}> {showLabels && ( <Container className={styles.nameWrapper} width={labelsWidth}> {typeof row.name === 'string' ? <Text width={labelsWidth}>{row.name}</Text> : row.name} </Container> )} <div className={classnames( styles.rowItems, 'rowItems', loading ? classnames(styles.loading, Classes.SKELETON) : undefined )} ref={rowIndex === 0 ? timelineRowRef : undefined} > {row.data.map((item, itemIndex) => ( <Container key={itemIndex} style={{ left: `${item.left}%`, right: `${item.right}%` }} className={styles.rowItem} > {item.left >= 0 && item.left <= 100 && ( <> {item.item && renderItem(item.item)} {!!item.batch.length && renderBatch?.(item.batch)} </> )} </Container> ))} </div> </div> ))} {shadedRegionProps && <TimeBasedShadedRegion {...shadedRegionProps} parentRef={timelineRowRef?.current} />} </div> ) } function calculateInternals(data: Array<any>, rowWidth: number, minItemsDistance: number) { if (rowWidth) { data.sort((a, b) => a.left - b.left) for (let i = 0, j = 1; j < data.length; ) { if (data[i].batch.length) { // previous is batch const x1 = (rowWidth * data[i].left) / 100 const x2 = (rowWidth * data[j].left) / 100 if (x2 - minItemsDistance <= x1) { data[i].batch.push(data[j].item) data[j].item = undefined data[i].right = data[j].right j++ } else { i = j j++ } } else { const x1 = rowWidth * (1 - data[i].right / 100) const x2 = (rowWidth * data[j].left) / 100 if (x2 - minItemsDistance <= x1) { data[i].batch = [data[i].item, data[j].item] data[i].item = data[j].item = undefined data[i].right = data[j].right j++ } else { i = j j++ } } } return data.filter(e => e.item || e.batch.length) } else { return [] } } |