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 | 125x 129x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 28x 125x 25x 25x 25x 25x 25x 25x 125x 50x 50x 50x 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 125x 67x 67x 67x 125x 25x 1x 1x 1x 1x 1x 1x 1x 1x 125x 125x 125x 125x 125x 2x 2x 2x 2x 2x 2x 125x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 12x 12x 12x 12x 12x 13x 13x 13x 13x 13x 25x 125x 125x 125x 14x 14x 14x 14x 14x 14x 14x 14x 14x 24x 24x 24x 24x 24x 14x 125x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 26x 26x 18x 18x 18x 18x 18x 26x 25x 25x 28x 28x 28x 28x 28x 28x 25x 25x 32x 32x 32x 32x 14x 14x 14x 14x 14x 14x 4x 4x 4x 4x 4x 4x 25x 25x 3x 3x 3x 3x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 125x 125x 125x 125x 125x 125x 125x 125x 28x 28x 28x 28x 28x 28x 28x 3x 25x 25x 25x 25x 1x 8x 25x 25x 25x 25x 25x 25x 25x 25x 25x 28x 28x 8x 8x 8x 8x 8x 28x 28x 8x 8x 28x 125x 125x 125x 125x 125x 125x 125x 125x 125x | import { css, html, TemplateResult } from 'lit';
import { property, state } from 'lit/decorators.js';
import { ModalSearchResult, SearchModal } from '../layout/SearchModal';
import { getUrl } from '../utils';
// The matched message event as the endpoint returns it
export interface TicketSearchEvent {
uuid: string;
type: string;
created_on: string;
ticket_uuid?: string;
msg?: { text?: string };
[key: string]: any;
}
export interface TicketSearchResult extends ModalSearchResult {
contact: { uuid: string; name: string };
ticket: { uuid: string; status: string };
event: TicketSearchEvent;
// The query that produced this result, so selection handlers can re-run
// the search within the ticket
query: string;
}
const BADGE_COLOR = '#6b7280';
// rough number of characters that fit on a result row - the snippet is
// windowed around the match so the match stays visible if CSS clips the tail
const SNIPPET_BUDGET = 60;
// the shortest prefix we'll fall back to when a query term doesn't appear
// literally - short enough to cover most inflections, long enough that the
// highlight still points at the word the user searched for
const MIN_PREFIX = 4;
// strips everything but letters and numbers, so punctuation in the query
// doesn't keep a term from matching the message text
const stripNonWord = (text: string) => text.replace(/[^\p{L}\p{N}]/gu, '');
// same, but only at the ends, so the words of a multi-word query stay apart
const stripOuterNonWord = (text: string) =>
text.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, '');
// the candidates to look for, in priority order, without duplicates
const withoutDupes = (candidates: string[]) =>
candidates.filter(
(candidate, index) => !!candidate && candidates.indexOf(candidate) === index
);
/**
* Searches message text across all of an org's ticket chats. Searching hits
* an endpoint, so it waits for Enter rather than running on every keystroke.
*/
export class TicketSearch extends SearchModal<TicketSearchResult> {
static styles = [
SearchModal.styles,
css`
.result-type-badge {
max-width: 35%;
overflow: hidden;
text-overflow: ellipsis;
}
.result-text {
flex-grow: 1;
}
.result-date {
flex-shrink: 0;
font-size: 11px;
color: #9ca3af;
}
`
];
@property({ type: String })
endpoint = '/ticket/search/';
// searching hits an endpoint, so wait for Enter
searchOnEnter = true;
// how many matches the last search found in tickets this user can't access,
// so an empty result set can say why it's empty
@state()
private dropped = 0;
// the in-flight request, aborted when it's superseded or the modal closes
private abortController: AbortController = null;
protected cancelSearch(): void {
this.abortSearch();
this.dropped = 0;
}
private abortSearch(): void {
if (this.abortController) {
this.abortController.abort();
this.abortController = null;
}
}
protected getSearchLabel(): string {
return 'Search tickets';
}
protected renderHint(): TemplateResult {
return html`<div class="hint">
<kbd>Enter</kbd> to search <kbd>Esc</kbd> to close
</div>`;
}
/**
* The endpoint filters matches down to the tickets this user can access
* after the search itself is capped, so a query that matched plenty can
* still come back empty. Say so rather than claiming nothing matched.
*/
protected getNoResultsMessage(): string {
return this.dropped > 0
? 'No matches in tickets you can access'
: super.getNoResultsMessage();
}
protected async performSearch(query: string): Promise<TicketSearchResult[]> {
this.abortSearch();
this.dropped = 0;
const controller = new AbortController();
this.abortController = controller;
const joiner = this.endpoint.includes('?') ? '&' : '?';
const response = await getUrl(
`${this.endpoint}${joiner}text=${encodeURIComponent(query)}`,
controller
);
if (this.abortController === controller) {
this.abortController = null;
}
const json = response.json as any;
this.dropped = json.dropped || 0;
const results = json.results || [];
return results.map((result: any) => this.toSearchResult(result, query));
}
/**
* Locates a term in the message text, falling back to progressively
* shorter prefixes of it. The backend matches on analyzed text, so a query
* term ("helping") can match a message that only contains a stemmed or
* inflected form of it ("helped") - highlighting the longest prefix that
* does appear keeps the match visible rather than showing nothing.
*/
private findTerm(
lowerText: string,
term: string,
minLength: number
): { start: number; length: number } | null {
for (let length = term.length; length >= minLength; length--) {
const idx = lowerText.indexOf(term.slice(0, length));
if (idx !== -1) {
return { start: idx, length };
}
}
return null;
}
private toSearchResult(result: any, query: string): TicketSearchResult {
const text = result.event?.msg?.text || result.event?.text || '';
const lowerText = text.toLowerCase();
const lowerQuery = query.trim().toLowerCase();
let matchStart = -1;
let matchLength = 0;
// highlight the whole query if it appears verbatim (with or without any
// punctuation it's wrapped in), otherwise the first matching term - the
// backend matches terms independently, and against analyzed text, so a
// term is tried both as typed and stripped of its punctuation
const queries = withoutDupes([lowerQuery, stripOuterNonWord(lowerQuery)]);
for (const candidate of queries) {
const idx = lowerText.indexOf(candidate);
if (idx !== -1) {
matchStart = idx;
matchLength = candidate.length;
break;
}
}
const terms = withoutDupes(
lowerQuery.split(/\s+/).reduce((all: string[], term) => {
all.push(term, stripNonWord(term));
return all;
}, [])
);
// literal matches first, so an exact hit on a later term beats a
// prefix hit on an earlier one
for (const minLength of [Infinity, MIN_PREFIX]) {
if (matchStart !== -1) {
break;
}
for (const term of terms) {
const match = this.findTerm(
lowerText,
term,
Math.min(minLength, term.length)
);
if (match) {
matchStart = match.start;
matchLength = match.length;
break;
}
}
}
if (matchStart === -1) {
matchStart = 0;
matchLength = 0;
}
return {
typeName: result.contact?.name || 'Unknown',
color: BADGE_COLOR,
fullText: text,
matchStart,
matchLength,
contact: result.contact,
ticket: result.ticket,
event: result.event,
query: query.trim()
};
}
/**
* Unlike the base (which anchors the match near the start and lets CSS
* clip the tail), message snippets are windowed to roughly center the
* match, with ellipses marking clipped context on either side.
*/
protected renderMatchText(result: TicketSearchResult): TemplateResult {
const text = result.fullText.replace(/\n/g, ' ');
const { matchStart, matchLength } = result;
if (matchLength === 0) {
return html`${text}`;
}
const matchEnd = matchStart + matchLength;
const highlight = (start: number, end: number) =>
html`${start > 0 ? '…' : ''}${text.slice(
start,
matchStart
)}<mark>${text.slice(matchStart, matchEnd)}</mark>${text.slice(
matchEnd,
end
)}${end < text.length ? '…' : ''}`;
if (text.length <= SNIPPET_BUDGET) {
return highlight(0, text.length);
}
// center the match in the character budget, shifting the window back
// into range when the match sits near either end of the text
const context = Math.max(0, SNIPPET_BUDGET - matchLength);
let start = matchStart - Math.floor(context / 2);
let end = matchEnd + Math.ceil(context / 2);
if (start < 0) {
end = Math.min(text.length, end - start);
start = 0;
}
if (end > text.length) {
start = Math.max(0, start - (end - text.length));
end = text.length;
}
return highlight(start, end);
}
protected renderResultContent(result: TicketSearchResult): TemplateResult {
return html`
${super.renderResultContent(result)}
<div class="result-date">
<temba-date
value=${result.event?.created_on}
display="duration"
></temba-date>
</div>
`;
}
}
|