@jsb188/app 1.1.11 → 1.1.13

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.
Files changed (64) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +17 -6
  3. package/src/constants/agent.ts +44 -57
  4. package/src/constants/app.ts +43 -0
  5. package/src/constants/wsActions.ts +1 -15
  6. package/src/i18n/account.ts +1 -1
  7. package/src/i18n/agent.ts +321 -96
  8. package/src/i18n/agentToolBusiness.ts +340 -0
  9. package/src/i18n/agentToolGrid.ts +121 -0
  10. package/src/i18n/agentToolWorkflow.ts +101 -0
  11. package/src/i18n/agent_note.ts +110 -0
  12. package/src/i18n/app.ts +23 -17
  13. package/src/i18n/auth.ts +25 -30
  14. package/src/i18n/billing.ts +3 -85
  15. package/src/i18n/c.ts +953 -0
  16. package/src/i18n/campaign.ts +164 -211
  17. package/src/i18n/datetime.ts +6 -0
  18. package/src/i18n/editor.ts +56 -0
  19. package/src/i18n/email_outreach_editor.ts +526 -0
  20. package/src/i18n/error.ts +430 -202
  21. package/src/i18n/field_doc_editor.ts +21 -0
  22. package/src/i18n/field_doc_programs.ts +51 -0
  23. package/src/i18n/field_docs.ts +171 -8
  24. package/src/i18n/form.ts +7 -491
  25. package/src/i18n/index.ts +23 -3
  26. package/src/i18n/log.ts +234 -39
  27. package/src/i18n/memory.ts +18 -5
  28. package/src/i18n/message.ts +1 -0
  29. package/src/i18n/note.ts +5 -3
  30. package/src/i18n/org.ts +97 -45
  31. package/src/i18n/schedule.ts +133 -0
  32. package/src/i18n/sheet.ts +105 -161
  33. package/src/i18n/sheet_formula.ts +165 -0
  34. package/src/i18n/workflow.ts +112 -11
  35. package/src/main.ts +2 -2
  36. package/src/platform.ts +89 -49
  37. package/src/types/ai.d.ts +16 -1
  38. package/src/types/app.d.ts +29 -73
  39. package/src/types/auth.d.ts +1 -1
  40. package/src/types/other.d.ts +8 -1
  41. package/src/utils/api.ts +60 -10
  42. package/src/utils/clientIP.ts +213 -0
  43. package/src/utils/color.ts +95 -0
  44. package/src/utils/csv.ts +19 -0
  45. package/src/utils/datetime.ts +207 -31
  46. package/src/utils/deferredSave.ts +89 -0
  47. package/src/utils/email.ts +50 -0
  48. package/src/utils/expression.helpers.ts +470 -0
  49. package/src/utils/expression.ts +1633 -239
  50. package/src/utils/language.ts +9 -0
  51. package/src/utils/logic.ts +9 -162
  52. package/src/utils/number.ts +30 -11
  53. package/src/utils/object.ts +69 -127
  54. package/src/utils/promise.ts +78 -7
  55. package/src/utils/queryReset.ts +110 -61
  56. package/src/utils/string.ts +80 -143
  57. package/src/utils/url_client.ts +115 -15
  58. package/src/constants/billing.ts +0 -146
  59. package/src/i18n/agentDoc.ts +0 -110
  60. package/src/types/form.d.ts +0 -101
  61. package/src/utils/billing.ts +0 -141
  62. package/src/utils/managedSessionBff.test.ts +0 -60
  63. package/src/utils/string.test.ts +0 -293
  64. package/tsconfig.json +0 -12
@@ -9,15 +9,19 @@ export function cn(...classNames: (string | boolean | undefined | null)[]) {
9
9
  return classNames.filter(Boolean).join(' ');
10
10
  }
11
11
 
