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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { IconEyeClosed, IconEyeOpened, IconPlus, IconRelationLink, IconTrash, IconWarning } from "@humansignal/icons";
import { Button, type ButtonProps } from "@humansignal/ui";
import chroma from "chroma-js";
import { observer } from "mobx-react";
import { type FC, forwardRef, useMemo, useState } from "react";
import { WithHotkey } from "../../../common/Hotkey/WithHotkey";
import { CREATE_RELATION_MODE } from "../../../stores/Annotation/LinkingModes";
import { cn } from "../../../utils/bem";
import { NodeIcon } from "../../Node/Node";
import { LockButton } from "../Components/LockButton";
import { RegionLabels } from "./RegionLabels";
 
interface RegionItemProps {
  region: any;
  withActions?: boolean;
  compact?: boolean;
  withIds?: boolean;
  mainDetails?: FC<{ region: any }>;
  metaDetails?: FC<{
    region: any;
    editMode?: boolean;
    cancelEditMode?: () => void;
    enterEditMode: () => void;
  }>;
}
 
export const RegionItem: FC<RegionItemProps> = observer(
  ({
    region,
    compact = false,
    withActions = true,
    withIds = true,
    mainDetails: MainDetails,
    metaDetails: MetaDetails,
  }) => {
    const { annotation } = region;
    const { selectedRegions: nodes } = annotation;
    const [editMode, setEditMode] = useState(false);
 
    const hasEditableRegions = useMemo(() => {
      return !!nodes.find((node: any) => !node.isReadOnly() && !node.classification);
    }, [nodes]);
 
    const color = useMemo(() => {
      const bgColor = region.background ?? region.getOneColor() ?? "#666";
 
      return chroma(bgColor).alpha(1);
    }, [region.background, region.style]);
 
    return (
      <div className={cn("detailed-region").mod({ compact }).toClassName()} data-testid="detailed-region">
        <div className={cn("detailed-region").elem("head").toClassName()} style={{ color: color.css() }}>
          <div className={cn("detailed-region").elem("title").toClassName()}>
            <div className={cn("detailed-region").elem("icon").toClassName()}>
              <NodeIcon node={region} />
            </div>
            <div className={cn("detailed-region").elem("index").toClassName()}>
              <span className={cn("detailed-region").elem("index_value").toClassName()}>{region.region_index}</span>
            </div>
            <RegionLabels region={region} />
          </div>
          {withIds && <span>{region.cleanId}</span>}
        </div>
        {MainDetails && (
          <div className={cn("detailed-region").elem("content").toClassName()}>
            <MainDetails region={region} />
          </div>
        )}
        {region.isDrawing && (
          <div className={cn("detailed-region").elem("warning").toClassName()}>
            <IconWarning />
            <div className={cn("detailed-region").elem("warning-text").toClassName()}>
              Incomplete {region.type?.replace("region", "") ?? "region"}
            </div>
          </div>
        )}
        {withActions && (
          <RegionAction
            region={region}
            editMode={editMode}
            annotation={annotation}
            hasEditableRegions={hasEditableRegions}
            onEditModeChange={setEditMode}
          />
        )}
        {MetaDetails && (
          <div className={cn("detailed-region").elem("content").toClassName()}>
            <MetaDetails
              region={region}
              editMode={editMode}
              enterEditMode={() => setEditMode(true)}
              cancelEditMode={() => setEditMode(false)}
            />
          </div>
        )}
      </div>
    );
  },
);
 
const RegionAction: FC<any> = observer(({ region, annotation, editMode, onEditModeChange }) => {
  const entityButtons: JSX.Element[] = [];
 
  entityButtons.push(
    <WithHotkey binging="region:relation">
      <RegionActionButton
        key="relation"
        variant={annotation.isLinkingMode ? "primary" : "neutral"}
        look={annotation.isLinkingMode ? "filled" : "string"}
        onClick={(_e: any, hotkey?: any) => {
          // If this is triggered by a hotkey, defer to the global bound handler for relations to avoid contention.
          if (hotkey) return;
          if (annotation.isLinkingMode) {
            annotation.stopLinkingMode();
          } else {
            annotation.startLinkingMode(CREATE_RELATION_MODE, region);
          }
        }}
        aria-label="Create Relation"
      >
        <IconRelationLink />
      </RegionActionButton>
    </WithHotkey>,
  );
 
  entityButtons.push(
    <WithHotkey binging="region:meta">
      <RegionActionButton
        key="meta"
        look={editMode ? "filled" : "string"}
        variant={editMode ? "primary" : "neutral"}
        onClick={() => onEditModeChange(!editMode)}
        aria-label="Edit region's meta"
      >
        <IconPlus />
      </RegionActionButton>
    </WithHotkey>,
  );
 
  return (
    <div className={cn("region-actions").toClassName()}>
      <div className={cn("region-actions").elem("group").mod({ align: "left" }).toClassName()}>
        {!region.isReadOnly() && entityButtons}
      </div>
      <div className={cn("region-actions").elem("group").mod({ align: "right" }).toClassName()}>
        <LockButton
          item={region}
          annotation={region?.annotation}
          hovered={true}
          locked={region?.locked}
          onClick={() => region.setLocked(!region.locked)}
          displayedHotkey="region:lock"
          variant="neutral"
          look="string"
          aria-label="Unlock Region"
          tooltip="Unlock Region"
        />
        {region.hideable && (
          <RegionActionButton
            aria-label={`${region.hidden ? "Show" : "Hide"} selected region`}
            variant="neutral"
            look="string"
            onClick={region.toggleHidden}
            tooltip={`${region.hidden ? "Show" : "Hide"} selected region`}
          >
            {region.hidden ? <IconEyeClosed /> : <IconEyeOpened />}
          </RegionActionButton>
        )}
        <RegionActionButton
          variant="negative"
          look="string"
          aria-label="Delete selected region"
          disabled={region.isReadOnly()}
          tooltip="Delete selected region"
          onClick={() => annotation.deleteRegion(region)}
        >
          <IconTrash />
        </RegionActionButton>
      </div>
    </div>
  );
});
 
const RegionActionButton: FC<ButtonProps> = forwardRef(({ children, ...props }, ref) => {
  return (
    <Button ref={ref} variant="neutral" look="string" size="small" {...props}>
      {children}
    </Button>
  );
});