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 | 125x 141x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 183x 183x 183x 183x 15x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 15x 15x 1x 1x 15x 76x 76x 15x 15x 15x 15x 15x 15x 15x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 125x 86x 86x 86x 86x 86x 16x 16x 16x 16x 16x 16x 16x 86x 86x 32x 32x 16x 16x 16x 16x 16x 16x 16x 32x 16x 16x 13x 16x 16x 1x 16x 16x 1x 16x 16x 16x 16x 16x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 18x 18x 18x 18x 32x 32x 32x 32x 75x 17x 17x 17x 75x 111x 111x 111x 111x 111x 125x 183x 183x 183x 183x 183x 72x 183x 76x 183x 183x 183x 183x 183x 35x 183x 148x 148x 148x 148x 183x 76x 183x 125x 125x 2x 2x 2x 125x 125x 125x 125x 125x 125x 3x 3x 125x 125x 125x 125x 125x 3x 3x 125x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x 2x 2x 125x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 183x 1x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 113x 113x 1x 1x 1x 1x 70x 70x 1x 1x 1x 1x 1x 1x 1x 1x 1x 70x 183x 183x 1x 1x 1x 1x 183x 146x 183x 183x 183x 1x 1x 183x 183x 183x 183x 183x 183x 183x 183x 183x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 183x 183x 37x 37x 37x 37x 37x 37x 35x 35x 1x 1x 1x 35x 35x 1x 1x 1x 1x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 2x 2x 2x 37x 125x 125x 163x 125x 125x 125x 125x 125x 125x 125x 125x 125x 163x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 161x 161x 161x 161x 161x 199x 161x 232x 143x 143x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 161x 161x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 141x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 124x 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 {
CustomEventType,
ObjectReference,
ScheduledEvent,
ScheduledEventType
} from '../interfaces';
import { EndpointMonitorElement } from '../store/EndpointMonitorElement';
import { Icon } from '../Icons';
interface EventsResponse {
now: string;
campaigns: ObjectReference[];
future_count: number;
future: ScheduledEvent[];
past: ScheduledEvent[];
next_before: string | null;
next_after: string | null;
}
// four distinct primary hues assigned per campaign - chosen for maximum spread
// so the first handful of campaigns read clearly apart. Purple is reserved for
// broadcasts and green is reserved for triggers (matches the flow pill hue).
// The trailing four entries are darker shades of the primaries for the rare
// case where a contact belongs to more than four campaigns at once.
const CAMPAIGN_COLORS = [
'#1a86d0', // blue
'#e8843f', // orange
'#ec407a', // pink
'#2bb2a6', // teal
'#0a5290', // deep blue
'#a25320', // deep orange
'#a02153', // deep pink
'#176a61' // deep teal
];
// shared "broadcast purple" used elsewhere for broadcasts
const BROADCAST_COLOR = '#8e5ea7';
// triggers use the same green as the flow pill
const TRIGGER_COLOR = '#16a34a';
export class ContactTimeline extends EndpointMonitorElement {
@property({ type: String })
contact: string;
@property({ type: Object, attribute: false })
data: EventsResponse;
@property({ type: String })
lang_now = 'Now';
@property({ type: String })
lang_show_older = 'Show older events';
@property({ type: String })
lang_show_more = 'Show more upcoming events';
@property({ type: String })
lang_more = 'Show more';
@property({ type: String })
lang_campaigns_label = 'Campaigns';
@property({ type: String })
lang_empty = 'No upcoming events';
@property({ type: String })
lang_empty_help =
'Events appear here when a contact joins a campaign. Scheduled flows and messages will also show up here.';
@property({ type: String })
lang_campaigns_link = 'View campaigns';
@property({ type: String })
lang_projected_info =
'This is a projected timeline based on current schedules. Past entries may not reflect what actually happened, and upcoming entries are limited to the next year.';
// older past events accumulated through pagination
@state()
private olderEvents: ScheduledEvent[] = [];
// further-upcoming events accumulated through pagination
@state()
private newerEvents: ScheduledEvent[] = [];
// cursor for paging further back, null when there's nothing older
@state()
private nextBefore: string | null = null;
// cursor for paging further forward, null when there's nothing further out
@state()
private nextAfter: string | null = null;
@state()
private loadingMore = false;
@state()
private loadingMoreFuture = false;
// stable campaign uuid -> color assignments
private campaignColors: { [uuid: string]: string } = {};
static get styles() {
return css`
:host {
display: block;
}
/* empty state follows the list design system: centered icon, a short
title, muted explanatory copy, and a single call-to-action link */
.empty {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
/* a host card sets --empty-padding to compact this treatment —
the default suits a full-height tab pane */
padding: var(--empty-padding, 7em 1em 4em);
color: var(--text-color);
}
.empty temba-icon {
margin-bottom: 0.75em;
--icon-color: var(--text-3, #7b8593);
}
/* the icon / blurb / link around the title — a host card hides
these so the empty card is just the stylized message */
.empty-extras {
display: var(--empty-extras-display, contents);
}
.empty-title {
font-weight: 600;
margin-bottom: 0.4em;
font-size: var(--empty-title-size, inherit);
color: var(--empty-title-color, inherit);
}
.empty-help {
font-size: 0.875em;
line-height: 1.5;
max-width: 22em;
margin-bottom: 1em;
color: var(--text-3, #7b8593);
}
.empty-link {
font-size: 0.875em;
font-weight: 500;
color: var(--color-link-primary);
text-decoration: none;
}
.empty-link:hover {
text-decoration: underline;
}
/* row of campaign pills the contact is currently a member of */
.campaigns {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 0.4em;
padding: 0 0.5em 0.5em;
}
.campaigns-label {
font-size: 0.75em;
font-weight: 500;
text-transform: uppercase;
letter-spacing: 0.05em;
color: var(--text-color);
opacity: 0.55;
margin-right: 0.25em;
}
/* each pill is colored with its campaign's hue - background, border
and text all derived from --pill-hue. clickable links to the
campaign's read page */
.campaign-pill {
display: inline-flex;
align-items: center;
gap: 6px;
font-size: 0.78em;
line-height: 1.5;
padding: 0.05em 0.65em;
border-radius: 999px;
white-space: nowrap;
/* plain bg / neutral border for browsers without color-mix();
the hue-tinted versions live in the @supports block below */
background: var(--color-widget-bg, #fff);
border: 1px solid var(--pill-hue);
color: var(--pill-hue);
cursor: pointer;
transition: background 100ms ease-in-out;
}
@supports (color: color-mix(in srgb, red, red)) {
.campaign-pill {
background: color-mix(
in srgb,
var(--pill-hue) 12%,
var(--color-widget-bg, #fff)
);
border: 1px solid
color-mix(
in srgb,
var(--pill-hue) 25%,
var(--color-widget-bg, #fff)
);
}
.campaign-pill:hover {
background: color-mix(
in srgb,
var(--pill-hue) 22%,
var(--color-widget-bg, #fff)
);
}
}
.campaign-pill:focus-visible {
outline: 2px solid var(--pill-hue);
outline-offset: 1px;
}
/* status-badge dot leading each campaign pill, in the same hue */
.campaign-dot {
display: inline-block;
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--pill-hue);
}
.timeline {
display: flex;
flex-direction: column;
padding: 0.5em 0;
}
.row {
display: flex;
align-items: stretch;
}
/* relative time, vertically centered on its dot. mute via color
(not opacity) so the hovered date tooltip renders at full alpha
and doesn't share a stacking context with the column */
.time {
width: 7em;
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;
/* same border on past and future - only the fill differs */
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);
}
/* past events are filled with a lighter tint of the same hue and keep
the solid border - they're settled, whether real or projected.
Plain widget bg first for browsers without color-mix(). */
.dot.past {
background: var(--color-widget-bg, #fff);
}
/* future events haven't occurred yet - signal that with a dotted
border and the faintest tint of the hue inside */
.dot.future {
border-style: dotted;
background: var(--color-widget-bg, #fff);
}
@supports (color: color-mix(in srgb, red, red)) {
.dot.past {
background: color-mix(
in srgb,
var(--dot-color) 25%,
var(--color-widget-bg, #fff)
);
}
.dot.future {
background: color-mix(
in srgb,
var(--dot-color) 8%,
var(--color-widget-bg, #fff)
);
}
}
/* the now dot and label both paint with the SPA content background
color - they look transparent but actually mask the rail and divider
behind them, so the "Now" marker reads as a clean break */
.dot.now {
border: 2px solid var(--border-strong, #9ca3af);
background: var(--color-widget-bg, #fff);
}
/* round button on the rail that pages further back / forward when
clicked - up arrow above the past, down arrow below the future */
.pager-dot {
width: 18px;
height: 18px;
border-radius: 50%;
box-sizing: border-box;
border: 2px solid var(--border-strong, #9ca3af);
background: var(--color-widget-bg, #fff);
display: flex;
align-items: center;
justify-content: center;
padding: 0;
cursor: pointer;
flex-shrink: 0;
z-index: 1;
--icon-color: var(--border-strong, #9ca3af);
transition:
border-color 0.15s ease,
color 0.15s ease;
}
.pager-dot:hover {
border-color: var(--color-link-primary);
--icon-color: var(--color-link-primary);
}
.pager-dot.loading {
cursor: default;
opacity: 0.5;
}
/* "More" label next to the pager dot - shares the event-row spacing
and font size so the pager row sits on the same vertical rhythm */
.pager-label {
display: flex;
align-items: center;
padding: 0.4em 0.75em;
margin: 0.1em 0;
color: var(--border-strong, #9ca3af);
cursor: pointer;
}
.pager-label:hover {
color: var(--color-link-primary);
}
/* hovering either the dot or the label highlights both so they read
as one interactive element - but the empty space of the row stays
inert (no hover, no click) */
.row.pager-row:has(.pager-dot:hover) .pager-label,
.row.pager-row:has(.pager-label:hover) .pager-label {
color: var(--color-link-primary);
}
.row.pager-row:has(.pager-label:hover) .pager-dot,
.row.pager-row:has(.pager-dot:hover) .pager-dot {
border-color: var(--color-link-primary);
--icon-color: var(--color-link-primary);
}
.row.pager-row.loading .pager-dot,
.row.pager-row.loading .pager-label,
.row.pager-row.loading:has(.pager-dot:hover) .pager-dot,
.row.pager-row.loading:has(.pager-dot:hover) .pager-label,
.row.pager-row.loading:has(.pager-label:hover) .pager-dot,
.row.pager-row.loading:has(.pager-label:hover) .pager-label {
cursor: default;
opacity: 0.5;
color: var(--border-strong, #9ca3af);
border-color: var(--border-strong, #9ca3af);
--icon-color: var(--border-strong, #9ca3af);
}
/* the "now" divider - a rule running the full width */
.row.now-row {
position: relative;
}
.row.now-row::after {
content: '';
position: absolute;
left: 0;
right: 0;
top: 50%;
border-top: 1px solid var(--color-borders, rgba(0, 0, 0, 0.2));
z-index: 0;
}
/* notice that campaign timelines are projections - sits below the
timeline, bordered with the same dotted style as past campaign dots */
.projection-note {
max-width: 500px;
margin: 0.5em 0.75em;
padding: 0.5em 0.75em;
border: 2px dotted var(--color-borders, rgba(0, 0, 0, 0.3));
border-radius: var(--curvature);
font-size: 0.8em;
line-height: 1.45;
color: var(--text-color);
opacity: 0.7;
}
.now-label {
z-index: 1;
display: flex;
align-items: center;
margin: 1.5em 0;
padding: 0 0.6em;
background: var(--color-widget-bg, #fff);
font-size: 0.75em;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.08em;
color: var(--text-color);
opacity: 0.5;
}
/* flat event entry - no card chrome. column layout stacks the title
above any secondary metadata like the repeat schedule */
.event {
flex-grow: 1;
min-width: 0;
display: flex;
flex-direction: column;
gap: 0.15em;
padding: 0.4em 0.75em;
margin: 0.1em 0;
border-radius: var(--curvature);
}
.event.clickable {
cursor: pointer;
}
.event.clickable:hover {
background: var(--color-selection, rgba(0, 0, 0, 0.04));
}
.title {
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 endpoint returns a custom {future, past, next_before} payload rather than
// the paginated {results: [...]} shape that the store's fetch machinery expects,
// so we fetch and assign data ourselves instead of relying on a monitored url
private async loadEvents(): Promise<void> {
if (!this.contact) {
this.data = null;
return;
}
// capture the contact at request time so a slower in-flight response for a
// previous contact can't overwrite data for the contact we're now showing
const requestedContact = this.contact;
try {
const response = await this.store.getUrl(
`/contact/timeline/${encodeURIComponent(this.contact)}/`,
{ force: true }
);
if (this.contact !== requestedContact) {
return;
}
this.data = response.json;
} catch {
// on failure leave the prior data in place so a transient blip doesn't
// wipe the timeline; data is only cleared when the contact changes
// (handled in updated()) or when there's no contact at all
}
}
public refresh(): void {
this.loadEvents();
}
protected updated(
changes: PropertyValueMap<any> | Map<PropertyKey, unknown>
): void {
super.updated(changes);
if (changes.has('contact')) {
// colors are assigned per-uuid in order of first appearance, so a switch
// to a new contact must drop the previous assignments to avoid drift
this.campaignColors = {};
// blank the timeline immediately on a contact switch so the previous
// contact's events aren't briefly visible while the new fetch is in flight
this.data = null;
// in-flight pager requests for the prior contact will bail on stale
// contact, so reset the loading flags so the new contact's pagers aren't
// blocked by the previous contact's still-resolving request
this.loadingMore = false;
this.loadingMoreFuture = false;
this.loadEvents();
}
if (changes.has('data')) {
// a fresh first page resets any paged-in events in both directions
this.olderEvents = [];
this.newerEvents = [];
this.nextBefore = this.data ? (this.data.next_before ?? null) : null;
this.nextAfter = this.data ? (this.data.next_after ?? null) : null;
// eagerly assign campaign colors for everything in the first page so
// render() can be a pure read of the map. Pager handlers extend the map
// when later pages introduce campaigns not in the first page.
if (this.data) {
this.assignCampaignColors([
...(this.data.past || []),
...(this.data.future || [])
]);
}
// only notify consumers when we actually have data - skip the null state
// between contact switch and first response (and any transient errors)
// so listeners don't see a spurious count:0
if (this.data !== null && this.data !== undefined) {
// the badge reflects the total count of upcoming events, not just
// what's currently visible on this page
const count =
typeof this.data.future_count === 'number'
? this.data.future_count
: Array.isArray(this.data.future)
? this.data.future.length
: 0;
// empty mirrors the render()'s empty-state condition so a host
// card can drop its chrome entirely when there's nothing to show
const empty =
count === 0 &&
!(Array.isArray(this.data.campaigns) && this.data.campaigns.length) &&
!(Array.isArray(this.data.future) && this.data.future.length) &&
!(Array.isArray(this.data.past) && this.data.past.length);
this.fireCustomEvent(CustomEventType.DetailsChanged, { count, empty });
}
}
}
// assign stable per-uuid colors for any campaigns seen in `events` that
// aren't already in the map. Called from updated() on data change and from
// pager handlers after a page is appended, so render() never mutates state.
private assignCampaignColors(events: ScheduledEvent[]): void {
for (const event of events) {
if (event.type !== ScheduledEventType.CampaignEvent || !event.campaign) {
continue;
}
const uuid = event.campaign.uuid;
if (!this.campaignColors[uuid]) {
const used = Object.keys(this.campaignColors).length;
this.campaignColors[uuid] =
CAMPAIGN_COLORS[used % CAMPAIGN_COLORS.length];
}
}
}
private getCampaignColor(uuid: string): string {
return this.campaignColors[uuid] || CAMPAIGN_COLORS[0];
}
private getColor(event: ScheduledEvent): string {
if (event.type === ScheduledEventType.ScheduledTrigger) {
return TRIGGER_COLOR;
}
if (event.type !== ScheduledEventType.CampaignEvent || !event.campaign) {
return BROADCAST_COLOR;
}
return this.getCampaignColor(event.campaign.uuid);
}
// broadcasts and triggers carry their type icon inside the dot;
// campaign events use a plain dot since the campaign pill carries the context
private getDotIcon(event: ScheduledEvent): string | null {
if (event.type === ScheduledEventType.ScheduledTrigger) {
return Icon.trigger;
}
if (event.type !== ScheduledEventType.CampaignEvent) {
return Icon.broadcast;
}
return null;
}
public handleEventClicked(event: ScheduledEvent) {
this.fireCustomEvent(CustomEventType.Selection, event);
}
// clicking a campaign or flow pill navigates to that entity's read page
// instead of bubbling up to the row's event-read navigation
public handlePillClicked(e: Event, ref: ObjectReference) {
e.stopPropagation();
this.fireCustomEvent(CustomEventType.Selection, ref);
}
// activate a non-button row on Enter/Space (matches native button behavior)
// so keyboard users can reach clickable timeline rows and pager labels
private handleActivationKey(e: KeyboardEvent, action: () => void) {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
action();
}
}
public async handleShowOlder() {
if (this.loadingMore || !this.nextBefore) {
return;
}
this.loadingMore = true;
// capture the contact at request time so a paged response that returns
// after the user has switched contacts can't append onto the new timeline
const requestedContact = this.contact;
const url = `/contact/timeline/${encodeURIComponent(
this.contact
)}/?before=${encodeURIComponent(this.nextBefore)}`;
try {
const response = await this.store.getUrl(url, { force: true });
if (this.contact !== requestedContact) {
return;
}
const page = response.json as EventsResponse;
const newPast = page.past || [];
this.assignCampaignColors(newPast);
this.olderEvents = [...this.olderEvents, ...newPast];
this.nextBefore = page.next_before ?? null;
} catch {
if (this.contact !== requestedContact) {
return;
}
// on failure leave the accumulated events in place but stop offering
// the same failing page - clearing the cursor hides the pager
this.nextBefore = null;
} finally {
// only clear the flag if we're still on the original contact - the
// contact-change handler in updated() already cleared it, and clearing
// again here would unblock the new contact's in-flight pager request
// and allow a duplicate fetch of the same cursor
if (this.contact === requestedContact) {
this.loadingMore = false;
}
}
}
public async handleShowMore() {
if (this.loadingMoreFuture || !this.nextAfter) {
return;
}
this.loadingMoreFuture = true;
const requestedContact = this.contact;
const url = `/contact/timeline/${encodeURIComponent(
this.contact
)}/?after=${encodeURIComponent(this.nextAfter)}`;
try {
const response = await this.store.getUrl(url, { force: true });
if (this.contact !== requestedContact) {
return;
}
const page = response.json as EventsResponse;
const newFuture = page.future || [];
this.assignCampaignColors(newFuture);
this.newerEvents = [...this.newerEvents, ...newFuture];
this.nextAfter = page.next_after ?? null;
} catch {
if (this.contact !== requestedContact) {
return;
}
this.nextAfter = null;
} finally {
if (this.contact === requestedContact) {
this.loadingMoreFuture = false;
}
}
}
private renderTime(event: ScheduledEvent): TemplateResult {
// anchor durations to the server's "now" so they stay tolerant of clock
// skew and don't silently drift when the page is left open
return html`
<temba-tip text=${this.store.formatDate(event.scheduled)} position="top">
${this.store.getShortDurationFromIso(event.scheduled, this.data?.now)}
</temba-tip>
`;
}
private renderEvent(event: ScheduledEvent): TemplateResult {
const clickable = event.type !== ScheduledEventType.SentBroadcast;
const isMessage = !!event.message;
// message events display the message text; flow-bearing events show a
// clickable flow pill linking to the flow (the "start" action is implied)
const body = isMessage
? 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.flow)}
>${event.flow.name}</temba-label
>`
: html``;
return html`
<div
class="event ${clickable ? 'clickable' : ''}"
role=${clickable ? 'button' : 'presentation'}
tabindex=${clickable ? '0' : '-1'}
@click=${clickable ? () => this.handleEventClicked(event) : null}
@keydown=${clickable
? (e: KeyboardEvent) =>
this.handleActivationKey(e, () => this.handleEventClicked(event))
: null}
>
<div class="title">${body}</div>
</div>
`;
}
private renderRow(
time: TemplateResult,
dot: TemplateResult,
content: TemplateResult,
first: boolean,
last: boolean
): TemplateResult {
return html`
<div class="row">
<div class="time">${time}</div>
<div class="track">
<div class="line ${first ? 'hidden' : ''}"></div>
${dot}
<div class="line ${last ? 'hidden' : ''}"></div>
</div>
${content}
</div>
`;
}
private renderCampaigns(campaigns: ObjectReference[]): TemplateResult | null {
if (campaigns.length === 0) {
return null;
}
return html`
<div class="campaigns">
<div class="campaigns-label">${this.lang_campaigns_label}</div>
${campaigns.map(
(campaign) =>
html`<div
class="campaign-pill"
style="--pill-hue:${this.getCampaignColor(campaign.uuid)}"
role="button"
tabindex="0"
@click=${(e: Event) => this.handlePillClicked(e, campaign)}
@keydown=${(e: KeyboardEvent) =>
this.handleActivationKey(e, () =>
this.handlePillClicked(e, campaign)
)}
>
<span class="campaign-dot"></span>${campaign.name}
</div>`
)}
</div>
`;
}
public render(): TemplateResult {
if (!this.data) {
return html``;
}
// tolerate a malformed/empty response rather than throwing
const campaigns = Array.isArray(this.data.campaigns)
? this.data.campaigns
: [];
const future = [
...(Array.isArray(this.data.future) ? this.data.future : []),
...this.newerEvents
];
const pastDescending = [
...(Array.isArray(this.data.past) ? this.data.past : []),
...this.olderEvents
];
if (
campaigns.length === 0 &&
future.length === 0 &&
pastDescending.length === 0
) {
return html`<div class="empty">
<slot name="empty">
<div class="empty-extras">
<temba-icon name=${Icon.schedule} size="2"></temba-icon>
</div>
<div class="empty-title">${this.lang_empty}</div>
<div class="empty-extras">
<div class="empty-help">${this.lang_empty_help}</div>
<a class="empty-link" href="/campaign/" onclick="goto(event, this)"
>${this.lang_campaigns_link}</a
>
</div>
</slot>
</div>`;
}
// the timeline reads oldest to newest, top to bottom: an "older" pager,
// then past events, the "now" divider, upcoming events, and a "show more"
// pager at the bottom
type Row = {
kind: 'event' | 'now' | 'more-past' | 'more-future';
event?: ScheduledEvent;
past?: boolean;
};
const rows: Row[] = [];
if (this.nextBefore) {
rows.push({ kind: 'more-past' });
}
[...pastDescending]
.reverse()
.forEach((event) => rows.push({ kind: 'event', event, past: true }));
rows.push({ kind: 'now' });
future.forEach((event) => rows.push({ kind: 'event', event, past: false }));
if (this.nextAfter) {
rows.push({ kind: 'more-future' });
}
return html`
${this.renderCampaigns(campaigns)}
<div class="timeline">
${rows.map((row, idx) => {
const first = idx === 0;
const last = idx === rows.length - 1;
if (row.kind === 'now') {
return html`
<div class="row now-row">
<div class="time"></div>
<div class="track">
<div class="line ${first ? 'hidden' : ''}"></div>
<div class="dot now"></div>
<div class="line ${last ? 'hidden' : ''}"></div>
</div>
<div class="now-label">${this.lang_now}</div>
</div>
`;
}
if (row.kind === 'more-past') {
return html`
<div class="row pager-row ${this.loadingMore ? 'loading' : ''}">
<div class="time"></div>
<div class="track">
<div class="line ${first ? 'hidden' : ''}"></div>
<button
class="pager-dot ${this.loadingMore ? 'loading' : ''}"
@click=${this.handleShowOlder}
aria-label=${this.lang_show_older}
title=${this.lang_show_older}
>
<temba-icon name=${Icon.up_double} size="1"></temba-icon>
</button>
<div class="line ${last ? 'hidden' : ''}"></div>
</div>
<div
class="pager-label"
role="button"
tabindex="0"
aria-label=${this.lang_show_older}
@click=${this.handleShowOlder}
@keydown=${(e: KeyboardEvent) =>
this.handleActivationKey(e, () => this.handleShowOlder())}
title=${this.lang_show_older}
>
${this.lang_more}
</div>
</div>
`;
}
if (row.kind === 'more-future') {
return html`
<div
class="row pager-row ${this.loadingMoreFuture ? 'loading' : ''}"
>
<div class="time"></div>
<div class="track">
<div class="line ${first ? 'hidden' : ''}"></div>
<button
class="pager-dot ${this.loadingMoreFuture ? 'loading' : ''}"
@click=${this.handleShowMore}
aria-label=${this.lang_show_more}
title=${this.lang_show_more}
>
<temba-icon name=${Icon.down_double} size="1"></temba-icon>
</button>
<div class="line ${last ? 'hidden' : ''}"></div>
</div>
<div
class="pager-label"
role="button"
tabindex="0"
aria-label=${this.lang_show_more}
@click=${this.handleShowMore}
@keydown=${(e: KeyboardEvent) =>
this.handleActivationKey(e, () => this.handleShowMore())}
title=${this.lang_show_more}
>
${this.lang_more}
</div>
</div>
`;
}
const dotIcon = this.getDotIcon(row.event);
return this.renderRow(
this.renderTime(row.event),
html`<div
class="dot ${row.past ? 'past' : 'future'}"
style="--dot-color:${this.getColor(row.event)}"
>
${dotIcon
? html`<temba-icon name=${dotIcon} size="0.65"></temba-icon>`
: null}
</div>`,
this.renderEvent(row.event),
first,
last
);
})}
</div>
${future.length > 0
? html`<div class="projection-note">${this.lang_projected_info}</div>`
: null}
`;
}
}
|