github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ui/src/redux/hover.ts (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 /** 12 * Monitors the currently hovered chart and point in time. 13 */ 14 15 import moment from "moment"; 16 import { Action } from "redux"; 17 18 import { PayloadAction } from "src/interfaces/action"; 19 import { AdminUIState } from "src/redux/state"; 20 21 export const HOVER_ON = "cockroachui/hover/HOVER_ON"; 22 export const HOVER_OFF = "cockroachui/hover/HOVER_OFF"; 23 24 /** 25 * HoverInfo is conveys the current hover position to the state. 26 */ 27 export interface HoverInfo { 28 hoverChart: string; 29 hoverTime: moment.Moment; 30 } 31 32 export class HoverState { 33 // Are we currently hovering over a chart? 34 currentlyHovering = false; 35 // Which chart are we hovering over? 36 hoverChart: string; 37 // What point in time are we hovering over? 38 hoverTime: moment.Moment; 39 } 40 41 export function hoverReducer(state = new HoverState(), action: Action): HoverState { 42 switch (action.type) { 43 case HOVER_ON: 44 const { payload: hi } = action as PayloadAction<HoverInfo>; 45 return { 46 currentlyHovering: true, 47 hoverChart: hi.hoverChart, 48 hoverTime: hi.hoverTime, 49 }; 50 case HOVER_OFF: 51 return new HoverState(); 52 default: 53 return state; 54 } 55 } 56 57 export function hoverOn(hi: HoverInfo): PayloadAction<HoverInfo> { 58 return { 59 type: HOVER_ON, 60 payload: hi, 61 }; 62 } 63 64 export function hoverOff(): Action { 65 return { 66 type: HOVER_OFF, 67 }; 68 } 69 70 /** 71 * Are we currently hovering, and if so, which chart and when? 72 */ 73 export const hoverStateSelector = (state: AdminUIState) => state.hover;