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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { createPortal } from "react-dom";
import { inject, observer } from "mobx-react";
import { isAlive } from "mobx-state-tree";
import truncate from "truncate-middle";
import clsx from "clsx";
import { format, isValid } from "date-fns";
import { useCopyText } from "@humansignal/core";
import { isDefined, userDisplayName } from "@humansignal/core/lib/utils/helpers";
import { cn } from "../../utils/bem";
import {
  IconAnnotationGroundTruth,
  IconAnnotationSkipped2,
  IconDraftCreated2,
  IconDuplicate,
  IconLink,
  IconTrashRect,
  IconCommentResolved,
  IconCommentUnresolved,
  IconSparks,
  IconStar,
  IconStarOutline,
  IconAnalytics,
  IconViewAll,
  IconClipboardCheck,
  IconCheckAlt,
  IconCrossAlt,
  IconEllipsisVertical,
} from "@humansignal/icons";
import { Tooltip, Userpic, ToastType, useToast, Badge, DropdownTrigger } from "@humansignal/ui";
import { TimeAgo } from "../../common/TimeAgo/TimeAgo";
import { useDropdown } from "@humansignal/ui";
import { isFF } from "../../utils/feature-flags";

// eslint-disable-next-line
// @ts-ignore
import { confirm } from "../../common/Modal/Modal";
import { type ContextMenuAction, ContextMenu, type MenuActionOnClick } from "../ContextMenu";
import { useResolveUser, isUserComplete } from "@humansignal/core/hooks/useResolveUser";
import "./AnnotationButton.prefix.css";

// Constants for name truncation
const NAME_TRUNCATE_START = 8;
const NAME_TRUNCATE_END = 6;
const NAME_TRUNCATE_THRESHOLD = 15;

// Utility function to detect if text is a person's name
const isPersonName = (text: string): boolean => {
  if (!text || text.includes("@")) return false; // Exclude emails
  const parts = text.trim().split(/\s+/);
  if (parts.length < 2) return false; // Need at least first + last name
  // Check if all parts are valid name parts (letters, hyphens, no numbers)
  return parts.every((part) => /^[a-zA-Z-]+$/.test(part) && part.length >= 2);
};

// Utility function to truncate person names
const truncatePersonName = (name: string): string => {
  // Only truncate if length exceeds threshold
  if (name.length <= NAME_TRUNCATE_THRESHOLD) return name;

  const parts = name.trim().split(/\s+/);
  if (parts.length === 0) return name;
  if (parts.length === 1) return name; // Single word, don't truncate

  const firstName = parts[0]; // Keep full
  const middleParts = parts.slice(1, -1).map((p) => `${p[0]}.`); // Middle names to initials
  const lastName = parts[parts.length - 1];
  const lastNameInitial = `${lastName[0]}.`;

  return [firstName, ...middleParts, lastNameInitial].join(" ");
};

interface AnnotationButtonInterface {
  entity?: any;
  capabilities?: any;
  annotationStore?: any;
  store: any;
  onAnnotationChange?: () => void;
}

const renderCommentIcon = (ent: any) => {
  if (ent.unresolved_comment_count > 0) {
    return IconCommentUnresolved;
  }
  if (ent.comment_count > 0) {
    return IconCommentResolved;
  }

  return null;
};

const renderCommentTooltip = (ent: any) => {
  if (ent.unresolved_comment_count > 0) {
    return "Unresolved Comments";
  }
  if (ent.comment_count > 0) {
    return "All Comments Resolved";
  }

  return "";
};

// Utility function to get review badge based on acceptedState
// Supports the same review statuses as the Annotators component: "accepted", "rejected", "fixed"
// Also supports "fixed_and_accepted" for backward compatibility (maps to "fixed")
const getReviewBadge = (acceptedState: string | null | undefined) => {
  if (!acceptedState) {
    return null;
  }

  let Icon = null;
  let badgeMod = "";

  switch (acceptedState) {
    case "accepted":
      Icon = IconCheckAlt;
      badgeMod = "accepted";
      break;
    case "rejected":
      Icon = IconCrossAlt;
      badgeMod = "rejected";
      break;
    case "fixed":
    case "fixed_and_accepted": // Backward compatibility
      Icon = IconCheckAlt;
      badgeMod = "fixed";
      break;
    default:
      return null;
  }

  // Use the same class structure as Annotators component
  const userPickBadge = cn("userpic-badge");
  const className = clsx(userPickBadge.toString(), userPickBadge.mod({ [badgeMod]: true }).toString());

  return (
    <div className={className}>
      <Icon />
    </div>
  );
};

const injector = inject(({ store }) => {
  return {
    store,
  };
});

const hoverIntentDelay = 300;

