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
import { type FC, useMemo } from "react";
import { observer } from "mobx-react";
import chroma from "chroma-js";
import { Button } from "@humansignal/ui";
import { IconCommentLinkTo, IconClose } from "@humansignal/icons";
import { cn } from "../../../utils/bem";
import { NodeIcon } from "../../Node/Node";
import { RegionLabel } from "../../SidePanels/OutlinerPanel/RegionLabel";
 
import "./LinkState.scss";
 
type LinkStateProps = {
  linking: boolean;
  region: MSTRegion;
  result: MSTResult;
  onUnlink?: (region: any) => void;
  interactive?: boolean;
};
 
export const LinkState: FC<LinkStateProps> = ({ linking, region, result, onUnlink, interactive }) => {
  const isVisible = linking || region;
  const mod = useMemo(() => {
    if (linking) return { action: true };
    if (region) return { display: true };
    return undefined;
  }, [linking, region]);
  if (!isVisible) return null;
  return (
    <div className={cn("link-state").mod(mod).toClassName()}>
      <div className={cn("link-state").elem("prefix").toClassName()}>
        <IconCommentLinkTo />
      </div>
      {mod?.action && "Select an object to link it to this comment."}
      {mod?.display && <LinkedRegion region={region} result={result} onUnlink={onUnlink} interactive={interactive} />}
    </div>
  );
};
 
type LinkedRegionProps = {
  region: any;
  result?: MSTResult;
  onUnlink?: (item: any) => void;
  interactive?: boolean;
};
 
const LinkedRegion: FC<LinkedRegionProps> = observer(({ region, result, interactive, onUnlink }) => {
  const itemColor = region?.background ?? region?.getOneColor?.();
  const isClassification: boolean = region.classification;
 
  const { mouseEnterHandler, mouseLeaveHandler, clickHandler } = useMemo(() => {
    if (!interactive) return {};
 
    const mouseEnterHandler = () => {
      region?.setHighlight?.(true);
    };
    const mouseLeaveHandler = () => {
      region?.setHighlight?.(false);
    };
    const clickHandler = () => {
      if (region.classification) return null;
      region.annotation.selectArea(region);
    };
    return { mouseEnterHandler, mouseLeaveHandler, clickHandler };
  }, [interactive, region]);
 
  const style = useMemo(() => {
    const color = chroma(itemColor ?? "#666").alpha(1);
    return {
      "--icon-color": color.css(),
      "--text-color": color.css(),
    };
  }, [itemColor]);
 
  return (
    <div
      className={cn("link-state-region").mod({ interactive }).toClassName()}
      style={style as any}
      onMouseEnter={mouseEnterHandler}
      onMouseLeave={mouseLeaveHandler}
      onClick={clickHandler}
    >
      {!isClassification && (
        <>
          <div className={cn("link-state-region").elem("icon").toClassName()}>
            <NodeIcon node={region} />
          </div>
          <div className={cn("link-state-region").elem("index").toClassName()}>{region.region_index}</div>
        </>
      )}
      {result ? (
        <div className={cn("link-state-region").elem("title").toClassName()}>
          <ResultText result={result} />
        </div>
      ) : (
        <div className={cn("link-state-region").elem("title").toClassName()}>
          <div className={cn("link-state-region").elem("label").toClassName()}>
            <RegionLabel item={region} />
          </div>
          {region?.text && (
            <div className={cn("link-state-region").elem("text").toClassName()}>
              {region.text.replace(/\\n/g, "\n")}
            </div>
          )}
        </div>
      )}
      {onUnlink && (
        <div className={cn("link-state-region").elem("close").toClassName()}>
          <Button
            size="small"
            variant="neutral"
            look="string"
            leading={<IconClose />}
            onClick={onUnlink}
            aria-label="Unlink comment"
          />
        </div>
      )}
    </div>
  );
});
 
/**
 * Simply displaying the content of classification result
 */
const ResultText: FC<{ result: MSTResult }> = observer(({ result }) => {
  const { from_name: control, type, mainValue } = result;
  const { name } = control;
 
  if (type === "textarea") return [name, mainValue.join(" | ")].join(": ");
  if (type === "choices") return [name, mainValue.join(", ")].join(": ");
  if (type === "taxonomy") {
    const values = mainValue.map((v: string[]) => v.join("/"));
    return [name, values.join(", ")].join(": ");
  }
 
  return [name, String(mainValue)].join(": ");
});