All files / src/live CampaignEvents.ts

98.67% Statements 1113/1128
78.19% Branches 104/133
96.66% Functions 29/30
98.67% Lines 1113/1128

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129125x 154x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 124x 124x 124x 125x 125x 125x 125x 138x 138x 138x 138x 138x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 1x 1x 1x 1x 1x 138x 124x 124x 125x 21x 21x 21x 21x 21x       21x 14x 14x 21x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 21x 21x 21x 21x 21x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 42x 42x 42x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 125x 85x 85x 64x 64x 85x 85x 85x 85x 85x 21x 21x 21x 21x 21x 21x 125x 125x 125x 125x 125x 42x 42x 42x 42x 42x 126x 126x 126x 126x 126x 84x 84x 84x 84x 84x 84x 84x 84x 84x 84x 84x 84x 84x 126x 126x 126x 126x 84x 84x 84x 84x 125x 125x 125x 125x 125x 30x 30x 125x 18x 18x 18x 18x 18x 18x 125x 125x 125x 125x 125x 125x 10x 10x 10x 10x 10x 10x 10x 4x 5x 5x 10x 10x 10x 1x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 1x 1x 1x 1x 1x 1x 125x 1x 1x 1x 1x 1x 1x 1x 1x 1x 125x 125x 125x 125x 125x 125x 125x 125x 1x 1x 1x 125x 125x 125x 125x 125x 1x 1x 1x 125x 125x 125x 1x 1x 1x 125x 125x 125x 125x 125x 125x 125x 100x 100x 100x 100x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 100x 100x 56x 56x 125x 1x 1x 1x 1x 1x 1x 42x 84x 126x 126x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 126x 126x 1x 1x 126x 1x 1x 126x 126x 126x 126x 126x 125x 126x 126x 126x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 84x 126x 126x 1x 1x 1x 1x 1x 1x 1x 126x 126x 1x 126x 126x 125x 126x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 125x 125x 125x 125x 125x 125x 125x 125x 84x 1x 1x 1x 1x 1x 1x 1x 1x 84x 84x 84x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 84x 84x 84x 84x 84x 84x 84x 42x 84x 84x 84x 84x 84x 84x 84x 84x 84x 84x 84x 84x 1x 1x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 85x 85x 85x 85x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x           85x 125x 43x 43x 85x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 85x 85x 84x 84x 85x 85x 85x 125x 125x 125x 125x 125x 1x 1x 1x 1x 125x 125x 6x 16x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x  
import { css, html, PropertyValueMap, TemplateResult } from 'lit';
import { property, state } from 'lit/decorators.js';
import {
  CampaignScheduleEvent,
  CustomEventType,
  ObjectReference
} from '../interfaces';
import { EndpointMonitorElement } from '../store/EndpointMonitorElement';
import { Icon } from '../Icons';
import { designTokens } from '../styles/designTokens';
 
interface CampaignEventsResponse {
  events: CampaignScheduleEvent[];
  campaign?: ObjectReference;
  can_edit?: boolean;
  can_delete?: boolean;
}
 
// one row in the recent-contacts popup
interface RecentFire {
  contact: ObjectReference;
  time: string;
}
 
// distinct hues assigned per relative-to field so each field's "line" reads
// clearly apart - the same palette (and assignment scheme) the contact
// timeline uses for campaigns, so colors mean "grouping" in both places
const FIELD_COLORS = [
  '#1a86d0', // blue
  '#e8843f', // orange
  '#ec407a', // pink
  '#2bb2a6', // teal
  '#0a5290', // deep blue
  '#a25320', // deep orange
  '#a02153', // deep pink
  '#176a61' // deep teal
];
 
// uppercase the first letter of a localized display string - offset-0
// events lead the detail schedule with "On <field>"
const capitalize = (text: string): string =>
  text ? text.charAt(0).toUpperCase() + text.slice(1) : text;
 
// a section is one subway line: all the events anchored to the same date
// field, split into those firing before the field's date and those firing
// on or after it
interface Section {
  key: string;
  name: string;
  system: boolean;
  before: CampaignScheduleEvent[];
  onAfter: CampaignScheduleEvent[];
}
 
