Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: heatmap small fixes #1545

Merged
merged 3 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/server/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ func (ctrl *Controller) serverMux() (http.Handler, error) {
{"/settings", ih},
{"/settings/{page}", ih},
{"/settings/{page}/{subpage}", ih},
{"/forbidden", ih},
{"/exemplars/single", ih},
{"/exemplars/merge", ih},
{"/explore", ih}},
ctrl.drainMiddleware,
ctrl.authMiddleware(ctrl.indexHandler()))
Expand Down
39 changes: 21 additions & 18 deletions webapp/javascript/components/Heatmap/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useRef, useMemo, useEffect } from 'react';
import React, { useState, useRef, useMemo, useEffect, RefObject } from 'react';
import useResizeObserver from '@react-hook/resize-observer';
import Color from 'color';
import cl from 'classnames';
Expand Down Expand Up @@ -33,14 +33,17 @@ interface HeatmapProps {
export function Heatmap({ heatmap, onSelection }: HeatmapProps) {
const canvasRef = useRef<HTMLCanvasElement>(null);
const heatmapRef = useRef<HTMLDivElement>(null);
const resizedSelectedAreaRef = useRef<HTMLDivElement>(null);
const [heatmapW, setHeatmapW] = useState(0);

const {
selectedCoordinates,
selectedAreaToHeatmapRatio,
hasSelectedArea,
resetSelection,
} = useHeatmapSelection({ canvasRef, heatmapW, heatmap, onSelection });
const { selectedCoordinates, selectedAreaToHeatmapRatio, resetSelection } =
useHeatmapSelection({
canvasRef,
resizedSelectedAreaRef,
heatmapW,
heatmap,
onSelection,
});

useEffect(() => {
if (heatmapRef.current) {
Expand Down Expand Up @@ -126,17 +129,14 @@ export function Heatmap({ heatmap, onSelection }: HeatmapProps) {
max={heatmap.maxValue}
ticksNumber={5}
/>
{hasSelectedArea &&
selectedCoordinates.end &&
selectedCoordinates.start && (
<ResizedSelectedArea
start={selectedCoordinates.start}
end={selectedCoordinates.end}
containerW={heatmapW}
resizeRatio={selectedAreaToHeatmapRatio}
handleClick={resetSelection}
/>
)}
<ResizedSelectedArea
resizedSelectedAreaRef={resizedSelectedAreaRef}
start={selectedCoordinates.start || { x: 0, y: 0 }}
end={selectedCoordinates.end || { x: 0, y: 0 }}
containerW={heatmapW}
resizeRatio={selectedAreaToHeatmapRatio}
handleClick={resetSelection}
/>
<svg role="img" className={styles.heatmapSvg} height={HEATMAP_HEIGHT}>
{heatmapGrid}
<foreignObject
Expand Down Expand Up @@ -184,6 +184,7 @@ export function Heatmap({ heatmap, onSelection }: HeatmapProps) {
}

interface ResizedSelectedArea {
resizedSelectedAreaRef: RefObject<HTMLDivElement>;
containerW: number;
start: SelectedAreaCoordsType;
end: SelectedAreaCoordsType;
Expand All @@ -192,6 +193,7 @@ interface ResizedSelectedArea {
}

function ResizedSelectedArea({
resizedSelectedAreaRef,
containerW,
start,
end,
Expand All @@ -207,6 +209,7 @@ function ResizedSelectedArea({

return (
<div
ref={resizedSelectedAreaRef}
data-testid="selection-resizable-canvas"
onClick={handleClick}
className={styles.selectedAreaBlock}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { useHeatmapSelection } from './useHeatmapSelection.hook';
import { heatmapMockData } from '../../services/exemplarsTestData';

const canvasEl = document.createElement('canvasEl');
const divEl = document.createElement('div');
const canvasRef = { current: canvasEl } as RefObject<HTMLCanvasElement>;
const resizedSelectedAreaRef = { current: divEl } as RefObject<HTMLDivElement>;

function createStore(preloadedState: any) {
const store = configureStore({
Expand All @@ -29,6 +31,7 @@ describe('Hook: useHeatmapSelection', () => {
() =>
useHeatmapSelection({
canvasRef,
resizedSelectedAreaRef,
heatmapW: 1234,
heatmap: heatmapMockData,
onSelection: () => ({}),
Expand All @@ -55,7 +58,6 @@ describe('Hook: useHeatmapSelection', () => {
expect(current).toMatchObject({
selectedCoordinates: { start: null, end: null },
selectedAreaToHeatmapRatio: 1,
hasSelectedArea: false,
resetSelection: expect.any(Function),
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface SelectedCoordinates {
}
interface UseHeatmapSelectionProps {
canvasRef: RefObject<HTMLCanvasElement>;
resizedSelectedAreaRef: RefObject<HTMLDivElement>;
heatmapW: number;
heatmap: Heatmap;
onSelection: (
Expand All @@ -27,23 +28,21 @@ interface UseHeatmapSelectionProps {
}
interface UseHeatmapSelection {
selectedCoordinates: SelectedCoordinates;
hasSelectedArea: boolean;
selectedAreaToHeatmapRatio: number;
resetSelection: () => void;
}

export const useHeatmapSelection = ({
canvasRef,
resizedSelectedAreaRef,
heatmapW,
heatmap,
onSelection,
}: UseHeatmapSelectionProps): UseHeatmapSelection => {
const [hasSelectedArea, setHasSelectedArea] = useState(false);
const [selectedCoordinates, setSelectedCoordinates] =
useState<SelectedCoordinates>(DEFAULT_SELECTED_COORDINATES);

const resetSelection = () => {
setHasSelectedArea(false);
setSelectedCoordinates(DEFAULT_SELECTED_COORDINATES);
startCoords = null;
endCoords = null;
Expand Down Expand Up @@ -108,7 +107,6 @@ export const useHeatmapSelection = ({
if (startCoords) {
const canvas = canvasRef.current as HTMLCanvasElement;
const { left, top, width, height } = canvas.getBoundingClientRect();
setHasSelectedArea(true);
clearRect(canvas);

const xCursorPosition = e.clientX - left;
Expand Down Expand Up @@ -188,12 +186,27 @@ export const useHeatmapSelection = ({
canvasRef.current.addEventListener('mousedown', startDrawing);
}

if (resizedSelectedAreaRef.current) {
resizedSelectedAreaRef.current.addEventListener(
'mousedown',
startDrawing
);
}

return () => {
if (canvasRef.current) {
canvasRef.current.removeEventListener('mousedown', startDrawing);
window.removeEventListener('mousemove', handleDrawingEvent);
window.removeEventListener('mouseup', endDrawing);
}

if (resizedSelectedAreaRef.current) {
resizedSelectedAreaRef.current.removeEventListener(
'mousedown',
startDrawing
);
}

window.removeEventListener('mousemove', handleDrawingEvent);
window.removeEventListener('mouseup', endDrawing);
};
}, [heatmap, heatmapW]);

Expand All @@ -210,7 +223,6 @@ export const useHeatmapSelection = ({
return {
selectedCoordinates,
selectedAreaToHeatmapRatio,
hasSelectedArea,
resetSelection,
};
};
2 changes: 1 addition & 1 deletion webapp/javascript/components/Heatmap/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Heatmap } from '@webapp/services/render';

import { getTimelineFormatDate } from '@webapp/util/formatDate';
import { SELECTED_AREA_BACKGROUND, HEATMAP_HEIGHT } from './constants';
import type { SelectedAreaCoordsType } from './useHeatmapSelection.hook';
import { getTimelineFormatDate } from '@webapp/util/formatDate';

export const drawRect = (
canvas: HTMLCanvasElement,
Expand Down
4 changes: 2 additions & 2 deletions webapp/javascript/services/exemplarsTestData.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export const heatmapMockData = {
endTime: 1659642600000,
endTime: 1859642600000000,
maxDepth: 23077,
maxValue: 110,
minDepth: 1,
minValue: 1,
startTime: 1659642000000,
startTime: 1659642000000000,
timeBuckets: 60,
valueBuckets: 32,
values: [
Expand Down