@jsb188/app 1.1.11 → 1.1.12
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/package.json +7 -4
- package/src/constants/agent.ts +39 -62
- package/src/constants/app.ts +17 -0
- package/src/constants/wsActions.ts +7 -15
- package/src/i18n/account.ts +1 -1
- package/src/i18n/agent.ts +326 -88
- package/src/i18n/agentToolBusiness.ts +196 -0
- package/src/i18n/agentToolGrid.ts +137 -0
- package/src/i18n/agentToolWorkflow.ts +101 -0
- package/src/i18n/agent_note.ts +109 -0
- package/src/i18n/app.ts +23 -17
- package/src/i18n/auth.ts +28 -30
- package/src/i18n/billing.ts +3 -85
- package/src/i18n/c.ts +907 -0
- package/src/i18n/campaign.ts +842 -140
- package/src/i18n/datetime.ts +6 -0
- package/src/i18n/editor.ts +58 -0
- package/src/i18n/error.ts +307 -168
- package/src/i18n/field_doc_editor.ts +21 -0
- package/src/i18n/field_doc_programs.ts +52 -0
- package/src/i18n/field_docs.ts +170 -8
- package/src/i18n/index.ts +21 -5
- package/src/i18n/log.ts +232 -38
- package/src/i18n/memory.ts +18 -4
- package/src/i18n/note.ts +5 -3
- package/src/i18n/org.ts +93 -44
- package/src/i18n/schedule.ts +209 -0
- package/src/i18n/sheet.ts +102 -145
- package/src/i18n/sheet_formula.ts +165 -0
- package/src/i18n/workflow.ts +48 -9
- package/src/main.ts +2 -2
- package/src/platform.ts +89 -49
- package/src/types/ai.d.ts +19 -0
- package/src/types/app.d.ts +50 -57
- package/src/types/other.d.ts +8 -1
- package/src/utils/api.ts +46 -0
- package/src/utils/clientIP.ts +213 -0
- package/src/utils/color.ts +77 -0
- package/src/utils/csv.ts +19 -0
- package/src/utils/datetime.ts +207 -31
- package/src/utils/deferredSave.ts +89 -0
- package/src/utils/email.ts +50 -0
- package/src/utils/expression.helpers.ts +470 -0
- package/src/utils/expression.test.ts +793 -0
- package/src/utils/expression.ts +1633 -239
- package/src/utils/language.ts +9 -0
- package/src/utils/logic.ts +9 -162
- package/src/utils/number.ts +30 -11
- package/src/utils/object.ts +29 -0
- package/src/utils/promise.ts +78 -7
- package/src/utils/queryReset.ts +101 -14
- package/src/utils/string.test.ts +24 -39
- package/src/utils/string.ts +73 -143
- package/src/utils/url_client.ts +83 -14
- package/src/constants/billing.ts +0 -146
- package/src/i18n/agentDoc.ts +0 -110
- package/src/i18n/form.ts +0 -496
- package/src/types/form.d.ts +0 -101
- package/src/utils/billing.ts +0 -141
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The visitor's IP address behind our proxies, and the proof the app server
|
|
3
|
+
* sends with the visitor IP it forwards to the API.
|
|
4
|
+
*
|
|
5
|
+
* Production traffic reaches the servers through a Google Cloud external
|
|
6
|
+
* Application Load Balancer, then nginx on each instance. The load balancer
|
|
7
|
+
* appends two addresses to `X-Forwarded-For`: the client it accepted the
|
|
8
|
+
* connection from, then its own; nginx may append the Google front end it
|
|
9
|
+
* heard from. Everything left of the client's entry is whatever the client
|
|
10
|
+
* sent, so only the entries our own proxies appended can be trusted.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const CLIENT_IP = {
|
|
14
|
+
/** Addresses of our own hops: Google front ends (the load balancer's proxies), private networks and loopback. */
|
|
15
|
+
proxyRanges: [
|
|
16
|
+
['35.191.0.0', 16],
|
|
17
|
+
['130.211.0.0', 22],
|
|
18
|
+
['10.0.0.0', 8],
|
|
19
|
+
['172.16.0.0', 12],
|
|
20
|
+
['192.168.0.0', 16],
|
|
21
|
+
['169.254.0.0', 16],
|
|
22
|
+
['127.0.0.0', 8],
|
|
23
|
+
] as const,
|
|
24
|
+
proof: {
|
|
25
|
+
version: '1',
|
|
26
|
+
purpose: 'ssr-client-ip',
|
|
27
|
+
// App server and API clocks are both NTP-synced; this only has to cover the request's flight
|
|
28
|
+
maxAgeMs: 30_000,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
* An IPv4 address as a 32-bit number, or null when the text is not one.
|
|
34
|
+
*/
|
|
35
|
+
function parseIPv4(value: string): number | null {
|
|
36
|
+
const parts = value.split('.');
|
|
37
|
+
if (parts.length !== 4) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let address = 0;
|
|
42
|
+
for (const part of parts) {
|
|
43
|
+
if (!/^\d{1,3}$/.test(part) || Number(part) > 255) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
address = address * 256 + Number(part);
|
|
47
|
+
}
|
|
48
|
+
return address;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/*
|
|
52
|
+
* Whether the text is an IPv6 address, as the URL parser reads a bracketed host.
|
|
53
|
+
*/
|
|
54
|
+
function isIPv6(value: string): boolean {
|
|
55
|
+
if (!value.includes(':') || !/^[0-9a-f:.]+$/i.test(value)) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
new URL(`http://[${value}]/`);
|
|
61
|
+
return true;
|
|
62
|
+
} catch {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/*
|
|
68
|
+
* Whether the text is an IPv4 or IPv6 address.
|
|
69
|
+
*/
|
|
70
|
+
export function isIPAddress(value: string): boolean {
|
|
71
|
+
return parseIPv4(value) !== null || isIPv6(value);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/*
|
|
75
|
+
* Whether an address belongs to one of our own hops (a Google front end, a
|
|
76
|
+
* private network, loopback) rather than to a client on the internet.
|
|
77
|
+
*/
|
|
78
|
+
function isProxyAddress(value: string): boolean {
|
|
79
|
+
const lower = value.toLowerCase();
|
|
80
|
+
// IPv4-mapped IPv6 (::ffff:10.0.0.1) is judged by the IPv4 address inside it
|
|
81
|
+
const ipv4 = parseIPv4(lower.startsWith('::ffff:') ? lower.slice(7) : lower);
|
|
82
|
+
if (ipv4 !== null) {
|
|
83
|
+
return CLIENT_IP.proxyRanges.some(([base, bits]) => {
|
|
84
|
+
const size = 2 ** (32 - bits);
|
|
85
|
+
return Math.floor(ipv4 / size) === Math.floor((parseIPv4(base) as number) / size);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Loopback, unique local (fc00::/7) and link-local (fe80::/10) IPv6
|
|
90
|
+
return lower === '::1' || /^f[cd][0-9a-f]{0,2}:/.test(lower) || /^fe[89ab][0-9a-f]?:/.test(lower);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/*
|
|
94
|
+
* The client address our load balancer saw connect, read from the right of an
|
|
95
|
+
* `X-Forwarded-For` chain: past our own trailing hops (nginx's Google front
|
|
96
|
+
* end, private networks), past the load balancer's own address (once, or
|
|
97
|
+
* repeated if a hop copied it), to the address the load balancer appended for
|
|
98
|
+
* the connection. Null when the chain cannot have come through our load
|
|
99
|
+
* balancer (fewer than two addresses, or nothing valid where the client should
|
|
100
|
+
* be), so the caller decides what an unproxied request means.
|
|
101
|
+
*/
|
|
102
|
+
export function getForwardedClientIP(forwardedFor: string | null | undefined): string | null {
|
|
103
|
+
const hops = (forwardedFor || '')
|
|
104
|
+
.split(',')
|
|
105
|
+
.map((hop) => hop.trim())
|
|
106
|
+
.filter(Boolean);
|
|
107
|
+
let index = hops.length - 1;
|
|
108
|
+
|
|
109
|
+
while (index >= 0 && isProxyAddress(hops[index])) {
|
|
110
|
+
index--;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const balancer = hops[index];
|
|
114
|
+
while (index >= 0 && hops[index] === balancer) {
|
|
115
|
+
index--;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const client = hops[index];
|
|
119
|
+
return client && isIPAddress(client) && !isProxyAddress(client) ? client : null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/*
|
|
123
|
+
* Convert bytes into lowercase hexadecimal.
|
|
124
|
+
*/
|
|
125
|
+
function bytesToHex(bytes: Uint8Array): string {
|
|
126
|
+
return Array.from(bytes)
|
|
127
|
+
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
128
|
+
.join('');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/*
|
|
132
|
+
* Compare two equal-length signatures without data-dependent early exits.
|
|
133
|
+
*/
|
|
134
|
+
function signaturesMatch(left: string, right: string): boolean {
|
|
135
|
+
if (left.length !== right.length) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
let difference = 0;
|
|
140
|
+
for (let index = 0; index < left.length; index++) {
|
|
141
|
+
difference |= left.charCodeAt(index) ^ right.charCodeAt(index);
|
|
142
|
+
}
|
|
143
|
+
return difference === 0;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/*
|
|
147
|
+
* Sign one forwarded client IP and its timestamp under the SSR client IP key domain.
|
|
148
|
+
*/
|
|
149
|
+
async function signClientIPProof(secret: string, timestamp: number, ipAddress: string): Promise<string> {
|
|
150
|
+
const { purpose, version } = CLIENT_IP.proof;
|
|
151
|
+
const key = await crypto.subtle.importKey(
|
|
152
|
+
'raw',
|
|
153
|
+
new TextEncoder().encode(`${purpose}:${secret}`),
|
|
154
|
+
{ name: 'HMAC', hash: 'SHA-256' },
|
|
155
|
+
false,
|
|
156
|
+
['sign'],
|
|
157
|
+
);
|
|
158
|
+
const signature = await crypto.subtle.sign('HMAC', key, new TextEncoder().encode(`${version}:${timestamp}:${ipAddress}`));
|
|
159
|
+
return bytesToHex(new Uint8Array(signature));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/*
|
|
163
|
+
* Create the short-lived proof the app server sends with the visitor IP it
|
|
164
|
+
* forwards to the API (`x-ssr-ip-proof` beside `x-ssr-ip`), signed with the
|
|
165
|
+
* deployment secret the two share. Null without a secret or a valid address.
|
|
166
|
+
*/
|
|
167
|
+
export async function createClientIPProof(secret: string, ipAddress: string, now: number = Date.now()): Promise<string | null> {
|
|
168
|
+
if (!secret || !isIPAddress(ipAddress) || !Number.isSafeInteger(now) || now <= 0) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return `${CLIENT_IP.proof.version}.${now}.${await signClientIPProof(secret, now, ipAddress)}`;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/*
|
|
176
|
+
* When a client IP proof claims it was signed, or null when it is not shaped
|
|
177
|
+
* like one; read without checking the signature, to diagnose a rejected proof
|
|
178
|
+
* (a stale one points to clock drift, a fresh one to a secret mismatch).
|
|
179
|
+
*/
|
|
180
|
+
export function getClientIPProofTimestamp(proof: string | null | undefined): number | null {
|
|
181
|
+
const timestamp = Number(proof?.split('.')[1]);
|
|
182
|
+
return Number.isSafeInteger(timestamp) && timestamp > 0 ? timestamp : null;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/*
|
|
186
|
+
* Verify that a forwarded client IP was signed by the app server within the
|
|
187
|
+
* last `maxAgeMs`, for exactly this address.
|
|
188
|
+
*/
|
|
189
|
+
export async function verifyClientIPProof(
|
|
190
|
+
proof: string | null | undefined,
|
|
191
|
+
ipAddress: string | null | undefined,
|
|
192
|
+
secret: string,
|
|
193
|
+
now: number = Date.now(),
|
|
194
|
+
): Promise<boolean> {
|
|
195
|
+
if (!proof || !ipAddress || !secret || !isIPAddress(ipAddress)) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const [version, timestampText, signature, ...extra] = proof.split('.');
|
|
200
|
+
const timestamp = Number(timestampText);
|
|
201
|
+
if (
|
|
202
|
+
extra.length ||
|
|
203
|
+
version !== CLIENT_IP.proof.version ||
|
|
204
|
+
!Number.isSafeInteger(timestamp) ||
|
|
205
|
+
timestamp <= 0 ||
|
|
206
|
+
Math.abs(now - timestamp) > CLIENT_IP.proof.maxAgeMs ||
|
|
207
|
+
!/^[0-9a-f]{64}$/.test(signature || '')
|
|
208
|
+
) {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return signaturesMatch(signature, await signClientIPProof(secret, timestamp, ipAddress));
|
|
213
|
+
}
|
package/src/utils/color.ts
CHANGED
|
@@ -32,6 +32,30 @@ const APP_COLOR_HEX_VALUES: Record<ColorEnum, string> = {
|
|
|
32
32
|
zinc: '#71717a',
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Return an app palette name only when the supplied value belongs to the palette.
|
|
37
|
+
*/
|
|
38
|
+
export function getAppColorName(
|
|
39
|
+
color: string | null | undefined,
|
|
40
|
+
): ColorEnum | undefined {
|
|
41
|
+
if (!color || !Object.hasOwn(APP_COLOR_HEX_VALUES, color)) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return color as ColorEnum;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Resolve an app palette name to its CSS hex value while preserving custom colors.
|
|
50
|
+
*/
|
|
51
|
+
export function getAppColorValue(
|
|
52
|
+
color: string | null | undefined,
|
|
53
|
+
): string | undefined {
|
|
54
|
+
return color && Object.hasOwn(APP_COLOR_HEX_VALUES, color)
|
|
55
|
+
? APP_COLOR_HEX_VALUES[color as ColorEnum]
|
|
56
|
+
: color || undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
35
59
|
/**
|
|
36
60
|
* Return one app palette hex color picked deterministically by index.
|
|
37
61
|
*/
|
|
@@ -200,3 +224,56 @@ export function isDarkColor(
|
|
|
200
224
|
|
|
201
225
|
return brightness < 128;
|
|
202
226
|
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Normalize one numeric RGB channel to an integer within the supported range.
|
|
230
|
+
*/
|
|
231
|
+
function getNormalizedRGBChannel(channel: number): number {
|
|
232
|
+
return Math.max(0, Math.min(255, Math.round(channel)));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Convert one numeric RGB channel into a two-digit hex value.
|
|
237
|
+
*/
|
|
238
|
+
function getHexColorChannel(channel: number): string {
|
|
239
|
+
return getNormalizedRGBChannel(channel).toString(16).padStart(2, '0');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Convert numeric RGB channels into a six-digit CSS hex color.
|
|
244
|
+
*/
|
|
245
|
+
export function getHexColorFromRGB(
|
|
246
|
+
red: number,
|
|
247
|
+
green: number,
|
|
248
|
+
blue: number,
|
|
249
|
+
): string {
|
|
250
|
+
return `#${getHexColorChannel(red)}${getHexColorChannel(green)}${getHexColorChannel(blue)}`;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Convert one sRGB channel into its linear-light value.
|
|
255
|
+
*/
|
|
256
|
+
function getLinearRGBChannel(channel: number): number {
|
|
257
|
+
const normalizedChannel = channel / 255;
|
|
258
|
+
return normalizedChannel <= 0.04045
|
|
259
|
+
? normalizedChannel / 12.92
|
|
260
|
+
: Math.pow((normalizedChannel + 0.055) / 1.055, 2.4);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Calculate WCAG relative luminance for a supported sRGB color.
|
|
265
|
+
*/
|
|
266
|
+
export function getRelativeLuminance(
|
|
267
|
+
color: string | null | undefined,
|
|
268
|
+
): number | null {
|
|
269
|
+
const rgb = getRGBColor(color);
|
|
270
|
+
if (!rgb) {
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return (
|
|
275
|
+
0.2126 * getLinearRGBChannel(rgb.red) +
|
|
276
|
+
0.7152 * getLinearRGBChannel(rgb.green) +
|
|
277
|
+
0.0722 * getLinearRGBChannel(rgb.blue)
|
|
278
|
+
);
|
|
279
|
+
}
|
package/src/utils/csv.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Identify spreadsheet formulas while preserving plain signed numbers and
|
|
3
|
+
* phone-like text. CSV quoting alone does not prevent formula execution.
|
|
4
|
+
*/
|
|
5
|
+
export function isCSVFormulaLikeValue(text: string) {
|
|
6
|
+
if (/^[=@\t\r\n]/.test(text)) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
return /^[+-]/.test(text) && !/^[+-]?[\d\s().-]+$/.test(text) && !/^[+-]?(?:\d+\.?\d*|\.\d+)[eE][+-]?\d+$/.test(text);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/* Escape one CSV cell and neutralize spreadsheet formulas with an apostrophe. */
|
|
13
|
+
export function escapeCSVValue(value: unknown) {
|
|
14
|
+
let text = String(value ?? '');
|
|
15
|
+
if (isCSVFormulaLikeValue(text)) {
|
|
16
|
+
text = `'${text}`;
|
|
17
|
+
}
|
|
18
|
+
return /[",\n\r]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text;
|
|
19
|
+
}
|
package/src/utils/datetime.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { DateTime } from 'luxon';
|
|
2
2
|
import i18n from '../i18n/index.ts';
|
|
3
3
|
import { getObject } from './object.ts';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
DEFAULT_TIMEZONE,
|
|
6
|
+
isValidTimeZone,
|
|
7
|
+
parseDateInTimezone,
|
|
8
|
+
} from './timeZone.ts';
|
|
5
9
|
|
|
6
10
|
/**
|
|
7
11
|
* Types
|
|
@@ -175,6 +179,27 @@ export function getDatePeriod(d1: Date, d2: Date): DatePeriodEnum {
|
|
|
175
179
|
return 'OLDER';
|
|
176
180
|
}
|
|
177
181
|
|
|
182
|
+
/*
|
|
183
|
+
* Format compact display dates consistently, showing the year only outside the current year.
|
|
184
|
+
* Calendar dates use UTC so timezone conversion cannot change their day.
|
|
185
|
+
*/
|
|
186
|
+
|
|
187
|
+
export function formatDisplayDate(value: string | Date, timeZone?: string | null): string {
|
|
188
|
+
const dateOnly = typeof value === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(value);
|
|
189
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
190
|
+
const zone = dateOnly ? 'UTC' : timeZone || undefined;
|
|
191
|
+
const yearFormat = new Intl.DateTimeFormat('en-US', { timeZone: zone, year: 'numeric' });
|
|
192
|
+
return new Intl.DateTimeFormat('en-US', {
|
|
193
|
+
timeZone: zone,
|
|
194
|
+
month: 'numeric',
|
|
195
|
+
day: '2-digit',
|
|
196
|
+
year: yearFormat.format(date) === yearFormat.format(new Date()) ? undefined : 'numeric',
|
|
197
|
+
hour: dateOnly ? undefined : 'numeric',
|
|
198
|
+
minute: dateOnly ? undefined : '2-digit',
|
|
199
|
+
hour12: true,
|
|
200
|
+
}).format(date);
|
|
201
|
+
}
|
|
202
|
+
|
|
178
203
|
/**
|
|
179
204
|
* Get full date with time
|
|
180
205
|
*/
|
|
@@ -256,6 +281,60 @@ export function getFullDateTime(
|
|
|
256
281
|
});
|
|
257
282
|
}
|
|
258
283
|
|
|
284
|
+
/*
|
|
285
|
+
* Convert one canonical instant into the local value required by a
|
|
286
|
+
* datetime-local input in the requested scheduling timezone.
|
|
287
|
+
*/
|
|
288
|
+
export function getDateTimeLocalInputValue(
|
|
289
|
+
value?: Date | string | number | null,
|
|
290
|
+
timeZone?: string | null,
|
|
291
|
+
): string | null {
|
|
292
|
+
const date = value ? new Date(value) : null;
|
|
293
|
+
if (!date || !Number.isFinite(date.getTime())) {
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const inputTimeZone =
|
|
298
|
+
timeZone && isValidTimeZone(timeZone) ? timeZone : DEFAULT_TIMEZONE;
|
|
299
|
+
const { calDate, time } = getCalDate(date, inputTimeZone);
|
|
300
|
+
return `${calDate}T${time.slice(0, 5)}`;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/*
|
|
304
|
+
* Convert a datetime-local input value into its canonical ISO instant in the
|
|
305
|
+
* requested scheduling timezone.
|
|
306
|
+
*/
|
|
307
|
+
export function normalizeDateTimeLocalInputValue(
|
|
308
|
+
value?: string | null,
|
|
309
|
+
timeZone?: string | null,
|
|
310
|
+
): string | null {
|
|
311
|
+
const trimmedValue = String(value || '').trim();
|
|
312
|
+
if (
|
|
313
|
+
!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,3})?)?$/.test(
|
|
314
|
+
trimmedValue,
|
|
315
|
+
)
|
|
316
|
+
) {
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const inputTimeZone =
|
|
321
|
+
timeZone && isValidTimeZone(timeZone) ? timeZone : DEFAULT_TIMEZONE;
|
|
322
|
+
const parsedDate = parseDateInTimezone(trimmedValue, inputTimeZone, 0, true);
|
|
323
|
+
if (!Number.isFinite(parsedDate.getTime())) {
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// Luxon advances nonexistent wall-clock times through daylight-saving gaps.
|
|
328
|
+
// A scheduler must reject that silent time change rather than save a
|
|
329
|
+
// different local time from the one the user entered.
|
|
330
|
+
const { calDate, time } = getCalDate(parsedDate, inputTimeZone);
|
|
331
|
+
if (`${calDate}T${time.slice(0, 5)}` !== trimmedValue.slice(0, 16)) {
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
return parsedDate.toISOString();
|
|
336
|
+
}
|
|
337
|
+
|
|
259
338
|
/**
|
|
260
339
|
* Check if date is same day
|
|
261
340
|
*/
|
|
@@ -704,39 +783,145 @@ function getDateTimeWithTimeZone(date: string | Date, timeZone: string | null) {
|
|
|
704
783
|
}
|
|
705
784
|
|
|
706
785
|
/**
|
|
707
|
-
* Convert today/yesterday date time into a label with time
|
|
786
|
+
* Convert today/yesterday date time into a label, with the time unless hidden
|
|
708
787
|
* @param dt - Parsed Luxon date time
|
|
709
788
|
* @param timeZone - Optional timezone for time formatting
|
|
789
|
+
* @param hideTime - Return only the day label
|
|
710
790
|
*/
|
|
711
791
|
|
|
712
|
-
function getTodayYesterdayTime(
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
792
|
+
function getTodayYesterdayTime(
|
|
793
|
+
dt: DateTime,
|
|
794
|
+
now: DateTime,
|
|
795
|
+
timeZone: string | null,
|
|
796
|
+
hideTime?: boolean,
|
|
797
|
+
) {
|
|
798
|
+
const dayKey = dt.hasSame(now, 'day')
|
|
799
|
+
? 'datetime.period_TODAY'
|
|
800
|
+
: dt.hasSame(now.minus({ days: 1 }), 'day')
|
|
801
|
+
? 'datetime.period_YESTERDAY'
|
|
802
|
+
: null;
|
|
803
|
+
|
|
804
|
+
if (!dayKey) {
|
|
805
|
+
return null;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
if (hideTime) {
|
|
809
|
+
return i18n.t(dayKey);
|
|
810
|
+
}
|
|
811
|
+
|
|
717
812
|
const timeText = getFullDateTime(dt.toJSDate(), {
|
|
718
813
|
timeZone,
|
|
719
814
|
hideDate: true,
|
|
720
815
|
});
|
|
816
|
+
return `${i18n.t(dayKey)}, ${timeText}`;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* Format a date time with a numeric month and day, adding a two-digit year only when it differs from the reference year.
|
|
821
|
+
* @param dt - Parsed Luxon date time
|
|
822
|
+
* @param now - Reference date time used to decide whether to show the year
|
|
823
|
+
* @param timeZone - Optional timezone for date and time formatting
|
|
824
|
+
* @param hideTime - Omit the hour and minute
|
|
825
|
+
*/
|
|
826
|
+
|
|
827
|
+
function getCompactDateTime(
|
|
828
|
+
dt: DateTime,
|
|
829
|
+
now: DateTime,
|
|
830
|
+
timeZone: string | null,
|
|
831
|
+
hideTime?: boolean,
|
|
832
|
+
) {
|
|
833
|
+
return new Intl.DateTimeFormat('en-US', {
|
|
834
|
+
day: 'numeric',
|
|
835
|
+
month: 'numeric',
|
|
836
|
+
timeZone: timeZone && isValidTimeZone(timeZone) ? timeZone : undefined,
|
|
837
|
+
year: dt.year === now.year ? undefined : '2-digit',
|
|
838
|
+
...(hideTime ? {} : { hour: 'numeric', hour12: true, minute: '2-digit' }),
|
|
839
|
+
}).format(dt.toJSDate());
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
/**
|
|
843
|
+
* Format an edited timestamp as a compact or expanded relative label, falling back to YYYY/MM/DD.
|
|
844
|
+
* @param date - Date to format
|
|
845
|
+
* @param timeZone - Optional timezone for calendar-day comparisons
|
|
846
|
+
* @param longer - Return expanded relative units when true
|
|
847
|
+
*/
|
|
848
|
+
|
|
849
|
+
export function getEditedTimeAgo(
|
|
850
|
+
date: string | Date,
|
|
851
|
+
timeZone?: string | null,
|
|
852
|
+
longer?: boolean,
|
|
853
|
+
) {
|
|
854
|
+
const dt = getDateTimeWithTimeZone(date, timeZone || null);
|
|
855
|
+
if (!dt) {
|
|
856
|
+
return null;
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
const now =
|
|
860
|
+
timeZone && isValidTimeZone(timeZone)
|
|
861
|
+
? DateTime.now().setZone(timeZone)
|
|
862
|
+
: DateTime.now();
|
|
863
|
+
const diffMinutes = now.diff(dt, 'minutes').minutes;
|
|
864
|
+
|
|
865
|
+
if (diffMinutes < 0) {
|
|
866
|
+
return dt.toFormat('yyyy/MM/dd');
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
if (diffMinutes <= 4) {
|
|
870
|
+
return i18n.t('datetime.just_now');
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
if (diffMinutes < 60) {
|
|
874
|
+
return i18n.t(
|
|
875
|
+
longer ? 'datetime.minutes_ago_ct' : 'datetime.minutes_ago_short_ct',
|
|
876
|
+
{
|
|
877
|
+
smart_count: Math.floor(diffMinutes),
|
|
878
|
+
},
|
|
879
|
+
);
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
const diffHours = diffMinutes / 60;
|
|
883
|
+
if (diffHours < 9) {
|
|
884
|
+
return i18n.t(
|
|
885
|
+
longer ? 'datetime.hours_ago_ct' : 'datetime.hours_ago_short_ct',
|
|
886
|
+
{
|
|
887
|
+
smart_count: Math.floor(diffHours),
|
|
888
|
+
},
|
|
889
|
+
);
|
|
890
|
+
}
|
|
721
891
|
|
|
722
892
|
if (dt.hasSame(now, 'day')) {
|
|
723
|
-
return
|
|
893
|
+
return i18n.t('datetime.today');
|
|
724
894
|
}
|
|
725
895
|
|
|
726
896
|
if (dt.hasSame(now.minus({ days: 1 }), 'day')) {
|
|
727
|
-
return
|
|
897
|
+
return i18n.t('datetime.yesterday');
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
const diffDays = now.startOf('day').diff(dt.startOf('day'), 'days').days;
|
|
901
|
+
if (diffDays < 7) {
|
|
902
|
+
return i18n.t(
|
|
903
|
+
longer ? 'datetime.days_ago_ct' : 'datetime.days_ago_short_ct',
|
|
904
|
+
{
|
|
905
|
+
smart_count: Math.floor(diffDays),
|
|
906
|
+
},
|
|
907
|
+
);
|
|
728
908
|
}
|
|
729
909
|
|
|
730
|
-
return
|
|
910
|
+
return dt.toFormat('yyyy/MM/dd');
|
|
731
911
|
}
|
|
732
912
|
|
|
733
913
|
/**
|
|
734
|
-
* Convert date time "time" ago
|
|
914
|
+
* Convert date time "time" ago: "Today, 10:21 PM", "Yesterday, 10:21 PM", or "9/14, 10:21 PM"
|
|
735
915
|
* @param date - Date to convert
|
|
736
916
|
* @param timeZone - Optional timezone for day boundary checks
|
|
917
|
+
* @param hideTime - Return only the day part ("Today", "Yesterday", "9/14") for narrow table cells
|
|
737
918
|
*/
|
|
738
919
|
|
|
739
|
-
export function getTimeAgo(
|
|
920
|
+
export function getTimeAgo(
|
|
921
|
+
date: string | Date,
|
|
922
|
+
timeZone: string | null,
|
|
923
|
+
hideTime?: boolean,
|
|
924
|
+
) {
|
|
740
925
|
const dt = getDateTimeWithTimeZone(date, timeZone);
|
|
741
926
|
if (!dt) {
|
|
742
927
|
return null;
|
|
@@ -746,27 +931,13 @@ export function getTimeAgo(date: string | Date, timeZone: string | null) {
|
|
|
746
931
|
timeZone && isValidTimeZone(timeZone)
|
|
747
932
|
? DateTime.now().setZone(timeZone)
|
|
748
933
|
: DateTime.now();
|
|
749
|
-
const todayYesterdayTime = getTodayYesterdayTime(dt, timeZone);
|
|
934
|
+
const todayYesterdayTime = getTodayYesterdayTime(dt, now, timeZone, hideTime);
|
|
750
935
|
|
|
751
936
|
if (todayYesterdayTime) {
|
|
752
937
|
return todayYesterdayTime;
|
|
753
938
|
}
|
|
754
939
|
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
if (diffMs < 0) {
|
|
758
|
-
return getFullDateTime(dt.toJSDate(), {
|
|
759
|
-
timeZone,
|
|
760
|
-
alwaysShowYear: true,
|
|
761
|
-
textDateStyle: 'short',
|
|
762
|
-
});
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
return getFullDateTime(dt.toJSDate(), {
|
|
766
|
-
timeZone,
|
|
767
|
-
alwaysShowYear: true,
|
|
768
|
-
textDateStyle: 'short',
|
|
769
|
-
});
|
|
940
|
+
return getCompactDateTime(dt, now, timeZone, hideTime);
|
|
770
941
|
}
|
|
771
942
|
|
|
772
943
|
/**
|
|
@@ -779,6 +950,7 @@ export function getFullDate(
|
|
|
779
950
|
outputStyle_:
|
|
780
951
|
| 'NUMERIC'
|
|
781
952
|
| 'NUMERIC_TIME'
|
|
953
|
+
| 'NUMERIC_TIME_COMPACT'
|
|
782
954
|
| 'DATE_ONLY_SHORT'
|
|
783
955
|
| 'DAY_IF_WEEK'
|
|
784
956
|
| 'TOMORROW_OR_NUMERIC'
|
|
@@ -828,10 +1000,13 @@ export function getFullDate(
|
|
|
828
1000
|
return new Intl.DateTimeFormat(locales, {
|
|
829
1001
|
timeZone: timeZone || undefined, // null is not allowed, it will throw error
|
|
830
1002
|
}).format(d);
|
|
831
|
-
case 'NUMERIC_TIME':
|
|
832
|
-
|
|
1003
|
+
case 'NUMERIC_TIME':
|
|
1004
|
+
case 'NUMERIC_TIME_COMPACT': {
|
|
1005
|
+
// Compact output: "9/01/2024, 8:30 PM"; default output: "9/1/2024, 8:30 PM".
|
|
1006
|
+
const compact = outputStyle === 'NUMERIC_TIME_COMPACT';
|
|
833
1007
|
const numericDate = new Intl.DateTimeFormat(locales, {
|
|
834
1008
|
timeZone: timeZone || undefined, // null is not allowed, it will throw error
|
|
1009
|
+
...(compact ? ({ month: 'numeric', day: '2-digit', year: 'numeric' } as const) : {}),
|
|
835
1010
|
}).format(d);
|
|
836
1011
|
|
|
837
1012
|
const numericTime = new Intl.DateTimeFormat(locales, {
|
|
@@ -841,7 +1016,8 @@ export function getFullDate(
|
|
|
841
1016
|
hour12: true,
|
|
842
1017
|
}).format(d);
|
|
843
1018
|
|
|
844
|
-
|
|
1019
|
+
const numericDateTime = `${numericDate}, ${numericTime}`;
|
|
1020
|
+
return compact ? numericDateTime.toUpperCase() : numericDateTime;
|
|
845
1021
|
|
|
846
1022
|
// return new Intl.DateTimeFormat(locales, {
|
|
847
1023
|
// month: '2-digit',
|