export class CampaignEvents extends EndpointMonitorElement {
  @property({ type: String })
  campaign: string;
 
  // optional endpoint override, defaults to the campaign's events endpoint
  @property({ type: String })
  endpoint: string;
 
  // title shown in the page header; a `title` slot overrides it
  @property({ type: String, attribute: 'header-title' })
  headerTitle = '';
 
  // subtitle under the title; a `subtitle` slot overrides it
  @property({ type: String })
  subtitle = '';
 
  // GET endpoint for the page's content menu (rapidpro's content-menu view);
  // menu clicks fire temba-selection with the item for the host to dispatch
  @property({ type: String, attribute: 'content-menu-endpoint' })
  contentMenuEndpoint = '';
 
  @property({ type: Object, attribute: false })
  data: CampaignEventsResponse;
 
  @property({ type: String })
  lang_scheduling = 'Scheduling';
 
  @property({ type: String })
  lang_edit = 'Edit';
 
  @property({ type: String })
  lang_delete = 'Delete';
 
  @property({ type: String })
  lang_recent_contacts = 'Recent Contacts';
 
  @property({ type: String })
  lang_scheduled = 'scheduled';
 
  @property({ type: String })
  lang_scheduled_contacts = 'Contacts scheduled for this event';

  @property({ type: String })
  lang_send_message = 'Send Message';
 
  @property({ type: String })
  lang_start_flow = 'Start Flow';
 
  @property({ type: String })
  lang_empty = 'No events';
 
  @property({ type: String })
  lang_empty_help =
    'Events send a message or start a flow for each contact in the group, offset from a date field on the contact.';
 
  // the event whose detail modal is open
  @state()
  private detailEvent: CampaignScheduleEvent = null;
 
  // the detail modal's recent contacts - null while the fetch is in flight
  @state()
  private fires: RecentFire[] = null;
 
