@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.cjs CHANGED
@@ -12343,13 +12343,16 @@ function requireSendId(sendId) {
12343
12343
  * for, what a domain needs ready before the product may select it, and the
12344
12344
  * fixed local part it writes from (null when it varies: a help desk team
12345
12345
  * writes from its alias). Every descriptor and array frozen.
12346
+ *
12347
+ * The help desk selects a domain once its web is ready: its portal moves
12348
+ * there at once, and its mail once the domain also sends and receives.
12346
12349
  */
12347
12350
  const MAIL_PRODUCTS = Object.freeze({
12348
12351
  help_desk: Object.freeze({
12349
12352
  key: 'help_desk',
12350
12353
  domain: SPARKVAULT_HELP_DESK_DOMAIN,
12351
12354
  capabilities: Object.freeze(['web', 'sending', 'receiving']),
12352
- selectable: Object.freeze(['sending', 'web']),
12355
+ selectable: Object.freeze(['web']),
12353
12356
  fromLocalPart: null
12354
12357
  }),
12355
12358
  notify: Object.freeze({
@@ -13259,6 +13262,24 @@ const CUSTOMER_UPLOAD_REFUSALS = Object.freeze({
13259
13262
  UPLOADS_OFF: 'customer_uploads_off'
13260
13263
  });
13261
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
+
13262
13283
  /** The most attachments one message carries. */
13263
13284
  const MAX_ATTACHMENTS_PER_MESSAGE = 20;
13264
13285
 
@@ -17137,6 +17158,23 @@ function lastExtension(name) {
17137
17158
  return shownExtension(closeUpSpaces(name));
17138
17159
  }
17139
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
+
17140
17178
  /**
17141
17179
  * What a help-desk attachment may be named and what its first bytes may not
17142
17180
  * be: the one list of the files a ticket accepts, beside the one list of
@@ -17784,10 +17822,10 @@ function hostedUrl(host, path) {
17784
17822
  return null;
17785
17823
  }
17786
17824
  }
17787
- /** 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. */
17788
17826
  function edited(input, prefill) {
17789
17827
  const text = input.value.trim();
17790
- return text !== '' && text !== (prefill ?? '').trim();
17828
+ return text !== '' && text !== prefill.trim();
17791
17829
  }
17792
17830
  function companySignInUrl(value) {
17793
17831
  try {
@@ -17878,9 +17916,11 @@ function mountHelpDeskForm(client, options) {
17878
17916
  const firstName = field('First name', 'given_name');
17879
17917
  const lastName = field('Last name (optional)', 'family_name');
17880
17918
  const fields = [subject, description, email, code, name, firstName, lastName];
17881
- subject.input.maxLength = 200;
17882
- subject.input.value = options.subject ?? '';
17883
- 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;
17884
17924
  email.input.type = 'email';
17885
17925
  email.input.autocomplete = 'email';
17886
17926
  code.input.inputMode = 'numeric';
@@ -18167,9 +18207,23 @@ function mountHelpDeskForm(client, options) {
18167
18207
  if (completed)
18168
18208
  return false;
18169
18209
  return (pending && state !== 'authenticating') || uncertainSubmission !== null || challenge !== null
18170
- || edited(subject.input, options.subject) || edited(description.input, options.body)
18210
+ || edited(subject.input, prefill.subject) || edited(description.input, prefill.body)
18171
18211
  || attachments?.hasEntries === true;
18172
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
+ }
18173
18227
  function showDone(ticket, customer) {
18174
18228
  const section = document.createElement('section');
18175
18229
  section.className = 'svhd-done';
@@ -18401,10 +18455,11 @@ function mountHelpDeskForm(client, options) {
18401
18455
  subject.input.value = subjectText;
18402
18456
  description.input.value = body;
18403
18457
  }
18404
- if (!subjectText || subjectText.length > 200 || !body.trim()) {
18405
- const refused = !subjectText || subjectText.length > 200 ? subject : description;
18406
- setFieldError(refused, !subjectText ? 'Add a subject.' : subjectText.length > 200 ? 'Keep the subject to 200 characters.' : 'Describe what you need help with.');
18407
- 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();
18408
18463
  return;
18409
18464
  }
18410
18465
  let widgetSignIn;
@@ -18716,7 +18771,24 @@ function mountHelpDeskForm(client, options) {
18716
18771
  setState('editing');
18717
18772
  },
18718
18773
  getState: () => state,
18719
- 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
+ },
18720
18792
  resumeAfterAccess() {
18721
18793
  if (!destroyed && !pending && !completed && uncertainSubmission === null)
18722
18794
  message('');
@@ -19682,6 +19754,58 @@ function createSupportLauncher(options, open) {
19682
19754
  };
19683
19755
  }
19684
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
+
19685
19809
  /** The support journey's only skin. Customer content stays in the product register. */
19686
19810
  function injectHelpDeskSupportStyles() {
19687
19811
  if (document.getElementById('sparkvault-help-desk-support-styles'))
@@ -20853,6 +20977,8 @@ class SupportView {
20853
20977
  }
20854
20978
  }
20855
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 }; }
20856
20982
  function notify(callback, value) {
20857
20983
  try {
20858
20984
  void Promise.resolve(callback?.(value)).catch(() => undefined);
@@ -20874,8 +21000,15 @@ function createHelpDeskSupport(config, options) {
20874
21000
  let kbConfig = null;
20875
21001
  let contact = null;
20876
21002
  let contactCustomer = null;
20877
- 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;
20878
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 = '';
20879
21012
  let nativeIdentity = null;
20880
21013
  let switchedAccount = false;
20881
21014
  let attempt = null;
@@ -20900,10 +21033,10 @@ function createHelpDeskSupport(config, options) {
20900
21033
  onBack: () => {
20901
21034
  if (destroyed || state.navigationLocked || state.identityPending)
20902
21035
  return;
20903
- 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)
20904
21039
  sequence += 1;
20905
- contactOpening = false;
20906
- }
20907
21040
  update({ busy: false });
20908
21041
  if (state.stage === 'feedback')
20909
21042
  update({ stage: 'article', error: undefined });
@@ -20930,30 +21063,14 @@ function createHelpDeskSupport(config, options) {
20930
21063
  if (destroyed || !state.article)
20931
21064
  return;
20932
21065
  void attempt?.then((active) => active?.solved());
20933
- contact?.destroy();
20934
- contact = null;
20935
- contactCustomer = null;
20936
- cancelArticleAssets?.();
20937
- cancelArticleAssets = null;
21066
+ endJourney();
20938
21067
  minimize();
20939
21068
  view.showToast('We’re happy we could help.');
20940
- sequence += 1;
20941
- attempt = null;
20942
- results = [];
20943
21069
  state = { visible: false, stage: 'issue', deskName: state.deskName, issue: '', position: state.position };
20944
21070
  update({});
20945
21071
  },
20946
21072
  onRestart: () => {
20947
- contact?.destroy();
20948
- contact = null;
20949
- contactCustomer = null;
20950
- cancelArticleAssets?.();
20951
- cancelArticleAssets = null;
20952
- sequence += 1;
20953
- attempt = null;
20954
- results = [];
20955
- requestedArticle = null;
20956
- pendingComposition = null;
21073
+ endJourney();
20957
21074
  update({ stage: 'issue', issue: '', results: [], article: undefined, receipt: undefined, error: undefined, busy: false });
20958
21075
  },
20959
21076
  onRetry: () => {
@@ -21015,7 +21132,7 @@ function createHelpDeskSupport(config, options) {
21015
21132
  if (destroyed)
21016
21133
  return;
21017
21134
  if (state.stage === 'issue' && !state.issue && input.issue)
21018
- state.issue = input.issue.slice(0, 12000);
21135
+ state.issue = input.issue;
21019
21136
  update({ visible: true });
21020
21137
  try {
21021
21138
  await connect();
@@ -21029,6 +21146,19 @@ function createHelpDeskSupport(config, options) {
21029
21146
  }
21030
21147
  }
21031
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
+ }
21032
21162
  function signIn(input = {}) {
21033
21163
  if (!client)
21034
21164
  return Promise.reject(new Error('Support is still loading. Please try again.'));
@@ -21061,7 +21191,7 @@ function createHelpDeskSupport(config, options) {
21061
21191
  if (destroyed || !issue.trim())
21062
21192
  return;
21063
21193
  const ownSequence = ++sequence;
21064
- 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 });
21065
21195
  results = [];
21066
21196
  try {
21067
21197
  const active = await connect();
@@ -21187,7 +21317,9 @@ function createHelpDeskSupport(config, options) {
21187
21317
  let activeAttempt = null;
21188
21318
  contact = mountHelpDeskForm(client, {
21189
21319
  target: view.contactTarget,
21190
- ...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),
21191
21323
  attachments: options.attachments,
21192
21324
  widgetIdentity: {
21193
21325
  customer,
@@ -21213,6 +21345,9 @@ function createHelpDeskSupport(config, options) {
21213
21345
  onSuccess: (ticket) => receipt(ticket),
21214
21346
  onStateChange: (next) => update({ navigationLocked: ['authenticating', 'submitting', 'uncertain'].includes(next) }),
21215
21347
  });
21348
+ contact.restoreComposition(composition);
21349
+ // A composition carried across re-authorization was filled from the issue as it read then.
21350
+ followIssue();
21216
21351
  const activeForm = contact;
21217
21352
  void attempt?.then((active) => {
21218
21353
  // Keep the mutable callback small: an analytics response arriving late
@@ -21222,9 +21357,23 @@ function createHelpDeskSupport(config, options) {
21222
21357
  });
21223
21358
  view.focusContact();
21224
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
+ }
21225
21373
  async function showContact(reason) {
21226
- if (destroyed || contactOpening)
21374
+ if (destroyed || contactOpening === sequence)
21227
21375
  return;
21376
+ followIssue();
21228
21377
  cancelArticleAssets?.();
21229
21378
  cancelArticleAssets = null;
21230
21379
  if (reason === 'direct' && state.kbState === 'unavailable' && !attempt) {
@@ -21233,8 +21382,8 @@ function createHelpDeskSupport(config, options) {
21233
21382
  if (reason !== 'direct')
21234
21383
  void attempt?.then((active) => active?.contactRequested(reason));
21235
21384
  update({ stage: 'contact', busy: !contact || state.ticketAccessRequired, error: undefined });
21236
- contactOpening = true;
21237
21385
  const ownSequence = ++sequence;
21386
+ contactOpening = ownSequence;
21238
21387
  try {
21239
21388
  const active = await connect();
21240
21389
  await active.refreshTicketAccessPolicy();
@@ -21257,7 +21406,9 @@ function createHelpDeskSupport(config, options) {
21257
21406
  if (destroyed || ownSequence !== sequence)
21258
21407
  return;
21259
21408
  update({ busy: false, ticketAccessRequired: false, ticketAccessMode: undefined });
21260
- 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);
21261
21412
  pendingComposition = null;
21262
21413
  }
21263
21414
  catch (value) {
@@ -21271,8 +21422,8 @@ function createHelpDeskSupport(config, options) {
21271
21422
  notify(options.onError, error);
21272
21423
  }
21273
21424
  finally {
21274
- if (ownSequence === sequence)
21275
- contactOpening = false;
21425
+ if (contactOpening === ownSequence)
21426
+ contactOpening = null;
21276
21427
  }
21277
21428
  }
21278
21429
  async function unlockTicketAccess(password) {
@@ -26182,7 +26333,7 @@ class SparkVault {
26182
26333
  }
26183
26334
  /** Get the SDK version */
26184
26335
  static get version() {
26185
- return '15.11.7';
26336
+ return '15.11.9';
26186
26337
  }
26187
26338
  }
26188
26339
  if (typeof window !== 'undefined') {