12
+ /**
13
+ * Escape user-controlled text before inserting it into an HTML string.
14
+ */
15
+
16
+ export function escapeHTML(value: string): string {
17
+ return value.replace(/[&<>"']/g, (character) => `&#${character.charCodeAt(0)};`);
18
+ }
19
+
12
20
  /**
13
21
  * Shorten text to space with length limit
14
22
  */
15
23
 
16
- export function shortenToSpace(
17
- str: string,
18
- len: number = 100,
19
- ellipsis: string = '...',
20
- ): string {
24
+ export function shortenToSpace(str: string, len: number = 100, ellipsis: string = '...'): string {
21
25
  if (!str || str.length < len) {
22
26
  return str;
23
27
  }
@@ -37,10 +41,7 @@ export function shortenToSpace(
37
41
  */
38
42
 
39
43
  export function guessFirstName(str: string, len: number = 40): string {
40
- let shortened =
41
- str
42
- ?.replace(/^(a|an|the|da|de|that|there|this|than|then)\b/i, '')
43
- ?.trim() || '';
44
+ let shortened = str?.replace(/^(a|an|the|da|de|that|there|this|than|then)\b/i, '')?.trim() || '';
44
45
  const spaceIx = shortened.indexOf(' ');
45
46
 
46
47
  if (spaceIx >= 0) {
@@ -51,10 +52,7 @@ export function guessFirstName(str: string, len: number = 40): string {
51
52
  shortened = shortened.substring(0, len);
52
53
  }
53
54
 
54
- const articleReplacedEnd = shortened.replace(
55
- /\b(a|an|the|da|de|that|there|this|than|then)$/i,
56
- '',
57
- );
55
+ const articleReplacedEnd = shortened.replace(/\b(a|an|the|da|de|that|there|this|than|then)$/i, '');
58
56
 
59
57
  if (articleReplacedEnd.length < 5) {
60
58
  return shortened.trim();
@@ -76,9 +74,7 @@ export function addIndefiniteArticle(word: string) {
76
74
  } else if (/^(honest|honor|hour|heir)\b/.test(raw)) {
77
75
  // Common silent-h words use "an"
78
76
  article = 'an';
79
- } else if (
80
- /^(uni([^nmd]|$)|use|user|euro|one|once|ubiquit|ufo|ukulele)\b/.test(raw)
81
- ) {
77
+ } else if (/^(uni([^nmd]|$)|use|user|euro|one|once|ubiquit|ufo|ukulele)\b/.test(raw)) {
82
78
  // Common "you"/"w" sound starts use "a"
83
79
  article = 'a';
84
80
  } else {
@@ -129,10 +125,7 @@ export function convertToInternationalPhone(str: string) {
129
125
  * NOTE: This will convert phone-like strings to international format
130
126
  */
131
127
 
132
- export function isPhone(
133
- str: string,
134
- doNotConvertInternational: boolean = false,
135
- ) {
128
+ export function isPhone(str: string, doNotConvertInternational: boolean = false) {
136
129
  let phoneNumber: string | null = str.trim();
137
130
  if (!doNotConvertInternational) {
138
131
  phoneNumber = convertToInternationalPhone(phoneNumber);
@@ -158,8 +151,7 @@ export function passwordStrength(str: string, minLength: number = 8) {
158
151
  const hasUppercase = /[A-Z]/.test(str);
159
152
  const hasDigit = /\d/.test(str);
160
153
  const hasSpecial = /\W/.test(str);
161
- const valid =
162
- isLongEnough && hasLowercase && hasUppercase && hasDigit && hasSpecial;
154
+ const valid = isLongEnough && hasLowercase && hasUppercase && hasDigit && hasSpecial;
163
155
  // return /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*\W)[a-zA-Z\d\W]{8,}$/.test(str);
164
156
  return {
165
157
  valid,
@@ -191,9 +183,7 @@ export function getAlphanumeric(str: string, space: boolean = false) {
191
183
  * Build normalized unique list of names from raw search inputs
192
184
  */
193
185
 
194
- export function getSearchNames(
195
- namesList: (string | null | undefined)[] = [],
196
- ): string[] {
186
+ export function getSearchNames(namesList: (string | null | undefined)[] = []): string[] {
197
187
  const parsedNames = (namesList || [])
198
188
  .flatMap((s) => (s || '').split(/\s*(?:,|;|\n|\band\b)\s*/i))
199
189
  .map((s) => s.trim())
@@ -225,10 +215,7 @@ export function regexFromText(text: string) {
225
215
  return null;
226
216
  }
227
217
 
228
- return new RegExp(
229
- '\\b' + (words.length === 1 ? words[0] : '(?:' + words.join('|') + ')'),
230
- 'gi',
231
- );
218
+ return new RegExp('\\b' + (words.length === 1 ? words[0] : '(?:' + words.join('|') + ')'), 'gi');
232
219
  }
233
220
 
234
221
  /**
@@ -259,11 +246,7 @@ export function camelCaseToDash(str: string) {
259
246
  * Shorten text to a paragraph length
260
247
  */
261
248
 
262
- export function shortenToParagraph(
263
- str: string,
264
- len: number,
265
- replaceDoubleLineBreaks?: boolean,
266
- ) {
249
+ export function shortenToParagraph(str: string, len: number, replaceDoubleLineBreaks?: boolean) {
267
250
  let trimedToParagraph = str || '';
268
251
  if (replaceDoubleLineBreaks) {
269
252
  trimedToParagraph = trimedToParagraph.replace(/\n\s*\n/g, '\n');
@@ -317,10 +300,7 @@ export function isInvisibleString(str: string) {
317
300
  */
318
301
 
319
302
  type AnyLineItem = string | undefined | false | null;
320
- type BuildSingleTextConditionalLine = [
321
- string[],
322
- BuildSingleTextLine | BuildSingleTextLine[],
323
- ];
303
+ type BuildSingleTextConditionalLine = [string[], BuildSingleTextLine | BuildSingleTextLine[]];
324
304
  type BuildSingleTextConditionalLineGroup = BuildSingleTextConditionalLine[];
325
305
  interface BuildSingleTextFeatureMap {
326
306
  [key: string]: BuildSingleTextLine | BuildSingleTextLine[];
@@ -330,21 +310,15 @@ export type BuildSingleTextLine =
330
310
  | AnyLineItem
331
311
  | BuildSingleTextFeatureMap
332
312
  | BuildSingleTextConditionalLine
333
- | BuildSingleTextConditionalLineGroup;
313
+ | BuildSingleTextConditionalLineGroup
314
+ | BuildSingleTextLine[];
334
315
 
335
316
  /*
336
317
  * Check whether a buildSingleText line uses the all-features conditional tuple syntax.
337
318
  */
338
319
 
339
- function isBuildSingleTextConditionalLine(
340
- line: BuildSingleTextLine | BuildSingleTextLine[],
341
- ): line is BuildSingleTextConditionalLine {
342
- return (
343
- Array.isArray(line) &&
344
- line.length === 2 &&
345
- Array.isArray(line[0]) &&
346
- line[0].every((feature) => typeof feature === 'string')
347
- );
320
+ function isBuildSingleTextConditionalLine(line: BuildSingleTextLine | BuildSingleTextLine[]): line is BuildSingleTextConditionalLine {
321
+ return Array.isArray(line) && line.length === 2 && Array.isArray(line[0]) && line[0].every((feature) => typeof feature === 'string');
348
322
  }
349
323
 
350
324
  /*
@@ -354,21 +328,14 @@ function isBuildSingleTextConditionalLine(
354
328
  function isBuildSingleTextConditionalLineGroup(
355
329
  line: BuildSingleTextLine | BuildSingleTextLine[],
356
330
  ): line is BuildSingleTextConditionalLineGroup {
357
- return (
358
- Array.isArray(line) &&
359
- line.length > 0 &&
360
- line.every((lineItem) => isBuildSingleTextConditionalLine(lineItem))
361
- );
331
+ return Array.isArray(line) && line.length > 0 && line.every((lineItem) => isBuildSingleTextConditionalLine(lineItem));
362
332
  }
363
333
 
364
334
  /*
365
335
  * Check whether a buildSingleText requirement matches the enabled features.
366
336
  */
367
337
 
368
- function doesBuildSingleTextRequirementMatch(
369
- requirement: string,
370
- features: string[],
371
- ) {
338
+ function doesBuildSingleTextRequirementMatch(requirement: string, features: string[]) {
372
339
  const isNegatedRequirement = requirement.startsWith('!');
373
340
  const feature = isNegatedRequirement ? requirement.slice(1) : requirement;
374
341
 
@@ -384,13 +351,8 @@ function doesBuildSingleTextRequirementMatch(
384
351
  * Check whether a buildSingleText conditional line matches the enabled features.
385
352
  */
386
353
 
387
- function doesBuildSingleTextConditionalLineMatch(
388
- conditionalLine: BuildSingleTextConditionalLine,
389
- features: string[],
390
- ) {
391
- return conditionalLine[0].every((feature) =>
392
- doesBuildSingleTextRequirementMatch(feature, features),
393
- );
354
+ function doesBuildSingleTextConditionalLineMatch(conditionalLine: BuildSingleTextConditionalLine, features: string[]) {
355
+ return conditionalLine[0].every((feature) => doesBuildSingleTextRequirementMatch(feature, features));
394
356
  }
395
357
 
396
358
  /*
@@ -403,22 +365,16 @@ function getBuildSingleTextConditionalText(
403
365
  features: string[],
404
366
  ) {
405
367
  return buildSingleText(
406
- Array.isArray(conditionalLines) &&
407
- !isBuildSingleTextConditionalLine(conditionalLines)
408
- ? conditionalLines
409
- : [conditionalLines],
368
+ Array.isArray(conditionalLines) && !isBuildSingleTextConditionalLine(conditionalLines) ? conditionalLines : [conditionalLines],
410
369
  delemiter,
411
370
  features,
412
371
  );
413
372
  }
414
373
 
415
- export function buildSingleText(
416
- lines: BuildSingleTextLine[],
417
- delemiter: string = '\n',
418
- features: string[] = [],
419
- ) {
374
+ /* Build prompt text from nested sections, feature conditions, and explicit blank lines. */
375
+ export function buildSingleText(lines: BuildSingleTextLine[], delemiter: string = '\n', features: string[] = []) {
420
376
  return lines
421
- .reduce((acc, line_) => {
377
+ .reduce<string[]>((acc, line_) => {
422
378
  const line = line_ || '';
423
379
  if (isBuildSingleTextConditionalLineGroup(line)) {
424
380
  const matchedConditionalLine = line.find((conditionalLine) =>
@@ -429,11 +385,7 @@ export function buildSingleText(
429
385
  return acc;
430
386
  }
431
387
 
432
- const conditionalText = getBuildSingleTextConditionalText(
433
- matchedConditionalLine[1],
434
- delemiter,
435
- features,
436
- );
388
+ const conditionalText = getBuildSingleTextConditionalText(matchedConditionalLine[1], delemiter, features);
437
389
 
438
390
  if (conditionalText || acc[acc.length - 1] !== '') {
439
391
  acc.push(conditionalText);
@@ -446,11 +398,7 @@ export function buildSingleText(
446
398
  const [, conditionalLines] = line;
447
399
 
448
400
  if (doesBuildSingleTextConditionalLineMatch(line, features)) {
449
- const conditionalText = getBuildSingleTextConditionalText(
450
- conditionalLines,
451
- delemiter,
452
- features,
453
- );
401
+ const conditionalText = getBuildSingleTextConditionalText(conditionalLines, delemiter, features);
454
402
 
455
403
  if (conditionalText || acc[acc.length - 1] !== '') {
456
404
  acc.push(conditionalText);
@@ -459,15 +407,18 @@ export function buildSingleText(
459
407
 
460
408
  return acc;
461
409
  }
410
+ if (Array.isArray(line)) {
411
+ const nestedText = buildSingleText(line, delemiter, features);
412
+ if (nestedText || acc[acc.length - 1] !== '') {
413
+ acc.push(nestedText);
414
+ }
415
+ return acc;
416
+ }
462
417
  if (line && typeof line === 'object') {
463
418
  for (const key in line) {
464
419
  const lineItem = line[key];
465
420
  if (features.includes(key)) {
466
- const nextLines =
467
- Array.isArray(lineItem) &&
468
- !isBuildSingleTextConditionalLine(lineItem)
469
- ? lineItem
470
- : [lineItem];
421
+ const nextLines = Array.isArray(lineItem) && !isBuildSingleTextConditionalLine(lineItem) ? lineItem : [lineItem];
471
422
  acc.push(buildSingleText(nextLines, delemiter, features));
472
423
  }
473
424
  }
@@ -477,7 +428,7 @@ export function buildSingleText(
477
428
  acc.push(line);
478
429
  }
479
430
  return acc;
480
- }, [] as string[])
431
+ }, [])
481
432
  .join(delemiter)
482
433
  .trim()
483
434
  .replace(/^[ \t]+(?=[A-Za-z0-9])/gm, '');
@@ -492,11 +443,7 @@ export function buildSingleText(
492
443
  * @returns Joined string with special last separator
493
444
  */
494
445
 
495
- export function joinReadable(
496
- texts: (string | null | undefined)[],
497
- separator: string = ', ',
498
- lastSeparator: string = ' and ',
499
- ): string {
446
+ export function joinReadable(texts: (string | null | undefined)[], separator: string = ', ', lastSeparator: string = ' and '): string {
500
447
  const lastIx = texts.length - 1;
501
448
  return texts.filter(Boolean).reduce<string>((acc, text, i) => {
502
449
  return acc + (!acc ? '' : i === lastIx ? lastSeparator : separator) + text;
@@ -512,11 +459,7 @@ export function joinReadable(
512
459
  * @returns Formatted text with optional brackets
513
460
  */
514
461
 
515
- export function textWithBrackets(
516
- text: string,
517
- bracketsText?: string | string[],
518
- brackets: [string, string] = ['(', ')'],
519
- ) {
462
+ export function textWithBrackets(text: string, bracketsText?: string | string[], brackets: [string, string] = ['(', ')']) {
520
463
  let bText;
521
464
  if (Array.isArray(bracketsText)) {
522
465
  bText = bracketsText.filter(Boolean).join(' ').trim();
@@ -554,7 +497,7 @@ export function removeGeneratedTextGarbage(str: string) {
554
497
  */
555
498
 
556
499
  export function escapeCSVValue(value: string): string {
557
- if (/[",\n]/.test(value)) {
500
+ if (/[",\r\n]/.test(value)) {
558
501
  return `"${value.replace(/"/g, '""')}"`;
559
502
  }
560
503
  return value;
@@ -584,18 +527,10 @@ export function formatPhoneNumber(phone?: string | null) {
584
527
  const match = phone?.match(/(\d{3})(\d{3})(\d{4})$/);
585
528
  if (match) {
586
529
  const [, area, first, second] = match;
587
- const countryCode = phone!
588
- .substring(0, phone!.length - area.length - first.length - second.length)
589
- .trim();
590
- const normalizedCountryCode = countryCode
591
- ? countryCode.startsWith('+')
592
- ? countryCode
593
- : `+${countryCode}`
594
- : '';
530
+ const countryCode = phone!.substring(0, phone!.length - area.length - first.length - second.length).trim();
531
+ const normalizedCountryCode = countryCode ? (countryCode.startsWith('+') ? countryCode : `+${countryCode}`) : '';
595
532
  // NOTE: This does not account for non North American (+1) phone numbers
596
- return `${normalizedCountryCode}${
597
- normalizedCountryCode ? ' ' : ''
598
- }(${area}) ${first}-${second}`;
533
+ return `${normalizedCountryCode}${normalizedCountryCode ? ' ' : ''}(${area}) ${first}-${second}`;
599
534
  }
600
535
 
601
536
  return phone;
@@ -609,11 +544,7 @@ export function formatPhoneNumber(phone?: string | null) {
609
544
  * @returns An array of text chunks.
610
545
  */
611
546
 
612
- export function splitTextIntoChunks(
613
- text: string,
614
- chunkSize: number,
615
- linebreakDistance: number = 50,
616
- ): string[] {
547
+ export function splitTextIntoChunks(text: string, chunkSize: number, linebreakDistance: number = 50): string[] {
617
548
  if (!text || chunkSize <= 0 || text.length < chunkSize) {
618
549
  return [text || ''];
619
550
  }
@@ -629,11 +560,7 @@ export function splitTextIntoChunks(
629
560
  const beforeBreak = text.lastIndexOf('\n', end);
630
561
 
631
562
  let chosenBreak = -1;
632
- if (
633
- beforeBreak !== -1 &&
634
- end - beforeBreak <= linebreakDistance &&
635
- beforeBreak > start
636
- ) {
563
+ if (beforeBreak !== -1 && end - beforeBreak <= linebreakDistance && beforeBreak > start) {
637
564
  chosenBreak = beforeBreak + 1;
638
565
  }
639
566
 
@@ -688,6 +615,13 @@ export function getNthString(num: number): string {
688
615
  }
689
616
  }
690
617
 
618
+ /*
619
+ * Check whether an optional address contains details beyond the country selection.
620
+ */
621
+ export function hasAddressDetails(address: Partial<AddressObj>) {
622
+ return [address.line1, address.line2, address.city, address.state, address.postalCode].some((value) => value?.trim());
623
+ }
624
+
691
625
  /**
692
626
  * Make address text
693
627
  * @param address - Address object
@@ -712,18 +646,9 @@ export function makeAddressText(
712
646
  : address.country;
713
647
 
714
648
  if (format === 'REGION_ONLY') {
715
- return [address.city, address.state, countryName]
716
- .filter(Boolean)
717
- .join(delimiter);
649
+ return [address.city, address.state, countryName].filter(Boolean).join(delimiter);
718
650
  } else if (format === 'SINGLE_LINE') {
719
- return [
720
- address.line1,
721
- address.line2,
722
- address.city,
723
- address.state,
724
- address.postalCode,
725
- countryName,
726
- ]
651
+ return [address.line1, address.line2, address.city, address.state, address.postalCode, countryName]
727
652
  .filter(Boolean)
728
653
  .join(delimiter);
729
654
  }
@@ -732,12 +657,7 @@ export function makeAddressText(
732
657
  return [
733
658
  address.line1,
734
659
  address.line2,
735
- [
736
- [address.city, address.state].filter(Boolean).join(', '),
737
- address.postalCode,
738
- ]
739
- .filter(Boolean)
740
- .join(' '),
660
+ [[address.city, address.state].filter(Boolean).join(', '), address.postalCode].filter(Boolean).join(' '),
741
661
  countryName,
742
662
  ]
743
663
  .filter(Boolean)
@@ -749,10 +669,7 @@ export function makeAddressText(
749
669
  * @returns Formatted display name
750
670
  */
751
671
 
752
- export function getDisplayName(
753
- firstName?: string | null,
754
- lastName?: string | null,
755
- ) {
672
+ export function getDisplayName(firstName?: string | null, lastName?: string | null) {
756
673
  const fullName = buildSingleText([firstName, lastName], ' ');
757
674
  const displayName = guessFirstName(fullName, 8) || '?';
758
675
 
@@ -777,3 +694,23 @@ export function hashStringFNV1a(value: string) {
777
694
 
778
695
  return (hash >>> 0).toString(16).padStart(8, '0');
779
696
  }
697
+
698
+ /**
699
+ * Remove markdown from text, so you can send it to SMS
700
+ * @param text - Text to remove markdown from
701
+ * @returns - Text without markdown
702
+ */
703
+
704
+ export function removeMarkdownForSMS(text: string) {
705
+ return (
706
+ text
707
+ // 4. Drop leading `#`‑style headings (handles up to ######).
708
+ .replace(/^[ \t]*#{1,6}[ \t]+/gm, '')
709
+ // 1. Bold: **text** (no line‑breaks inside)
710
+ .replace(/\*\*([^\n*]+?)\*\*/g, '$1')
711
+ // 2. Italic: *text* (single asterisks only, not part of **)
712
+ .replace(/(?<!\*)\*([^\n*]+?)\*(?!\*)/g, '$1')
713
+ // 3. Italic: _text_ (underscores)
714
+ .replace(/_([^\n_]+?)_/g, '$1')
715
+ );
716
+ }
@@ -1,12 +1,15 @@
1
- type LinkType =
2
- | 'USER'
3
- | 'KAJI'
4
- | 'DATASET'
5
- | 'VOICE'
6
- | 'FEED'
7
- | 'ROOM'
8
- | 'THREAD'
9
- | 'OTHER';
1
+ type LinkType = 'USER' | 'KAJI' | 'DATASET' | 'VOICE' | 'FEED' | 'ROOM' | 'THREAD' | 'OTHER';
2
+
3
+ /*
4
+ * Format a keyboard shortcut with the command modifier used by the current
5
+ * client platform.
6
+ */
7
+ export function getClientShortcutText(key: string, shiftKey: boolean = false): string {
8
+ const platform = globalThis.navigator?.platform || globalThis.navigator?.userAgent || '';
9
+ return [/Mac|iPhone|iPod|iPad/i.test(platform) ? 'cmd' : 'ctrl', shiftKey ? 'shift' : null, key.toLowerCase()]
10
+ .filter(Boolean)
11
+ .join('+');
12
+ }
10
13
 
11
14
  // links to consider for User cards
12
15
  // instagram
@@ -22,11 +25,7 @@ type LinkType =
22
25
  * Uploaded assets
23
26
  */
24
27
 
25
- export function makeUploadsUrl(
26
- urlPath?: string | null,
27
- size?: 'small' | 'medium' | 'original',
28
- download?: boolean,
29
- ): string {
28
+ export function makeUploadsUrl(urlPath?: string | null, size?: 'small' | 'medium' | 'original', download?: boolean): string {
30
29
  if (!urlPath) {
31
30
  return '';
32
31
  }
@@ -41,9 +40,67 @@ export function makeUploadsUrl(
41
40
 
42
41
  const searchPath = searchParams.length ? `?${searchParams.join('&')}` : '';
43
42
 
43
+ // This is suppose to point to prod or dev assets server url.
44
+ // But for testing and easier-dev purposes, we're going to always point to prod.
44
45
  return 'https://assets.chiefhappiness.co/marketday/' + urlPath + searchPath;
45
46
  }
46
47
 
48
+ /*
49
+ * Resolve a private file on the production assets server.
50
+ */
51
+ export function makeSecureAssetsUrl(path?: string | null): string {
52
+ // This is suppose to point to prod or dev assets server url.
53
+ // But for testing and easier-dev purposes, we're going to always point to prod.
54
+ const base = 'https://assets.chiefhappiness.co/';
55
+ if (!path || !URL.canParse(path, base)) return '';
56
+ const url = new URL(path, base);
57
+ if (!/^https?:$/.test(url.protocol) || url.origin !== new URL(base).origin || url.username || url.password) return '';
58
+ if (!/^\/files\/\d+\/\d+\/\d+$/.test(url.pathname)) return '';
59
+ return url.href;
60
+ }
61
+
62
+ /**
63
+ * Local object URLs
64
+ */
65
+
66
+ // Hold counts and pending revocations of object URLs shared between components
67
+ const objectUrlHolds = new Map<string, number>();
68
+ const objectUrlRevokeTimers = new Map<string, ReturnType<typeof setTimeout>>();
69
+
70
+ /*
71
+ * Add one hold on a local object URL (e.g. an image preview from
72
+ * URL.createObjectURL()) and cancel a revocation scheduled by a holder that
73
+ * just released it.
74
+ */
75
+ export function retainObjectUrl(url: string) {
76
+ clearTimeout(objectUrlRevokeTimers.get(url));
77
+ objectUrlRevokeTimers.delete(url);
78
+ objectUrlHolds.set(url, (objectUrlHolds.get(url) || 0) + 1);
79
+ }
80
+
81
+ /*
82
+ * Release one hold on a local object URL and revoke it shortly after its last
83
+ * holder lets go, so a holder that unmounts in the same render as the one
84
+ * taking over (e.g. a page handing a preview to the next page) never breaks it.
85
+ */
86
+ export function releaseObjectUrl(url: string) {
87
+ const holds = (objectUrlHolds.get(url) || 0) - 1;
88
+ if (holds > 0) {
89
+ objectUrlHolds.set(url, holds);
90
+ return;
91
+ }
92
+
93
+ objectUrlHolds.delete(url);
94
+ clearTimeout(objectUrlRevokeTimers.get(url));
95
+ objectUrlRevokeTimers.set(
96
+ url,
97
+ setTimeout(() => {
98
+ objectUrlRevokeTimers.delete(url);
99
+ URL.revokeObjectURL(url);
100
+ }, 1000),
101
+ );
102
+ }
103
+
47
104
  /**
48
105
  * Make url for static assets
49
106
  */
@@ -129,9 +186,52 @@ export function locationSearchToObject(searchParams?: string) {
129
186
  */
130
187
 
131
188
  export function isChatPage() {
132
- const pathname = globalThis?.location?.pathname?.toLowerCase();
189
+ // Servers have no location; the cast keeps this type-safe without the DOM library
190
+ const pathname = (globalThis as { location?: { pathname?: string } }).location?.pathname?.toLowerCase();
133
191
  if (pathname) {
134
192
  return /^\/(?:chat|dm)\//i.test(pathname);
135
193
  }
136
194
  return false;
137
195
  }
196
+
197
+ /**
198
+ * Check if a pathname falls under any of the given route prefixes.
199
+ * Matching is segment-aware: '/app/s' or '/app/s/' matches '/app/s' and '/app/s/12345', but not '/app/sheets'.
200
+ * Only the pathname is compared, so query strings and hashes never affect the result.
201
+ */
202
+
203
+ export function isPathnameUnderRoutes(pathname: string, routes: string[]) {
204
+ const [path] = pathname.split(/[?#]/);
205
+ return routes.some((route) => {
206
+ const prefix = route.replace(/\/+$/, '');
207
+ return prefix === '' || path === prefix || path.startsWith(prefix + '/');
208
+ });
209
+ }
210
+
211
+ /*
212
+ * Accept explicit web/contact links and relative destinations without permitting browser scheme normalization bypasses.
213
+ */
214
+ export function getSafeLinkDestination(destination: string): string | null {
215
+ if (!destination || /[\s\u0000-\u001f\u007f-\u009f\\]/u.test(destination) || /&(?:#(?:x[\da-f]+|\d+)|[a-z]+);/i.test(destination)) {
216
+ return null;
217
+ }
218
+
219
+ const scheme = /^([a-z][a-z0-9+.-]*):/i.exec(destination)?.[1]?.toLowerCase();
220
+ if (scheme) {
221
+ if (!['http', 'https', 'mailto', 'tel'].includes(scheme)) {
222
+ return null;
223
+ }
224
+ if (scheme === 'http' || scheme === 'https') {
225
+ try {
226
+ const url = new URL(destination);
227
+ return url.hostname ? destination : null;
228
+ } catch {
229
+ return null;
230
+ }
231
+ }
232
+ return destination.length > scheme.length + 1 ? destination : null;
233
+ }
234
+
235
+ // A colon before a path/query/fragment boundary is never a relative path.
236
+ return /^[^/?#]*:/.test(destination) ? null : destination;
237
+ }