  static get styles() {
    return css`
      ${designTokens}
 
      /* The page-header bar at the top, then one card per anchor field
         in a padded region below. */
      :host {
        display: flex;
        flex-direction: column;
        font-family: var(--font);
        color: var(--text-1);
        font-size: 13.5px;
      }
 
      /* The header is the same flush surface bar the fill-window lists
         render — full width, the lists' 12px inset, no card chrome —
         so a list page and this read page share one header treatment. */
      .header-panel {
        background: var(--surface);
        padding: 0 12px;
        border-bottom: 1px solid var(--border);
      }
 
      /* Card panel — the same surface treatment as the content lists so
         the read page reads as part of the same family. The anchor dots
         and tint mixes paint with the widget background, so inside a
         card that must be the card surface. */
      .panel {
        background: var(--surface);
        border-radius: var(--r);
        overflow: hidden;
        box-shadow: var(--shadow-1);
        --color-widget-bg: var(--surface);
      }
 
      /* badges (group, archived) get their own row between the header and
         the cards. Slotted links shouldn't pick up anchor styling - the
         labels inside them carry the look. */
      .badges {
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 12px 12px 0;
      }
 
      .badges ::slotted(*) {
        display: flex;
        align-items: center;
        gap: 8px;
        text-decoration: none;
      }
 
      /* the per-field cards stack in a padded region below the header */
      .content {
        display: flex;
        flex-direction: column;
        gap: 12px;
        padding: 12px;
      }
 
      /* the section cards sit inside the events wrapper, which stays out
         of layout so the cards participate in the content's column gap */
      .events {
        display: contents;
      }
 
      /* empty state follows the list design system: centered icon, a short
         title and muted explanatory copy */
      .empty {
        display: flex;
        flex-direction: column;
        align-items: center;
        text-align: center;
        padding: 4em 1em;
        color: var(--text-color);
      }
 
      .empty temba-icon {
        margin-bottom: 0.75em;
        --icon-color: var(--text-3, #7b8593);
      }
 
      .empty-title {
        font-weight: 600;
        margin-bottom: 0.4em;
      }
 
      .empty-help {
        font-size: 0.875em;
        line-height: 1.5;
        max-width: 22em;
        margin-bottom: 1em;
        color: var(--text-3, #7b8593);
      }
 
      /* one subway line per anchor field, each in its own card */
      .section {
        display: flex;
        flex-direction: column;
        padding: 0.9em 12px 1em;
      }
 
      .row {
        display: flex;
        align-items: stretch;
      }
 
      /* the schedule offset ("3 days before"), vertically centered on its
         dot. mute via color (not opacity) so the hovered delivery-hour
         tooltip renders at full alpha */
      .time {
        width: 9em;
        flex-shrink: 0;
        display: flex;
        align-items: center;
        justify-content: flex-end;
        padding-right: 0.65em;
        font-size: 0.8em;
        color: var(--text-3, #7b8593);
        white-space: nowrap;
      }
 
      .time temba-tip {
        cursor: default;
      }
 
      /* the subway rail */
      .track {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 1.75em;
        flex-shrink: 0;
      }
 
      .line {
        width: 2px;
        flex: 1;
        background: var(--color-borders, rgba(0, 0, 0, 0.12));
      }
 
      .line.hidden {
        background: transparent;
      }
 
      .dot {
        width: 18px;
        height: 18px;
        border-radius: 50%;
        box-sizing: border-box;
        flex-shrink: 0;
        z-index: 1;
        border: 2px solid var(--dot-color);
        display: flex;
        align-items: center;
        justify-content: center;
        /* icons inside the dot take the dot's hue */
        --icon-color: var(--dot-color);
        /* plain widget bg for browsers without color-mix(); the tinted
           fill below needs the @supports gate since var() keeps an
           invalid declaration alive until computed-value time */
        background: var(--color-widget-bg, #fff);
      }
 
      @supports (color: color-mix(in srgb, red, red)) {
        .dot {
          background: color-mix(
            in srgb,
            var(--dot-color) 12%,
            var(--color-widget-bg, #fff)
          );
        }
      }
 
      /* the anchor marks the field's date itself - painted with the widget
         background so it masks the rail behind it and reads as the station
         the line is built around */
      .dot.anchor {
        background: var(--color-widget-bg, #fff);
      }
 
      /* system fields aren't real contact fields - dashed and muted */
      .system-pill {
        display: inline-flex;
        align-items: center;
        gap: 5px;
        font-size: 0.85em;
        line-height: 1.5;
        padding: 0.05em 0.65em;
        border-radius: 999px;
        white-space: nowrap;
        border: 1px dashed var(--border-strong, #9ca3af);
        color: var(--text-3, #7b8593);
        --icon-color: var(--text-3, #7b8593);
      }
 
      .anchor-label {
        display: flex;
        align-items: center;
        margin: 0.5em 0;
        padding: 0 0.6em;
      }
 
      /* flat event entry - no card chrome */
      .event {
        flex-grow: 1;
        min-width: 0;
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 0.4em 0.75em;
        margin: 0.1em 0;
        border-radius: var(--curvature);
        /* transparent by default so the hover border doesn't shift layout */
        border: 1px solid transparent;
      }
 
      .count {
        flex-shrink: 0;
        margin-left: auto;
        display: flex;
        align-items: center;
        gap: 0.35em;
        font-size: 0.8em;
        color: var(--text-3, #7b8593);
        white-space: nowrap;
        --icon-color: var(--text-3, #7b8593);
      }
 
      .event.clickable {
        cursor: pointer;
      }
 
      .event.clickable:hover {
        border-color: var(--border, #e4e7ec);
      }
 
      .title {
        flex: 1;
        min-width: 0;
        display: flex;
        align-items: center;
        gap: 0.45em;
        color: var(--text-color);
        line-height: 1.4;
      }
 
      .title-text {
        flex: 1;
        min-width: 0;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
      }
 
      /* the detail modal stands in for an event read page - page-like, no
         colored header bar. The header and gutter pin to the top and bottom
         with full-bleed rules; the body between them scrolls when needed. */
      .detail {
        display: flex;
        flex-direction: column;
      }
 
      .detail-body {
        display: flex;
        flex-direction: column;
        gap: 1.25em;
        overflow-y: auto;
        max-height: calc(100vh - 300px);
        padding: 1.25em 1.75em;
      }
 
      /* header and gutter share uniform padding on all sides, with the
         title vertically centered against the action buttons */
      .detail-header {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 1em;
        border-bottom: 1px solid var(--border, #e4e7ec);
      }
 
      .detail-title {
        flex: 1;
        min-width: 0;
        display: flex;
        flex-direction: column;
        gap: 4px;
      }
 
      .detail-campaign {
        font-size: 15.5px;
        font-weight: var(--w-semibold, 600);
        color: var(--text-1);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
 
      /* the schedule line leads the body - the offset, anchor field and
         delivery hour on the left, the labeled scheduled count on the right */
      .detail-schedule-row {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
      }
 
      .detail-schedule {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 0.9em;
        color: var(--text-3, #7b8593);
      }
 
      .detail-count {
        flex-shrink: 0;
        display: flex;
        align-items: center;
        gap: 0.35em;
        font-size: 0.85em;
        color: var(--text-3, #7b8593);
        --icon-color: var(--text-3, #7b8593);
      }
 
      .detail-actions {
        flex-shrink: 0;
        display: flex;
        align-items: center;
        gap: 8px;
      }
 
      /* match the page header's content-menu buttons so the modal's actions
         read like page actions */
      .menu-button {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        box-sizing: border-box;
        height: 26px;
        padding: 0 10px;
        border: 1px solid var(--border-strong, #9ca3af);
        border-radius: var(--r-sm);
        font-size: 12.5px;
        font-weight: var(--w-regular, 400);
        cursor: pointer;
        user-select: none;
        background: var(--surface);
        color: var(--text-1);
        white-space: nowrap;
      }
 
      .menu-button:hover {
        background: var(--sunken);
      }
 
      .menu-button.destructive {
        color: #dc2626;
      }
 
      .menu-button.destructive:hover {
        border-color: #dc2626;
        /* pre-mixed fallback for browsers without color-mix() */
        background: #fdf2f2;
      }
 
      /* the event's action, contained with a leading type label. Children
         hug their content so a flow pill doesn't stretch the box's width. */
      .detail-action {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        gap: 0.6em;
        padding: 0.9em 1em 1em;
        /* pre-mixed fallback for browsers without color-mix() */
        background: #f9fafa;
        border: 1px solid var(--border, #e4e7ec);
        border-radius: var(--r);
        line-height: 1.5;
      }
 
      @supports (color: color-mix(in srgb, red, red)) {
        .menu-button.destructive:hover {
          background: color-mix(in srgb, #dc2626 6%, var(--surface, #fff));
        }
        .detail-action {
          background: color-mix(
            in srgb,
            var(--sunken, #f1f3f5) 45%,
            var(--surface, #fff)
          );
        }
      }
 
      /* the close (✕) in the header's upper right - the square
         hover-wash button treatment (the pager buttons'), sitting
         just outboard of the action buttons */
      .detail-close {
        flex-shrink: 0;
        margin-left: -4px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 28px;
        height: 28px;
        border-radius: var(--r-sm);
        color: var(--text-3, #7b8593);
        cursor: pointer;
        user-select: none;
      }
      .detail-close:hover {
        background: var(--sunken);
        color: var(--text-1);
      }
      .detail-close temba-icon {
        --icon-color: currentColor;
      }
 
      .detail-section-title {
        margin-top: 0.25em;
        font-size: 0.75em;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: var(--text-3, #7b8593);
      }
 
      /* recent contacts - a simple name/time table */
      .fires {
        min-height: 2em;
        margin: -0.25em 0 0.5em;
      }
 
      .fire-row {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 1em;
        padding: 0.4em 0.5em;
        border-radius: var(--r-sm);
        cursor: pointer;
      }
 
      .fire-row:hover {
        background: var(--color-selection, rgba(0, 0, 0, 0.04));
      }
 
      .fire-name {
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
 
      .fire-row temba-date {
        flex-shrink: 0;
        font-size: 0.85em;
        color: var(--text-3, #7b8593);
      }
    `;
  }
 
