@sparkvault/sdk-js 15.11.8 → 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.cjs CHANGED
@@ -13262,6 +13262,24 @@ const CUSTOMER_UPLOAD_REFUSALS = Object.freeze({
13262
13262
  UPLOADS_OFF: 'customer_uploads_off'
13263
13263
  });
13264
13264
 
13265
+ /** The longest subject, in characters. */
13266
+ const MAX_SUBJECT_LEN = 200;
13267
+
13268
+ /**
13269
+ * The largest message body, in UTF-8 bytes of its safe markup. A body's stored
13270
+ * written form can be longer, so it is read back with
13271
+ * `parseSafeMarkup(stored, { maxBytes: serializedBound(MAX_BODY_BYTES) })`
13272
+ * (@sparkvault/app-sdk/safe-markup).
13273
+ */
13274
+ const MAX_BODY_BYTES = 65536;
13275
+
13276
+ /**
13277
+ * What a customer's message over MAX_BODY_BYTES reads, word for word in the
13278
+ * portal's message editor and the SDK's support form: neither sends it, and
13279
+ * both keep every character for the customer to shorten.
13280
+ */
13281
+ const MESSAGE_TOO_LONG_COPY = 'This message is too long to send. Shorten it and try again.';
13282
+
13265
13283
  /** The most attachments one message carries. */
13266
13284
  const MAX_ATTACHMENTS_PER_MESSAGE = 20;
13267
13285
 
@@ -17140,6 +17158,23 @@ function lastExtension(name) {
17140
17158
  return shownExtension(closeUpSpaces(name));
17141
17159
  }
17142
17160
 
17161
+ /**
17162
+ * The longest prefix of `text` within `maxLength` UTF-16 units that splits no
17163
+ * character.
17164
+ *
17165
+ * @param {string} text
17166
+ * @param {number} maxLength
17167
+ * @returns {string}
17168
+ */
17169
+ function truncateToLength(text, maxLength) {
17170
+ let truncated = '';
17171
+ for (const character of text) {
17172
+ if (truncated.length + character.length > maxLength) break;
17173
+ truncated += character;
17174
+ }
17175
+ return truncated;
17176
+ }
17177
+
17143
17178
  /**
17144
17179
  * What a help-desk attachment may be named and what its first bytes may not
17145
17180
  * be: the one list of the files a ticket accepts, beside the one list of
@@ -17787,10 +17822,10 @@ function hostedUrl(host, path) {
17787
17822
  return null;
17788
17823
  }
17789
17824
  }
17790
- /** Whether a field holds text of the customer's own: anything but nothing or what the host prefilled. */
17825
+ /** Whether a field holds text of the customer's own: anything but nothing or what the form was filled with. */
17791
17826
  function edited(input, prefill) {
17792
17827
  const text = input.value.trim();
17793
- return text !== '' && text !== (prefill ?? '').trim();
17828
+ return text !== '' && text !== prefill.trim();
17794
17829
  }
