@sparkvault/sdk-js 15.11.7 → 15.11.9

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.
package/dist/index.mjs CHANGED
@@ -12339,13 +12339,16 @@ function requireSendId(sendId) {
12339
12339
  * for, what a domain needs ready before the product may select it, and the
12340
12340
  * fixed local part it writes from (null when it varies: a help desk team
12341
12341
  * writes from its alias). Every descriptor and array frozen.
12342
+ *
12343
+ * The help desk selects a domain once its web is ready: its portal moves
12344
+ * there at once, and its mail once the domain also sends and receives.
12342
12345
  */
12343
12346
  const MAIL_PRODUCTS = Object.freeze({
12344
12347
  help_desk: Object.freeze({
12345
12348
  key: 'help_desk',
12346
12349
  domain: SPARKVAULT_HELP_DESK_DOMAIN,
12347
12350
  capabilities: Object.freeze(['web', 'sending', 'receiving']),
12348
- selectable: Object.freeze(['sending', 'web']),
12351
+ selectable: Object.freeze(['web']),
12349
12352
  fromLocalPart: null
12350
12353
  }),
12351
12354
  notify: Object.freeze({
@@ -13255,6 +13258,24 @@ const CUSTOMER_UPLOAD_REFUSALS = Object.freeze({
13255
13258
  UPLOADS_OFF: 'customer_uploads_off'
13256
13259
  });
13257
13260
 
13261
+ /** The longest subject, in characters. */
13262
+ const MAX_SUBJECT_LEN = 200;
13263
+
13264
+ /**
13265
+ * The largest message body, in UTF-8 bytes of its safe markup. A body's stored
13266
+ * written form can be longer, so it is read back with
13267
+ * `parseSafeMarkup(stored, { maxBytes: serializedBound(MAX_BODY_BYTES) })`
13268
+ * (@sparkvault/app-sdk/safe-markup).
13269
+ */
13270
+ const MAX_BODY_BYTES = 65536;
13271
+
13272
+ /**
13273
+ * What a customer's message over MAX_BODY_BYTES reads, word for word in the
13274
+ * portal's message editor and the SDK's support form: neither sends it, and
13275
+ * both keep every character for the customer to shorten.
13276
+ */
13277
+ const MESSAGE_TOO_LONG_COPY = 'This message is too long to send. Shorten it and try again.';
13278
+
13258
13279
  /** The most attachments one message carries. */
13259
13280
  const MAX_ATTACHMENTS_PER_MESSAGE = 20;
13260
13281
 
@@ -17133,6 +17154,23 @@ function lastExtension(name) {
17133
17154
  return shownExtension(closeUpSpaces(name));
17134
17155
  }
17135
17156
 
17157
+ /**
17158
+ * The longest prefix of `text` within `maxLength` UTF-16 units that splits no
17159
+ * character.
17160
+ *
17161
+ * @param {string} text
17162
+ * @param {number} maxLength
17163
+ * @returns {string}
17164
+ */
17165
+ function truncateToLength(text, maxLength) {
17166
+ let truncated = '';
17167
+ for (const character of text) {
17168
+ if (truncated.length + character.length > maxLength) break;
17169
+ truncated += character;
17170
+ }
17171
+ return truncated;
17172
+ }
17173
+
17136
17174
  /**
17137
17175
  * What a help-desk attachment may be named and what its first bytes may not
17138
17176
  * be: the one list of the files a ticket accepts, beside the one list of
@@ -17780,10 +17818,10 @@ function hostedUrl(host, path) {
17780
17818
  return null;
17781
17819
  }
17782
17820
  }
17783
- /** Whether a field holds text of the customer's own: anything but nothing or what the host prefilled. */
17821
+ /** Whether a field holds text of the customer's own: anything but nothing or what the form was filled with. */
17784
17822
  function edited(input, prefill) {
17785
17823
  const text = input.value.trim();
17786
- return text !== '' && text !== (prefill ?? '').trim();
17824
+ return text !== '' && text !== prefill.trim();
17787
17825
  }
17788
17826
  function companySignInUrl(value) {
17789
17827
  try {
@@ -17874,9 +17912,11 @@ function mountHelpDeskForm(client, options) {
17874
17912
  const firstName = field('First name', 'given_name');
17875
17913
  const lastName = field('Last name (optional)', 'family_name');
17876
17914
  const fields = [subject, description, email, code, name, firstName, lastName];
17877
- subject.input.maxLength = 200;
17878
- subject.input.value = options.subject ?? '';
17879
- description.input.value = options.body ?? '';
17915
+ // What the host, or the support controller, filled the form with: text the customer did not write.
17916
+ const prefill = { subject: options.subject ?? '', body: options.body ?? '' };
17917
+ subject.input.maxLength = MAX_SUBJECT_LEN;
17918
+ subject.input.value = prefill.subject;
17919
+ description.input.value = prefill.body;
17880
17920
  email.input.type = 'email';
17881
17921
  email.input.autocomplete = 'email';
17882
17922
  code.input.inputMode = 'numeric';
@@ -18163,9 +18203,23 @@ function mountHelpDeskForm(client, options) {
18163
18203
  if (completed)
18164
18204
  return false;
18165
18205
  return (pending && state !== 'authenticating') || uncertainSubmission !== null || challenge !== null
18166
- || edited(subject.input, options.subject) || edited(description.input, options.body)
18206
+ || edited(subject.input, prefill.subject) || edited(description.input, prefill.body)
18167
18207
  || attachments?.hasEntries === true;
18168
18208
  }
18209
+ /** The field a composition the ticket write would refuse is refused under, with what it reads there; null for one it takes. */
18210
+ function refusal(subjectText, body) {
18211
+ if (!subjectText)
18212
+ return [subject, 'Add a subject.'];
18213
+ if (subjectText.length > MAX_SUBJECT_LEN)
18214
+ return [subject, `Keep the subject to ${MAX_SUBJECT_LEN} characters.`];
18215
+ if (!body.trim())
18216
+ return [description, 'Describe what you need help with.'];
18217
+ // The limit counts UTF-8 bytes, so a message of fewer characters can be over it;
18218
+ // a message of more UTF-16 units than that is over it without being encoded.
18219
+ if (body.length > MAX_BODY_BYTES || new TextEncoder().encode(body).length > MAX_BODY_BYTES)
18220
+ return [description, MESSAGE_TOO_LONG_COPY];
18221
+ return null;
18222
+ }
18169
18223
  function showDone(ticket, customer) {
18170
18224
  const section = document.createElement('section');
18171
18225
  section.className = 'svhd-done';
@@ -18397,10 +18451,11 @@ function mountHelpDeskForm(client, options) {
18397
18451
  subject.input.value = subjectText;
18398
18452
  description.input.value = body;
18399
18453
  }
18400
- if (!subjectText || subjectText.length > 200 || !body.trim()) {
18401
- const refused = !subjectText || subjectText.length > 200 ? subject : description;
18402
- setFieldError(refused, !subjectText ? 'Add a subject.' : subjectText.length > 200 ? 'Keep the subject to 200 characters.' : 'Describe what you need help with.');
18403
- refused.input.focus();
18454
+ const refused = refusal(subjectText, body);
18455
+ if (refused) {
18456
+ const [entry, text] = refused;
18457
+ setFieldError(entry, text);
18458
+ entry.input.focus();
18404
18459
  return;
18405
18460
  }
18406
18461
  let widgetSignIn;
@@ -18712,7 +18767,24 @@ function mountHelpDeskForm(client, options) {
18712
18767
  setState('editing');
18713
18768
  },
18714
18769
  getState: () => state,
18715
- getComposition: safeComposition,
18770
+ restoreComposition(composition) {
18771
+ if (destroyed)
18772
+ return;
18773
+ subject.input.value = composition.subject;
18774
+ description.input.value = composition.body;
18775
+ },
18776
+ refill(composition) {
18777
+ if (destroyed || safeComposition() === null)
18778
+ return false;
18779
+ for (const [entry, key] of [[subject, 'subject'], [description, 'body']]) {
18780
+ if (!edited(entry.input, prefill[key])) {
18781
+ entry.input.value = composition[key];
18782
+ clearFieldError(entry);
18783
+ }
18784
+ prefill[key] = composition[key];
18785
+ }
18786
+ return true;
18787
+ },
18716
18788
  resumeAfterAccess() {
18717
18789
  if (!destroyed && !pending && !completed && uncertainSubmission === null)
18718
18790
  message('');
@@ -19678,6 +19750,58 @@ function createSupportLauncher(options, open) {
19678
19750
  };
19679
19751
  }
19680
19752
 
19753
+ /** A title written with a period: in "Dear Mr. Smith," that period ends no sentence. */
19754
+ const HONORIFIC = String.raw `(?:Mr|Mrs|Ms|Mx|Dr|Prof)\.`;
19755
+ /**
19756
+ * Where a sentence ends: after a period, exclamation or question mark that
19757
+ * whitespace follows. An honorific's period is matched first, whole, and left
19758
+ * as it is.
19759
+ */
19760
+ const SENTENCE_END = new RegExp(String.raw `\b${HONORIFIC}(?=\s)|([.!?])(?=\s)`, 'g');
19761
+ /** The word a greeting opens with, whole: "Hi", "Hello", "Good morning", "Morning". */
19762
+ const SALUTATION = /^(?:hi|hello|hey|hiya|howdy|greetings|dear|good\s+day|(?:good\s+)?(?:morning|afternoon|evening))(?!\p{L})/iu;
19763
+ /** The few words greetings use for whom they address. */
19764
+ const ADDRESS_WORDS = 'again|there|team|all|everyone|everybody|folks|guys|support|customer|service|sir|madam|or|and';
19765
+ /**
19766
+ * What may follow the salutation in a greeting: up to four words of address,
19767
+ * each an honorific, a name or one of the few words greetings use, in lower
19768
+ * case or capitals ("Mr. Smith", "Privacy Bee team", "there", "AGAIN", "Sir
19769
+ * or Madam"), then anything but letters and digits: punctuation, symbols and
19770
+ * emoji. The words of an issue ("Hi, I can't sign in", "Hey refund please",
19771
+ * "HELLO I CANT LOGIN") are none of these, so a sentence holding them is the
19772
+ * issue. A name is a capital and lowercase letters, again for each capital of
19773
+ * one like PayPal or McKenzie, so a word in capitals is shouted, not a name.
19774
+ * Case-sensitive, so that case tells a name: a case-insensitive \p{Lu} matches
19775
+ * every letter. A name's apostrophes and hyphens only join letters, so a word
19776
+ * of address ends in a letter or an honorific's period, and the closing run
19777
+ * never competes with it for more than that one period; that keeps the read
19778
+ * linear.
19779
+ */
19780
+ const ADDRESS = new RegExp(String.raw `^(?:[\s,/&]+(?:${HONORIFIC}|${ADDRESS_WORDS}|${ADDRESS_WORDS.toUpperCase()}|\p{Lu}\p{Ll}*(?:\p{Lu}\p{Ll}+)*(?:['\u2019-]\p{L}+)*)){0,4}[^\p{L}\p{N}]*$`, 'u');
19781
+ const TO_WHOM = /^to\s+whom\s+it\s+may\s+concern[^\p{L}\p{N}]*$/iu;
19782
+ const LETTER = /\p{L}/u;
19783
+ /** A line or sentence that only greets: "Hi there,", "Good morning team!", "Dear Sir or Madam,", "To whom it may concern:". */
19784
+ function isGreeting(sentence) {
19785
+ const salutation = SALUTATION.exec(sentence);
19786
+ return salutation ? ADDRESS.test(sentence.slice(salutation[0].length)) : TO_WHOM.test(sentence);
19787
+ }
19788
+ /**
19789
+ * The subject a support issue prefills: its first line or sentence that holds
19790
+ * a word and says more than a greeting (its first one when none does),
19791
+ * shortened at a word boundary with an ellipsis to fit MAX_SUBJECT_LEN. The
19792
+ * message carries the whole issue, so the subject only needs to name it.
19793
+ */
19794
+ function issueSubject(issue) {
19795
+ const sentences = issue.replace(SENTENCE_END, (match, end) => end ? `${end}\n` : match)
19796
+ .split('\n').map((sentence) => sentence.trim()).filter(Boolean);
19797
+ const subject = sentences.find((sentence) => LETTER.test(sentence) && !isGreeting(sentence)) ?? sentences[0] ?? '';
19798
+ if (subject.length <= MAX_SUBJECT_LEN)
19799
+ return subject;
19800
+ const kept = truncateToLength(subject, MAX_SUBJECT_LEN - 1);
19801
+ const atWord = /\s/u.test(subject.charAt(kept.length)) ? kept : kept.replace(/\s+\S*$/u, '');
19802
+ return `${atWord.trimEnd()}…`;
19803
+ }
19804
+
19681
19805
  /** The support journey's only skin. Customer content stays in the product register. */
19682
19806
  function injectHelpDeskSupportStyles() {
19683
19807
  if (document.getElementById('sparkvault-help-desk-support-styles'))
@@ -20849,6 +20973,8 @@ class SupportView {
20849
20973
  }
20850
20974
  }
20851
20975
 
20976
+ /** What contact starts from: the issue's subject and, as the message, every character of the issue. */
20977
+ function composeIssue(issue) { return { subject: issueSubject(issue), body: issue }; }
20852
20978
  function notify(callback, value) {
20853
20979
  try {
20854
20980
  void Promise.resolve(callback?.(value)).catch(() => undefined);
@@ -20870,8 +20996,15 @@ function createHelpDeskSupport(config, options) {
20870
20996
  let kbConfig = null;
20871
20997
  let contact = null;
20872
20998
  let contactCustomer = null;
20873
- let contactOpening = false;
20999
+ // The sequence of the contact opening in flight. It blocks another opening
21000
+ // only while it is the current sequence, so whatever supersedes it frees it.
21001
+ let contactOpening = null;
20874
21002
  let pendingComposition = null;
21003
+ // The issue, trimmed, that the held composition (the mounted form, or the one
21004
+ // a re-authorized form will take) was last filled from: '' for one filled from
21005
+ // none. A re-authorized form is filled from it too, so the text it carries is
21006
+ // the customer's own, or not, as it was in the form it came from.
21007
+ let composedFrom = '';
20875
21008
  let nativeIdentity = null;
20876
21009
  let switchedAccount = false;
20877
21010
  let attempt = null;
@@ -20896,10 +21029,10 @@ function createHelpDeskSupport(config, options) {
20896
21029
  onBack: () => {
20897
21030
  if (destroyed || state.navigationLocked || state.identityPending)
20898
21031
  return;
20899
- if (state.busy) {
21032
+ // A held form shows at once while contact still checks access behind it,
21033
+ // so a contact opening in flight is cancelled even when nothing is busy.
21034
+ if (state.busy || contactOpening === sequence)
20900
21035
  sequence += 1;
20901
- contactOpening = false;
20902
- }
20903
21036
  update({ busy: false });
20904
21037
  if (state.stage === 'feedback')
20905
21038
  update({ stage: 'article', error: undefined });
@@ -20926,30 +21059,14 @@ function createHelpDeskSupport(config, options) {
20926
21059
  if (destroyed || !state.article)
20927
21060
  return;
20928
21061
  void attempt?.then((active) => active?.solved());
20929
- contact?.destroy();
20930
- contact = null;
20931
- contactCustomer = null;
20932
- cancelArticleAssets?.();
20933
- cancelArticleAssets = null;
21062
+ endJourney();
20934
21063
  minimize();
20935
21064
  view.showToast('We’re happy we could help.');
20936
- sequence += 1;
20937
- attempt = null;
20938
- results = [];
20939
21065
  state = { visible: false, stage: 'issue', deskName: state.deskName, issue: '', position: state.position };
20940
21066
  update({});
20941
21067
  },
20942
21068
  onRestart: () => {
20943
- contact?.destroy();
20944
- contact = null;
20945
- contactCustomer = null;
20946
- cancelArticleAssets?.();
20947
- cancelArticleAssets = null;
20948
- sequence += 1;
20949
- attempt = null;
20950
- results = [];
20951
- requestedArticle = null;
20952
- pendingComposition = null;
21069
+ endJourney();
20953
21070
  update({ stage: 'issue', issue: '', results: [], article: undefined, receipt: undefined, error: undefined, busy: false });
20954
21071
  },
20955
21072
  onRetry: () => {
@@ -21011,7 +21128,7 @@ function createHelpDeskSupport(config, options) {
21011
21128
  if (destroyed)
21012
21129
  return;
21013
21130
  if (state.stage === 'issue' && !state.issue && input.issue)
21014
- state.issue = input.issue.slice(0, 12000);
21131
+ state.issue = input.issue;
21015
21132
  update({ visible: true });
21016
21133
  try {
21017
21134
  await connect();
@@ -21025,6 +21142,19 @@ function createHelpDeskSupport(config, options) {
21025
21142
  }
21026
21143
  }
21027
21144
  function minimize() { update({ visible: false }); }
21145
+ /** Forget a finished journey: its form and the files it authorized, any composition held for it, its suggestions and article. */
21146
+ function endJourney() {
21147
+ contact?.destroy();
21148
+ contact = null;
21149
+ contactCustomer = null;
21150
+ cancelArticleAssets?.();
21151
+ cancelArticleAssets = null;
21152
+ sequence += 1;
21153
+ attempt = null;
21154
+ results = [];
21155
+ requestedArticle = null;
21156
+ pendingComposition = null;
21157
+ }
21028
21158
  function signIn(input = {}) {
21029
21159
  if (!client)
21030
21160
  return Promise.reject(new Error('Support is still loading. Please try again.'));
@@ -21057,7 +21187,7 @@ function createHelpDeskSupport(config, options) {
21057
21187
  if (destroyed || !issue.trim())
21058
21188
  return;
21059
21189
  const ownSequence = ++sequence;
21060
- update({ stage: 'results', issue: issue.slice(0, 12000), busy: true, error: undefined, results: [], article: undefined, kbState: undefined });
21190
+ update({ stage: 'results', issue, busy: true, error: undefined, results: [], article: undefined, kbState: undefined });
21061
21191
  results = [];
21062
21192
  try {
21063
21193
  const active = await connect();
@@ -21183,7 +21313,9 @@ function createHelpDeskSupport(config, options) {
21183
21313
  let activeAttempt = null;
21184
21314
  contact = mountHelpDeskForm(client, {
21185
21315
  target: view.contactTarget,
21186
- ...composition,
21316
+ // Filled with the issue its composition came from, so text the customer
21317
+ // wrote in an earlier form and carried here stays their own.
21318
+ ...composeIssue(composedFrom),
21187
21319
  attachments: options.attachments,
21188
21320
  widgetIdentity: {
21189
21321
  customer,
@@ -21209,6 +21341,9 @@ function createHelpDeskSupport(config, options) {
21209
21341
  onSuccess: (ticket) => receipt(ticket),
21210
21342
  onStateChange: (next) => update({ navigationLocked: ['authenticating', 'submitting', 'uncertain'].includes(next) }),
21211
21343
  });
21344
+ contact.restoreComposition(composition);
21345
+ // A composition carried across re-authorization was filled from the issue as it read then.
21346
+ followIssue();
21212
21347
  const activeForm = contact;
21213
21348
  void attempt?.then((active) => {
21214
21349
  // Keep the mutable callback small: an analytics response arriving late
@@ -21218,9 +21353,23 @@ function createHelpDeskSupport(config, options) {
21218
21353
  });
21219
21354
  view.focusContact();
21220
21355
  }
21356
+ /**
21357
+ * Bring the mounted form to the issue as the customer last wrote it: an issue
21358
+ * rewritten since the form was filled fills each field they have not written
21359
+ * in, and text they wrote in the form stays theirs (`refill`). An unchanged
21360
+ * or emptied issue says nothing new.
21361
+ */
21362
+ function followIssue() {
21363
+ const issue = state.issue.trim();
21364
+ if (!contact || !issue || issue === composedFrom)
21365
+ return;
21366
+ if (contact.refill(composeIssue(state.issue)))
21367
+ composedFrom = issue;
21368
+ }
21221
21369
  async function showContact(reason) {
21222
- if (destroyed || contactOpening)
21370
+ if (destroyed || contactOpening === sequence)
21223
21371
  return;
21372
+ followIssue();
21224
21373
  cancelArticleAssets?.();
21225
21374
  cancelArticleAssets = null;
21226
21375
  if (reason === 'direct' && state.kbState === 'unavailable' && !attempt) {
@@ -21229,8 +21378,8 @@ function createHelpDeskSupport(config, options) {
21229
21378
  if (reason !== 'direct')
21230
21379
  void attempt?.then((active) => active?.contactRequested(reason));
21231
21380
  update({ stage: 'contact', busy: !contact || state.ticketAccessRequired, error: undefined });
21232
- contactOpening = true;
21233
21381
  const ownSequence = ++sequence;
21382
+ contactOpening = ownSequence;
21234
21383
  try {
21235
21384
  const active = await connect();
21236
21385
  await active.refreshTicketAccessPolicy();
@@ -21253,7 +21402,9 @@ function createHelpDeskSupport(config, options) {
21253
21402
  if (destroyed || ownSequence !== sequence)
21254
21403
  return;
21255
21404
  update({ busy: false, ticketAccessRequired: false, ticketAccessMode: undefined });
21256
- mountContact(pendingComposition ?? { subject: state.issue.trim().split(/\n|(?<=[.!?])\s/)[0].slice(0, 200), body: state.issue }, customer);
21405
+ if (!pendingComposition)
21406
+ composedFrom = state.issue.trim();
21407
+ mountContact(pendingComposition ?? composeIssue(state.issue), customer);
21257
21408
  pendingComposition = null;
21258
21409
  }
21259
21410
  catch (value) {
@@ -21267,8 +21418,8 @@ function createHelpDeskSupport(config, options) {
21267
21418
  notify(options.onError, error);
21268
21419
  }
21269
21420
  finally {
21270
- if (ownSequence === sequence)
21271
- contactOpening = false;
21421
+ if (contactOpening === ownSequence)
21422
+ contactOpening = null;
21272
21423
  }
21273
21424
  }
21274
21425
  async function unlockTicketAccess(password) {
@@ -26178,7 +26329,7 @@ class SparkVault {
26178
26329
  }
26179
26330
  /** Get the SDK version */
26180
26331
  static get version() {
26181
- return '15.11.7';
26332
+ return '15.11.9';
26182
26333
  }
26183
26334
  }
26184
26335
  if (typeof window !== 'undefined') {