  // the endpoint returns a custom {events: [...]} payload rather than the
  // paginated {results: [...]} shape that the store's fetch machinery
  // expects, so we fetch and assign data ourselves. The base class's
  // inherited `url` property is unsupported here - setting it would race
  // its monitored fetch against ours.
  private getEndpoint(): string | null {
    if (this.endpoint) {
      return this.endpoint;
    }
    return this.campaign
      ? `/campaign/events/${encodeURIComponent(this.campaign)}/`
      : null;
  }
 
  private async loadEvents(): Promise<void> {
    // capture the endpoint at request time so a slower in-flight response for
    // a previous campaign can't overwrite data for the one we're now showing
    const requestedEndpoint = this.getEndpoint();
    if (!requestedEndpoint) {
      this.data = null;
      return;
    }
 
    try {
      const response = await this.store.getUrl(requestedEndpoint, {
        force: true
      });
      if (this.getEndpoint() !== requestedEndpoint) {
        return;
      }
      this.data = response.json;
    } catch {
      // on failure leave the prior data in place so a transient blip doesn't
      // wipe the schedule; data is only cleared when the campaign changes
    }
  }
 
  public refresh(): void {
    this.loadEvents();
  }
 
  protected updated(
    changes: PropertyValueMap<any> | Map<PropertyKey, unknown>
  ): void {
    super.updated(changes);
 
    if (changes.has('campaign') || changes.has('endpoint')) {
      // blank immediately on a campaign switch so the previous campaign's
      // events aren't briefly visible while the new fetch is in flight
      this.data = null;
      this.loadEvents();
    }
 
    if (changes.has('data') && this.data) {
      const events = Array.isArray(this.data.events) ? this.data.events : [];
      this.fireCustomEvent(CustomEventType.DetailsChanged, {
        count: events.length
      });
    }
  }
 