17795
17830
  function companySignInUrl(value) {
17796
17831
  try {
@@ -17881,9 +17916,11 @@ function mountHelpDeskForm(client, options) {
17881
17916
  const firstName = field('First name', 'given_name');
17882
17917
  const lastName = field('Last name (optional)', 'family_name');
17883
17918
  const fields = [subject, description, email, code, name, firstName, lastName];
17884
- subject.input.maxLength = 200;
17885
- subject.input.value = options.subject ?? '';
17886
- description.input.value = options.body ?? '';
17919
+ // What the host, or the support controller, filled the form with: text the customer did not write.
17920
+ const prefill = { subject: options.subject ?? '', body: options.body ?? '' };
17921
+ subject.input.maxLength = MAX_SUBJECT_LEN;
17922
+ subject.input.value = prefill.subject;
17923
+ description.input.value = prefill.body;
17887
17924
  email.input.type = 'email';
17888
17925
  email.input.autocomplete = 'email';
17889
17926
  code.input.inputMode = 'numeric';
@@ -18170,9 +18207,23 @@ function mountHelpDeskForm(client, options) {
18170
18207
  if (completed)
18171
18208
  return false;
18172
18209
  return (pending && state !== 'authenticating') || uncertainSubmission !== null || challenge !== null
18173
- || edited(subject.input, options.subject) || edited(description.input, options.body)
18210
+ || edited(subject.input, prefill.subject) || edited(description.input, prefill.body)
18174
18211
  || attachments?.hasEntries === true;
18175
18212
  }
18213
+ /** The field a composition the ticket write would refuse is refused under, with what it reads there; null for one it takes. */
18214
+ function refusal(subjectText, body) {
18215
+ if (!subjectText)
18216
+ return [subject, 'Add a subject.'];
18217
+ if (subjectText.length > MAX_SUBJECT_LEN)
18218
+ return [subject, `Keep the subject to ${MAX_SUBJECT_LEN} characters.`];
18219
+ if (!body.trim())
18220
+ return [description, 'Describe what you need help with.'];
18221
+ // The limit counts UTF-8 bytes, so a message of fewer characters can be over it;
18222
+ // a message of more UTF-16 units than that is over it without being encoded.
18223
+ if (body.length > MAX_BODY_BYTES || new TextEncoder().encode(body).length > MAX_BODY_BYTES)
18224
+ return [description, MESSAGE_TOO_LONG_COPY];
18225
+ return null;
18226
+ }
18176
18227
  function showDone(ticket, customer) {
18177
18228
  const section = document.createElement('section');
18178
18229
  section.className = 'svhd-done';
@@ -18404,10 +18455,11 @@ function mountHelpDeskForm(client, options) {
18404
18455
  subject.input.value = subjectText;
18405
18456
  description.input.value = body;
18406
18457
  }
18407
- if (!subjectText || subjectText.length > 200 || !body.trim()) {
18408
- const refused = !subjectText || subjectText.length > 200 ? subject : description;
18409
- setFieldError(refused, !subjectText ? 'Add a subject.' : subjectText.length > 200 ? 'Keep the subject to 200 characters.' : 'Describe what you need help with.');
18410
- refused.input.focus();
18458
+ const refused = refusal(subjectText, body);
18459
+ if (refused) {
18460
+ const [entry, text] = refused;
18461
+ setFieldError(entry, text);
18462
+ entry.input.focus();
18411
18463
  return;
18412
18464
  }
18413
18465
  let widgetSignIn;
@@ -18719,7 +18771,24 @@ function mountHelpDeskForm(client, options) {
18719
18771
  setState('editing');
18720
18772
  },
18721
18773
  getState: () => state,
18722
- getComposition: safeComposition,
18774
+ restoreComposition(composition) {
18775
+ if (destroyed)
18776
+ return;
18777
+ subject.input.value = composition.subject;
18778
+ description.input.value = composition.body;
18779
+ },
18780
+ refill(composition) {
18781
+ if (destroyed || safeComposition() === null)
18782
+ return false;
18783
+ for (const [entry, key] of [[subject, 'subject'], [description, 'body']]) {
18784
+ if (!edited(entry.input, prefill[key])) {
18785
+ entry.input.value = composition[key];
18786
+ clearFieldError(entry);
18787
+ }
18788
+ prefill[key] = composition[key];
18789
+ }
18790
+ return true;
18791
+ },
18723
18792
  resumeAfterAccess() {
18724
18793
  if (!destroyed && !pending && !completed && uncertainSubmission === null)
18725
18794
  message('');
@@ -19685,6 +19754,58 @@ function createSupportLauncher(options, open) {
19685
19754
  };
19686
19755
  }
19687
19756
 
19757
+ /** A title written with a period: in "Dear Mr. Smith," that period ends no sentence. */
19758
+ const HONORIFIC = String.raw `(?:Mr|Mrs|Ms|Mx|Dr|Prof)\.`;
19759
+ /**
19760
+ * Where a sentence ends: after a period, exclamation or question mark that
19761
+ * whitespace follows. An honorific's period is matched first, whole, and left
19762
+ * as it is.
19763
+ */
19764
+ const SENTENCE_END = new RegExp(String.raw `\b${HONORIFIC}(?=\s)|([.!?])(?=\s)`, 'g');
19765
+ /** The word a greeting opens with, whole: "Hi", "Hello", "Good morning", "Morning". */
19766
+ const SALUTATION = /^(?:hi|hello|hey|hiya|howdy|greetings|dear|good\s+day|(?:good\s+)?(?:morning|afternoon|evening))(?!\p{L})/iu;
19767
+ /** The few words greetings use for whom they address. */
19768
+ const ADDRESS_WORDS = 'again|there|team|all|everyone|everybody|folks|guys|support|customer|service|sir|madam|or|and';
19769
+ /**
19770
+ * What may follow the salutation in a greeting: up to four words of address,
19771
+ * each an honorific, a name or one of the few words greetings use, in lower
19772
+ * case or capitals ("Mr. Smith", "Privacy Bee team", "there", "AGAIN", "Sir
19773
+ * or Madam"), then anything but letters and digits: punctuation, symbols and
19774
+ * emoji. The words of an issue ("Hi, I can't sign in", "Hey refund please",
19775
+ * "HELLO I CANT LOGIN") are none of these, so a sentence holding them is the
19776
+ * issue. A name is a capital and lowercase letters, again for each capital of
19777
+ * one like PayPal or McKenzie, so a word in capitals is shouted, not a name.
19778
+ * Case-sensitive, so that case tells a name: a case-insensitive \p{Lu} matches
19779
+ * every letter. A name's apostrophes and hyphens only join letters, so a word
19780
+ * of address ends in a letter or an honorific's period, and the closing run
19781
+ * never competes with it for more than that one period; that keeps the read
19782
+ * linear.
19783
+ */
19784
+ 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');
19785
+ const TO_WHOM = /^to\s+whom\s+it\s+may\s+concern[^\p{L}\p{N}]*$/iu;
19786
+ const LETTER = /\p{L}/u;
19787
+ /** A line or sentence that only greets: "Hi there,", "Good morning team!", "Dear Sir or Madam,", "To whom it may concern:". */
19788
+ function isGreeting(sentence) {
19789
+ const salutation = SALUTATION.exec(sentence);
19790
+ return salutation ? ADDRESS.test(sentence.slice(salutation[0].length)) : TO_WHOM.test(sentence);
19791
+ }
19792
+ /**
19793
+ * The subject a support issue prefills: its first line or sentence that holds
19794
+ * a word and says more than a greeting (its first one when none does),
19795
+ * shortened at a word boundary with an ellipsis to fit MAX_SUBJECT_LEN. The
19796
+ * message carries the whole issue, so the subject only needs to name it.
19797
+ */
19798
+ function issueSubject(issue) {
19799
+ const sentences = issue.replace(SENTENCE_END, (match, end) => end ? `${end}\n` : match)
19800
+ .split('\n').map((sentence) => sentence.trim()).filter(Boolean);
19801
+ const subject = sentences.find((sentence) => LETTER.test(sentence) && !isGreeting(sentence)) ?? sentences[0] ?? '';
19802
+ if (subject.length <= MAX_SUBJECT_LEN)
19803
+ return subject;
19804
+ const kept = truncateToLength(subject, MAX_SUBJECT_LEN - 1);
19805
+ const atWord = /\s/u.test(subject.charAt(kept.length)) ? kept : kept.replace(/\s+\S*$/u, '');
19806
+ return `${atWord.trimEnd()}…`;
19807
+ }
19808
+
19688
19809
  /** The support journey's only skin. Customer content stays in the product register. */
19689
19810
  function injectHelpDeskSupportStyles() {
19690
19811
  if (document.getElementById('sparkvault-help-desk-support-styles'))
@@ -20856,6 +20977,8 @@ class SupportView {
20856
20977
  }
20857
20978
  }
20858
20979
 
20980
+ /** What contact starts from: the issue's subject and, as the message, every character of the issue. */
20981
+ function composeIssue(issue) { return { subject: issueSubject(issue), body: issue }; }
20859
20982
  function notify(callback, value) {
20860
20983
  try {
20861
20984
  void Promise.resolve(callback?.(value)).catch(() => undefined);
@@ -20877,8 +21000,15 @@ function createHelpDeskSupport(config, options) {
20877
21000
  let kbConfig = null;
20878
21001
  let contact = null;
20879
21002
  let contactCustomer = null;
20880
- let contactOpening = false;
21003
+ // The sequence of the contact opening in flight. It blocks another opening
21004
+ // only while it is the current sequence, so whatever supersedes it frees it.
21005
+ let contactOpening = null;
20881
21006
  let pendingComposition = null;
21007
+ // The issue, trimmed, that the held composition (the mounted form, or the one
21008
+ // a re-authorized form will take) was last filled from: '' for one filled from
21009
+ // none. A re-authorized form is filled from it too, so the text it carries is
21010
+ // the customer's own, or not, as it was in the form it came from.
21011
+ let composedFrom = '';
20882
21012
  let nativeIdentity = null;
20883
21013
  let switchedAccount = false;
20884
21014
  let attempt = null;
@@ -20903,10 +21033,10 @@ function createHelpDeskSupport(config, options) {
20903
21033
  onBack: () => {
20904
21034
  if (destroyed || state.navigationLocked || state.identityPending)
20905
21035
  return;
20906
- if (state.busy) {
21036
+ // A held form shows at once while contact still checks access behind it,
21037
+ // so a contact opening in flight is cancelled even when nothing is busy.
21038
+ if (state.busy || contactOpening === sequence)
20907
21039
  sequence += 1;
20908
- contactOpening = false;
20909
- }
20910
21040
  update({ busy: false });
20911
21041
  if (state.stage === 'feedback')
20912
21042
  update({ stage: 'article', error: undefined });
@@ -20933,30 +21063,14 @@ function createHelpDeskSupport(config, options) {
20933
21063
  if (destroyed || !state.article)
20934
21064
  return;
20935
21065
  void attempt?.then((active) => active?.solved());
20936
- contact?.destroy();
20937
- contact = null;
20938
- contactCustomer = null;
20939
- cancelArticleAssets?.();
20940
- cancelArticleAssets = null;
21066
+ endJourney();
20941
21067
  minimize();
20942
21068
  view.showToast('We’re happy we could help.');
20943
- sequence += 1;
20944
- attempt = null;
20945
- results = [];
20946
21069
  state = { visible: false, stage: 'issue', deskName: state.deskName, issue: '', position: state.position };
20947
21070
  update({});
20948
21071
  },
20949
21072
  onRestart: () => {
20950
- contact?.destroy();
20951
- contact = null;
20952
- contactCustomer = null;
20953
- cancelArticleAssets?.();
20954
- cancelArticleAssets = null;
20955
- sequence += 1;
20956
- attempt = null;
20957
- results = [];
20958
- requestedArticle = null;
20959
- pendingComposition = null;
21073
+ endJourney();
20960
21074
  update({ stage: 'issue', issue: '', results: [], article: undefined, receipt: undefined, error: undefined, busy: false });
20961
21075
  },
20962
21076
  onRetry: () => {
@@ -21018,7 +21132,7 @@ function createHelpDeskSupport(config, options) {
21018
21132
  if (destroyed)
21019
21133
  return;
21020
21134
  if (state.stage === 'issue' && !state.issue && input.issue)
21021
- state.issue = input.issue.slice(0, 12000);
21135
+ state.issue = input.issue;
21022
21136
  update({ visible: true });
21023
21137
  try {
21024
21138
  await connect();
@@ -21032,6 +21146,19 @@ function createHelpDeskSupport(config, options) {
21032
21146
  }
21033
21147
  }
21034
21148
  function minimize() { update({ visible: false }); }
21149
+ /** Forget a finished journey: its form and the files it authorized, any composition held for it, its suggestions and article. */
21150
+ function endJourney() {
21151
+ contact?.destroy();
21152
+ contact = null;
21153
+ contactCustomer = null;
21154
+ cancelArticleAssets?.();
21155
+ cancelArticleAssets = null;
21156
+ sequence += 1;
21157
+ attempt = null;
21158
+ results = [];
21159
+ requestedArticle = null;
21160
+ pendingComposition = null;
21161
+ }
21035
21162
  function signIn(input = {}) {
21036
21163
  if (!client)
21037
21164
  return Promise.reject(new Error('Support is still loading. Please try again.'));
@@ -21064,7 +21191,7 @@ function createHelpDeskSupport(config, options) {
21064
21191
  if (destroyed || !issue.trim())
21065
21192
  return;
21066
21193
  const ownSequence = ++sequence;
21067
- update({ stage: 'results', issue: issue.slice(0, 12000), busy: true, error: undefined, results: [], article: undefined, kbState: undefined });
21194
+ update({ stage: 'results', issue, busy: true, error: undefined, results: [], article: undefined, kbState: undefined });
21068
21195
  results = [];
21069
21196
  try {
21070
21197
  const active = await connect();
@@ -21190,7 +21317,9 @@ function createHelpDeskSupport(config, options) {
21190
21317
  let activeAttempt = null;
21191
21318
  contact = mountHelpDeskForm(client, {
21192
21319
  target: view.contactTarget,
21193
- ...composition,
21320
+ // Filled with the issue its composition came from, so text the customer
21321
+ // wrote in an earlier form and carried here stays their own.
21322
+ ...composeIssue(composedFrom),
21194
21323
  attachments: options.attachments,
21195
21324
  widgetIdentity: {
21196
21325
  customer,
@@ -21216,6 +21345,9 @@ function createHelpDeskSupport(config, options) {
21216
21345
  onSuccess: (ticket) => receipt(ticket),
21217
21346
  onStateChange: (next) => update({ navigationLocked: ['authenticating', 'submitting', 'uncertain'].includes(next) }),
21218
21347
  });
21348
+ contact.restoreComposition(composition);
21349
+ // A composition carried across re-authorization was filled from the issue as it read then.
21350
+ followIssue();
21219
21351
  const activeForm = contact;
21220
21352
  void attempt?.then((active) => {
21221
21353
  // Keep the mutable callback small: an analytics response arriving late
@@ -21225,9 +21357,23 @@ function createHelpDeskSupport(config, options) {
21225
21357
  });
21226
21358
  view.focusContact();
21227
21359
  }
21360
+ /**
21361
+ * Bring the mounted form to the issue as the customer last wrote it: an issue
21362
+ * rewritten since the form was filled fills each field they have not written
21363
+ * in, and text they wrote in the form stays theirs (`refill`). An unchanged
21364
+ * or emptied issue says nothing new.
21365
+ */
21366
+ function followIssue() {
21367
+ const issue = state.issue.trim();
21368
+ if (!contact || !issue || issue === composedFrom)
21369
+ return;
21370
+ if (contact.refill(composeIssue(state.issue)))
21371
+ composedFrom = issue;
21372
+ }
21228
21373
  async function showContact(reason) {
21229
- if (destroyed || contactOpening)
21374
+ if (destroyed || contactOpening === sequence)
21230
21375
  return;
21376
+ followIssue();
21231
21377
  cancelArticleAssets?.();
21232
21378
  cancelArticleAssets = null;
21233
21379
  if (reason === 'direct' && state.kbState === 'unavailable' && !attempt) {
@@ -21236,8 +21382,8 @@ function createHelpDeskSupport(config, options) {
21236
21382
  if (reason !== 'direct')
21237
21383
  void attempt?.then((active) => active?.contactRequested(reason));
21238
21384
  update({ stage: 'contact', busy: !contact || state.ticketAccessRequired, error: undefined });
21239
- contactOpening = true;
21240
21385
  const ownSequence = ++sequence;
21386
+ contactOpening = ownSequence;
21241
21387
  try {
21242
21388
  const active = await connect();
21243
21389
  await active.refreshTicketAccessPolicy();
@@ -21260,7 +21406,9 @@ function createHelpDeskSupport(config, options) {
21260
21406
  if (destroyed || ownSequence !== sequence)
21261
21407
  return;
21262
21408
  update({ busy: false, ticketAccessRequired: false, ticketAccessMode: undefined });
21263
- mountContact(pendingComposition ?? { subject: state.issue.trim().split(/\n|(?<=[.!?])\s/)[0].slice(0, 200), body: state.issue }, customer);
21409
+ if (!pendingComposition)
21410
+ composedFrom = state.issue.trim();
21411
+ mountContact(pendingComposition ?? composeIssue(state.issue), customer);
21264
21412
  pendingComposition = null;
21265
21413
  }
21266
21414
  catch (value) {
@@ -21274,8 +21422,8 @@ function createHelpDeskSupport(config, options) {
21274
21422
  notify(options.onError, error);
21275
21423
  }
21276
21424
  finally {
21277
- if (ownSequence === sequence)
21278
- contactOpening = false;
21425
+ if (contactOpening === ownSequence)
21426
+ contactOpening = null;
21279
21427
  }
21280
21428
  }
21281
21429
  async function unlockTicketAccess(password) {
@@ -26185,7 +26333,7 @@ class SparkVault {
26185
26333
  }
26186
26334
  /** Get the SDK version */
26187
26335
  static get version() {
26188
- return '15.11.8';
26336
+ return '15.11.9';
26189
26337
  }
26190
26338
  }
26191
26339
  if (typeof window !== 'undefined') {