import { ReactNode, SVGProps, memo, useRef, useEffect, useMemo, useState, } from 'react' import { pointer, select } from 'd3-selection' import { Axis, AxisScale } from 'd3-axis' import { brushX, D3BrushEvent } from 'd3-brush' import { area, line, curveStepAfter, CurveFactory } from 'd3-shape' import { nanoid } from 'nanoid' import dayjs from 'dayjs' import clsx from 'clsx' import { Contract } from 'common/contract' import { Row } from 'web/components/layout/row' export type Point = { x: X; y: Y; datum?: T } export type XScale

= P extends Point ? AxisScale : never export type YScale

= P extends Point ? AxisScale : never export const MARGIN = { top: 20, right: 10, bottom: 20, left: 40 } export const MARGIN_X = MARGIN.right + MARGIN.left export const MARGIN_Y = MARGIN.top + MARGIN.bottom export const XAxis = (props: { w: number; h: number; axis: Axis }) => { const { h, axis } = props const axisRef = useRef(null) useEffect(() => { if (axisRef.current != null) { select(axisRef.current) .transition() .duration(250) .call(axis) .select('.domain') .attr('stroke-width', 0) } }, [h, axis]) return } export const YAxis = (props: { w: number; h: number; axis: Axis }) => { const { w, h, axis } = props const axisRef = useRef(null) useEffect(() => { if (axisRef.current != null) { select(axisRef.current) .transition() .duration(250) .call(axis) .call((g) => g.selectAll('.tick line').attr('x2', w).attr('stroke-opacity', 0.1) ) .select('.domain') .attr('stroke-width', 0) } }, [w, h, axis]) return } const LinePathInternal = ( props: { data: P[] px: number | ((p: P) => number) py: number | ((p: P) => number) curve?: CurveFactory } & SVGProps ) => { const { data, px, py, curve, ...rest } = props const d3Line = line

(px, py).curve(curve ?? curveStepAfter) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return } export const LinePath = memo(LinePathInternal) as typeof LinePathInternal const AreaPathInternal = ( props: { data: P[] px: number | ((p: P) => number) py0: number | ((p: P) => number) py1: number | ((p: P) => number) curve?: CurveFactory } & SVGProps ) => { const { data, px, py0, py1, curve, ...rest } = props const d3Area = area

(px, py0, py1).curve(curve ?? curveStepAfter) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return } export const AreaPath = memo(AreaPathInternal) as typeof AreaPathInternal export const AreaWithTopStroke = (props: { color: string data: P[] px: number | ((p: P) => number) py0: number | ((p: P) => number) py1: number | ((p: P) => number) curve?: CurveFactory }) => { const { color, data, px, py0, py1, curve } = props return ( ) } export const SVGChart = >(props: { children: ReactNode w: number h: number xAxis: Axis yAxis: Axis onSelect?: (ev: D3BrushEvent) => void onMouseOver?: (mouseX: number, mouseY: number) => P | undefined Tooltip?: TooltipComponent

}) => { const { children, w, h, xAxis, yAxis, onMouseOver, onSelect, Tooltip } = props const [mouseState, setMouseState] = useState() const overlayRef = useRef(null) const innerW = w - MARGIN_X const innerH = h - MARGIN_Y const clipPathId = useMemo(() => nanoid(), []) const justSelected = useRef(false) useEffect(() => { if (onSelect != null && overlayRef.current) { const brush = brushX().extent([ [0, 0], [innerW, innerH], ]) brush.on('end', (ev) => { // when we clear the brush after a selection, that would normally cause // another 'end' event, so we have to suppress it with this flag if (!justSelected.current) { justSelected.current = true onSelect(ev) setMouseState(undefined) if (overlayRef.current) { select(overlayRef.current).call(brush.clear) } } else { justSelected.current = false } }) // mqp: shape-rendering null overrides the default d3-brush shape-rendering // of `crisp-edges`, which seems to cause graphical glitches on Chrome // (i.e. the bug where the area fill flickers white) select(overlayRef.current) .call(brush) .select('.selection') .attr('shape-rendering', 'null') } }, [innerW, innerH, onSelect]) const onPointerMove = (ev: React.PointerEvent) => { if (ev.pointerType === 'mouse' && onMouseOver) { const [mouseX, mouseY] = pointer(ev) const p = onMouseOver(mouseX, mouseY) if (p != null) { setMouseState({ top: mouseY - 10, left: mouseX + 60, p }) } else { setMouseState(undefined) } } } const onPointerLeave = () => { setMouseState(undefined) } return (

{mouseState && Tooltip && ( )} {children}
) } export type TooltipProps

= { p: P; xScale: XScale

} export type TooltipComponent

= React.ComponentType> export type TooltipPosition = { top: number; left: number } export const TooltipContainer = ( props: TooltipPosition & { className?: string; children: React.ReactNode } ) => { const { top, left, className, children } = props return (

{children}
) } export type LegendItem = { color: string; label: string; value?: string } export const Legend = (props: { className?: string; items: LegendItem[] }) => { const { items, className } = props return (
    {items.map((item) => (
  1. {item.label} {item.value}
  2. ))}
) } export const getDateRange = (contract: Contract) => { const { createdTime, closeTime, resolutionTime } = contract const isClosed = !!closeTime && Date.now() > closeTime const endDate = resolutionTime ?? (isClosed ? closeTime : null) return [new Date(createdTime), endDate ? new Date(endDate) : null] as const } export const getRightmostVisibleDate = ( contractEnd: Date | null | undefined, lastActivity: Date | null | undefined, now: Date ) => { if (contractEnd != null) { return contractEnd } else if (lastActivity != null) { // client-DB clock divergence may cause last activity to be later than now return new Date(Math.max(lastActivity.getTime(), now.getTime())) } else { return now } } export const formatPct = (n: number, digits?: number) => { return `${(n * 100).toFixed(digits ?? 0)}%` } export const formatDate = ( date: Date, opts: { includeYear: boolean; includeHour: boolean; includeMinute: boolean } ) => { const { includeYear, includeHour, includeMinute } = opts const d = dayjs(date) const now = Date.now() if ( d.add(1, 'minute').isAfter(now) && d.subtract(1, 'minute').isBefore(now) ) { return 'Now' } else { const dayName = d.isSame(now, 'day') ? 'Today' : d.add(1, 'day').isSame(now, 'day') ? 'Yesterday' : null let format = dayName ? `[${dayName}]` : 'MMM D' if (includeMinute) { format += ', h:mma' } else if (includeHour) { format += ', ha' } else if (includeYear) { format += ', YYYY' } return d.format(format) } } export const formatDateInRange = (d: Date, start: Date, end: Date) => { const opts = { includeYear: !dayjs(start).isSame(end, 'year'), includeHour: dayjs(start).add(8, 'day').isAfter(end), includeMinute: dayjs(end).diff(start, 'hours') < 2, } return formatDate(d, opts) }