  // events arrive sorted by field name then offset; group them into one
  // section per field, split around the field's date
  private getSections(events: CampaignScheduleEvent[]): Section[] {
    const sections: Section[] = [];
    const byKey: { [key: string]: Section } = {};
 
    for (const event of events) {
      const key = event.relative_to?.key || '';
      let section = byKey[key];
      if (!section) {
        section = {
          key,
          name: event.relative_to?.name || '',
          system: !!event.relative_to?.system,
          before: [],
          onAfter: []
        };
        byKey[key] = section;
        sections.push(section);
      }
      if (event.offset < 0) {
        section.before.push(event);
      } else {
        section.onAfter.push(event);
      }
    }
    return sections;
  }
 
  // whether this event can be edited - scheduling-state events are locked
  // until mailroom finishes rescheduling them
  private canEdit(event: CampaignScheduleEvent): boolean {
    return !!this.data?.can_edit && event.status === 'ready';
  }
 
  private canDelete(): boolean {
    return !!this.data?.can_delete;
  }
 
  // in place of an event read page, clicking an event opens its detail
  // modal, which also loads who the event recently fired for
  public handleEventClicked(event: CampaignScheduleEvent) {
    this.detailEvent = event;
    this.loadFires(event);
  }
 
  private async loadFires(event: CampaignScheduleEvent): Promise<void> {
    this.fires = null;
    try {
      const response = await this.store.getUrl(event.fires_url, {
        force: true
      });
      if (this.detailEvent !== event) {
        return;
      }
      this.fires = Array.isArray(response.json?.fires)
        ? response.json.fires
        : [];
    } catch {
      if (this.detailEvent === event) {
        this.fires = [];
      }
    }
  }
 
  // edit / delete close the detail modal before the host opens the edit or
  // delete-confirm modal in its place - modals never stack
  public handleEditClicked() {
    const event = this.detailEvent;
    this.detailEvent = null;
    this.fireCustomEvent(CustomEventType.Selection, {
      action: 'edit_event',
      event
    });
  }
 
  public handleDeleteClicked() {
    const event = this.detailEvent;
    this.detailEvent = null;
    this.fireCustomEvent(CustomEventType.Selection, {
      action: 'delete_event',
      event
    });
  }
 