function AnnotationButtonTooltip({
  displayUsername,
  isDraft,
  isDraftSaved,
  isPrediction,
  isSkipped,
  isSubmitted,
  isGroundTruth,
  acceptedState,
  predictionScore,
  lastUpdated,
  annotationId,
  containerRef,
  isTooltipOpen,
  onMouseEnter,
  onMouseLeave,
  position,
}: {
  displayUsername: string;
  isDraft: boolean;
  isDraftSaved: boolean;
  isPrediction: boolean;
  isSkipped: boolean;
  isSubmitted: boolean;
  isGroundTruth?: boolean;
  acceptedState: string | null | undefined;
  predictionScore?: number | null;
  lastUpdated?: string | null;
  annotationId?: string | number | null;
  containerRef?: React.MutableRefObject<HTMLElement | undefined>;
  isTooltipOpen?: boolean;
  onMouseEnter?: (e: React.MouseEvent) => void;
  onMouseLeave?: (e: React.MouseEvent) => void;
  position?: { top: number; left: number };
}) {
  // Determine status badge (only for annotations, not predictions)
  // Draft/Submitted are separate from Skipped/Ground Truth
  const statusBadge = useMemo(() => {
    if (isPrediction) {
      return null; // Status doesn't apply to predictions
    }

    // Priority order: Draft > Accepted/Rejected/Fixed > Submitted
    // Skipped and Ground Truth are handled separately
    // Check for both ephemeral drafts (isDraft) and saved drafts (isDraftSaved)
    // Exception: If Draft AND Skipped, show both Draft and Skipped
    if (isDraft || isDraftSaved) {
      return { label: "Draft", variant: "primary" as const };
    }
    if (acceptedState) {
      switch (acceptedState) {
        case "accepted":
          return { label: "Accepted", variant: "positive" as const };
        case "rejected":
          return { label: "Rejected", variant: "negative" as const };
        case "fixed":
        case "fixed_and_accepted":
          return { label: "Fixed", variant: "warning" as const };
        default:
          break;
      }
    }
    // Exception: If Submitted AND Skipped, only show Skipped (don't show Submitted)
    if (isSubmitted && !isSkipped) {
      return { label: "Submitted", variant: "positive" as const };
    }

    return null;
  }, [isPrediction, isDraft, isDraftSaved, acceptedState, isSubmitted, isSkipped]);

  // Format date using date-fns, matching Data Manager format: "MMM dd yyyy, HH:mm:ss" (e.g., "Jan 15 2024, 14:30:45")
  const formatDate = useCallback((dateString: string | null | undefined): string | null => {
    if (!dateString) return null;

    try {
      const date = new Date(dateString);
      if (!isValid(date)) return null;

      // Use the same format as Data Manager's DateTimeCell
      return format(date, "MMM dd yyyy, HH:mm:ss");
    } catch {
      return null;
    }
  }, []);

  const tooltipData = useMemo(() => {
    const rows: Array<{ label: string; value: string }> = [];

    // Add Annotation ID first if available
    if (annotationId) {
      rows.push({ label: "Annotation ID", value: String(annotationId) });
    }

    // Add Type for all annotations/predictions
    if (isPrediction) {
      rows.push({ label: "Type", value: "Prediction" });
      if (isDefined(predictionScore)) {
        rows.push({ label: "Prediction Score", value: `${(predictionScore * 100).toFixed(2)}%` });
      }
    } else {
      rows.push({ label: "Type", value: "Annotation" });
    }

    // Add Last Updated after Type
    if (lastUpdated) {
      const formattedDate = formatDate(lastUpdated);
      if (formattedDate) {
        rows.push({ label: "Last Updated", value: formattedDate });
      }
    }

    return rows;
  }, [annotationId, isPrediction, predictionScore, lastUpdated, formatDate]);

  const tooltipBadges = useMemo(() => {
    const badges: Array<{ label: string; variant: "primary" | "positive" | "negative" | "warning" }> = [];
    if (statusBadge) badges.push(statusBadge);
    if (isSkipped) badges.push({ label: "Skipped", variant: "negative" });
    if (isGroundTruth) badges.push({ label: "Ground Truth", variant: "warning" });
    return badges;
  }, [statusBadge, isSkipped, isGroundTruth]);

  const isRenderable = tooltipData.length > 0 || !!displayUsername || tooltipBadges.length > 0 || !!annotationId;

  if (!isRenderable) {
    return null;
  }

  if (!isTooltipOpen || !position) {
    return null;
  }

  const tooltipContent = (
    <div
      className={cn("annotation-button").elem("tooltipContainer").mod({ open: isTooltipOpen }).toClassName()}
      ref={containerRef as any}
      onMouseEnter={onMouseEnter}
      onMouseLeave={onMouseLeave}
      style={{
        position: "fixed",
        top: `${position.top}px`,
        left: `${position.left}px`,
      }}
    >
      {tooltipBadges.length > 0 && (
        <div className={cn("annotation-button").elem("tooltipBadges").toClassName()}>
          {tooltipBadges.map(({ label, variant }) => (
            <Badge key={label} variant={variant} shape="rounded">
              {label}
            </Badge>
          ))}
        </div>
      )}
      {displayUsername && (
        <div className={cn("annotation-button").elem("tooltipContainerTitle").toClassName()}>{displayUsername}</div>
      )}
      {tooltipData.length > 0 && (
        <div className={cn("annotation-button").elem("tooltipContainerInfo").toClassName()}>
          {tooltipData.map((row, index) => (
            <div
              key={`${row.label}-${row.value}-${index}`}
              className={cn("annotation-button").elem("infoRow").toClassName()}
            >
              <div className={cn("annotation-button").elem("infoRowLabel").toClassName()}>{row.label}</div>
              <div className={cn("annotation-button").elem("infoRowValue").toClassName()}>{row.value}</div>
            </div>
          ))}
        </div>
      )}
    </div>
  );

  return typeof document !== "undefined" ? createPortal(tooltipContent, document.body) : null;
}

