@nyaruka/temba-components 0.170.1 → 0.172.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,4 @@
1
- import { html, css, LitElement, TemplateResult } from 'lit';
2
- import { property, state, query } from 'lit/decorators.js';
1
+ import { property } from 'lit/decorators.js';
3
2
  import {
4
3
  FlowDefinition,
5
4
  Node,
@@ -37,8 +36,8 @@ import {
37
36
  NodeConfig
38
37
  } from './types';
39
38
  import { localizeAction } from './utils';
40
- import { Icon } from '../Icons';
41
39
  import { getTranslatableCategoriesForNode } from './categoryLocalization';
40
+ import { ModalSearchResult, SearchModal } from '../layout/SearchModal';
42
41
 
43
42
  // Sticky note badge colors: border matches the sticky's border, fill is 25% lighter
44
43
  const STICKY_BADGE_COLORS: Record<
@@ -52,24 +51,10 @@ const STICKY_BADGE_COLORS: Record<
52
51
  gray: { border: '#6b7280', fill: '#8f949f', text: '#fff' }
53
52
  };
54
53
 
55
- export interface SearchResult {
54
+ export interface SearchResult extends ModalSearchResult {
56
55
  nodeUuid: string;
57
56
  // For actions: the action object; for nodes (splits): null
58
57
  action: Action | null;
59
- // The config name (e.g. "Send Message", "Call Webhook")
60
- typeName: string;
61
- // Color for the type badge background
62
- color: string;
63
- // Optional border color for the badge (used by sticky notes)
64
- borderColor?: string;
65
- // Optional text color override for the badge
66
- textColor?: string;
67
- // The full text that was searched
68
- fullText: string;
69
- // The index of the match start in fullText
70
- matchStart: number;
71
- // The length of the match
72
- matchLength: number;
73
58
  // For sticky notes: which field matched ('title' or 'body')
74
59
  stickyField?: 'title' | 'body';
75
60
  }
@@ -353,183 +338,7 @@ function localizeCategoryName(
353
338
  return originalName;
354
339
  }
355
340
 
356
- export class FlowSearch extends LitElement {
357
- static styles = css`
358
- :host {
359
- position: fixed;
360
- top: 0;
361
- left: 0;
362
- right: 0;
363
- bottom: 0;
364
- z-index: 100000;
365
- display: none;
366
- font-family: var(--font-family, sans-serif);
367
- }
368
-
369
- :host([open]) {
370
- display: flex;
371
- align-items: flex-start;
372
- justify-content: center;
373
- }
374
-
375
- .backdrop {
376
- position: absolute;
377
- top: 0;
378
- left: 0;
379
- right: 0;
380
- bottom: 0;
381
- background: rgba(0, 0, 0, 0.3);
382
- }
383
-
384
- .search-container {
385
- position: relative;
386
- margin-top: 80px;
387
- width: 560px;
388
- max-height: calc(100vh - 160px);
389
- background: white;
390
- border-radius: 14px;
391
- box-shadow:
392
- 0 24px 80px rgba(0, 0, 0, 0.2),
393
- 0 0 0 1px rgba(0, 0, 0, 0.05);
394
- display: flex;
395
- flex-direction: column;
396
- overflow: hidden;
397
- animation: searchSlideIn 0.15s ease-out;
398
- }
399
-
400
- @keyframes searchSlideIn {
401
- from {
402
- opacity: 0;
403
- transform: scale(0.98) translateY(-8px);
404
- }
405
- to {
406
- opacity: 1;
407
- transform: scale(1) translateY(0);
408
- }
409
- }
410
-
411
- .search-input-row {
412
- display: flex;
413
- align-items: center;
414
- padding: 14px 18px;
415
- border-bottom: 1px solid #e5e7eb;
416
- gap: 10px;
417
- }
418
-
419
- .search-icon {
420
- color: #9ca3af;
421
- flex-shrink: 0;
422
- }
423
-
424
- .search-icon temba-icon {
425
- --icon-color: #9ca3af;
426
- display: block;
427
- }
428
-
429
- input {
430
- flex: 1;
431
- border: none;
432
- outline: none;
433
- font-size: 16px;
434
- background: transparent;
435
- color: #111;
436
- font-family: inherit;
437
- }
438
-
439
- input::placeholder {
440
- color: #9ca3af;
441
- }
442
-
443
- .results {
444
- overflow-y: auto;
445
- max-height: 400px;
446
- }
447
-
448
- .result-item {
449
- display: flex;
450
- align-items: center;
451
- padding: 10px 18px;
452
- gap: 12px;
453
- cursor: pointer;
454
- border-bottom: 1px solid #f3f4f6;
455
- transition: background 0.08s;
456
- }
457
-
458
- .result-item:last-child {
459
- border-bottom: none;
460
- }
461
-
462
- .result-item:hover,
463
- .result-item.highlighted {
464
- background: #f0f4ff;
465
- }
466
-
467
- .result-type-badge {
468
- flex-shrink: 0;
469
- font-size: 11px;
470
- font-weight: 600;
471
- color: white;
472
- padding: 2px 10px;
473
- border-radius: 8px;
474
- border: 2px solid transparent;
475
- white-space: nowrap;
476
- text-align: center;
477
- box-sizing: border-box;
478
- }
479
-
480
- .result-text {
481
- min-width: 0;
482
- font-size: 14px;
483
- color: #374151;
484
- line-height: 1.4;
485
- white-space: nowrap;
486
- overflow: hidden;
487
- text-overflow: ellipsis;
488
- }
489
-
490
- .result-text mark {
491
- background: #fef08a;
492
- color: inherit;
493
- border-radius: 2px;
494
- padding: 0 1px;
495
- }
496
-
497
- .no-results {
498
- padding: 24px 18px;
499
- text-align: center;
500
- color: #9ca3af;
501
- font-size: 14px;
502
- }
503
-
504
- .result-count {
505
- padding: 8px 18px;
506
- font-size: 12px;
507
- color: #9ca3af;
508
- border-top: 1px solid #e5e7eb;
509
- text-align: right;
510
- flex-shrink: 0;
511
- }
512
-
513
- .hint {
514
- padding: 10px 18px;
515
- font-size: 12px;
516
- color: #9ca3af;
517
- text-align: center;
518
- }
519
-
520
- kbd {
521
- background: #f3f4f6;
522
- border: 1px solid #d1d5db;
523
- border-radius: 4px;
524
- padding: 1px 5px;
525
- font-size: 11px;
526
- font-family: inherit;
527
- }
528
- `;
529
-
530
- @property({ type: Boolean, reflect: true })
531
- open = false;
532
-
341
+ export class FlowSearch extends SearchModal<SearchResult> {
533
342
  @property({ attribute: false })
534
343
  definition: FlowDefinition | null = null;
535
344
 
@@ -542,96 +351,16 @@ export class FlowSearch extends LitElement {
542
351
  @property({ type: Boolean })
543
352
  includeCategories = false;
544
353
 
545
- @state()
546
- private searchQuery = '';
547
-
548
- @state()
549
- private results: SearchResult[] = [];
550
-
551
- @state()
552
- private highlightedIndex = 0;
553
-
554
- @query('input')
555
- private inputEl!: HTMLInputElement;
556
-
557
- public show(): void {
558
- this.open = true;
559
- this.searchQuery = '';
560
- this.results = [];
561
- this.highlightedIndex = 0;
562
- this.updateComplete.then(() => {
563
- this.inputEl?.focus();
564
- this.inputEl?.select();
565
- });
566
- }
567
-
568
- public hide(): void {
569
- this.open = false;
570
- this.searchQuery = '';
571
- this.results = [];
572
- }
573
-
574
- private handleInput(e: InputEvent): void {
575
- const input = e.target as HTMLInputElement;
576
- this.searchQuery = input.value;
577
- this.performSearch();
578
- this.highlightedIndex = 0;
579
- }
580
-
581
- private handleKeyDown(e: KeyboardEvent): void {
582
- if (e.key === 'Escape') {
583
- e.preventDefault();
584
- this.hide();
585
- return;
586
- }
587
- if (e.key === 'ArrowDown' || (e.ctrlKey && e.key === 'n')) {
588
- e.preventDefault();
589
- if (this.results.length > 0) {
590
- this.highlightedIndex =
591
- (this.highlightedIndex + 1) % this.results.length;
592
- }
593
- return;
594
- }
595
- if (e.key === 'ArrowUp' || (e.ctrlKey && e.key === 'p')) {
596
- e.preventDefault();
597
- if (this.results.length > 0) {
598
- this.highlightedIndex =
599
- (this.highlightedIndex - 1 + this.results.length) %
600
- this.results.length;
601
- }
602
- return;
603
- }
604
- if (e.key === 'Enter') {
605
- e.preventDefault();
606
- if (this.results.length > 0) {
607
- this.selectResult(this.results[this.highlightedIndex]);
608
- }
609
- return;
610
- }
611
- }
612
-
613
- private handleBackdropClick(): void {
614
- this.hide();
615
- }
616
-
617
- private selectResult(result: SearchResult): void {
618
- this.hide();
619
- this.dispatchEvent(
620
- new CustomEvent('temba-search-result-selected', {
621
- detail: result,
622
- bubbles: true,
623
- composed: true
624
- })
625
- );
354
+ protected getSearchLabel(): string {
355
+ return this.scope === 'table' ? 'Search this table' : 'Search this flow';
626
356
  }
627
357
 
628
- private performSearch(): void {
629
- if (!this.definition || !this.searchQuery.trim()) {
630
- this.results = [];
631
- return;
358
+ protected performSearch(rawQuery: string): SearchResult[] {
359
+ if (!this.definition) {
360
+ return [];
632
361
  }
633
362
 
634
- const query = this.searchQuery.toLowerCase();
363
+ const query = rawQuery.toLowerCase();
635
364
 
636
365
  // Get the localization map for the active language (if translating)
637
366
  const isTranslating =
@@ -641,11 +370,10 @@ export class FlowSearch extends LitElement {
641
370
  : undefined;
642
371
 
643
372
  if (this.scope === 'table') {
644
- this.results = this.performTableSearch(query, langLocalization);
645
- return;
373
+ return this.performTableSearch(query, langLocalization);
646
374
  }
647
375
 
648
- this.results = this.performFlowSearch(query, langLocalization);
376
+ return this.performFlowSearch(query, langLocalization);
649
377
  }
650
378
 
651
379
  private performFlowSearch(
@@ -889,112 +617,4 @@ export class FlowSearch extends LitElement {
889
617
 
890
618
  return results;
891
619
  }
892
-
893
- private renderMatchText(result: SearchResult): TemplateResult {
894
- const { matchStart, matchLength } = result;
895
- const matchEnd = matchStart + matchLength;
896
-
897
- // Replace newlines with spaces for single-line display
898
- const text = result.fullText.replace(/\n/g, ' ');
899
-
900
- // Type-name matches show an unhighlighted content preview
901
- if (matchLength === 0) {
902
- return html`${text}`;
903
- }
904
-
905
- // Show leading context before the match, then the match, then trailing.
906
- // CSS text-overflow:ellipsis clips the trailing text, so we keep the match
907
- // near the start. When the match is near the end of the text, show more
908
- // leading context since there's less trailing text to display.
909
- const afterMatch = text.length - matchEnd;
910
- const contextBefore = afterMatch < 30 ? 50 : 20;
911
- const start = Math.max(0, matchStart - contextBefore);
912
- const prefix = start > 0 ? '\u2026' : '';
913
-
914
- const before = text.slice(start, matchStart);
915
- const match = text.slice(matchStart, matchEnd);
916
- const after = text.slice(matchEnd);
917
-
918
- return html`${prefix}${before}<mark>${match}</mark>${after}`;
919
- }
920
-
921
- updated(changedProps: Map<string, unknown>): void {
922
- if (changedProps.has('highlightedIndex')) {
923
- const highlighted = this.shadowRoot?.querySelector(
924
- '.result-item.highlighted'
925
- );
926
- highlighted?.scrollIntoView({ block: 'nearest' });
927
- }
928
- }
929
-
930
- render(): TemplateResult {
931
- const isTableScope = this.scope === 'table';
932
- const searchLabel = isTableScope ? 'Search this table' : 'Search this flow';
933
-
934
- return html`
935
- <div class="backdrop" @click=${this.handleBackdropClick}></div>
936
- <div
937
- class="search-container"
938
- role="dialog"
939
- aria-modal="true"
940
- aria-label=${searchLabel}
941
- @click=${(e: Event) => e.stopPropagation()}
942
- >
943
- <div class="search-input-row">
944
- <div class="search-icon">
945
- <temba-icon name=${Icon.search} size="1.5"></temba-icon>
946
- </div>
947
- <input
948
- type="text"
949
- placeholder="${searchLabel}..."
950
- aria-label=${searchLabel}
951
- .value=${this.searchQuery}
952
- @input=${this.handleInput}
953
- @keydown=${this.handleKeyDown}
954
- />
955
- </div>
956
- ${this.searchQuery.trim()
957
- ? this.results.length > 0
958
- ? html`
959
- <div class="results">
960
- ${this.results.map(
961
- (result, index) => html`
962
- <div
963
- class="result-item ${index === this.highlightedIndex
964
- ? 'highlighted'
965
- : ''}"
966
- @click=${() => this.selectResult(result)}
967
- @mouseenter=${() => {
968
- this.highlightedIndex = index;
969
- }}
970
- >
971
- <div
972
- class="result-type-badge"
973
- style="background:${result.color};border-color:${result.borderColor ||
974
- result.color}${result.textColor
975
- ? `;color:${result.textColor}`
976
- : ''}"
977
- >
978
- ${result.typeName}
979
- </div>
980
- <div class="result-text">
981
- ${this.renderMatchText(result)}
982
- </div>
983
- </div>
984
- `
985
- )}
986
- </div>
987
- <div class="result-count">
988
- ${this.results.length}
989
- result${this.results.length !== 1 ? 's' : ''}
990
- </div>
991
- `
992
- : html`<div class="no-results">No matches found</div>`
993
- : html`<div class="hint">
994
- <kbd>↑</kbd> <kbd>↓</kbd> to navigate &nbsp; <kbd>Enter</kbd> to
995
- open &nbsp; <kbd>Esc</kbd> to close
996
- </div>`}
997
- </div>
998
- `;
999
- }
1000
620
  }
@@ -117,7 +117,8 @@ export const send_msg: ActionConfig = {
117
117
  type: 'text',
118
118
  placeholder: 'Expression (e.g. @contact.photo)',
119
119
  required: true,
120
- evaluated: true
120
+ evaluated: true,
121
+ maxLength: 8192
121
122
  }
122
123
  }
123
124
  }
@@ -45,6 +45,7 @@ export const set_contact_field: ActionConfig = {
45
45
  label: 'Value',
46
46
  placeholder: 'Enter field value...',
47
47
  evaluated: true,
48
+ maxLength: 10000,
48
49
  helpText:
49
50
  'The new value for the contact field. You can use expressions like @contact.name'
50
51
  }
@@ -20,6 +20,7 @@ export const set_contact_name: ActionConfig = {
20
20
  placeholder: 'Enter contact name...',
21
21
  required: true,
22
22
  evaluated: true,
23
+ maxLength: 1000,
23
24
  helpText:
24
25
  'The new name for the contact. You can use expressions like @contact.name'
25
26
  }
@@ -0,0 +1,115 @@
1
+ import { produce } from 'immer';
2
+ import { FlowDefinition } from '../store/flow-definition';
3
+ import { normalizeUuid } from '../store/identity';
4
+
5
+ export interface FlowDependency {
6
+ type: string;
7
+ name: string;
8
+ uuid?: string;
9
+ key?: string;
10
+ missing?: boolean;
11
+ }
12
+
13
+ const dependencyIdentity = (dependency: FlowDependency): string | null => {
14
+ const identity = dependency.uuid || dependency.key;
15
+ return identity ? `${dependency.type}:${identity}` : null;
16
+ };
17
+
18
+ /**
19
+ * Returns a definition whose embedded reference names have been replaced by
20
+ * the canonical dependency names supplied by the server. References with a
21
+ * UUID are safe to recognize anywhere in the definition because flow-owned
22
+ * UUIDs don't appear in its dependency list. Field references use keys and
23
+ * only occur under a `field` property; scoping keyed matching there avoids
24
+ * confusing a field key with a result name or another keyed structure.
25
+ */
26
+ export const resolveDependencyNames = (
27
+ definition: FlowDefinition,
28
+ dependencies: FlowDependency[] = []
29
+ ): FlowDefinition => {
30
+ if (!definition || dependencies.length === 0) {
31
+ return definition;
32
+ }
33
+
34
+ const namesByUuid = new Map<string, string>();
35
+ const fieldNamesByKey = new Map<string, string>();
36
+ for (const dependency of dependencies) {
37
+ if (dependency.uuid && dependency.name != null) {
38
+ // normalized on both set and lookup so a reference embedded in the
39
+ // definition in a non-canonical form still matches, the same way the
40
+ // store's asset cache resolves it
41
+ namesByUuid.set(normalizeUuid(dependency.uuid), dependency.name);
42
+ } else if (
43
+ dependency.type === 'field' &&
44
+ dependency.key &&
45
+ dependency.name != null
46
+ ) {
47
+ fieldNamesByKey.set(dependency.key, dependency.name);
48
+ }
49
+ }
50
+
51
+ return produce(definition, (draft: any) => {
52
+ const visit = (value: any, propertyName?: string) => {
53
+ if (!value || typeof value !== 'object') {
54
+ return;
55
+ }
56
+ if (Array.isArray(value)) {
57
+ // keep the owning property name so `field` scoping still applies to
58
+ // references nested in an array
59
+ value.forEach((item) => visit(item, propertyName));
60
+ return;
61
+ }
62
+
63
+ if (typeof value.uuid === 'string' && 'name' in value) {
64
+ const canonical = namesByUuid.get(normalizeUuid(value.uuid));
65
+ if (canonical != null) {
66
+ value.name = canonical;
67
+ }
68
+ } else if (
69
+ propertyName === 'field' &&
70
+ typeof value.key === 'string' &&
71
+ 'name' in value
72
+ ) {
73
+ const canonical = fieldNamesByKey.get(value.key);
74
+ if (canonical != null) {
75
+ value.name = canonical;
76
+ }
77
+ }
78
+
79
+ for (const [key, child] of Object.entries(value)) {
80
+ visit(child, key);
81
+ }
82
+ };
83
+
84
+ visit(draft);
85
+ });
86
+ };
87
+
88
+ /**
89
+ * Replaces every matching dependency whose canonical name has changed. Assets
90
+ * the loaded flow isn't interested in are ignored.
91
+ */
92
+ export const replaceDependencies = (
93
+ dependencies: FlowDependency[] = [],
94
+ changed: FlowDependency[]
95
+ ): FlowDependency[] | null => {
96
+ const changedByIdentity = new Map<string, FlowDependency>();
97
+ for (const dependency of changed) {
98
+ const identity = dependencyIdentity(dependency);
99
+ if (identity) {
100
+ changedByIdentity.set(identity, dependency);
101
+ }
102
+ }
103
+
104
+ let replaced = false;
105
+ const updated = dependencies.map((dependency) => {
106
+ const identity = dependencyIdentity(dependency);
107
+ const replacement = identity ? changedByIdentity.get(identity) : null;
108
+ if (!replacement || replacement.name === dependency.name) {
109
+ return dependency;
110
+ }
111
+ replaced = true;
112
+ return { ...dependency, name: replacement.name };
113
+ });
114
+ return replaced ? updated : null;
115
+ };
@@ -43,6 +43,7 @@ export const split_by_ticket: NodeConfig = {
43
43
  required: false,
44
44
  evaluated: true,
45
45
  placeholder: 'Enter a note for the ticket (optional)',
46
+ maxLength: 10000,
46
47
  minHeight: 100
47
48
  }
48
49
  },
@@ -34,6 +34,7 @@ export const wait_for_dial: NodeConfig = {
34
34
  label: 'Phone Number',
35
35
  required: true,
36
36
  evaluated: true,
37
+ maxLength: 1000,
37
38
  placeholder: 'Phone number or expression'
38
39
  },
39
40
  dial_limit_seconds: {