  // clicking a flow pill navigates to the flow instead of opening the row's
  // detail modal (and closes the modal when clicked from inside it)
  public handlePillClicked(e: Event, event: CampaignScheduleEvent) {
    e.stopPropagation();
    this.detailEvent = null;
    this.fireCustomEvent(CustomEventType.Selection, event.flow);
  }
 
  // navigating to a contact from the detail modal closes it first
  public handleFireClicked(fire: RecentFire) {
    this.detailEvent = null;
    this.fireCustomEvent(CustomEventType.Selection, fire.contact);
  }
 
  // the dialog closes itself on its Close button / ESC (firing only
  // temba-button-clicked) and on mask clicks (temba-dialog-hidden) - our
  // open state must reset on every path or the next open is a no-op change
  // and the modal never reopens
  private handleDetailClosed = () => {
    this.detailEvent = null;
  };
 
  // activate a non-button row on Enter/Space (matches native button behavior)
  private handleActivationKey(e: KeyboardEvent, action: () => void) {
    if (e.key === 'Enter' || e.key === ' ') {
      e.preventDefault();
      action();
    }
  }
 
  // system fields (Created On, Last Seen On, ...) aren't editable contact
  // fields, so they get a dashed, muted pill instead of the field pill
  private renderFieldPill(field: {
    name?: string;
    system?: boolean;
  }): TemplateResult {
    if (field?.system) {
      return html`<span class="system-pill">
        <temba-icon name="fields" size="0.9"></temba-icon>
        ${field?.name}
      </span>`;
    }
    return html`<temba-label type="field" icon="fields"
      >${field?.name}</temba-label
    >`;
  }
 
  private renderTime(event: CampaignScheduleEvent): TemplateResult {
    // when the event fires at a specific hour, surface it on hover
    if (event.delivery_hour_display) {
      return html`
        <temba-tip text=${event.delivery_hour_display} position="top">
          ${event.offset_display}
        </temba-tip>
      `;
    }
    return html`${event.offset_display}`;
  }
 
  // the scheduled count (or scheduling state), shown inside the event's
  // selectable outline and on the detail modal
  private renderCount(event: CampaignScheduleEvent): TemplateResult {
    if (event.status === 'scheduling') {
      return html`<div class="count">${this.lang_scheduling}</div>`;
    }
    return html`
      <div class="count" title=${this.lang_scheduled_contacts}>
        ${(event.count || 0).toLocaleString()}
        <temba-icon name=${Icon.contact} size="0.9"></temba-icon>
      </div>
    `;
  }
 
  private renderEvent(event: CampaignScheduleEvent): TemplateResult {
    // message events display the message text; flow events show a clickable
    // flow pill linking to the flow (the "start" action is implied)
    const body =
      event.type === 'message'
        ? html`<div class="title-text">
            <temba-expression-highlight
              >${event.message}</temba-expression-highlight
            >
          </div>`
        : event.flow
          ? html`<temba-label
              type="flow"
              clickable
              @click=${(e: Event) => this.handlePillClicked(e, event)}
              >${event.flow.name}</temba-label
            >`
          : html``;
 
    // in place of an event read page, opening a row shows its detail modal
    return html`
      <div
        class="event clickable"
        role="button"
        tabindex="0"
        @click=${() => this.handleEventClicked(event)}
        @keydown=${(e: KeyboardEvent) =>
          this.handleActivationKey(e, () => this.handleEventClicked(event))}
      >
        <div class="title">${body}</div>
        ${this.renderCount(event)}
      </div>
    `;
  }
 
  private renderEventRow(
    event: CampaignScheduleEvent,
    color: string,
    first: boolean,
    last: boolean
  ): TemplateResult {
    return html`
      <div class="row">
        <div class="time">${this.renderTime(event)}</div>
        <div class="track">
          <div class="line ${first ? 'hidden' : ''}"></div>
          <div class="dot" style="--dot-color:${color}">
            <temba-icon
              name=${event.type === 'message' ? Icon.message : Icon.flow}
              size="0.65"
            ></temba-icon>
          </div>
          <div class="line ${last ? 'hidden' : ''}"></div>
        </div>
        ${this.renderEvent(event)}
      </div>
    `;
  }
 