// AnnotationButtonContextMenu component - must be defined outside AnnotationButton
// to maintain stable component reference and prevent hooks order issues
const AnnotationButtonContextMenu = injector(
  observer(
    ({
      entity,
      capabilities,
      store,
      isGroundTruth,
      iconSize,
      onAnnotationChange,
      annotationStore,
    }: AnnotationButtonInterface & {
      isGroundTruth?: boolean;
      iconSize: number;
      onAnnotationChange?: () => void;
      annotationStore: any;
    }) => {
      // Check if entity is alive - must be done before any hooks
      const entityIsAlive = isAlive(entity);

      const annotationLink = useMemo(() => {
        if (!entityIsAlive || !entity.pk) {
          return "";
        }
        const url = new URL(window.location.href);
        if (entity.pk) {
          url.searchParams.set("annotation", entity.pk);
        }
        // In case of targeting directly an annotation, we don't want to show the region in the URL
        // otherwise it will be shown as a region link
        url.searchParams.delete("region");
        return url.toString();
      }, [entityIsAlive, entity.pk]);
      const [copyLink] = useCopyText({ defaultText: annotationLink });
      const toast = useToast();
      const dropdown = useDropdown();
      const clickHandler = () => {
        onAnnotationChange?.();
        dropdown?.close();
      };
      const setGroundTruth = useCallback<MenuActionOnClick>(() => {
        entity.setGroundTruth(!isGroundTruth);
        clickHandler();
      }, [entity, isGroundTruth]);
      const duplicateAnnotation = useCallback<MenuActionOnClick>(() => {
        const c = annotationStore.addAnnotationFromPrediction(entity);

        window.setTimeout(() => {
          annotationStore.selectAnnotation(c.id, { exitViewAll: true });
          clickHandler();
        });
      }, [entity, annotationStore]);
      const linkAnnotation = useCallback<MenuActionOnClick>(() => {
        copyLink();
        dropdown?.close();
        toast?.show({
          message: "Annotation link copied to clipboard",
          type: ToastType.info,
        });
      }, [copyLink, toast, dropdown]);
      const [copyAnnotationId] = useCopyText({ defaultText: entity.pk?.toString() ?? entity.id?.toString() ?? "" });
      const copyAnnotationIdHandler = useCallback<MenuActionOnClick>(() => {
        copyAnnotationId();
        dropdown?.close();
        toast?.show({
          message: "Annotation ID copied to clipboard",
          type: ToastType.info,
        });
      }, [copyAnnotationId, toast, dropdown]);
      const openPerformanceDashboard = useCallback<MenuActionOnClick>(() => {
        // Only available in LSE
        const isLSE = (window as any).APP_SETTINGS?.version?.edition === "Enterprise";
        if (!isLSE) return;

        const url = new URL(window.location.origin);
        const useNewAnalytics = isFF("fflag_feat_all_fit_778_analytics_short");

        // Route to different dashboards based on feature flag
        if (useNewAnalytics) {
          url.pathname = "/analytics/member-performance";
        } else {
          url.pathname = "/performance";
        }

        // Add user, project, and annotation context
        if (entity.user?.id) {
          url.searchParams.set("user", entity.user.id);
        }

        const projectMatch = window.location.pathname.match(/\/projects\/(\d+)/);
        if (projectMatch) {
          url.searchParams.set("project", projectMatch[1]);
        }

        window.open(url.toString(), "_blank");
        dropdown?.close();
      }, [entity, dropdown]);
      const showOtherAnnotations = useCallback<MenuActionOnClick>(() => {
        annotationStore.toggleViewingAllAnnotations();
        clickHandler();
      }, [annotationStore, onAnnotationChange]);
      const deleteAnnotation = useCallback(() => {
        clickHandler();
        confirm({
          title: "Delete annotation?",
          body: (
            <>
              This will <strong>delete all existing regions</strong>. Are you sure you want to delete them?
              <br />
              This action cannot be undone.
            </>
          ),
          buttonLook: "negative",
          okText: "Delete",
          onOk: () => {
            entity.list.deleteAnnotation(entity);
          },
        });
      }, [entity, onAnnotationChange]);
      const isPrediction = entity.type === "prediction";
      const isDraft = !isDefined(entity.pk);
      const showGroundTruth = capabilities.groundTruthEnabled && !isPrediction && !isDraft;
      const showDuplicateAnnotation = capabilities.enableCreateAnnotation && !isDraft;
      const isLSE = (window as any).APP_SETTINGS?.version?.edition === "Enterprise";

      // Check if project ID is available (from store or URL)
      const hasProjectId = !!window.location.pathname.match(/\/projects\/(\d+)/);

      const actions = useMemo<ContextMenuAction[]>(
        () => [
          {
            label: "Copy Annotation ID",
            onClick: copyAnnotationIdHandler,
            icon: <IconClipboardCheck width={20} height={20} />,
            enabled: !isDraft,
          },
          {
            label: `${isGroundTruth ? "Unset " : "Set "} as Ground Truth`,
            onClick: setGroundTruth,
            icon: isGroundTruth ? (
              <IconStar color="#FFC53D" width={iconSize} height={iconSize} />
            ) : (
              <IconStarOutline width={iconSize} height={iconSize} />
            ),
            enabled: showGroundTruth,
          },
          {
            label: "Duplicate Annotation",
            onClick: duplicateAnnotation,
            icon: <IconDuplicate width={20} height={20} />,
            enabled: showDuplicateAnnotation,
          },
          {
            label: "Copy Annotation Link",
            onClick: linkAnnotation,
            icon: <IconLink />,
            enabled: !isDraft && store.hasInterface("annotations:copy-link"),
          },
          {
            label: "Open Performance Dashboard",
            onClick: openPerformanceDashboard,
            icon: <IconAnalytics width={20} height={20} />,
            enabled: isLSE && hasProjectId && !isDraft && !isPrediction,
          },
          {
            label: "Show Other Annotations",
            onClick: showOtherAnnotations,
            icon: <IconViewAll width={20} height={20} />,
            enabled: true,
          },
          {
            label: "Delete Annotation",
            onClick: deleteAnnotation,
            icon: <IconTrashRect />,
            separator: true,
            danger: true,
            enabled: capabilities.enableAnnotationDelete && !isPrediction,
          },
        ],
        [
          entity,
          isGroundTruth,
          isPrediction,
          isDraft,
          isLSE,
          hasProjectId,
          capabilities.enableAnnotationDelete,
          capabilities.enableCreateAnnotation,
          capabilities.groundTruthEnabled,
          copyAnnotationIdHandler,
          openPerformanceDashboard,
          showOtherAnnotations,
          deleteAnnotation,
          setGroundTruth,
          duplicateAnnotation,
          linkAnnotation,
          iconSize,
          store,
        ],
      );

      // Return null if entity is not alive, but only after all hooks have been called
      if (!entityIsAlive) {
        return null;
      }

      return <ContextMenu actions={actions} />;
    },
  ),
);

