All files / src/form/select Omnibox.ts

73.23% Statements 104/142
100% Branches 7/7
33.33% Functions 2/6
73.23% Lines 104/142

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 143125x   125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x                       125x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 125x 125x 4x 4x 4x 2x 2x 2x 4x 1x 1x 1x 1x 1x 1x 1x 4x 125x                               125x                       125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 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 { PropertyValues, TemplateResult, html } from 'lit';
import { property } from 'lit/decorators.js';
import { styleMap } from 'lit-html/directives/style-map.js';
import { Select, SelectOption } from './Select';
import { Icon } from '../../Icons';
 
enum OmniType {
  Group = 'group',
  Contact = 'contact'
}
 
export interface OmniOption extends SelectOption {
  id: string;
  name: string;
  type: OmniType;
  urn?: string;
  count?: number;
  contact?: string;
  scheme?: string;
}
 
const postNameStyle = {
  color: 'var(--color-text-dark)',
  padding: '0px 6px',
  fontSize: '12px'
};
 
export class Omnibox extends Select<OmniOption> {
  @property({ type: String })
  valueKey = 'uuid';

  @property({ type: Boolean })
  groups = false;

  @property({ type: Boolean })
  contacts = false;

  @property({ type: String })
  placeholder = 'Select recipients';

  @property({ type: Boolean })
  multi = true;
 
  @property({ type: Boolean })
  jsonValue = true;
 
  @property({ type: Boolean })
  searchable = true;
 
  @property({ type: Boolean })
  searchOnFocus = true;
 
  @property({ type: Boolean })
  queryParam = 'search';
 
  public willUpdate(changes: PropertyValues): void {
    if (
      (changes.has('groups') || changes.has('contacts')) &&
      (this.groups || this.contacts)
    ) {
      let types = '&types=';
      if (this.groups) {
        types += 'g';
      }
 
      if (this.contacts) {
        types += 'c';
      }
 
      this.endpoint = this.endpoint + types;
    }
    super.willUpdate(changes);
  }
 
  /** An option in the drop down */
  public renderOptionDefault(option: OmniOption): TemplateResult {
    return html`
      <div style="display:flex;">
        <div style="margin-right: 8px">${this.getIcon(option)}</div>
        <div style="flex: 1">${option.name}</div>
        <div
          style="background: rgba(50, 50, 50, 0.15); margin-left: 5px; display: flex; align-items: center; border-radius: 4px"
        >
          ${this.getPostName(option)}
        </div>
      </div>
    `;
  }

  private getPostName(option: OmniOption): TemplateResult {
    const style = { ...postNameStyle };

    if (option.urn && option.type === OmniType.Contact) {
      if (option.urn !== option.name) {
        return html`<div style=${styleMap(style)}>${option.urn}</div>`;
      }
    }

    if (option.type === OmniType.Group) {
      return html`
        <div style=${styleMap(style)}>${option.count.toLocaleString()}</div>
      `;
    }

    return null;
  }
 
  /**
   * Chip rendering — icon + name, plus the group count when the option
   * is a Group. Counts are intentionally suppressed in the base Select
   * chip (noise for action editors like Add to Group), but Omnibox is
   * the start-flow recipients picker where group size is a key part
   * of the chip's identity. Contacts skip the post-name URN that the
   * dropdown shows — chips already have a tight footprint.
   */
  public renderSelectedItemDefault(option: OmniOption): TemplateResult {
    const base = super.renderOptionDefault(option);
    if (
      option.type === OmniType.Group &&
      option.count !== undefined &&
      option.count !== null
    ) {
      return html`<div style="display:flex; align-items:center; gap:6px;">
        ${base}<span
          style="opacity:0.7; font-size:11px; font-variant-numeric: tabular-nums; font-weight: var(--w-medium);"
          >${option.count.toLocaleString()}</span
        >
      </div>`;
    }
    return base;
  }
 
  private getIcon(option: OmniOption): TemplateResult {
    if (option.type === OmniType.Group) {
      return html`<temba-icon name="${Icon.group}"></temba-icon>`;
    }
 
    if (option.type === OmniType.Contact) {
      return html`<temba-icon name="${Icon.contact}"></temba-icon>`;
    }
  }
}