  // the anchor row marks the field's date itself - events before it sit
  // above, events on or after it sit below
  private renderAnchorRow(
    section: Section,
    color: string,
    first: boolean,
    last: boolean
  ): TemplateResult {
    return html`
      <div class="row anchor-row">
        <div class="time"></div>
        <div class="track">
          <div class="line ${first ? 'hidden' : ''}"></div>
          <div class="dot anchor" style="--dot-color:${color}"></div>
          <div class="line ${last ? 'hidden' : ''}"></div>
        </div>
        <div class="anchor-label">${this.renderFieldPill(section)}</div>
      </div>
    `;
  }
 
  // each field's line takes its hue from the section's position, so colors
  // are a pure function of the current payload
  private renderSection(section: Section, index: number): TemplateResult {
    const color = FIELD_COLORS[index % FIELD_COLORS.length];
    const total = section.before.length + 1 + section.onAfter.length;
 
    let idx = 0;
    const rows: TemplateResult[] = [];
    for (const event of section.before) {
      rows.push(this.renderEventRow(event, color, idx === 0, false));
      idx++;
    }
    rows.push(
      this.renderAnchorRow(section, color, idx === 0, idx === total - 1)
    );
    idx++;
    for (const event of section.onAfter) {
      rows.push(this.renderEventRow(event, color, false, idx === total - 1));
      idx++;
    }
 
    return html`<div class="panel section">${rows}</div>`;
  }
 
  // the page header — title + content menu — is temba-page-header, the
  // same header the content lists embed, so a list page and this read
  // page share one header treatment. Slot presence is probed at render
  // time (not reactive) - fine while hosts slot content declaratively.
  private renderHeader(): TemplateResult | null {
    const hasSubtitle =
      this.subtitle || this.querySelector('[slot="subtitle"]');
    if (!this.headerTitle && !this.contentMenuEndpoint && !hasSubtitle) {
      return null;
    }
    return html`
      <div class="header-panel">
        <temba-page-header content-menu-endpoint=${this.contentMenuEndpoint}>
          <slot name="title" slot="title">${this.headerTitle}</slot>
          ${hasSubtitle
            ? html`<slot name="subtitle" slot="subtitle"
                >${this.subtitle}</slot
              >`
            : null}
        </temba-page-header>
      </div>
    `;
  }
 
  private renderSchedule(): TemplateResult {
    if (!this.data) {
      return html``;
    }
 
    // tolerate a malformed/empty response rather than throwing
    const events = Array.isArray(this.data.events) ? this.data.events : [];
 
    if (events.length === 0) {
      return html`<div class="content">
        <div class="panel">
          <div class="empty">
            <slot name="empty">
              <temba-icon name=${Icon.campaign} size="2"></temba-icon>
              <div class="empty-title">${this.lang_empty}</div>
              <div class="empty-help">${this.lang_empty_help}</div>
            </slot>
          </div>
        </div>
      </div>`;
    }
 
    // each field's line is its own card
    return html`
      <div class="content">
        <div class="events">
          ${this.getSections(events).map((section, index) =>
            this.renderSection(section, index)
          )}
        </div>
      </div>
    `;
  }
 
  // badges (the campaign's group, archived state) get their own row
  // between the header and the cards. Same render-time slot probe as the
  // header - late-slotted badges won't toggle the row.
  private renderBadges(): TemplateResult | null {
    if (!this.querySelector('[slot="badges"]')) {
      return null;
    }
    return html`<div class="badges"><slot name="badges"></slot></div>`;
  }
 