export const AnnotationButton = observer(
  ({ entity, capabilities, annotationStore, onAnnotationChange }: AnnotationButtonInterface) => {
    // Check if entity is alive - must be done before any hooks to avoid accessing dead entity
    // But we'll return null AFTER all hooks are called to maintain hook order
    const entityIsAlive = isAlive(entity);

    const iconSize = 32;
    // Guard entity property access - use safe defaults if entity is not alive
    const isPrediction = entityIsAlive ? entity.type === "prediction" : false;
    // Use entity.user if it has meaningful display data (name/email); otherwise
    // fall back to createdBy (which comes from created_username in stub annotations).
    const resolvedUser =
      entityIsAlive && entity.user && isUserComplete(entity.user)
        ? entity.user
        : { firstName: entityIsAlive ? entity.createdBy || "Admin" : "Unknown" };
    const username = entityIsAlive ? userDisplayName(resolvedUser) : "Unknown";
    const [isGroundTruth, setIsGroundTruth] = useState<boolean>();
    const isDraft = entityIsAlive && !isPrediction && !isDefined(entity.pk);
    const isDraftSaved = entityIsAlive && !isPrediction && entity.draftId > 0;
    const isSkipped = entityIsAlive && !isPrediction && entity.skipped === true;
    // isSubmitted should be independent of skipped/ground truth status
    const isSubmitted = entityIsAlive && !isPrediction && !isDraft && !isDraftSaved;
    const infoIsHidden = annotationStore.store?.hasInterface("annotations:hide-info");
    let hiddenUser = null;

    // Tooltip state and refs
    const buttonRef = useRef<HTMLElement>();
    const mainSectionRef = useRef<HTMLElement>();
    const tooltipContainerRef = useRef<HTMLElement>();
    const timeoutRef = useRef<number>();
    const leaveTimeoutRef = useRef<number>();
    const [isTooltipOpen, setTooltipOpen] = useState(false);
    const [tooltipPosition, setTooltipPosition] = useState<{ top: number; left: number } | undefined>(undefined);
    const [isContextMenuOpen, setContextMenuOpen] = useState(false);

    // Lazily resolve incomplete user data when the button comes into view.
    // When annotations are loaded as stubs, entity.user may only have an ID
    // (no name/email), so this fetches the full user and updates the MST store.
    const enrichUser = useCallback(
      (userData: any) => annotationStore?.store?.enrichUsers?.([userData]),
      [annotationStore],
    );
    useResolveUser({ user: entity?.user, onUserResolved: enrichUser, elementRef: buttonRef });

    if (infoIsHidden && entityIsAlive) {
      // this data can be missing in tests, but we don't have `infoIsHidden` there, so hiding logic like this
      const currentUser = annotationStore.store.user;
      const isCurrentUser = entity.user?.id === currentUser.id || entity.createdBy === currentUser.email;
      hiddenUser = { email: isCurrentUser ? "Me" : "User" };
    }

    const displayUsername = hiddenUser ? hiddenUser.email : username;

    // Apply smart truncation based on content type
    const isName = isPersonName(displayUsername);
    const displayNameTruncated = isName
      ? displayUsername.length > NAME_TRUNCATE_THRESHOLD
        ? truncatePersonName(displayUsername)
        : displayUsername
      : displayUsername.length > NAME_TRUNCATE_THRESHOLD
        ? truncate(displayUsername, NAME_TRUNCATE_START, NAME_TRUNCATE_END, "...")
        : displayUsername;

    const CommentIcon = renderCommentIcon(entity);
    // need to find a more reliable way to grab this value
    // const historyActionType = annotationStore.history.toJSON()?.[0]?.actionType;

    useEffect(() => {
      if (entityIsAlive) {
        setIsGroundTruth(entity.ground_truth);
      }
    }, [entityIsAlive, entity, entity.ground_truth]);

    // Click outside handler for tooltip
    useEffect(() => {
      function handleClickOutside(e: MouseEvent) {
        const target = e.target as HTMLElement;
        if (!buttonRef.current?.contains(target) && !tooltipContainerRef.current?.contains(target)) {
          setTooltipOpen(false);
        }
      }
      document.addEventListener("mousedown", handleClickOutside);
      return () => {
        document.removeEventListener("mousedown", handleClickOutside);
      };
    }, []);

    // Track mouse position and check if tooltip should remain open
    useEffect(() => {
      const handleMouseMove = (e: MouseEvent) => {
        (window as any).lastMouseX = e.clientX;
        (window as any).lastMouseY = e.clientY;

        // If tooltip is open, check if mouse is still over button or tooltip
        if (isTooltipOpen) {
          const elementAtPoint = document.elementFromPoint(e.clientX, e.clientY);
          // Only call contains() if elementAtPoint is actually a Node
          const isOverTooltip = elementAtPoint && tooltipContainerRef.current?.contains(elementAtPoint as Node);
          const isOverButton = elementAtPoint && buttonRef.current?.contains(elementAtPoint as Node);
          const isOverTrigger = (elementAtPoint as HTMLElement)?.closest?.(".annotation-button__trigger");

          // Close tooltip if mouse is not over button or tooltip (or is over trigger)
          if (!isOverTooltip && (!isOverButton || isOverTrigger)) {
            setTooltipOpen(false);
          }
        }
      };
      document.addEventListener("mousemove", handleMouseMove);
      return () => {
        document.removeEventListener("mousemove", handleMouseMove);
      };
    }, [isTooltipOpen]);

    // Cleanup timeouts on unmount
    useEffect(() => {
      return () => {
        if (timeoutRef.current) {
          clearTimeout(timeoutRef.current);
        }
        if (leaveTimeoutRef.current) {
          clearTimeout(leaveTimeoutRef.current);
        }
      };
    }, []);

    const recalculateTooltipPosition = useCallback(() => {
      if (buttonRef.current) {
        const anchorPosition = buttonRef.current.getBoundingClientRect();

        // Estimate tooltip dimensions (we'll update after render)
        const estimatedTooltipWidth = 250;
        const estimatedTooltipHeight = 100;

        // Check if tooltip would go off the bottom of the screen
        const spaceBelow = window.innerHeight - anchorPosition.bottom;
        const spaceAbove = anchorPosition.top;

        let top: number;
        let left: number;

        // Position below by default, above if not enough space
        if (spaceBelow < estimatedTooltipHeight + 20 && spaceAbove > spaceBelow) {
          // Position above
          top = anchorPosition.top - estimatedTooltipHeight - 12;
        } else {
          // Position below
          top = anchorPosition.bottom + 12;
        }

        // Center horizontally, but adjust if too close to edges
        left = anchorPosition.left + anchorPosition.width / 2 - estimatedTooltipWidth / 2;

        // Adjust if too close to right edge
        if (left + estimatedTooltipWidth > window.innerWidth - 20) {
          left = window.innerWidth - estimatedTooltipWidth - 20;
        }

        // Adjust if too close to left edge
        if (left < 20) {
          left = 20;
        }

        setTooltipPosition({ top, left });

        // Update arrow position after tooltip renders
        requestAnimationFrame(() => {
          if (tooltipContainerRef.current) {
            const containerPosition = tooltipContainerRef.current.getBoundingClientRect();
            const arrowOffset = anchorPosition.left + anchorPosition.width / 2 - containerPosition.left;
            tooltipContainerRef.current.style.setProperty("--tooltip-arrow-position", `${arrowOffset}px`);
          }
        });
      }
    }, []);

    const handleTooltipEnter = useCallback(
      (e: React.MouseEvent) => {
        // Don't show tooltip if context menu is open
        if (isContextMenuOpen) {
          return;
        }

        // Clear any pending leave timeout
        if (leaveTimeoutRef.current) {
          clearTimeout(leaveTimeoutRef.current);
          leaveTimeoutRef.current = undefined;
        }

        const isTrigger = (e.target as HTMLElement)?.closest?.(".annotation-button__trigger");

        // Don't show tooltip if hovering over trigger
        if (isTrigger) {
          return;
        }

        if (timeoutRef.current) {
          clearTimeout(timeoutRef.current);
        }

        timeoutRef.current = window.setTimeout(() => {
          recalculateTooltipPosition();
          setTooltipOpen(true);
        }, hoverIntentDelay);
      },
      [recalculateTooltipPosition, isContextMenuOpen],
    );

    const handleTooltipLeave = useCallback((e: React.MouseEvent | React.FocusEvent) => {
      // Clear any pending enter timeout
      if (timeoutRef.current) {
        clearTimeout(timeoutRef.current);
        timeoutRef.current = undefined;
      }

      // Clear any existing leave timeout
      if (leaveTimeoutRef.current) {
        clearTimeout(leaveTimeoutRef.current);
      }

      const relatedTarget = ((e as React.MouseEvent).relatedTarget ||
        (e as React.FocusEvent).relatedTarget) as HTMLElement | null;

      // Check if we're moving to the tooltip container
      // Only call contains() if relatedTarget is actually a Node
      const isMovingToTooltip = relatedTarget && tooltipContainerRef.current?.contains(relatedTarget as Node);

      // If not moving to tooltip, check if moving back to button (but not trigger)
      // Only call contains() if relatedTarget is actually a Node
      const isMovingToButton = relatedTarget && buttonRef.current?.contains(relatedTarget as Node);
      const isMovingToTrigger = relatedTarget?.closest?.(".annotation-button__trigger");

      // If moving to tooltip or button (but not trigger), keep tooltip open
      if (isMovingToTooltip || (isMovingToButton && !isMovingToTrigger)) {
        return;
      }

      // Use a small delay to allow mouse to move to tooltip, but close if mouse is truly gone
      leaveTimeoutRef.current = window.setTimeout(() => {
        // Check current mouse position
        const currentMouseX = (window as any).lastMouseX;
        const currentMouseY = (window as any).lastMouseY;

        if (currentMouseX !== undefined && currentMouseY !== undefined) {
          const elementAtPoint = document.elementFromPoint(currentMouseX, currentMouseY);
          // Only call contains() if elementAtPoint is actually a Node
          const isOverTooltip = elementAtPoint && tooltipContainerRef.current?.contains(elementAtPoint as Node);
          const isOverButton = elementAtPoint && buttonRef.current?.contains(elementAtPoint as Node);
          const isOverTrigger = (elementAtPoint as HTMLElement)?.closest?.(".annotation-button__trigger");

          // Close tooltip unless mouse is over tooltip or button (but not trigger)
          if (!isOverTooltip && (!isOverButton || isOverTrigger)) {
            setTooltipOpen(false);
          }
        } else {
          // No mouse position available, close tooltip
          setTooltipOpen(false);
        }

        leaveTimeoutRef.current = undefined;
      }, 100); // Small delay to allow mouse movement to tooltip
    }, []);

    const handleTooltipContainerLeave = useCallback(() => {
      // When mouse leaves tooltip container, close immediately
      if (leaveTimeoutRef.current) {
        clearTimeout(leaveTimeoutRef.current);
        leaveTimeoutRef.current = undefined;
      }
      setTooltipOpen(false);
    }, []);

    const handleTooltipFocus = useCallback(() => {
      if (isContextMenuOpen) {
        return;
      }

      if (timeoutRef.current) {
        clearTimeout(timeoutRef.current);
      }

      timeoutRef.current = window.setTimeout(() => {
        recalculateTooltipPosition();
        setTooltipOpen(true);
      }, hoverIntentDelay);
    }, [recalculateTooltipPosition, isContextMenuOpen]);

    const handleTriggerEnter = useCallback(() => {
      // Close tooltip immediately when hovering over trigger
      setTooltipOpen(false);
      if (timeoutRef.current) {
        clearTimeout(timeoutRef.current);
        timeoutRef.current = undefined;
      }
      if (leaveTimeoutRef.current) {
        clearTimeout(leaveTimeoutRef.current);
        leaveTimeoutRef.current = undefined;
      }
    }, []);

    const clickHandler = useCallback(() => {
      if (!entityIsAlive) return;
      const { selected, id, type } = entity;

      if (!selected) {
        if (type === "prediction") {
          annotationStore.selectPrediction(id, { exitViewAll: true });
        } else {
          annotationStore.selectAnnotation(id, { exitViewAll: true });
        }
      }
    }, [entityIsAlive, entity, annotationStore]);

    // Get review status from task source (LSE-only feature)
    // The backend builds the annotators array by iterating through annotations in order,
    // so we match by finding the annotation's position in the backend's annotation list
    const getReviewStatus = useCallback(() => {
      // Only available in LSE for non-predictions
      const isLSE = (window as any).APP_SETTINGS?.version?.edition === "Enterprise";
      if (!isLSE || !entityIsAlive || isPrediction) {
        return null;
      }

      // Parse task source to get annotators array and backend annotations list
      const task = annotationStore?.store?.task;
      const sourceStr = task?.dataObj?.source;
      if (!sourceStr) return null;

      try {
        const taskSource = typeof sourceStr === "string" ? JSON.parse(sourceStr) : sourceStr;
        const annotators = taskSource?.annotators;
        const backendAnnotations = taskSource?.annotations;

        if (!Array.isArray(annotators) || !Array.isArray(backendAnnotations)) {
          return null;
        }

        // Find the current annotation's index in the backend's annotations array
        // The backend builds annotators array by iterating through this same list
        const annotationIndex = backendAnnotations.findIndex((ann: any) => {
          if (entity.pk && ann.id) {
            return String(ann.id) === String(entity.pk);
          }
          return false;
        });

        if (annotationIndex === -1 || annotationIndex >= annotators.length) {
          return null;
        }

        // Use the same index to get the corresponding annotator's review status
        return annotators[annotationIndex]?.review ?? null;
      } catch {
        return null;
      }
    }, [entityIsAlive, isPrediction, annotationStore, entity.pk]);

    // Return null if entity is not alive, but only after all hooks have been called
    if (!entityIsAlive) {
      return null;
    }

    // After this point, entityIsAlive is guaranteed to be true, so we can safely access entity properties
    const acceptedState = getReviewStatus();
    const reviewBadge = getReviewBadge(acceptedState);

    return (
      <div
        className={cn("annotation-button")
          .mod({
            selected: entity.selected,
            groundTruth: isGroundTruth,
            draft: isDraft && !isDraftSaved, // Ephemeral draft only
            draftSaved: isDraftSaved, // Saved draft
            submitted: isSubmitted,
            skipped: isSkipped,
            triggerOpened: isContextMenuOpen,
          })
          .toClassName()}
        data-annotation-id={entity.pk ?? entity.id}
        ref={buttonRef as any}
        onMouseEnter={handleTooltipEnter}
        onMouseLeave={handleTooltipLeave}
        onFocus={handleTooltipFocus}
        onBlur={handleTooltipLeave}
      >
        <div
          className={cn("annotation-button").elem("mainSection").toClassName()}
          onClick={clickHandler}
          ref={mainSectionRef as any}
        >
          <div className={cn("annotation-button").elem("picSection").toClassName()}>
            <Userpic
              className={cn("annotation-button").elem("userpic").mod({ prediction: isPrediction }).toClassName()}
              // @ts-expect-error - block attribute for Selenium test compatibility
              block="lsf-annotation-button"
              username={isPrediction ? entity.createdBy : null}
              user={hiddenUser ?? (isUserComplete(entity.user) ? entity.user : { email: entity.createdBy })}
              size={24}
              badge={
                reviewBadge
                  ? {
                      bottomRight: reviewBadge,
                    }
                  : undefined
              }
            >
              {isPrediction && <IconSparks style={{ width: 18, height: 18 }} />}
            </Userpic>
            {/* TODO: Remove block. Selenium is using this anchor that was mistakenly propagated into this element. */}
            {/* to do: return these icons when we have a better way to grab the history action type */}
            {/* {historyActionType === 'accepted' && <Elem name='status' mod={{ approved: true }}><IconCheckBold /></Elem>}
          {historyActionType && (
            <Elem name='status' mod={{ skipped: true }}>
              <IconCrossBold />
            </Elem>
          )}
          {entity.history.canUndo && (
            <Elem name='status' mod={{ updated: true }}>
              <IconCheckBold />
            </Elem>
          )} */}
          </div>
          <div className={cn("annotation-button").elem("main").toClassName()}>
            <div className={cn("annotation-button").elem("user").toClassName()}>
              <span className={cn("annotation-button").elem("name").toClassName()}>{displayNameTruncated}</span>
            </div>
            {!infoIsHidden && (
              <div className={cn("annotation-button").elem("info").toClassName()}>
                <TimeAgo className={cn("annotation-button").elem("date").toClassName()} date={entity.createdDate} />
                {isPrediction && isDefined(entity.score) && (
                  <span title={`Prediction score = ${entity.score}`}>
                    {" · "} {(entity.score * 100).toFixed(2)}%
                  </span>
                )}
              </div>
            )}
          </div>
          {!isPrediction && (
            <div className={cn("annotation-button").elem("icons").toClassName()}>
              {(entity.draftId > 0 || isDraft) && (
                <Tooltip title="Draft">
                  <div className={cn("annotation-button").elem("icon").mod({ draft: true }).toClassName()}>
                    <IconDraftCreated2 color="#617ADA" />
                  </div>
                </Tooltip>
              )}
              {entity.skipped && (
                <Tooltip title="Skipped">
                  <div className={cn("annotation-button").elem("icon").mod({ skipped: true }).toClassName()}>
                    <IconAnnotationSkipped2 color="#DD0000" />
                  </div>
                </Tooltip>
              )}
              {isGroundTruth && (
                <Tooltip title="Ground-truth">
                  <div className={cn("annotation-button").elem("icon").mod({ groundTruth: true }).toClassName()}>
                    <IconAnnotationGroundTruth />
                  </div>
                </Tooltip>
              )}
              {CommentIcon && (
                <Tooltip title={renderCommentTooltip(entity)}>
                  <div className={cn("annotation-button").elem("icon").mod({ comments: true }).toClassName()}>
                    <CommentIcon />
                  </div>
                </Tooltip>
              )}
            </div>
          )}
          <AnnotationButtonTooltip
            displayUsername={displayUsername}
            isDraft={isDraft}
            isDraftSaved={isDraftSaved}
            isPrediction={isPrediction}
            isSkipped={isSkipped}
            isSubmitted={isSubmitted}
            isGroundTruth={isGroundTruth}
            acceptedState={acceptedState}
            predictionScore={isPrediction ? entity.score : null}
            lastUpdated={entity.createdDate}
            annotationId={entity.pk ?? entity.id}
            containerRef={tooltipContainerRef}
            isTooltipOpen={isTooltipOpen}
            onMouseEnter={handleTooltipEnter}
            onMouseLeave={handleTooltipContainerLeave}
            position={tooltipPosition}
          />
        </div>
        {entityIsAlive && (
          <DropdownTrigger
            content={
              <AnnotationButtonContextMenu
                entity={entity}
                capabilities={capabilities}
                annotationStore={annotationStore}
                store={annotationStore.store}
                isGroundTruth={isGroundTruth}
                iconSize={iconSize}
                onAnnotationChange={onAnnotationChange}
              />
            }
            onToggle={(isOpen) => {
              setContextMenuOpen(isOpen);
              // Close tooltip when context menu opens
              if (isOpen) {
                setTooltipOpen(false);
                if (timeoutRef.current) {
                  clearTimeout(timeoutRef.current);
                  timeoutRef.current = undefined;
                }
              }
            }}
          >
            <div
              className={cn("annotation-button").elem("trigger").toClassName()}
              onMouseEnter={handleTriggerEnter}
              onClick={(e) => e.stopPropagation()}
            >
              <IconEllipsisVertical width={20} height={20} />
            </div>
          </DropdownTrigger>
        )}
      </div>
    );
  },
);