Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
import { useCallback, useEffect, useState } from "react";
import { Circle, Group, Image, Layer, Rect } from "react-konva";
import { IconCheck, IconCross } from "@humansignal/icons";
import Konva from "konva";
import chroma from "chroma-js";
import { observer } from "mobx-react";
import { isDefined } from "../../utils/utilities";
 
const getItemPosition = (item) => {
  const { shapeRef: shape, bboxCoordsCanvas: bbox } = item;
  let width;
  let height;
  let x;
  let y;
 
  if (isDefined(bbox)) {
    [width, height, x, y] = [bbox.right - bbox.left, bbox.bottom - bbox.top, bbox.left, bbox.top];
  } else if (isDefined(shape)) {
    [width, height] = [shape?.width() ?? 0, shape?.height() ?? 0];
    [x, y] = [item.x + width / 2 - 32, item.x + width / 2 - 32];
  } else {
    return null;
  }
 
  return {
    x: x + width / 2 - 32,
    y: y + height + 10,
  };
};
 
export const SuggestionControls = observer(({ item, useLayer }) => {
  const position = getItemPosition(item);
  const [hovered, setHovered] = useState(false);
  const scale = 1 / item.parent.zoomScale;
 
  if (position) {
    const size = {
      width: 64,
      height: 32,
    };
 
    const groupPosition = useLayer
      ? {
          x: 0,
          y: 0,
          scaleX: 1,
          scaleY: 1,
        }
      : {
          x: position.x,
          y: position.y,
          scaleX: scale,
          scaleY: scale,
        };
 
    const layerPosition = useLayer
      ? {
          x: position.x,
          y: position.y,
          scaleX: scale,
          scaleY: scale,
        }
      : {};
 
    const content = (
      <Group
        {...size}
        {...groupPosition}
        opacity={item.highlighted || hovered ? 1 : 0.5}
        onMouseEnter={() => setHovered(true)}
        onMouseLeave={() => setHovered(false)}
      >
        <Rect x={0} y={0} width={64} height={32} fill="#000" cornerRadius={16} />
        <ControlButton
          onClick={() => item.annotation.rejectSuggestion(item.id)}
          fill="#DD0000"
          iconColor="#fff"
          icon={IconCross}
        />
        <ControlButton
          x={32}
          onClick={() => item.annotation.acceptSuggestion(item.id)}
          fill="#98C84E"
          iconColor="#fff"
          icon={IconCheck}
        />
      </Group>
    );
 
    return useLayer ? (
      <Layer {...size} {...layerPosition}>
        {content}
      </Layer>
    ) : (
      content
    );
  }
  return null;
});
 
const ControlButton = ({ x = 0, fill, iconColor, onClick, icon }) => {
  const [img, setImg] = useState(new window.Image());
  const imageSize = 16;
  const imageOffset = 32 / 2 - imageSize / 2;
  const color = chroma(iconColor ?? "#fff");
  const [hovered, setHovered] = useState(false);
 
  useEffect(() => {
    const iconImage = new window.Image();
 
    iconImage.onload = () => {
      setImg(iconImage);
    };
    iconImage.width = 12;
    iconImage.height = 12;
    iconImage.src = icon;
  }, [icon]);
 
  const applyFilter = useCallback(
    /**
     * @param {import("konva/types/shapes/Image").Image} imgInstance Instance of a Konva Image object
     */
    (imgInstance) => {
      if (imgInstance) {
        const [red, green, blue, alpha] = color.rgba();
 
        imgInstance.cache();
        imgInstance.setAttrs({
          red,
          green,
          blue,
          alpha,
        });
      }
    },
    [],
  );
 
  return (
    <Group
      x={x}
      width={32}
      height={32}
      onClick={onClick}
      onMouseEnter={() => setHovered(true)}
      onMouseLeave={() => setHovered(false)}
    >
      <Circle x={16} y={16} radius={14} opacity={hovered ? 1 : 0.2} fill={hovered ? fill : "#fff"} />
      <Image
        ref={(node) => applyFilter(node)}
        x={imageOffset}
        y={imageOffset}
        width={imageSize}
        height={imageSize}
        image={img}
        filters={[Konva.Filters.RGB]}
      />
    </Group>
  );
};