  // the recent contacts section - omitted entirely when the event hasn't
  // fired for anyone
  private renderFires(): TemplateResult | null {
    if (!this.fires || this.fires.length === 0) {
      return null;
    }
    return html`
      <div class="detail-section-title">${this.lang_recent_contacts}</div>
      <div class="fires">
        ${this.fires.map(
          (fire) => html`
            <div
              class="fire-row"
              role="button"
              tabindex="0"
              @click=${() => this.handleFireClicked(fire)}
              @keydown=${(e: KeyboardEvent) =>
                this.handleActivationKey(e, () => this.handleFireClicked(fire))}
            >
              <div class="fire-name">${fire.contact.name}</div>
              <temba-date value=${fire.time} display="timedate"></temba-date>
            </div>
          `
        )}
      </div>
    `;
  }
 
  // modal detail view for an event, standing in for an event read page -
  // page-like (no colored header bar) with the schedule as its title, edit
  // and delete actions, the content, and the recent contacts
  private renderDetail(): TemplateResult {
    const event = this.detailEvent;
 
    return html`
      <temba-dialog
        size="xlarge"
        variant="flat"
        primaryButtonName=""
        cancelButtonName=""
        hideOnClick
        ?open=${!!event}
        @temba-dialog-hidden=${this.handleDetailClosed}
        @keyup=${(e: KeyboardEvent) => {
          if (e.key === 'Escape') {
            this.handleDetailClosed();
          }
        }}
      >
        ${event
          ? html`
              <div class="detail">
                <div class="detail-header">
                  <div class="detail-title">
                    <div class="detail-campaign">
                      ${this.data?.campaign?.name || this.headerTitle}
                    </div>
                  </div>
                  ${this.canEdit(event) || this.canDelete()
                    ? html`<div class="detail-actions">
                        ${this.canEdit(event)
                          ? html`<button
                              class="menu-button"
                              @click=${this.handleEditClicked}
                            >
                              ${this.lang_edit}
                            </button>`
                          : null}
                        ${this.canDelete()
                          ? html`<button
                              class="menu-button destructive"
                              @click=${this.handleDeleteClicked}
                            >
                              ${this.lang_delete}
                            </button>`
                          : null}
                      </div>`
                    : null}
                  <div
                    class="detail-close"
                    role="button"
                    tabindex="0"
                    aria-label="Close"
                    @click=${this.handleDetailClosed}
                    @keydown=${(e: KeyboardEvent) =>
                      this.handleActivationKey(e, this.handleDetailClosed)}
                  >
                    <temba-icon name=${Icon.close} size="1.4"></temba-icon>
                  </div>
                </div>
 
                <div class="detail-body">
                  <div class="detail-schedule-row">
                    <div class="detail-schedule">
                      <span
                        >${event.offset === 0
                          ? capitalize(event.offset_display)
                          : event.offset_display}</span
                      >
                      ${this.renderFieldPill(event.relative_to)}
                      ${event.delivery_hour_display
                        ? html`<span>${event.delivery_hour_display}</span>`
                        : null}
                    </div>
                    <div
                      class="detail-count"
                      title=${event.status === 'scheduling'
                        ? ''
                        : this.lang_scheduled_contacts}
                    >
                      ${event.status === 'scheduling'
                        ? html`${this.lang_scheduling}`
                        : html`<temba-icon
                              name=${Icon.contact}
                              size="0.9"
                            ></temba-icon>
                            ${(event.count || 0).toLocaleString()}
                            ${this.lang_scheduled}`}
                    </div>
                  </div>
 
                  <div class="detail-action">
                    <div class="detail-section-title">
                      ${event.type === 'message'
                        ? this.lang_send_message
                        : this.lang_start_flow}
                    </div>
                    ${event.type === 'message'
                      ? html`<temba-expression-highlight
                          >${event.message}</temba-expression-highlight
                        >`
                      : event.flow
                        ? html`<temba-label
                            type="flow"
                            clickable
                            @click=${(e: Event) =>
                              this.handlePillClicked(e, event)}
                            >${event.flow.name}</temba-label
                          >`
                        : null}
                  </div>
 
                  ${this.renderFires()}
                </div>
              </div>
            `
          : null}
      </temba-dialog>
    `;
  }
 
  public render(): TemplateResult {
    return html`${this.renderHeader()} ${this.renderBadges()}
    ${this.renderSchedule()} ${this.renderDetail()}`;
  }
}