zalo-tui 0.1.0 → 0.2.0
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/CHANGELOG.md +27 -0
- package/README.md +17 -9
- package/dist/zalo-tui.js +960 -152
- package/package.json +2 -1
package/dist/zalo-tui.js
CHANGED
|
@@ -1,9 +1,79 @@
|
|
|
1
1
|
// src/index.tsx
|
|
2
2
|
import { render } from "ink";
|
|
3
3
|
|
|
4
|
+
// package.json
|
|
5
|
+
var package_default = {
|
|
6
|
+
name: "zalo-tui",
|
|
7
|
+
version: "0.2.0",
|
|
8
|
+
description: "A terminal client for Zalo personal accounts, built on zca-js",
|
|
9
|
+
keywords: [
|
|
10
|
+
"zalo",
|
|
11
|
+
"tui",
|
|
12
|
+
"terminal",
|
|
13
|
+
"chat",
|
|
14
|
+
"ink"
|
|
15
|
+
],
|
|
16
|
+
license: "MIT",
|
|
17
|
+
author: "paulcoding810",
|
|
18
|
+
repository: {
|
|
19
|
+
type: "git",
|
|
20
|
+
url: "git+https://github.com/paulcoding810/zalo-tui.git"
|
|
21
|
+
},
|
|
22
|
+
homepage: "https://github.com/paulcoding810/zalo-tui#readme",
|
|
23
|
+
bugs: {
|
|
24
|
+
url: "https://github.com/paulcoding810/zalo-tui/issues"
|
|
25
|
+
},
|
|
26
|
+
type: "module",
|
|
27
|
+
bin: {
|
|
28
|
+
"zalo-tui": "bin/zalo-tui.js"
|
|
29
|
+
},
|
|
30
|
+
files: [
|
|
31
|
+
"bin",
|
|
32
|
+
"dist",
|
|
33
|
+
"CHANGELOG.md"
|
|
34
|
+
],
|
|
35
|
+
scripts: {
|
|
36
|
+
start: "tsx src/index.tsx",
|
|
37
|
+
build: "esbuild src/index.tsx --bundle --platform=node --format=esm --target=node22 --packages=external --outfile=dist/zalo-tui.js",
|
|
38
|
+
typecheck: "tsc --noEmit",
|
|
39
|
+
test: "tsx --test test/*.test.ts",
|
|
40
|
+
prepack: "pnpm typecheck && pnpm test && pnpm build"
|
|
41
|
+
},
|
|
42
|
+
engines: {
|
|
43
|
+
node: ">=22"
|
|
44
|
+
},
|
|
45
|
+
os: [
|
|
46
|
+
"darwin",
|
|
47
|
+
"linux"
|
|
48
|
+
],
|
|
49
|
+
packageManager: "pnpm@10.18.1",
|
|
50
|
+
dependencies: {
|
|
51
|
+
"image-size": "2.1.0",
|
|
52
|
+
ink: "^8.0.0",
|
|
53
|
+
jsqr: "^1.4.0",
|
|
54
|
+
pngjs: "^7.0.0",
|
|
55
|
+
qrcode: "^1.5.4",
|
|
56
|
+
react: "^19.3.1",
|
|
57
|
+
"string-width": "8.3.0",
|
|
58
|
+
"zca-js": "2.3.0"
|
|
59
|
+
},
|
|
60
|
+
devDependencies: {
|
|
61
|
+
"@types/node": "^26.6.6",
|
|
62
|
+
"@types/pngjs": "^6.0.5",
|
|
63
|
+
"@types/qrcode": "^1.5.6",
|
|
64
|
+
"@types/react": "^19.3.0",
|
|
65
|
+
esbuild: "0.28.2",
|
|
66
|
+
tsx: "^4.23.15",
|
|
67
|
+
typescript: "^7.0.2"
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
4
71
|
// src/notify.ts
|
|
5
72
|
import { spawn } from "node:child_process";
|
|
73
|
+
import { accessSync, constants } from "node:fs";
|
|
74
|
+
import { delimiter, join } from "node:path";
|
|
6
75
|
var NOTIFY_MODES = ["full", "sender", "off"];
|
|
76
|
+
var GROUP_NOTIFY_MODES = ["all", "mentions"];
|
|
7
77
|
function nextNotifyMode(mode) {
|
|
8
78
|
return NOTIFY_MODES[(NOTIFY_MODES.indexOf(mode) + 1) % NOTIFY_MODES.length];
|
|
9
79
|
}
|
|
@@ -14,8 +84,9 @@ function notifySkipReason(m, state, muted, now = Date.now()) {
|
|
|
14
84
|
if (state.notify === "off") return "notifications off";
|
|
15
85
|
if (m.isSelf) return "own message";
|
|
16
86
|
if (m.threadKey === state.activeKey && state.terminalFocused) return "chat open and terminal focused";
|
|
87
|
+
const isGroup = state.threads[m.threadKey]?.kind === "group";
|
|
88
|
+
if (isGroup && state.notifyGroups === "mentions") return m.mentionsMe ? null : "group message without mention";
|
|
17
89
|
if ((muted.get(m.threadKey) ?? 0) > now) return "chat muted";
|
|
18
|
-
if (state.threads[m.threadKey]?.kind === "group" && !m.mentionsMe) return "group message without mention";
|
|
19
90
|
return null;
|
|
20
91
|
}
|
|
21
92
|
function notificationContent(m, state) {
|
|
@@ -25,21 +96,52 @@ function notificationContent(m, state) {
|
|
|
25
96
|
const chars = Array.from(m.text.replace(/\s+/g, " ").trim());
|
|
26
97
|
return { title, body: chars.length > 120 ? chars.slice(0, 119).join("") + "\u2026" : chars.join("") };
|
|
27
98
|
}
|
|
28
|
-
function
|
|
99
|
+
function notificationCommand(title, body, group, env) {
|
|
100
|
+
if (env.platform === "darwin" && env.terminalNotifier) {
|
|
101
|
+
const literal = (value) => `\\${value || " "}`;
|
|
102
|
+
const args = ["-title", "Zalo", "-subtitle", literal(title), "-message", literal(body), "-group", `zalo-tui:${group}`];
|
|
103
|
+
if (env.terminalApp) args.push("-activate", env.terminalApp);
|
|
104
|
+
const execute = env.onClick?.(group);
|
|
105
|
+
if (execute) args.push("-execute", execute);
|
|
106
|
+
return [env.terminalNotifier, args];
|
|
107
|
+
}
|
|
108
|
+
if (env.platform === "darwin") {
|
|
109
|
+
return [
|
|
110
|
+
"osascript",
|
|
111
|
+
["-e", "on run argv", "-e", 'display notification (item 2 of argv) with title "Zalo" subtitle (item 1 of argv)', "-e", "end run", title, body]
|
|
112
|
+
];
|
|
113
|
+
}
|
|
114
|
+
if (env.platform === "linux") return ["notify-send", ["--app-name=Zalo", title, body]];
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
var notifierEnv;
|
|
118
|
+
var clickCommand;
|
|
119
|
+
function setNotificationClick(command) {
|
|
120
|
+
clickCommand = command;
|
|
121
|
+
}
|
|
122
|
+
function findOnPath(name) {
|
|
123
|
+
for (const dir of (process.env.PATH ?? "").split(delimiter)) {
|
|
124
|
+
if (!dir) continue;
|
|
125
|
+
const candidate = join(dir, name);
|
|
126
|
+
try {
|
|
127
|
+
accessSync(candidate, constants.X_OK);
|
|
128
|
+
return candidate;
|
|
129
|
+
} catch {
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
function currentEnv() {
|
|
135
|
+
notifierEnv ??= {
|
|
136
|
+
platform: process.platform,
|
|
137
|
+
terminalNotifier: process.platform === "darwin" ? findOnPath("terminal-notifier") : null,
|
|
138
|
+
terminalApp: process.env.__CFBundleIdentifier || void 0
|
|
139
|
+
};
|
|
140
|
+
return { ...notifierEnv, onClick: clickCommand };
|
|
141
|
+
}
|
|
142
|
+
function showNotification(title, body, group) {
|
|
29
143
|
process.stdout.write("\x07");
|
|
30
|
-
const command =
|
|
31
|
-
"osascript",
|
|
32
|
-
[
|
|
33
|
-
"-e",
|
|
34
|
-
"on run argv",
|
|
35
|
-
"-e",
|
|
36
|
-
'display notification (item 2 of argv) with title "Zalo" subtitle (item 1 of argv)',
|
|
37
|
-
"-e",
|
|
38
|
-
"end run",
|
|
39
|
-
title,
|
|
40
|
-
body
|
|
41
|
-
]
|
|
42
|
-
] : process.platform === "linux" ? ["notify-send", ["--app-name=Zalo", title, body]] : null;
|
|
144
|
+
const command = notificationCommand(title, body, group, currentEnv());
|
|
43
145
|
if (!command) return;
|
|
44
146
|
try {
|
|
45
147
|
const child = spawn(command[0], command[1], { stdio: "ignore", detached: true });
|
|
@@ -50,6 +152,74 @@ function showNotification(title, body) {
|
|
|
50
152
|
}
|
|
51
153
|
}
|
|
52
154
|
|
|
155
|
+
// src/openRequest.ts
|
|
156
|
+
import { readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
157
|
+
import { join as join2 } from "node:path";
|
|
158
|
+
var KEY = /^(user|group):\d+$/;
|
|
159
|
+
function openRequestFile(dir, pid = process.pid) {
|
|
160
|
+
return join2(dir, `open-${pid}`);
|
|
161
|
+
}
|
|
162
|
+
function runningMarker(dir, pid = process.pid) {
|
|
163
|
+
return join2(dir, `running-${pid}`);
|
|
164
|
+
}
|
|
165
|
+
function shellQuote(value) {
|
|
166
|
+
return `'${value.replaceAll("'", `'\\''`)}'`;
|
|
167
|
+
}
|
|
168
|
+
function openChatCommand(key, dir, pid = process.pid) {
|
|
169
|
+
if (!KEY.test(key)) return null;
|
|
170
|
+
const marker = shellQuote(runningMarker(dir, pid));
|
|
171
|
+
return `[ -e ${marker} ] && printf %s ${key} > ${shellQuote(openRequestFile(dir, pid))} && kill -USR2 ${pid}`;
|
|
172
|
+
}
|
|
173
|
+
function readOpenRequest(file) {
|
|
174
|
+
let key;
|
|
175
|
+
try {
|
|
176
|
+
key = readFileSync(file, "utf8").trim();
|
|
177
|
+
} catch {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
rmSync(file, { force: true });
|
|
181
|
+
return KEY.test(key) ? key : null;
|
|
182
|
+
}
|
|
183
|
+
function isAlive(pid) {
|
|
184
|
+
try {
|
|
185
|
+
process.kill(pid, 0);
|
|
186
|
+
return true;
|
|
187
|
+
} catch (error) {
|
|
188
|
+
return error.code === "EPERM";
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function removeStaleRequests(dir) {
|
|
192
|
+
let names;
|
|
193
|
+
try {
|
|
194
|
+
names = readdirSync(dir);
|
|
195
|
+
} catch {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
for (const name of names) {
|
|
199
|
+
const match = /^(?:open|running)-(\d+)$/.exec(name);
|
|
200
|
+
if (match && Number(match[1]) !== process.pid && !isAlive(Number(match[1]))) rmSync(join2(dir, name), { force: true });
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function listenForOpenRequests(dir, onOpen) {
|
|
204
|
+
const file = openRequestFile(dir);
|
|
205
|
+
const marker = runningMarker(dir);
|
|
206
|
+
removeStaleRequests(dir);
|
|
207
|
+
const handler = () => {
|
|
208
|
+
const key = readOpenRequest(file);
|
|
209
|
+
if (key) onOpen(key);
|
|
210
|
+
};
|
|
211
|
+
process.on("SIGUSR2", handler);
|
|
212
|
+
try {
|
|
213
|
+
writeFileSync(marker, "", { mode: 384 });
|
|
214
|
+
} catch {
|
|
215
|
+
}
|
|
216
|
+
return () => {
|
|
217
|
+
process.off("SIGUSR2", handler);
|
|
218
|
+
rmSync(marker, { force: true });
|
|
219
|
+
rmSync(file, { force: true });
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
53
223
|
// src/format.ts
|
|
54
224
|
var LABELS = {
|
|
55
225
|
"chat.photo": "photo",
|
|
@@ -78,7 +248,7 @@ function rawMessageText(data) {
|
|
|
78
248
|
if (!content || typeof content !== "object") return `[${label}]`;
|
|
79
249
|
const title = str(content.title);
|
|
80
250
|
const href = str(content.href);
|
|
81
|
-
if (label === "link") return ["[link]", title, href].filter(Boolean).join(" ");
|
|
251
|
+
if (label === "link") return ["[link]", title === href ? "" : title, href].filter(Boolean).join(" ");
|
|
82
252
|
return title ? `[${label}] ${title}` : `[${label}]`;
|
|
83
253
|
}
|
|
84
254
|
function webUrl(value) {
|
|
@@ -166,6 +336,30 @@ function hyperlink(url, text) {
|
|
|
166
336
|
return `\x1B]8;;${safe}\x07${text}\x1B]8;;\x07`;
|
|
167
337
|
}
|
|
168
338
|
|
|
339
|
+
// src/search.ts
|
|
340
|
+
function fold(text) {
|
|
341
|
+
return text.normalize("NFD").replace(new RegExp("\\p{M}", "gu"), "").replace(/đ/g, "d").replace(/Đ/g, "D").toLowerCase();
|
|
342
|
+
}
|
|
343
|
+
function messageMatches(m, query) {
|
|
344
|
+
const q = fold(query.trim());
|
|
345
|
+
if (!q || m.recalled) return false;
|
|
346
|
+
return fold(m.text).includes(q) || !!m.url && fold(m.url).includes(q);
|
|
347
|
+
}
|
|
348
|
+
function searchMessages(messages, query) {
|
|
349
|
+
return messages.filter((m) => messageMatches(m, query)).map((m) => m.id);
|
|
350
|
+
}
|
|
351
|
+
function stepMatch(matches, messages, fromId, direction) {
|
|
352
|
+
if (matches.length === 0) return null;
|
|
353
|
+
const order = new Map(messages.map((m, i) => [m.id, i]));
|
|
354
|
+
const from = fromId !== null && order.has(fromId) ? order.get(fromId) : direction < 0 ? messages.length : -1;
|
|
355
|
+
if (direction < 0) {
|
|
356
|
+
for (let i = matches.length - 1; i >= 0; i--) if (order.get(matches[i]) < from) return matches[i];
|
|
357
|
+
return matches[matches.length - 1];
|
|
358
|
+
}
|
|
359
|
+
for (const id of matches) if (order.get(id) > from) return id;
|
|
360
|
+
return matches[0];
|
|
361
|
+
}
|
|
362
|
+
|
|
169
363
|
// src/store.ts
|
|
170
364
|
function threadKey(kind, id) {
|
|
171
365
|
return `${kind}:${id}`;
|
|
@@ -181,11 +375,17 @@ function initialState() {
|
|
|
181
375
|
threads: {},
|
|
182
376
|
messages: {},
|
|
183
377
|
reactions: {},
|
|
378
|
+
people: {},
|
|
184
379
|
unread: {},
|
|
380
|
+
read: { since: Infinity, chats: {} },
|
|
381
|
+
divider: null,
|
|
185
382
|
activeKey: null,
|
|
383
|
+
activeRead: false,
|
|
186
384
|
connection: { state: "idle" },
|
|
187
385
|
notify: "full",
|
|
188
|
-
|
|
386
|
+
notifyGroups: "all",
|
|
387
|
+
terminalFocused: true,
|
|
388
|
+
openRequest: null
|
|
189
389
|
};
|
|
190
390
|
}
|
|
191
391
|
function upsertThreads(state, threads) {
|
|
@@ -197,20 +397,61 @@ function upsertThreads(state, threads) {
|
|
|
197
397
|
}
|
|
198
398
|
return { ...state, threads: next };
|
|
199
399
|
}
|
|
200
|
-
function
|
|
400
|
+
function readPoint(state, key) {
|
|
401
|
+
let point = state.read.chats[key] ?? state.read.since;
|
|
402
|
+
for (const m of state.messages[key] ?? []) if (m.isSelf && m.ts > point) point = m.ts;
|
|
403
|
+
return point;
|
|
404
|
+
}
|
|
405
|
+
function recount(state, keys) {
|
|
406
|
+
let unread = state.unread;
|
|
407
|
+
for (const key of keys) {
|
|
408
|
+
const point = readPoint(state, key);
|
|
409
|
+
const count = (state.messages[key] ?? []).filter((m) => !m.isSelf && !m.recalled && m.ts > point).length;
|
|
410
|
+
if ((unread[key] ?? 0) === count) continue;
|
|
411
|
+
if (unread === state.unread) unread = { ...unread };
|
|
412
|
+
if (count > 0) unread[key] = count;
|
|
413
|
+
else delete unread[key];
|
|
414
|
+
}
|
|
415
|
+
return unread === state.unread ? state : { ...state, unread };
|
|
416
|
+
}
|
|
417
|
+
function newestTs(state, key) {
|
|
418
|
+
const list = state.messages[key] ?? [];
|
|
419
|
+
return list.length > 0 ? list[list.length - 1].ts : 0;
|
|
420
|
+
}
|
|
421
|
+
function markRead(state, key, now = Date.now()) {
|
|
422
|
+
const newest = newestTs(state, key);
|
|
423
|
+
const ts = Math.max(state.read.chats[key] ?? 0, newest || now);
|
|
424
|
+
const next = { ...state, read: { ...state.read, chats: { ...state.read.chats, [key]: ts } } };
|
|
425
|
+
return recount(next, [key]);
|
|
426
|
+
}
|
|
427
|
+
function applyReadState(state, read) {
|
|
428
|
+
const chats = {};
|
|
429
|
+
for (const [key, ts] of Object.entries(read.chats ?? {})) if (typeof ts === "number" && key.includes(":")) chats[key] = ts;
|
|
430
|
+
const since = typeof read.since === "number" ? read.since : Date.now();
|
|
431
|
+
return recount({ ...state, read: { since, chats } }, Object.keys(state.messages));
|
|
432
|
+
}
|
|
433
|
+
function addMessages(state, incoming) {
|
|
201
434
|
if (incoming.length === 0) return state;
|
|
202
435
|
const messages = { ...state.messages };
|
|
203
436
|
const threads = { ...state.threads };
|
|
204
|
-
const unread = { ...state.unread };
|
|
205
437
|
const touched = /* @__PURE__ */ new Set();
|
|
206
438
|
for (const raw of incoming) {
|
|
207
|
-
const msg = {
|
|
439
|
+
const msg = {
|
|
440
|
+
...raw,
|
|
441
|
+
text: cleanText(raw.text),
|
|
442
|
+
senderName: cleanText(raw.senderName),
|
|
443
|
+
quote: raw.quote ? { from: cleanText(raw.quote.from), text: cleanText(raw.quote.text) } : void 0
|
|
444
|
+
};
|
|
208
445
|
const list = touched.has(msg.threadKey) ? messages[msg.threadKey] : [...messages[msg.threadKey] ?? []];
|
|
209
446
|
touched.add(msg.threadKey);
|
|
210
447
|
messages[msg.threadKey] = list;
|
|
211
448
|
const existing = list.findIndex((m) => m.id === msg.id);
|
|
212
449
|
if (existing !== -1) {
|
|
213
|
-
|
|
450
|
+
const old = list[existing];
|
|
451
|
+
const serverTs = old.isSelf && msg.source && !old.source && msg.ts !== old.ts;
|
|
452
|
+
if (msg.cliMsgId && !old.cliMsgId || msg.source && !old.source) {
|
|
453
|
+
list[existing] = { ...old, cliMsgId: old.cliMsgId ?? msg.cliMsgId, source: old.source ?? msg.source, ts: serverTs ? msg.ts : old.ts };
|
|
454
|
+
}
|
|
214
455
|
continue;
|
|
215
456
|
}
|
|
216
457
|
list.push(msg);
|
|
@@ -222,20 +463,19 @@ function addMessages(state, incoming, countUnread) {
|
|
|
222
463
|
} else if (msg.ts > thread.lastTs) {
|
|
223
464
|
threads[msg.threadKey] = { ...thread, lastTs: msg.ts };
|
|
224
465
|
}
|
|
225
|
-
if (countUnread && !msg.isSelf && msg.threadKey !== state.activeKey) {
|
|
226
|
-
unread[msg.threadKey] = (unread[msg.threadKey] ?? 0) + 1;
|
|
227
|
-
}
|
|
228
466
|
}
|
|
229
467
|
for (const key of touched) messages[key].sort((a, b) => a.ts - b.ts);
|
|
230
|
-
|
|
468
|
+
let next = { ...state, messages, threads };
|
|
469
|
+
if (state.activeKey && state.activeRead && state.terminalFocused && touched.has(state.activeKey)) next = markRead(next, state.activeKey, 0);
|
|
470
|
+
return recount(next, touched);
|
|
231
471
|
}
|
|
232
472
|
function markRecalled(state, key, msgId) {
|
|
233
473
|
const list = state.messages[key];
|
|
234
474
|
if (!list || !list.some((m) => m.id === msgId)) return state;
|
|
235
|
-
return {
|
|
475
|
+
return recount({
|
|
236
476
|
...state,
|
|
237
477
|
messages: { ...state.messages, [key]: list.map((m) => m.id === msgId ? { ...m, recalled: true } : m) }
|
|
238
|
-
};
|
|
478
|
+
}, [key]);
|
|
239
479
|
}
|
|
240
480
|
function reactionKey(threadKey2, msgId) {
|
|
241
481
|
return `${threadKey2}|${msgId}`;
|
|
@@ -245,33 +485,66 @@ function applyReaction(state, threadKey2, msgIds, uid, icon, ts) {
|
|
|
245
485
|
for (const msgId of msgIds) {
|
|
246
486
|
const key = reactionKey(threadKey2, msgId);
|
|
247
487
|
const current = reactions[key] ?? {};
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (icon)
|
|
251
|
-
|
|
488
|
+
const entry = current[uid] ?? { icons: {}, cleared: -Infinity };
|
|
489
|
+
let updated;
|
|
490
|
+
if (icon) {
|
|
491
|
+
if (ts <= entry.cleared || (entry.icons[icon] ?? -Infinity) >= ts) continue;
|
|
492
|
+
updated = { ...entry, icons: { ...entry.icons, [icon]: ts } };
|
|
493
|
+
} else {
|
|
494
|
+
if (ts <= entry.cleared) continue;
|
|
495
|
+
updated = { icons: Object.fromEntries(Object.entries(entry.icons).filter(([, at]) => at > ts)), cleared: ts };
|
|
496
|
+
}
|
|
497
|
+
const next = { ...current, [uid]: updated };
|
|
252
498
|
reactions = { ...reactions, [key]: next };
|
|
253
499
|
}
|
|
254
500
|
return reactions === state.reactions ? state : { ...state, reactions };
|
|
255
501
|
}
|
|
256
502
|
function reactionSummary(entries, selfId = "") {
|
|
257
503
|
const counts = /* @__PURE__ */ new Map();
|
|
258
|
-
for (const [uid, {
|
|
259
|
-
const
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
504
|
+
for (const [uid, { icons }] of Object.entries(entries ?? {})) {
|
|
505
|
+
for (const icon of Object.keys(icons)) {
|
|
506
|
+
const entry = counts.get(icon) ?? { icon, count: 0, mine: false };
|
|
507
|
+
entry.count += 1;
|
|
508
|
+
if (selfId && uid === selfId) entry.mine = true;
|
|
509
|
+
counts.set(icon, entry);
|
|
510
|
+
}
|
|
263
511
|
}
|
|
264
512
|
return [...counts.values()].sort((a, b) => b.count - a.count || a.icon.localeCompare(b.icon));
|
|
265
513
|
}
|
|
514
|
+
function personName(state, uid) {
|
|
515
|
+
return state.people[uid] ?? (state.threads[`user:${uid}`]?.placeholder ? null : state.threads[`user:${uid}`]?.name ?? null);
|
|
516
|
+
}
|
|
517
|
+
function reactionOwners(entries, state) {
|
|
518
|
+
const owners = [];
|
|
519
|
+
for (const [uid, { icons }] of Object.entries(entries ?? {})) {
|
|
520
|
+
const sorted = Object.entries(icons).sort((a, b) => a[1] - b[1]);
|
|
521
|
+
if (sorted.length === 0) continue;
|
|
522
|
+
const mine = !!state.selfId && uid === state.selfId;
|
|
523
|
+
owners.push({ uid, name: mine ? "You" : personName(state, uid), icons: sorted.map(([icon]) => icon), ts: sorted[sorted.length - 1][1], mine });
|
|
524
|
+
}
|
|
525
|
+
return owners.sort((a, b) => b.ts - a.ts || a.uid.localeCompare(b.uid));
|
|
526
|
+
}
|
|
527
|
+
function rememberPeople(state, names) {
|
|
528
|
+
const changed = Object.entries(names).map(([uid, name]) => [uid, cleanText(name)]).filter(([uid, name]) => uid && name && name !== uid && state.people[uid] !== name);
|
|
529
|
+
if (changed.length === 0) return state;
|
|
530
|
+
return { ...state, people: { ...state.people, ...Object.fromEntries(changed) } };
|
|
531
|
+
}
|
|
266
532
|
function removeMessage(state, key, msgId) {
|
|
267
533
|
const list = state.messages[key];
|
|
268
534
|
if (!list || !list.some((m) => m.id === msgId)) return state;
|
|
269
|
-
return { ...state, messages: { ...state.messages, [key]: list.filter((m) => m.id !== msgId) } };
|
|
535
|
+
return recount({ ...state, messages: { ...state.messages, [key]: list.filter((m) => m.id !== msgId) } }, [key]);
|
|
536
|
+
}
|
|
537
|
+
function openThread(state, key, read = true) {
|
|
538
|
+
const opened = { ...state, activeKey: key, activeRead: read };
|
|
539
|
+
if (!read) return opened;
|
|
540
|
+
const divider = state.unread[key] ? { key, ts: readPoint(state, key) } : state.divider?.key === key ? state.divider : null;
|
|
541
|
+
return markRead({ ...opened, divider }, key);
|
|
270
542
|
}
|
|
271
|
-
function
|
|
272
|
-
const
|
|
273
|
-
|
|
274
|
-
|
|
543
|
+
function dividerTs(state) {
|
|
544
|
+
const key = state.activeKey;
|
|
545
|
+
if (!key) return null;
|
|
546
|
+
if (state.divider?.key === key) return state.divider.ts;
|
|
547
|
+
return state.unread[key] ? readPoint(state, key) : null;
|
|
275
548
|
}
|
|
276
549
|
function setConnection(state, connection) {
|
|
277
550
|
return { ...state, connection };
|
|
@@ -298,8 +571,10 @@ function applyActivity(state, activity) {
|
|
|
298
571
|
return { ...state, threads };
|
|
299
572
|
}
|
|
300
573
|
function sortedThreads(state, filter = "") {
|
|
301
|
-
const q = filter.trim()
|
|
302
|
-
const list = Object.values(state.threads).filter(
|
|
574
|
+
const q = fold(filter.trim());
|
|
575
|
+
const list = Object.values(state.threads).filter(
|
|
576
|
+
(t) => !q || fold(t.name).includes(q) || (state.messages[t.key] ?? []).some((m) => messageMatches(m, filter))
|
|
577
|
+
);
|
|
303
578
|
return list.sort((a, b) => {
|
|
304
579
|
if (a.lastTs !== b.lastTs) return b.lastTs - a.lastTs;
|
|
305
580
|
return a.name.localeCompare(b.name);
|
|
@@ -467,7 +742,7 @@ function classifyInput(text, isFile2, home) {
|
|
|
467
742
|
}
|
|
468
743
|
|
|
469
744
|
// src/qr.ts
|
|
470
|
-
import { writeFileSync } from "node:fs";
|
|
745
|
+
import { writeFileSync as writeFileSync2 } from "node:fs";
|
|
471
746
|
import jsQR from "jsqr";
|
|
472
747
|
import { PNG } from "pngjs";
|
|
473
748
|
import QRCode from "qrcode";
|
|
@@ -482,7 +757,7 @@ async function renderQR(base64Png, fallbackPath) {
|
|
|
482
757
|
}
|
|
483
758
|
} catch {
|
|
484
759
|
}
|
|
485
|
-
|
|
760
|
+
writeFileSync2(fallbackPath, buffer, { mode: 384 });
|
|
486
761
|
return { path: fallbackPath };
|
|
487
762
|
}
|
|
488
763
|
|
|
@@ -610,38 +885,51 @@ function metaAction(msgType, content) {
|
|
|
610
885
|
}
|
|
611
886
|
|
|
612
887
|
// src/zalo/session.ts
|
|
613
|
-
import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync as
|
|
888
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync as readFileSync2, rmSync as rmSync2, writeFileSync as writeFileSync3 } from "node:fs";
|
|
614
889
|
import { homedir as homedir2 } from "node:os";
|
|
615
|
-
import { join } from "node:path";
|
|
890
|
+
import { join as join3 } from "node:path";
|
|
616
891
|
import { imageSizeFromFile } from "image-size/fromFile";
|
|
617
892
|
import { statSync } from "node:fs";
|
|
618
893
|
import { Zalo } from "zca-js";
|
|
619
|
-
var stateDir = process.env.ZALO_TUI_HOME ??
|
|
620
|
-
var credentialsPath =
|
|
621
|
-
var qrPath =
|
|
622
|
-
var settingsPath =
|
|
894
|
+
var stateDir = process.env.ZALO_TUI_HOME ?? join3(homedir2(), ".config", "zalo-tui");
|
|
895
|
+
var credentialsPath = join3(stateDir, "credentials.json");
|
|
896
|
+
var qrPath = join3(stateDir, "qr.png");
|
|
897
|
+
var settingsPath = join3(stateDir, "settings.json");
|
|
623
898
|
function loadSettings() {
|
|
624
899
|
try {
|
|
625
|
-
return JSON.parse(
|
|
900
|
+
return JSON.parse(readFileSync2(settingsPath, "utf8"));
|
|
626
901
|
} catch {
|
|
627
902
|
return {};
|
|
628
903
|
}
|
|
629
904
|
}
|
|
630
905
|
function saveSettings(settings) {
|
|
631
906
|
ensureStateDir();
|
|
632
|
-
|
|
907
|
+
writeFileSync3(settingsPath, JSON.stringify({ ...loadSettings(), ...settings }), { mode: 384 });
|
|
633
908
|
}
|
|
634
|
-
var activityPath =
|
|
909
|
+
var activityPath = join3(stateDir, "activity.json");
|
|
635
910
|
function loadActivity() {
|
|
636
911
|
try {
|
|
637
|
-
return JSON.parse(
|
|
912
|
+
return JSON.parse(readFileSync2(activityPath, "utf8"));
|
|
638
913
|
} catch {
|
|
639
914
|
return {};
|
|
640
915
|
}
|
|
641
916
|
}
|
|
642
917
|
function saveActivity(activity) {
|
|
643
918
|
ensureStateDir();
|
|
644
|
-
|
|
919
|
+
writeFileSync3(activityPath, JSON.stringify(activity), { mode: 384 });
|
|
920
|
+
}
|
|
921
|
+
var readPath2 = join3(stateDir, "read.json");
|
|
922
|
+
function loadReadState() {
|
|
923
|
+
try {
|
|
924
|
+
const value = JSON.parse(readFileSync2(readPath2, "utf8"));
|
|
925
|
+
return value && typeof value.since === "number" && value.chats && typeof value.chats === "object" ? value : null;
|
|
926
|
+
} catch {
|
|
927
|
+
return null;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
function saveReadState(read) {
|
|
931
|
+
ensureStateDir();
|
|
932
|
+
writeFileSync3(readPath2, JSON.stringify(read), { mode: 384 });
|
|
645
933
|
}
|
|
646
934
|
function ensureStateDir() {
|
|
647
935
|
mkdirSync(stateDir, { recursive: true, mode: 448 });
|
|
@@ -650,7 +938,7 @@ function ensureStateDir() {
|
|
|
650
938
|
function loadCredentials() {
|
|
651
939
|
if (!existsSync(credentialsPath)) return null;
|
|
652
940
|
try {
|
|
653
|
-
const parsed = JSON.parse(
|
|
941
|
+
const parsed = JSON.parse(readFileSync2(credentialsPath, "utf8"));
|
|
654
942
|
return parsed.imei && parsed.cookie && parsed.userAgent ? parsed : null;
|
|
655
943
|
} catch {
|
|
656
944
|
return null;
|
|
@@ -658,7 +946,7 @@ function loadCredentials() {
|
|
|
658
946
|
}
|
|
659
947
|
function saveCredentials(credentials) {
|
|
660
948
|
ensureStateDir();
|
|
661
|
-
|
|
949
|
+
writeFileSync3(credentialsPath, JSON.stringify(credentials), { mode: 384 });
|
|
662
950
|
chmodSync(credentialsPath, 384);
|
|
663
951
|
}
|
|
664
952
|
async function imageMetadataGetter(filePath) {
|
|
@@ -683,18 +971,58 @@ function loginWithQR(callback) {
|
|
|
683
971
|
|
|
684
972
|
// src/debug.ts
|
|
685
973
|
import { appendFileSync } from "node:fs";
|
|
686
|
-
import { join as
|
|
974
|
+
import { join as join4 } from "node:path";
|
|
687
975
|
var enabled = !!process.env.ZALO_TUI_DEBUG;
|
|
688
976
|
function debugLog(line) {
|
|
689
977
|
if (!enabled) return;
|
|
690
978
|
try {
|
|
691
979
|
ensureStateDir();
|
|
692
|
-
appendFileSync(
|
|
980
|
+
appendFileSync(join4(stateDir, "debug.log"), `${(/* @__PURE__ */ new Date()).toISOString()} ${line}
|
|
693
981
|
`, { mode: 384 });
|
|
694
982
|
} catch {
|
|
695
983
|
}
|
|
696
984
|
}
|
|
697
985
|
|
|
986
|
+
// src/mentions.ts
|
|
987
|
+
var EVERYONE = { uid: "-1", name: "All" };
|
|
988
|
+
function mentionQuery(chars, cursor) {
|
|
989
|
+
let i = cursor;
|
|
990
|
+
while (i > 0 && chars[i - 1] !== "@" && !/\s/.test(chars[i - 1])) i--;
|
|
991
|
+
if (i === 0 || chars[i - 1] !== "@") return null;
|
|
992
|
+
if (i > 1 && !/\s/.test(chars[i - 2])) return null;
|
|
993
|
+
return { start: i - 1, query: chars.slice(i, cursor).join("") };
|
|
994
|
+
}
|
|
995
|
+
function filterMembers(members, query, limit = 5) {
|
|
996
|
+
const q = fold(query);
|
|
997
|
+
const starts = [];
|
|
998
|
+
const contains = [];
|
|
999
|
+
for (const m of members) {
|
|
1000
|
+
const name = fold(m.name);
|
|
1001
|
+
if (!q || name.startsWith(q) || name.split(/\s+/).some((w) => w.startsWith(q))) starts.push(m);
|
|
1002
|
+
else if (name.includes(q)) contains.push(m);
|
|
1003
|
+
}
|
|
1004
|
+
return [...starts, ...contains].slice(0, limit);
|
|
1005
|
+
}
|
|
1006
|
+
function mentionPositions(text, picks) {
|
|
1007
|
+
const used = /* @__PURE__ */ new Set();
|
|
1008
|
+
const out = [];
|
|
1009
|
+
for (const pick of picks) {
|
|
1010
|
+
const token = `@${pick.name}`;
|
|
1011
|
+
let from = 0;
|
|
1012
|
+
while (true) {
|
|
1013
|
+
const pos = text.indexOf(token, from);
|
|
1014
|
+
if (pos === -1) break;
|
|
1015
|
+
from = pos + 1;
|
|
1016
|
+
const after = text[pos + token.length];
|
|
1017
|
+
if (used.has(pos) || after !== void 0 && /[\p{L}\p{N}_]/u.test(after)) continue;
|
|
1018
|
+
used.add(pos);
|
|
1019
|
+
out.push({ pos, uid: pick.uid, len: token.length });
|
|
1020
|
+
break;
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
return out.sort((a, b) => a.pos - b.pos);
|
|
1024
|
+
}
|
|
1025
|
+
|
|
698
1026
|
// src/zalo/client.ts
|
|
699
1027
|
var ATTACHMENT_TIMEOUT_MS = 9e4;
|
|
700
1028
|
var REACTION_FALLBACK_MS = 3e3;
|
|
@@ -723,6 +1051,8 @@ var ZaloClient = class {
|
|
|
723
1051
|
historyLoaded = /* @__PURE__ */ new Set();
|
|
724
1052
|
muted = /* @__PURE__ */ new Map();
|
|
725
1053
|
lastNotified = /* @__PURE__ */ new Map();
|
|
1054
|
+
lookedUp = /* @__PURE__ */ new Set();
|
|
1055
|
+
members = /* @__PURE__ */ new Map();
|
|
726
1056
|
selfId = "";
|
|
727
1057
|
deleted = /* @__PURE__ */ new Set();
|
|
728
1058
|
recalledIds = /* @__PURE__ */ new Set();
|
|
@@ -730,6 +1060,7 @@ var ZaloClient = class {
|
|
|
730
1060
|
historyPending = /* @__PURE__ */ new Set();
|
|
731
1061
|
reactionTimer = null;
|
|
732
1062
|
savedActivity = "";
|
|
1063
|
+
savedRead = "";
|
|
733
1064
|
saveTimer = null;
|
|
734
1065
|
unsubscribe = null;
|
|
735
1066
|
toChatMessage(m) {
|
|
@@ -745,9 +1076,29 @@ var ZaloClient = class {
|
|
|
745
1076
|
ts: Number(m.data.ts) || Date.now(),
|
|
746
1077
|
text: messageText(m.data),
|
|
747
1078
|
mentionsMe: mentions.some((x) => x.uid === this.selfId || x.uid === "-1" || x.type === 1),
|
|
748
|
-
url: attachmentUrl(m.data.content)
|
|
1079
|
+
url: attachmentUrl(m.data.content),
|
|
1080
|
+
quote: this.quoteOf(m),
|
|
1081
|
+
source: {
|
|
1082
|
+
content: m.data.content,
|
|
1083
|
+
msgType: m.data.msgType,
|
|
1084
|
+
propertyExt: m.data.propertyExt,
|
|
1085
|
+
uidFrom: m.isSelf ? this.selfId || String(m.data.uidFrom) : String(m.data.uidFrom),
|
|
1086
|
+
msgId: String(m.data.msgId),
|
|
1087
|
+
cliMsgId: String(m.data.cliMsgId ?? ""),
|
|
1088
|
+
ts: String(m.data.ts),
|
|
1089
|
+
ttl: Number(m.data.ttl) || 0
|
|
1090
|
+
}
|
|
749
1091
|
};
|
|
750
1092
|
}
|
|
1093
|
+
quoteOf(m) {
|
|
1094
|
+
const q = m.data.quote;
|
|
1095
|
+
if (!q) return void 0;
|
|
1096
|
+
const owner = String(q.ownerId ?? "");
|
|
1097
|
+
const state = this.store.getState();
|
|
1098
|
+
const from = owner && owner === this.selfId ? "me" : q.fromD || owner && personName(state, owner) || "someone";
|
|
1099
|
+
const text = typeof q.msg === "string" && q.msg.trim() ? q.msg : q.attach ? "[attachment]" : "[message]";
|
|
1100
|
+
return { from, text };
|
|
1101
|
+
}
|
|
751
1102
|
async init() {
|
|
752
1103
|
const selfId = this.api.getOwnId();
|
|
753
1104
|
this.selfId = selfId;
|
|
@@ -769,6 +1120,8 @@ var ZaloClient = class {
|
|
|
769
1120
|
const activity = loadActivity();
|
|
770
1121
|
this.savedActivity = JSON.stringify(activity);
|
|
771
1122
|
this.store.update((s) => applyActivity(s, activity));
|
|
1123
|
+
const read = loadReadState() ?? { since: Date.now(), chats: {} };
|
|
1124
|
+
this.store.update((s) => applyReadState(s, read));
|
|
772
1125
|
for (const t of Object.values(this.store.getState().threads)) if (t.placeholder) void this.resolveName(t.key);
|
|
773
1126
|
this.unsubscribe = this.store.subscribe(() => this.scheduleSave());
|
|
774
1127
|
this.attachListener();
|
|
@@ -794,12 +1147,22 @@ var ZaloClient = class {
|
|
|
794
1147
|
}, 2e3);
|
|
795
1148
|
}
|
|
796
1149
|
saveNow() {
|
|
797
|
-
const
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
1150
|
+
const state = this.store.getState();
|
|
1151
|
+
const json = JSON.stringify(activitySnapshot(state));
|
|
1152
|
+
if (json !== this.savedActivity) {
|
|
1153
|
+
try {
|
|
1154
|
+
saveActivity(JSON.parse(json));
|
|
1155
|
+
this.savedActivity = json;
|
|
1156
|
+
} catch {
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
const read = JSON.stringify(state.read);
|
|
1160
|
+
if (read !== this.savedRead && Number.isFinite(state.read.since)) {
|
|
1161
|
+
try {
|
|
1162
|
+
saveReadState(state.read);
|
|
1163
|
+
this.savedRead = read;
|
|
1164
|
+
} catch {
|
|
1165
|
+
}
|
|
803
1166
|
}
|
|
804
1167
|
}
|
|
805
1168
|
async loadMuted() {
|
|
@@ -815,12 +1178,12 @@ var ZaloClient = class {
|
|
|
815
1178
|
const now = Date.now();
|
|
816
1179
|
for (const m of messages) {
|
|
817
1180
|
const kind = state.threads[m.threadKey]?.kind ?? "unknown";
|
|
818
|
-
const skip = notifySkipReason(m, state, this.muted, now) ?? (now - (this.lastNotified.get(m.threadKey) ?? 0) < 5e3 ? "throttled" : null);
|
|
1181
|
+
const skip = notifySkipReason(m, state, this.muted, now) ?? (!m.mentionsMe && now - (this.lastNotified.get(m.threadKey) ?? 0) < 5e3 ? "throttled" : null);
|
|
819
1182
|
debugLog(`notify ${kind} ${skip ? `skipped: ${skip}` : "shown"} (terminal ${state.terminalFocused ? "focused" : "unfocused"})`);
|
|
820
1183
|
if (skip) continue;
|
|
821
1184
|
this.lastNotified.set(m.threadKey, now);
|
|
822
1185
|
const { title, body } = notificationContent(m, state);
|
|
823
|
-
showNotification(title, body);
|
|
1186
|
+
showNotification(title, body, m.threadKey);
|
|
824
1187
|
}
|
|
825
1188
|
}
|
|
826
1189
|
attachListener() {
|
|
@@ -888,8 +1251,10 @@ var ZaloClient = class {
|
|
|
888
1251
|
const converted = regular.map((m) => this.toChatMessage(m)).filter((m) => !this.deleted.has(m.id)).map((m) => this.recalledIds.has(m.id) ? { ...m, recalled: true } : m);
|
|
889
1252
|
const before = this.store.getState().messages;
|
|
890
1253
|
const fresh = converted.filter((m) => !before[m.threadKey]?.some((x) => x.id === m.id));
|
|
1254
|
+
const senders = {};
|
|
1255
|
+
for (const m of regular) if (!m.isSelf && m.data.dName) senders[String(m.data.uidFrom)] = m.data.dName;
|
|
891
1256
|
this.store.update((s) => {
|
|
892
|
-
let next = addMessages(s, converted,
|
|
1257
|
+
let next = rememberPeople(addMessages(s, converted), senders);
|
|
893
1258
|
for (const { key, action } of actions) {
|
|
894
1259
|
for (const id of action.ids) next = action.kind === "recall" ? markRecalled(next, key, id) : removeMessage(next, key, id);
|
|
895
1260
|
}
|
|
@@ -916,6 +1281,24 @@ var ZaloClient = class {
|
|
|
916
1281
|
} catch {
|
|
917
1282
|
}
|
|
918
1283
|
}
|
|
1284
|
+
async lookupPeople(uids) {
|
|
1285
|
+
const state = this.store.getState();
|
|
1286
|
+
const missing = [...new Set(uids)].filter(
|
|
1287
|
+
(uid) => /^\d+$/.test(uid) && uid !== state.selfId && !personName(state, uid) && !this.lookedUp.has(uid)
|
|
1288
|
+
);
|
|
1289
|
+
if (missing.length === 0) return;
|
|
1290
|
+
for (const uid of missing) this.lookedUp.add(uid);
|
|
1291
|
+
try {
|
|
1292
|
+
const { changed_profiles } = await this.api.getUserInfo(missing);
|
|
1293
|
+
const names = {};
|
|
1294
|
+
for (const [key, p] of Object.entries(changed_profiles ?? {})) {
|
|
1295
|
+
const name = p?.displayName || p?.zaloName;
|
|
1296
|
+
if (name) names[key.split("_")[0]] = name;
|
|
1297
|
+
}
|
|
1298
|
+
this.store.update((s) => rememberPeople(s, names));
|
|
1299
|
+
} catch {
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
919
1302
|
connect() {
|
|
920
1303
|
this.stopping = false;
|
|
921
1304
|
this.store.update((s) => setConnection(s, { state: "connecting" }));
|
|
@@ -938,14 +1321,45 @@ var ZaloClient = class {
|
|
|
938
1321
|
this.historyLoaded.delete(key);
|
|
939
1322
|
}
|
|
940
1323
|
}
|
|
941
|
-
|
|
1324
|
+
groupMembers(key) {
|
|
1325
|
+
const cached = this.members.get(key);
|
|
1326
|
+
if (cached) return cached;
|
|
1327
|
+
const load = this.loadMembers(key);
|
|
1328
|
+
this.members.set(key, load);
|
|
1329
|
+
load.catch(() => this.members.delete(key));
|
|
1330
|
+
return load;
|
|
1331
|
+
}
|
|
1332
|
+
async loadMembers(key) {
|
|
1333
|
+
const { kind, id } = parseKey(key);
|
|
1334
|
+
if (kind !== "group") return [];
|
|
1335
|
+
const info = (await this.api.getGroupInfo(id)).gridInfoMap[id];
|
|
1336
|
+
const ids = info?.memberIds?.length ? info.memberIds : (info?.memVerList ?? []).map((v) => v.split("_")[0]);
|
|
1337
|
+
const uids = [...new Set(ids.map(String))].filter((uid) => uid && uid !== this.selfId);
|
|
1338
|
+
const state = this.store.getState();
|
|
1339
|
+
const missing = uids.filter((uid) => !personName(state, uid));
|
|
1340
|
+
for (const batch of chunk(missing, 50)) {
|
|
1341
|
+
const res = await this.api.getGroupMembersInfo(batch).catch(() => null);
|
|
1342
|
+
const names = {};
|
|
1343
|
+
for (const [k, p] of Object.entries(res?.profiles ?? {})) {
|
|
1344
|
+
const name = p?.displayName || p?.zaloName;
|
|
1345
|
+
if (name) names[(p?.id || k).split("_")[0]] = name;
|
|
1346
|
+
}
|
|
1347
|
+
this.store.update((s) => rememberPeople(s, names));
|
|
1348
|
+
}
|
|
1349
|
+
return uids;
|
|
1350
|
+
}
|
|
1351
|
+
async send(key, text, replyTo, picks = []) {
|
|
942
1352
|
const { kind, id } = parseKey(key);
|
|
943
1353
|
const type = kind === "group" ? ThreadType.Group : ThreadType.User;
|
|
944
|
-
const
|
|
1354
|
+
const quote = replyTo?.source && replyTo.source.cliMsgId ? replyTo.source : void 0;
|
|
1355
|
+
if (replyTo && !quote) throw new Error("this message can't be replied to yet");
|
|
1356
|
+
const mentions = kind === "group" ? mentionPositions(text, picks) : [];
|
|
1357
|
+
const result = await this.api.sendMessage({ msg: text, quote, mentions: mentions.length ? mentions : void 0 }, id, type);
|
|
945
1358
|
const msgId = result.message?.msgId;
|
|
946
1359
|
if (msgId === void 0) throw new Error("Zalo did not accept the message");
|
|
1360
|
+
const quoted = replyTo ? { from: replyTo.isSelf ? "me" : replyTo.senderName, text: replyTo.text } : void 0;
|
|
947
1361
|
this.store.update(
|
|
948
|
-
(s) => addMessages(s, [{ id: String(msgId), threadKey: key, senderName: "me", isSelf: true, ts: Date.now(), text }]
|
|
1362
|
+
(s) => addMessages(s, [{ id: String(msgId), threadKey: key, senderName: "me", isSelf: true, ts: Date.now(), text, quote: quoted }])
|
|
949
1363
|
);
|
|
950
1364
|
}
|
|
951
1365
|
async sendAttachment(key, request) {
|
|
@@ -972,7 +1386,7 @@ var ZaloClient = class {
|
|
|
972
1386
|
}
|
|
973
1387
|
const caption = request.kind === "photo" && request.caption ? ` ${request.caption}` : "";
|
|
974
1388
|
local.push({ id: String(sent), threadKey: key, senderName: "me", isSelf: true, ts: now, text: `[${request.kind}] ${name}${caption}` });
|
|
975
|
-
this.store.update((s) => addMessages(s, local
|
|
1389
|
+
this.store.update((s) => addMessages(s, local));
|
|
976
1390
|
}
|
|
977
1391
|
destination(message) {
|
|
978
1392
|
if (!message.cliMsgId) throw new Error("this message can't be changed yet");
|
|
@@ -1075,22 +1489,97 @@ function ChatList({ threads, selected, activeKey, unread, filter, filtering, foc
|
|
|
1075
1489
|
// src/ui/Composer.tsx
|
|
1076
1490
|
import { Box as Box2, Text as Text2, useInput, usePaste } from "ink";
|
|
1077
1491
|
import { useEffect, useRef, useState } from "react";
|
|
1078
|
-
|
|
1079
|
-
|
|
1492
|
+
|
|
1493
|
+
// src/ui/composerLayout.ts
|
|
1494
|
+
import stringWidth from "string-width";
|
|
1495
|
+
function charWidth(ch) {
|
|
1496
|
+
return ch === "\n" ? 0 : Math.max(1, stringWidth(ch));
|
|
1497
|
+
}
|
|
1498
|
+
function composerLines(chars, width, measure = charWidth) {
|
|
1499
|
+
const w = Math.max(2, width);
|
|
1500
|
+
const lines = [];
|
|
1501
|
+
let start = 0;
|
|
1502
|
+
let col = 0;
|
|
1503
|
+
for (let i = 0; i <= chars.length; i++) {
|
|
1504
|
+
if (i === chars.length) {
|
|
1505
|
+
lines.push({ start, end: i, soft: false });
|
|
1506
|
+
if (col >= w) {
|
|
1507
|
+
lines[lines.length - 1].soft = true;
|
|
1508
|
+
lines.push({ start: i, end: i, soft: false });
|
|
1509
|
+
}
|
|
1510
|
+
break;
|
|
1511
|
+
}
|
|
1512
|
+
if (chars[i] === "\n") {
|
|
1513
|
+
lines.push({ start, end: i, soft: false });
|
|
1514
|
+
start = i + 1;
|
|
1515
|
+
col = 0;
|
|
1516
|
+
continue;
|
|
1517
|
+
}
|
|
1518
|
+
const cw = measure(chars[i]);
|
|
1519
|
+
if (col + cw > w && i > start) {
|
|
1520
|
+
lines.push({ start, end: i, soft: true });
|
|
1521
|
+
start = i;
|
|
1522
|
+
col = 0;
|
|
1523
|
+
}
|
|
1524
|
+
col += cw;
|
|
1525
|
+
}
|
|
1526
|
+
return lines;
|
|
1527
|
+
}
|
|
1528
|
+
function cursorLine(lines, cursor) {
|
|
1529
|
+
let index = 0;
|
|
1530
|
+
lines.forEach((l, i) => {
|
|
1531
|
+
if (l.start <= cursor) index = i;
|
|
1532
|
+
});
|
|
1533
|
+
return index;
|
|
1534
|
+
}
|
|
1535
|
+
function composerWindow(lines, cursor, maxRows) {
|
|
1536
|
+
const rows = Math.max(1, Math.min(lines.length, maxRows));
|
|
1537
|
+
const line = cursorLine(lines, cursor);
|
|
1538
|
+
let top = lines.length - rows;
|
|
1539
|
+
if (line < top) top = line;
|
|
1540
|
+
return { top, rows };
|
|
1541
|
+
}
|
|
1542
|
+
function verticalMove(lines, chars, cursor, direction, measure = charWidth) {
|
|
1543
|
+
const line = cursorLine(lines, cursor);
|
|
1544
|
+
const target = lines[line + direction];
|
|
1545
|
+
if (!target) return null;
|
|
1546
|
+
let column = 0;
|
|
1547
|
+
for (let i = lines[line].start; i < cursor; i++) column += measure(chars[i]);
|
|
1548
|
+
const last = target.soft && target.end > target.start ? target.end - 1 : target.end;
|
|
1549
|
+
let pos = target.start;
|
|
1550
|
+
let col = 0;
|
|
1551
|
+
while (pos < last && col + measure(chars[pos]) <= column) col += measure(chars[pos++]);
|
|
1552
|
+
return pos;
|
|
1553
|
+
}
|
|
1554
|
+
function wordStart(chars, cursor) {
|
|
1555
|
+
let i = cursor;
|
|
1556
|
+
while (i > 0 && /\s/.test(chars[i - 1])) i--;
|
|
1557
|
+
while (i > 0 && !/\s/.test(chars[i - 1])) i--;
|
|
1558
|
+
return i;
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
// src/ui/Composer.tsx
|
|
1562
|
+
import { Fragment, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
1563
|
+
function Composer({ draftKey, active, enabled: enabled2, error, width, maxRows, maxHeight, onHeight, members, onMentionStart, onSuggesting, onSubmit }) {
|
|
1080
1564
|
const [chars, setChars] = useState([]);
|
|
1081
1565
|
const [cursor, setCursor] = useState(0);
|
|
1566
|
+
const [picks, setPicks] = useState([]);
|
|
1567
|
+
const [suggestion, setSuggestion] = useState(0);
|
|
1568
|
+
const [dismissed, setDismissed] = useState(null);
|
|
1082
1569
|
const [sending, setSending] = useState(false);
|
|
1083
1570
|
const drafts = useRef(/* @__PURE__ */ new Map());
|
|
1084
1571
|
const currentKey = useRef(draftKey);
|
|
1085
1572
|
useEffect(() => {
|
|
1086
1573
|
if (currentKey.current === draftKey) return;
|
|
1087
1574
|
if (currentKey.current) {
|
|
1088
|
-
if (chars.length) drafts.current.set(currentKey.current, { chars, cursor });
|
|
1575
|
+
if (chars.length) drafts.current.set(currentKey.current, { chars, cursor, picks });
|
|
1089
1576
|
else drafts.current.delete(currentKey.current);
|
|
1090
1577
|
}
|
|
1091
|
-
const next = draftKey && drafts.current.get(draftKey) || { chars: [], cursor: 0 };
|
|
1578
|
+
const next = draftKey && drafts.current.get(draftKey) || { chars: [], cursor: 0, picks: [] };
|
|
1092
1579
|
setChars(next.chars);
|
|
1093
1580
|
setCursor(next.cursor);
|
|
1581
|
+
setPicks(next.picks);
|
|
1582
|
+
setDismissed(null);
|
|
1094
1583
|
currentKey.current = draftKey;
|
|
1095
1584
|
}, [draftKey]);
|
|
1096
1585
|
const insert = (text) => {
|
|
@@ -1098,20 +1587,56 @@ function Composer({ draftKey, active, enabled: enabled2, error, onSubmit }) {
|
|
|
1098
1587
|
setChars((c) => [...c.slice(0, cursor), ...add, ...c.slice(cursor)]);
|
|
1099
1588
|
setCursor((p) => p + add.length);
|
|
1100
1589
|
};
|
|
1590
|
+
const lines = composerLines(chars, width - 3);
|
|
1591
|
+
const cursorRow = cursorLine(lines, cursor);
|
|
1592
|
+
const query = members && active ? mentionQuery(chars, cursor) : null;
|
|
1593
|
+
const suggestions = query && dismissed !== query.start ? filterMembers([...members, EVERYONE], query.query) : [];
|
|
1594
|
+
const chosen = Math.min(suggestion, Math.max(0, suggestions.length - 1));
|
|
1595
|
+
const status = sending ? " sending\u2026" : error ? ` ${error}` : "";
|
|
1596
|
+
const budget = Math.max(1, maxHeight - (status ? 1 : 0));
|
|
1597
|
+
const suggestionRows = Math.min(suggestions.length, Math.max(0, budget - 1));
|
|
1598
|
+
const suggestionTop = Math.min(Math.max(0, chosen - suggestionRows + 1), Math.max(0, suggestions.length - suggestionRows));
|
|
1599
|
+
const suggesting = suggestionRows > 0;
|
|
1600
|
+
const view = composerWindow(lines, cursor, Math.max(1, Math.min(maxRows, budget - suggestionRows)));
|
|
1601
|
+
useEffect(() => {
|
|
1602
|
+
if (!query) setDismissed(null);
|
|
1603
|
+
}, [!!query]);
|
|
1604
|
+
useEffect(() => {
|
|
1605
|
+
if (query) onMentionStart?.();
|
|
1606
|
+
}, [query?.start]);
|
|
1607
|
+
useEffect(() => {
|
|
1608
|
+
setSuggestion(0);
|
|
1609
|
+
}, [query?.start, query?.query]);
|
|
1610
|
+
useEffect(() => onSuggesting?.(suggesting), [suggesting]);
|
|
1611
|
+
const accept = (member) => {
|
|
1612
|
+
if (!query) return;
|
|
1613
|
+
const add = Array.from(`@${member.name} `);
|
|
1614
|
+
setChars((c) => [...c.slice(0, query.start), ...add, ...c.slice(cursor)]);
|
|
1615
|
+
setCursor(query.start + add.length);
|
|
1616
|
+
setPicks((p) => [...p, member]);
|
|
1617
|
+
};
|
|
1101
1618
|
usePaste(insert, { isActive: active && !sending });
|
|
1102
1619
|
useInput(
|
|
1103
1620
|
(input, key) => {
|
|
1621
|
+
if (suggesting) {
|
|
1622
|
+
if (key.upArrow) return setSuggestion(Math.max(0, chosen - 1));
|
|
1623
|
+
if (key.downArrow) return setSuggestion(Math.min(suggestions.length - 1, chosen + 1));
|
|
1624
|
+
if (key.return && !key.shift && !key.meta || key.tab) return accept(suggestions[chosen]);
|
|
1625
|
+
if (key.escape) return setDismissed(query.start);
|
|
1626
|
+
}
|
|
1627
|
+
if (key.return && (key.shift || key.meta)) return insert("\n");
|
|
1104
1628
|
if (key.return) {
|
|
1105
1629
|
const text = chars.join("").trim();
|
|
1106
1630
|
if (!text || !enabled2) return;
|
|
1107
1631
|
const sentFrom = currentKey.current;
|
|
1108
1632
|
setSending(true);
|
|
1109
|
-
void onSubmit(text).then((ok) => {
|
|
1633
|
+
void onSubmit(text, picks).then((ok) => {
|
|
1110
1634
|
setSending(false);
|
|
1111
1635
|
if (!ok) return;
|
|
1112
1636
|
if (currentKey.current === sentFrom) {
|
|
1113
1637
|
setChars([]);
|
|
1114
1638
|
setCursor(0);
|
|
1639
|
+
setPicks([]);
|
|
1115
1640
|
} else if (sentFrom) {
|
|
1116
1641
|
drafts.current.delete(sentFrom);
|
|
1117
1642
|
}
|
|
@@ -1126,6 +1651,16 @@ function Composer({ draftKey, active, enabled: enabled2, error, onSubmit }) {
|
|
|
1126
1651
|
setChars((c) => c.slice(cursor));
|
|
1127
1652
|
return setCursor(0);
|
|
1128
1653
|
}
|
|
1654
|
+
if (key.ctrl && input === "w" || key.meta && (key.backspace || key.delete)) {
|
|
1655
|
+
const start = wordStart(chars, cursor);
|
|
1656
|
+
setChars((c) => [...c.slice(0, start), ...c.slice(cursor)]);
|
|
1657
|
+
return setCursor(start);
|
|
1658
|
+
}
|
|
1659
|
+
if (key.upArrow || key.downArrow) {
|
|
1660
|
+
const next = verticalMove(lines, chars, cursor, key.upArrow ? -1 : 1);
|
|
1661
|
+
if (next !== null) setCursor(next);
|
|
1662
|
+
return;
|
|
1663
|
+
}
|
|
1129
1664
|
if (key.backspace || key.delete) {
|
|
1130
1665
|
if (cursor === 0) return;
|
|
1131
1666
|
setChars((c) => [...c.slice(0, cursor - 1), ...c.slice(cursor)]);
|
|
@@ -1137,21 +1672,41 @@ function Composer({ draftKey, active, enabled: enabled2, error, onSubmit }) {
|
|
|
1137
1672
|
},
|
|
1138
1673
|
{ isActive: active && !sending }
|
|
1139
1674
|
);
|
|
1140
|
-
const
|
|
1141
|
-
const
|
|
1142
|
-
const
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1675
|
+
const placeholder = chars.length === 0 && !active;
|
|
1676
|
+
const visibleRows = placeholder ? 1 : view.rows;
|
|
1677
|
+
const height = visibleRows + (status ? 1 : 0) + suggestionRows;
|
|
1678
|
+
useEffect(() => onHeight(height), [height]);
|
|
1679
|
+
return /* @__PURE__ */ jsxs2(Box2, { flexDirection: "column", children: [
|
|
1680
|
+
suggestions.slice(suggestionTop, suggestionTop + suggestionRows).map((m, j) => {
|
|
1681
|
+
const i = suggestionTop + j;
|
|
1682
|
+
return /* @__PURE__ */ jsxs2(Text2, { wrap: "truncate-end", children: [
|
|
1683
|
+
/* @__PURE__ */ jsx2(Text2, { color: "cyan", children: i === chosen ? "\u25B6 " : " " }),
|
|
1684
|
+
/* @__PURE__ */ jsxs2(Text2, { inverse: i === chosen, children: [
|
|
1685
|
+
"@",
|
|
1686
|
+
m.name
|
|
1687
|
+
] }),
|
|
1688
|
+
j === 0 ? /* @__PURE__ */ jsx2(Text2, { dimColor: true, children: " \u2191\u2193 Enter/Tab pick \xB7 Esc" }) : null
|
|
1689
|
+
] }, m.uid);
|
|
1690
|
+
}),
|
|
1691
|
+
placeholder ? /* @__PURE__ */ jsxs2(Text2, { wrap: "truncate-end", children: [
|
|
1692
|
+
/* @__PURE__ */ jsx2(Text2, { color: "gray", children: "> " }),
|
|
1693
|
+
/* @__PURE__ */ jsx2(Text2, { dimColor: true, children: enabled2 ? "i or Enter to type a message" : "Open a chat to type" })
|
|
1694
|
+
] }) : lines.slice(view.top, view.top + view.rows).map((line, i) => {
|
|
1695
|
+
const row = view.top + i;
|
|
1696
|
+
const hasCursor = active && row === cursorRow;
|
|
1697
|
+
const text = chars.slice(line.start, line.end).join("");
|
|
1698
|
+
const offset = cursor - line.start;
|
|
1699
|
+
const at = hasCursor ? chars[cursor] === void 0 || chars[cursor] === "\n" ? " " : chars[cursor] : "";
|
|
1700
|
+
return /* @__PURE__ */ jsxs2(Text2, { wrap: "truncate-end", children: [
|
|
1701
|
+
/* @__PURE__ */ jsx2(Text2, { color: active ? "cyan" : "gray", children: row === 0 ? "> " : view.top > 0 && i === 0 ? "\u2191 " : " " }),
|
|
1702
|
+
hasCursor ? /* @__PURE__ */ jsxs2(Fragment, { children: [
|
|
1703
|
+
Array.from(text).slice(0, offset).join(""),
|
|
1704
|
+
/* @__PURE__ */ jsx2(Text2, { inverse: true, children: at }),
|
|
1705
|
+
Array.from(text).slice(offset + 1).join("")
|
|
1706
|
+
] }) : text
|
|
1707
|
+
] }, row);
|
|
1708
|
+
}),
|
|
1709
|
+
status ? /* @__PURE__ */ jsx2(Text2, { color: sending ? void 0 : "red", dimColor: sending, wrap: "truncate-end", children: status }) : null
|
|
1155
1710
|
] });
|
|
1156
1711
|
}
|
|
1157
1712
|
|
|
@@ -1177,9 +1732,16 @@ function LoginScreen({ qr, status, notice }) {
|
|
|
1177
1732
|
// src/ui/MessagePane.tsx
|
|
1178
1733
|
import { Box as Box4, Text as Text4 } from "ink";
|
|
1179
1734
|
import { jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
|
|
1180
|
-
function buildLines(messages, width, reactions = {}, selfId = "") {
|
|
1735
|
+
function buildLines(messages, width, reactions = {}, selfId = "", dividerTs2 = null) {
|
|
1181
1736
|
const lines = [];
|
|
1737
|
+
let dividerDone = dividerTs2 === null;
|
|
1182
1738
|
for (const m of messages) {
|
|
1739
|
+
if (!dividerDone && !m.isSelf && !m.recalled && m.ts > dividerTs2) {
|
|
1740
|
+
dividerDone = true;
|
|
1741
|
+
const label = " new messages ";
|
|
1742
|
+
const side = Math.max(2, Math.floor((width - label.length) / 2));
|
|
1743
|
+
lines.push({ key: "divider", msgId: "", time: "", sender: "", isSelf: false, indent: 0, segments: [{ text: `${"\u2500".repeat(side)}${label}${"\u2500".repeat(side)}` }], dim: false, divider: true });
|
|
1744
|
+
}
|
|
1183
1745
|
const time = formatTime(m.ts);
|
|
1184
1746
|
const sender = m.isSelf ? "me" : m.senderName;
|
|
1185
1747
|
const prefixLength = Array.from(`${time} ${sender}: `).length;
|
|
@@ -1187,14 +1749,30 @@ function buildLines(messages, width, reactions = {}, selfId = "") {
|
|
|
1187
1749
|
const labelled = !!m.url && !m.text.includes(m.url);
|
|
1188
1750
|
const text = m.recalled ? "[recalled]" : labelled ? `${m.text} \u2197` : m.text;
|
|
1189
1751
|
const ranges = m.recalled ? [] : labelled ? [{ start: 0, end: Array.from(text).length, url: m.url }] : textLinkRanges(text);
|
|
1752
|
+
const quoted = !!m.quote && !m.recalled;
|
|
1753
|
+
if (quoted) {
|
|
1754
|
+
const room = Math.max(10, width - prefixLength);
|
|
1755
|
+
const full = Array.from(`\u21B3 ${m.quote.from}: ${m.quote.text.replace(/\s+/g, " ")}`);
|
|
1756
|
+
lines.push({
|
|
1757
|
+
key: `${m.id}-q`,
|
|
1758
|
+
msgId: m.id,
|
|
1759
|
+
time,
|
|
1760
|
+
sender,
|
|
1761
|
+
isSelf: m.isSelf,
|
|
1762
|
+
indent: 0,
|
|
1763
|
+
segments: [{ text: full.length > room ? full.slice(0, room - 1).join("") + "\u2026" : full.join("") }],
|
|
1764
|
+
dim: false,
|
|
1765
|
+
quoted: true
|
|
1766
|
+
});
|
|
1767
|
+
}
|
|
1190
1768
|
wrapWithOffsets(text, Math.max(10, width - prefixLength)).forEach(
|
|
1191
1769
|
(line, i) => lines.push({
|
|
1192
1770
|
key: `${m.id}-${i}`,
|
|
1193
1771
|
msgId: m.id,
|
|
1194
|
-
time: i === 0 ? time : "",
|
|
1195
|
-
sender: i === 0 ? sender : "",
|
|
1772
|
+
time: i === 0 && !quoted ? time : "",
|
|
1773
|
+
sender: i === 0 && !quoted ? sender : "",
|
|
1196
1774
|
isSelf: m.isSelf,
|
|
1197
|
-
indent: i === 0 ? 0 : prefixLength,
|
|
1775
|
+
indent: i === 0 && !quoted ? 0 : prefixLength,
|
|
1198
1776
|
segments: linkSegments(line, ranges),
|
|
1199
1777
|
dim: !!m.recalled
|
|
1200
1778
|
})
|
|
@@ -1216,8 +1794,8 @@ function buildLines(messages, width, reactions = {}, selfId = "") {
|
|
|
1216
1794
|
return lines;
|
|
1217
1795
|
}
|
|
1218
1796
|
function paneLayout(options) {
|
|
1219
|
-
const { messages, reactions, selfId, scroll, selectedId, followSelection, width, height } = options;
|
|
1220
|
-
const lines = buildLines(messages, width - 4, reactions, selfId);
|
|
1797
|
+
const { messages, reactions, selfId, dividerTs: dividerTs2 = null, scroll, selectedId, followSelection, width, height } = options;
|
|
1798
|
+
const lines = buildLines(messages, width - 4, reactions, selfId, dividerTs2);
|
|
1221
1799
|
const max = Math.max(0, lines.length - height);
|
|
1222
1800
|
let offset = Math.min(scroll, max);
|
|
1223
1801
|
const first = selectedId && followSelection ? lines.findIndex((l) => l.msgId === selectedId) : -1;
|
|
@@ -1229,7 +1807,7 @@ function paneLayout(options) {
|
|
|
1229
1807
|
}
|
|
1230
1808
|
const visible = lines.slice(Math.max(0, lines.length - height - offset), lines.length - offset);
|
|
1231
1809
|
const rows = Array.from({ length: height }, () => null);
|
|
1232
|
-
visible.forEach((l, i) => rows[height - visible.length + i] = l.msgId);
|
|
1810
|
+
visible.forEach((l, i) => rows[height - visible.length + i] = l.msgId || null);
|
|
1233
1811
|
return { lines, visible, offset, max, rows };
|
|
1234
1812
|
}
|
|
1235
1813
|
function MessagePane({ thread, layout, selectedId, focused, height }) {
|
|
@@ -1245,7 +1823,7 @@ function MessagePane({ thread, layout, selectedId, focused, height }) {
|
|
|
1245
1823
|
/* @__PURE__ */ jsxs4(Box4, { flexDirection: "column", flexGrow: 1, justifyContent: "flex-end", children: [
|
|
1246
1824
|
!thread ? /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: "Pick a chat on the left and press Enter." }) : null,
|
|
1247
1825
|
thread && lines.length === 0 ? /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: "No messages loaded yet." }) : null,
|
|
1248
|
-
visible.map((l) => /* @__PURE__ */ jsxs4(Text4, { wrap: "truncate-end", dimColor: l.dim, inverse: l.msgId === selectedId, children: [
|
|
1826
|
+
visible.map((l) => /* @__PURE__ */ jsxs4(Text4, { wrap: "truncate-end", dimColor: l.dim, color: l.divider ? "red" : void 0, inverse: !!l.msgId && l.msgId === selectedId, children: [
|
|
1249
1827
|
l.time ? /* @__PURE__ */ jsxs4(Text4, { dimColor: true, children: [
|
|
1250
1828
|
l.time,
|
|
1251
1829
|
" "
|
|
@@ -1256,7 +1834,8 @@ function MessagePane({ thread, layout, selectedId, focused, height }) {
|
|
|
1256
1834
|
] }) : null,
|
|
1257
1835
|
" ".repeat(l.indent),
|
|
1258
1836
|
l.reactions?.map((r, i) => /* @__PURE__ */ jsx4(Text4, { color: r.mine ? "cyan" : void 0, bold: r.mine, dimColor: !r.mine, children: `${reactionIcon(r.icon)} ${r.count} ` }, i)),
|
|
1259
|
-
l.segments
|
|
1837
|
+
l.quoted ? /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: l.segments[0]?.text }) : null,
|
|
1838
|
+
(l.quoted ? [] : l.segments).map(
|
|
1260
1839
|
(s, i) => s.url ? /* @__PURE__ */ jsx4(Text4, { underline: true, children: hyperlink(s.url, s.text) }, i) : s.text
|
|
1261
1840
|
)
|
|
1262
1841
|
] }, l.key))
|
|
@@ -1271,60 +1850,75 @@ var SECTIONS = [
|
|
|
1271
1850
|
[
|
|
1272
1851
|
"Commands (type in the message box)",
|
|
1273
1852
|
[
|
|
1274
|
-
["/photo <path> [
|
|
1275
|
-
["/file <path> [
|
|
1276
|
-
["
|
|
1277
|
-
["
|
|
1853
|
+
["/photo <path> [text]", "send a photo"],
|
|
1854
|
+
["/file <path> [text]", "send a file"],
|
|
1855
|
+
["", "paths may use ~/, quotes, \\ "],
|
|
1856
|
+
["drag file in, Enter", "send it, after asking"],
|
|
1857
|
+
["//text", "text starting with /"],
|
|
1278
1858
|
["/help or ?", "this help"]
|
|
1279
1859
|
]
|
|
1280
1860
|
],
|
|
1281
1861
|
[
|
|
1282
1862
|
"Chat list",
|
|
1283
1863
|
[
|
|
1284
|
-
["j/k
|
|
1285
|
-
["
|
|
1286
|
-
["
|
|
1864
|
+
["j/k g/G Ctrl+d/u", "move, ends, half page"],
|
|
1865
|
+
["", "highlighting only previews"],
|
|
1866
|
+
["Enter/i \xB7 l/Tab", "open and type \xB7 messages"],
|
|
1867
|
+
["/ \xB7 Esc", "filter by name or text \xB7 clear"],
|
|
1868
|
+
["n \xB7 m \xB7 r", "notify \xB7 groups \xB7 reconnect"]
|
|
1287
1869
|
]
|
|
1288
1870
|
],
|
|
1289
1871
|
[
|
|
1290
1872
|
"Messages",
|
|
1291
1873
|
[
|
|
1292
|
-
["j/k
|
|
1293
|
-
["
|
|
1294
|
-
["
|
|
1295
|
-
["
|
|
1296
|
-
["
|
|
1874
|
+
["j/k g/G Ctrl+d/u", "select, ends, half page"],
|
|
1875
|
+
["/ \xB7 n \xB7 N", "search chat \xB7 older \xB7 newer"],
|
|
1876
|
+
["r", "reply (Esc in the box cancels)"],
|
|
1877
|
+
["e", "react: 1-6, 0 off, search"],
|
|
1878
|
+
["w", "who reacted"],
|
|
1879
|
+
["d \xB7 o", "recall or delete \xB7 open"],
|
|
1880
|
+
["Enter/i \xB7 h/Esc/Tab", "type \xB7 back to chats"]
|
|
1297
1881
|
]
|
|
1298
1882
|
],
|
|
1299
1883
|
[
|
|
1300
1884
|
"Message box",
|
|
1301
1885
|
[
|
|
1302
|
-
["Enter \xB7
|
|
1303
|
-
["\u2190\u2192
|
|
1886
|
+
["Enter \xB7 Alt+Enter", "send \xB7 new line"],
|
|
1887
|
+
["\u2190\u2192\u2191\u2193 Ctrl+a/e", "move, start/end"],
|
|
1888
|
+
["Ctrl+w \xB7 Ctrl+u", "delete word \xB7 to start"],
|
|
1889
|
+
["@ in a group", "mention: \u2191\u2193, Enter or Tab picks"],
|
|
1890
|
+
["Esc", "back (drafts are kept)"]
|
|
1304
1891
|
]
|
|
1305
1892
|
],
|
|
1306
1893
|
[
|
|
1307
1894
|
"Anywhere",
|
|
1308
1895
|
[
|
|
1309
|
-
["PgUp/PgDn \xB7 wheel", "scroll
|
|
1310
|
-
["click", "open chat, select
|
|
1896
|
+
["PgUp/PgDn \xB7 wheel", "scroll"],
|
|
1897
|
+
["click", "open chat, select, type"],
|
|
1311
1898
|
["Shift-drag", "select text"],
|
|
1312
1899
|
["Ctrl+C", "quit"]
|
|
1313
1900
|
]
|
|
1314
1901
|
]
|
|
1315
1902
|
];
|
|
1316
|
-
|
|
1903
|
+
var LINES = SECTIONS.flatMap(([title, rows]) => [{ title }, ...rows.map(([key, text]) => ({ key, text }))]);
|
|
1904
|
+
var HELP_LINES = LINES.length;
|
|
1905
|
+
function HelpPane({ width, height, offset }) {
|
|
1317
1906
|
const keyWidth = Math.max(...SECTIONS.flatMap(([, rows]) => rows.map(([k]) => Array.from(k).length))) + 2;
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1907
|
+
const start = Math.max(0, Math.min(offset, LINES.length - height));
|
|
1908
|
+
const more = start + height < LINES.length;
|
|
1909
|
+
return /* @__PURE__ */ jsxs5(Box5, { flexDirection: "column", flexGrow: 1, width, height: height + 2, borderStyle: "round", borderColor: "cyan", children: [
|
|
1910
|
+
/* @__PURE__ */ jsx5(Box5, { position: "absolute", marginTop: -1, marginLeft: 1, children: /* @__PURE__ */ jsxs5(Text5, { bold: true, children: [
|
|
1911
|
+
" Help \xB7 ",
|
|
1912
|
+
start > 0 || more ? "j/k scroll \xB7 " : "",
|
|
1913
|
+
"any other key closes "
|
|
1914
|
+
] }) }),
|
|
1915
|
+
LINES.slice(start, start + height).map(
|
|
1916
|
+
(line, i) => "title" in line ? /* @__PURE__ */ jsx5(Text5, { color: "cyan", bold: true, wrap: "truncate-end", children: line.title }, i) : /* @__PURE__ */ jsxs5(Text5, { wrap: "truncate-end", children: [
|
|
1323
1917
|
" ",
|
|
1324
|
-
/* @__PURE__ */ jsx5(Text5, { color: "yellow", children:
|
|
1325
|
-
|
|
1326
|
-
] },
|
|
1327
|
-
|
|
1918
|
+
/* @__PURE__ */ jsx5(Text5, { color: "yellow", children: line.key.padEnd(keyWidth) }),
|
|
1919
|
+
line.text
|
|
1920
|
+
] }, i)
|
|
1921
|
+
)
|
|
1328
1922
|
] });
|
|
1329
1923
|
}
|
|
1330
1924
|
|
|
@@ -1413,7 +2007,7 @@ function visibleChoices(frame, total, cursor) {
|
|
|
1413
2007
|
|
|
1414
2008
|
// src/ui/ReactionPopup.tsx
|
|
1415
2009
|
import { Box as Box8, Text as Text8 } from "ink";
|
|
1416
|
-
import { Fragment, jsx as jsx8, jsxs as jsxs8 } from "react/jsx-runtime";
|
|
2010
|
+
import { Fragment as Fragment2, jsx as jsx8, jsxs as jsxs8 } from "react/jsx-runtime";
|
|
1417
2011
|
var QUICK_REACTIONS = ["/-heart", "/-strong", ":>", ":o", ":-((", ":-h"];
|
|
1418
2012
|
var QUICK_CELL_WIDTH = 6;
|
|
1419
2013
|
var REACTION_LIST_TOP = 2;
|
|
@@ -1444,7 +2038,7 @@ function ReactionPopup({
|
|
|
1444
2038
|
] }) : null,
|
|
1445
2039
|
listRows > 0 && visible.length === 0 ? /* @__PURE__ */ jsx8(Text8, { dimColor: true, children: "no match" }) : null,
|
|
1446
2040
|
visible.map((r, i) => /* @__PURE__ */ jsx8(Text8, { wrap: "truncate-end", inverse: start + i === cursor, children: `${start + i === cursor ? "\u25B6" : " "} ${r.emoji} ${r.name}` }, r.code)),
|
|
1447
|
-
showFooter ? /* @__PURE__ */ jsxs8(
|
|
2041
|
+
showFooter ? /* @__PURE__ */ jsxs8(Fragment2, { children: [
|
|
1448
2042
|
/* @__PURE__ */ jsx8(Box8, { flexGrow: 1 }),
|
|
1449
2043
|
/* @__PURE__ */ jsx8(Text8, { dimColor: true, wrap: "truncate-end", children: "1-6 quick \xB7 \u2191\u2193 Enter \xB7 0 remove \xB7 Esc" })
|
|
1450
2044
|
] }) : null
|
|
@@ -1488,9 +2082,9 @@ import { Text as Text9 } from "ink";
|
|
|
1488
2082
|
import { jsxs as jsxs9 } from "react/jsx-runtime";
|
|
1489
2083
|
var COLORS = { idle: "gray", connecting: "yellow", connected: "green", closed: "red" };
|
|
1490
2084
|
var NOTIFY_LABEL = { full: "notify on", sender: "notify sender", off: "notify off" };
|
|
1491
|
-
function StatusBar({ connection, selfName, focus, notify: notify2, toast }) {
|
|
2085
|
+
function StatusBar({ connection, selfName, focus, notify: notify2, notifyGroups: notifyGroups2, toast }) {
|
|
1492
2086
|
const label = connection.state === "closed" ? `disconnected: ${connection.reason ?? ""} \xB7 r reconnect` : connection.state;
|
|
1493
|
-
const hints = focus === "list" ? "j/k move \xB7
|
|
2087
|
+
const hints = focus === "list" ? "j/k move \xB7 Enter open \xB7 / filter \xB7 ? help" : focus === "input" ? "Enter send \xB7 Alt+Enter new line \xB7 Esc back" : "r reply \xB7 e react \xB7 w who \xB7 d delete \xB7 o open \xB7 / search \xB7 ? help";
|
|
1494
2088
|
return /* @__PURE__ */ jsxs9(Text9, { wrap: "truncate-end", children: [
|
|
1495
2089
|
/* @__PURE__ */ jsxs9(Text9, { color: COLORS[connection.state], children: [
|
|
1496
2090
|
"\u25CF ",
|
|
@@ -1505,6 +2099,7 @@ function StatusBar({ connection, selfName, focus, notify: notify2, toast }) {
|
|
|
1505
2099
|
selfName,
|
|
1506
2100
|
" \xB7 ",
|
|
1507
2101
|
NOTIFY_LABEL[notify2],
|
|
2102
|
+
notify2 !== "off" && notifyGroups2 === "mentions" ? " (groups: @me)" : "",
|
|
1508
2103
|
" \xB7 ",
|
|
1509
2104
|
hints
|
|
1510
2105
|
] })
|
|
@@ -1627,6 +2222,7 @@ function ErrorScreen({ message }) {
|
|
|
1627
2222
|
/* @__PURE__ */ jsx9(Text10, { dimColor: true, children: "r to try again \xB7 Ctrl+C to quit" })
|
|
1628
2223
|
] });
|
|
1629
2224
|
}
|
|
2225
|
+
var OWNERS_WIDTH = 40;
|
|
1630
2226
|
function ChatScreen({ store: store2, client }) {
|
|
1631
2227
|
const state = useStoreState();
|
|
1632
2228
|
const { columns, rows } = useWindowSize();
|
|
@@ -1641,9 +2237,15 @@ function ChatScreen({ store: store2, client }) {
|
|
|
1641
2237
|
const [picker, setPicker] = useState2(null);
|
|
1642
2238
|
const [deleting, setDeleting] = useState2(false);
|
|
1643
2239
|
const [deleteCursor, setDeleteCursor] = useState2(0);
|
|
1644
|
-
const [showHelp,
|
|
2240
|
+
const [showHelp, setShowHelpState] = useState2(false);
|
|
2241
|
+
const [helpOffset, setHelpOffset] = useState2(0);
|
|
2242
|
+
const setShowHelp = (value) => {
|
|
2243
|
+
setHelpOffset(0);
|
|
2244
|
+
setShowHelpState(value);
|
|
2245
|
+
};
|
|
1645
2246
|
const [linkChoices, setLinkChoices] = useState2(null);
|
|
1646
2247
|
const [linkCursor, setLinkCursor] = useState2(0);
|
|
2248
|
+
const [ownersCursor, setOwnersCursor] = useState2(null);
|
|
1647
2249
|
const [pending, setPending] = useState2(null);
|
|
1648
2250
|
const resolvePending = useRef2(null);
|
|
1649
2251
|
const [selectedKey, setSelectedKey] = useState2(null);
|
|
@@ -1669,28 +2271,58 @@ function ChatScreen({ store: store2, client }) {
|
|
|
1669
2271
|
const selected = found === -1 ? 0 : found;
|
|
1670
2272
|
const active = state.activeKey ? state.threads[state.activeKey] ?? null : null;
|
|
1671
2273
|
const messages = state.activeKey ? state.messages[state.activeKey] ?? [] : [];
|
|
1672
|
-
const
|
|
2274
|
+
const [composerRows, setComposerRows] = useState2(1);
|
|
2275
|
+
const [replyTo, setReplyTo] = useState2(null);
|
|
2276
|
+
const [mentionOpen, setMentionOpen] = useState2(false);
|
|
2277
|
+
const [memberIds, setMemberIds] = useState2({});
|
|
2278
|
+
const [search, setSearch] = useState2(null);
|
|
2279
|
+
const replyBar = !!replyTo && replyTo.threadKey === state.activeKey && !pending;
|
|
2280
|
+
const searchBar = !!search && !pending;
|
|
2281
|
+
const paneHeight = Math.max(3, rows - 3 - (pending ? 1 : composerRows) - (replyBar ? 1 : 0) - (searchBar ? 1 : 0));
|
|
1673
2282
|
const sidebarWidth = Math.max(18, Math.min(32, Math.floor(columns / 3)));
|
|
1674
2283
|
const pageSize = Math.max(1, paneHeight - 2);
|
|
1675
2284
|
const msgIndex = messages.findIndex((m) => m.id === selectedMsgId);
|
|
1676
|
-
const popupOpen = picker !== null || deleting || linkChoices !== null;
|
|
2285
|
+
const popupOpen = picker !== null || deleting || linkChoices !== null || ownersCursor !== null;
|
|
1677
2286
|
const popupTargetGone = popupOpen && msgIndex === -1;
|
|
1678
2287
|
useEffect2(() => {
|
|
1679
2288
|
if (!popupTargetGone) return;
|
|
1680
2289
|
setPicker(null);
|
|
1681
2290
|
setDeleting(false);
|
|
1682
2291
|
setLinkChoices(null);
|
|
2292
|
+
setOwnersCursor(null);
|
|
1683
2293
|
}, [popupTargetGone]);
|
|
1684
2294
|
const selectedMsg = focus === "messages" ? messages[msgIndex === -1 ? messages.length - 1 : msgIndex] : void 0;
|
|
2295
|
+
const owners = selectedMsg && !selectedMsg.recalled ? reactionOwners(state.reactions[reactionKey(selectedMsg.threadKey, selectedMsg.id)], state) : [];
|
|
2296
|
+
const ownersOpen = ownersCursor !== null && owners.length > 0;
|
|
2297
|
+
useEffect2(() => {
|
|
2298
|
+
if (ownersCursor !== null && owners.length === 0) setOwnersCursor(null);
|
|
2299
|
+
}, [ownersCursor !== null && owners.length === 0]);
|
|
2300
|
+
useEffect2(() => {
|
|
2301
|
+
const request = state.openRequest;
|
|
2302
|
+
if (!request || !store2.getState().threads[request.key]) return;
|
|
2303
|
+
setShowHelp(false);
|
|
2304
|
+
setPicker(null);
|
|
2305
|
+
setDeleting(false);
|
|
2306
|
+
setLinkChoices(null);
|
|
2307
|
+
setOwnersCursor(null);
|
|
2308
|
+
if (pending) finishPending("cancel");
|
|
2309
|
+
setFiltering(false);
|
|
2310
|
+
setFilter("");
|
|
2311
|
+
open(request.key, true);
|
|
2312
|
+
}, [state.openRequest]);
|
|
1685
2313
|
const flash = (text) => {
|
|
1686
2314
|
setToast(text);
|
|
1687
2315
|
setTimeout(() => setToast((t) => t === text ? void 0 : t), 2500);
|
|
1688
2316
|
};
|
|
1689
|
-
const open = (key, focusInput) => {
|
|
2317
|
+
const open = (key, focusInput, read = true) => {
|
|
1690
2318
|
setSelectedKey(key);
|
|
1691
2319
|
if (focusInput) setFocus("input");
|
|
1692
|
-
|
|
1693
|
-
|
|
2320
|
+
const current = store2.getState();
|
|
2321
|
+
if (key === current.activeKey) {
|
|
2322
|
+
if (read && !current.activeRead) store2.update((s) => openThread(s, key));
|
|
2323
|
+
return;
|
|
2324
|
+
}
|
|
2325
|
+
store2.update((s) => openThread(s, key, read));
|
|
1694
2326
|
setScroll(0);
|
|
1695
2327
|
setFollowing(true);
|
|
1696
2328
|
setSelectedMsgId(null);
|
|
@@ -1701,6 +2333,7 @@ function ChatScreen({ store: store2, client }) {
|
|
|
1701
2333
|
messages,
|
|
1702
2334
|
reactions: state.reactions,
|
|
1703
2335
|
selfId: state.selfId,
|
|
2336
|
+
dividerTs: dividerTs(state),
|
|
1704
2337
|
scroll,
|
|
1705
2338
|
selectedId: selectedMsg?.id ?? null,
|
|
1706
2339
|
followSelection: follow,
|
|
@@ -1715,6 +2348,7 @@ function ChatScreen({ store: store2, client }) {
|
|
|
1715
2348
|
if (popupOpen) return;
|
|
1716
2349
|
if (x <= sidebarWidth) {
|
|
1717
2350
|
if (filtering || threads.length === 0) return;
|
|
2351
|
+
if (focus !== "list") setFocus("list");
|
|
1718
2352
|
setSelectedKey(threads[Math.min(threads.length - 1, Math.max(0, selected + direction))]?.key ?? null);
|
|
1719
2353
|
} else {
|
|
1720
2354
|
scrollBy(direction < 0 ? WHEEL_LINES : -WHEEL_LINES);
|
|
@@ -1748,6 +2382,10 @@ function ChatScreen({ store: store2, client }) {
|
|
|
1748
2382
|
if (url) openLink(url);
|
|
1749
2383
|
return;
|
|
1750
2384
|
}
|
|
2385
|
+
if (ownersOpen) {
|
|
2386
|
+
if (!popupContentAt(popupFrameFor(OWNERS_WIDTH, choicePopupHeight(owners.length)), x, y)) setOwnersCursor(null);
|
|
2387
|
+
return;
|
|
2388
|
+
}
|
|
1751
2389
|
if (deleting) {
|
|
1752
2390
|
const options = deleteOptions(selectedMsg?.isSelf ?? false);
|
|
1753
2391
|
const frame = popupFrameFor(34, choicePopupHeight(options.length));
|
|
@@ -1785,12 +2423,61 @@ function ChatScreen({ store: store2, client }) {
|
|
|
1785
2423
|
}
|
|
1786
2424
|
};
|
|
1787
2425
|
useEffect2(() => onClick((e) => click.current(e.x, e.y)), []);
|
|
2426
|
+
const activeUnread = state.activeKey ? state.unread[state.activeKey] ?? 0 : 0;
|
|
2427
|
+
const lastFocus = useRef2(focus);
|
|
2428
|
+
useEffect2(() => {
|
|
2429
|
+
const entered = focus !== "list" && lastFocus.current === "list";
|
|
2430
|
+
lastFocus.current = focus;
|
|
2431
|
+
const s = store2.getState();
|
|
2432
|
+
if (!s.activeKey || !s.terminalFocused) return;
|
|
2433
|
+
if (entered && !s.activeRead || s.activeRead && activeUnread > 0) store2.update((x) => openThread(x, s.activeKey));
|
|
2434
|
+
}, [focus, state.activeKey, state.activeRead, state.terminalFocused, activeUnread]);
|
|
1788
2435
|
const highlighted = threads[selected]?.key ?? null;
|
|
1789
2436
|
useEffect2(() => {
|
|
1790
2437
|
if (!highlighted || highlighted === store2.getState().activeKey) return;
|
|
1791
|
-
const timer = setTimeout(() => open(highlighted, false), PREVIEW_DELAY_MS);
|
|
2438
|
+
const timer = setTimeout(() => open(highlighted, false, false), PREVIEW_DELAY_MS);
|
|
1792
2439
|
return () => clearTimeout(timer);
|
|
1793
2440
|
}, [highlighted]);
|
|
2441
|
+
const matches = useMemo(() => search?.query.trim() ? searchMessages(messages, search.query) : [], [messages, search?.query]);
|
|
2442
|
+
const jumpTo = (id) => {
|
|
2443
|
+
if (!id) return flash("no matches");
|
|
2444
|
+
setFollowing(true);
|
|
2445
|
+
setSelectedMsgId(id);
|
|
2446
|
+
};
|
|
2447
|
+
const openFromList = (key) => {
|
|
2448
|
+
const name = state.threads[key]?.name ?? "";
|
|
2449
|
+
const query = filter.trim();
|
|
2450
|
+
const hits = query && !fold(name).includes(fold(query)) ? searchMessages(state.messages[key] ?? [], query) : [];
|
|
2451
|
+
if (hits.length === 0) return open(key, true);
|
|
2452
|
+
open(key, false);
|
|
2453
|
+
setFocus("messages");
|
|
2454
|
+
setFilter("");
|
|
2455
|
+
setSearch({ query, editing: false, key });
|
|
2456
|
+
setFollowing(true);
|
|
2457
|
+
setSelectedMsgId(hits[hits.length - 1]);
|
|
2458
|
+
};
|
|
2459
|
+
useEffect2(() => setSearch((s) => s && s.key !== state.activeKey ? null : s), [state.activeKey]);
|
|
2460
|
+
const members = useMemo(() => {
|
|
2461
|
+
const key = state.activeKey;
|
|
2462
|
+
if (!key || state.threads[key]?.kind !== "group") return null;
|
|
2463
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2464
|
+
const out = [];
|
|
2465
|
+
const add = (uid) => {
|
|
2466
|
+
if (!uid || uid === state.selfId || seen.has(uid)) return;
|
|
2467
|
+
const name = personName(state, uid);
|
|
2468
|
+
if (!name) return;
|
|
2469
|
+
seen.add(uid);
|
|
2470
|
+
out.push({ uid, name });
|
|
2471
|
+
};
|
|
2472
|
+
for (const m of [...state.messages[key] ?? []].reverse()) if (!m.isSelf && m.senderId) add(m.senderId);
|
|
2473
|
+
for (const uid of memberIds[key] ?? []) add(uid);
|
|
2474
|
+
return out;
|
|
2475
|
+
}, [state.activeKey, state.messages, state.people, state.threads, memberIds]);
|
|
2476
|
+
const loadMembers = () => {
|
|
2477
|
+
const key = state.activeKey;
|
|
2478
|
+
if (!key || state.threads[key]?.kind !== "group" || memberIds[key]) return;
|
|
2479
|
+
client.groupMembers(key).then((uids) => setMemberIds((m) => ({ ...m, [key]: uids }))).catch(() => flash("couldn't load group members"));
|
|
2480
|
+
};
|
|
1794
2481
|
const react = (icon) => {
|
|
1795
2482
|
const target = selectedMsg;
|
|
1796
2483
|
setPicker(null);
|
|
@@ -1808,9 +2495,11 @@ function ChatScreen({ store: store2, client }) {
|
|
|
1808
2495
|
return false;
|
|
1809
2496
|
}
|
|
1810
2497
|
};
|
|
1811
|
-
const sendTextTo = async (key, text) => {
|
|
2498
|
+
const sendTextTo = async (key, text, picks = []) => {
|
|
1812
2499
|
try {
|
|
1813
|
-
|
|
2500
|
+
const quote = replyTo && replyTo.threadKey === key ? replyTo : void 0;
|
|
2501
|
+
await client.send(key, text, quote, picks);
|
|
2502
|
+
if (quote) setReplyTo((r) => r === quote ? null : r);
|
|
1814
2503
|
setSendError(void 0);
|
|
1815
2504
|
setScroll(0);
|
|
1816
2505
|
return true;
|
|
@@ -1826,7 +2515,7 @@ function ChatScreen({ store: store2, client }) {
|
|
|
1826
2515
|
resolvePending.current = null;
|
|
1827
2516
|
if (!p || !resolve) return;
|
|
1828
2517
|
if (action === "cancel") return resolve(false);
|
|
1829
|
-
void (action === "send" ? sendAttachmentTo(p.key, p.request) : sendTextTo(p.key, p.text)).then(resolve);
|
|
2518
|
+
void (action === "send" ? sendAttachmentTo(p.key, p.request) : sendTextTo(p.key, p.text, p.picks)).then(resolve);
|
|
1830
2519
|
};
|
|
1831
2520
|
const removeSelected = (mode) => {
|
|
1832
2521
|
const target = selectedMsg;
|
|
@@ -1840,9 +2529,22 @@ function ChatScreen({ store: store2, client }) {
|
|
|
1840
2529
|
};
|
|
1841
2530
|
useInput2((input, key) => {
|
|
1842
2531
|
if (showHelp) {
|
|
2532
|
+
const maxOffset = Math.max(0, HELP_LINES - paneHeight);
|
|
2533
|
+
const step = key.pageDown || key.ctrl && input === "d" ? paneHeight : key.pageUp || key.ctrl && input === "u" ? -paneHeight : 0;
|
|
2534
|
+
if (input === "j" || key.downArrow || input === "k" || key.upArrow || step) {
|
|
2535
|
+
const delta = step || (input === "j" || key.downArrow ? 1 : -1);
|
|
2536
|
+
setHelpOffset((o) => Math.max(0, Math.min(maxOffset, o + delta)));
|
|
2537
|
+
return;
|
|
2538
|
+
}
|
|
1843
2539
|
setShowHelp(false);
|
|
1844
2540
|
return;
|
|
1845
2541
|
}
|
|
2542
|
+
if (ownersCursor !== null) {
|
|
2543
|
+
if (key.escape || key.return || input === "w" || input === "q") return setOwnersCursor(null);
|
|
2544
|
+
if (key.upArrow || input === "k") return setOwnersCursor((c) => Math.max(0, (c ?? 0) - 1));
|
|
2545
|
+
if (key.downArrow || input === "j") return setOwnersCursor((c) => Math.min(owners.length - 1, (c ?? 0) + 1));
|
|
2546
|
+
return;
|
|
2547
|
+
}
|
|
1846
2548
|
if (linkChoices) {
|
|
1847
2549
|
if (key.escape) return setLinkChoices(null);
|
|
1848
2550
|
if (key.upArrow || input === "k") return setLinkCursor((c) => Math.max(0, c - 1));
|
|
@@ -1888,16 +2590,39 @@ function ChatScreen({ store: store2, client }) {
|
|
|
1888
2590
|
}
|
|
1889
2591
|
if (key.pageUp) return scrollBy(pageSize);
|
|
1890
2592
|
if (key.pageDown) return scrollBy(-pageSize);
|
|
2593
|
+
if (focus === "input" && mentionOpen && (key.tab || key.escape)) return;
|
|
1891
2594
|
if (key.tab) {
|
|
1892
2595
|
setFocus(focus === "list" && state.activeKey ? "messages" : "list");
|
|
1893
2596
|
return;
|
|
1894
2597
|
}
|
|
1895
2598
|
if (focus === "input") {
|
|
1896
|
-
if (key.escape)
|
|
2599
|
+
if (key.escape) {
|
|
2600
|
+
if (replyBar) return setReplyTo(null);
|
|
2601
|
+
setFocus(returnFocus);
|
|
2602
|
+
}
|
|
1897
2603
|
return;
|
|
1898
2604
|
}
|
|
1899
2605
|
const half = Math.max(1, Math.floor(paneHeight / 2));
|
|
2606
|
+
if (focus === "messages" && search?.editing) {
|
|
2607
|
+
if (key.escape) return setSearch(null);
|
|
2608
|
+
if (key.return) {
|
|
2609
|
+
if (!search.query.trim()) return setSearch(null);
|
|
2610
|
+
setSearch({ ...search, editing: false });
|
|
2611
|
+
return jumpTo(stepMatch(matches, messages, null, -1));
|
|
2612
|
+
}
|
|
2613
|
+
if (key.backspace || key.delete) return setSearch({ ...search, query: Array.from(search.query).slice(0, -1).join("") });
|
|
2614
|
+
if (input && !key.ctrl && !key.meta && !key.tab) setSearch({ ...search, query: search.query + input });
|
|
2615
|
+
return;
|
|
2616
|
+
}
|
|
2617
|
+
if (focus === "messages" && search) {
|
|
2618
|
+
if (key.escape) return setSearch(null);
|
|
2619
|
+
if (input === "n" || input === "N") {
|
|
2620
|
+
if (matches.length === 0) return flash("no matches");
|
|
2621
|
+
return jumpTo(stepMatch(matches, messages, selectedMsg?.id ?? null, input === "n" ? -1 : 1));
|
|
2622
|
+
}
|
|
2623
|
+
}
|
|
1900
2624
|
if (focus === "messages") {
|
|
2625
|
+
if (input === "/") return setSearch({ query: "", editing: true, key: state.activeKey });
|
|
1901
2626
|
const i = msgIndex === -1 ? messages.length - 1 : msgIndex;
|
|
1902
2627
|
const next2 = navStep(input, key, i, messages.length, half);
|
|
1903
2628
|
if (next2 !== null) {
|
|
@@ -1911,6 +2636,19 @@ function ChatScreen({ store: store2, client }) {
|
|
|
1911
2636
|
setSelectedMsgId(selectedMsg.id);
|
|
1912
2637
|
return setPicker({ query: "", cursor: 0 });
|
|
1913
2638
|
}
|
|
2639
|
+
if (input === "r" && selectedMsg) {
|
|
2640
|
+
if (selectedMsg.recalled) return flash("can't reply to a recalled message");
|
|
2641
|
+
if (!selectedMsg.source?.cliMsgId) return flash("this message can't be replied to yet");
|
|
2642
|
+
setReplyTo(selectedMsg);
|
|
2643
|
+
return setFocus("input");
|
|
2644
|
+
}
|
|
2645
|
+
if (input === "w" && selectedMsg) {
|
|
2646
|
+
if (owners.length === 0) return flash("no reactions on this message");
|
|
2647
|
+
setSelectedMsgId(selectedMsg.id);
|
|
2648
|
+
setOwnersCursor(0);
|
|
2649
|
+
void client.lookupPeople(owners.map((o) => o.uid));
|
|
2650
|
+
return;
|
|
2651
|
+
}
|
|
1914
2652
|
if (input === "d" && selectedMsg && !selectedMsg.recalled) {
|
|
1915
2653
|
setSelectedMsgId(selectedMsg.id);
|
|
1916
2654
|
setDeleteCursor(0);
|
|
@@ -1934,6 +2672,8 @@ function ChatScreen({ store: store2, client }) {
|
|
|
1934
2672
|
setFiltering(false);
|
|
1935
2673
|
} else if (key.return) {
|
|
1936
2674
|
setFiltering(false);
|
|
2675
|
+
const t = threads[selected];
|
|
2676
|
+
if (t) openFromList(t.key);
|
|
1937
2677
|
} else if (key.backspace || key.delete) {
|
|
1938
2678
|
setFilter((f) => Array.from(f).slice(0, -1).join(""));
|
|
1939
2679
|
} else if (input && !key.ctrl && !key.meta && !key.upArrow && !key.downArrow) {
|
|
@@ -1950,7 +2690,7 @@ function ChatScreen({ store: store2, client }) {
|
|
|
1950
2690
|
}
|
|
1951
2691
|
if (key.return) {
|
|
1952
2692
|
const t = threads[selected];
|
|
1953
|
-
if (t)
|
|
2693
|
+
if (t) openFromList(t.key);
|
|
1954
2694
|
return;
|
|
1955
2695
|
}
|
|
1956
2696
|
if (input === "?") return setShowHelp(true);
|
|
@@ -1962,13 +2702,19 @@ function ChatScreen({ store: store2, client }) {
|
|
|
1962
2702
|
saveSettings({ notify: notify2 });
|
|
1963
2703
|
return flash(`notifications: ${notify2 === "sender" ? "sender only" : notify2}`);
|
|
1964
2704
|
}
|
|
2705
|
+
if (input === "m") {
|
|
2706
|
+
const notifyGroups2 = state.notifyGroups === "all" ? "mentions" : "all";
|
|
2707
|
+
store2.update((s) => ({ ...s, notifyGroups: notifyGroups2 }));
|
|
2708
|
+
saveSettings({ notifyGroups: notifyGroups2 });
|
|
2709
|
+
return flash(notifyGroups2 === "all" ? "groups: notify every message" : "groups: notify only when mentioned");
|
|
2710
|
+
}
|
|
1965
2711
|
if (input === "r" && state.connection.state === "closed") client.connect();
|
|
1966
2712
|
});
|
|
1967
|
-
const submit = async (text) => {
|
|
2713
|
+
const submit = async (text, picks = []) => {
|
|
1968
2714
|
const key = state.activeKey;
|
|
1969
2715
|
if (!key) return false;
|
|
1970
2716
|
const action = classifyInput(text, isFile);
|
|
1971
|
-
if (action.kind === "text") return sendTextTo(key, action.text);
|
|
2717
|
+
if (action.kind === "text") return sendTextTo(key, action.text, picks);
|
|
1972
2718
|
if (action.kind === "help") {
|
|
1973
2719
|
setSendError(void 0);
|
|
1974
2720
|
setShowHelp(true);
|
|
@@ -1982,11 +2728,12 @@ function ChatScreen({ store: store2, client }) {
|
|
|
1982
2728
|
if (!parsed.confirm) return sendAttachmentTo(key, parsed);
|
|
1983
2729
|
return new Promise((resolve) => {
|
|
1984
2730
|
resolvePending.current = resolve;
|
|
1985
|
-
setPending({ key, text, request: parsed, size: statSync2(parsed.path).size });
|
|
2731
|
+
setPending({ key, text, picks, request: parsed, size: statSync2(parsed.path).size });
|
|
1986
2732
|
});
|
|
1987
2733
|
};
|
|
1988
2734
|
const deleteFrame = deleting ? popupFrameFor(34, choicePopupHeight(deleteOptions(selectedMsg?.isSelf ?? false).length)) : null;
|
|
1989
2735
|
const linkFrame = linkChoices ? popupFrameFor(56, choicePopupHeight(linkChoices.length)) : null;
|
|
2736
|
+
const ownersFrame = ownersOpen ? popupFrameFor(OWNERS_WIDTH, choicePopupHeight(owners.length)) : null;
|
|
1990
2737
|
const reactionFrame = picker ? popupFrameFor(40, 14) : null;
|
|
1991
2738
|
return /* @__PURE__ */ jsxs10(Box9, { flexDirection: "column", width: columns, height: rows, children: [
|
|
1992
2739
|
/* @__PURE__ */ jsxs10(Box9, { children: [
|
|
@@ -2005,7 +2752,7 @@ function ChatScreen({ store: store2, client }) {
|
|
|
2005
2752
|
info: listInfo
|
|
2006
2753
|
}
|
|
2007
2754
|
),
|
|
2008
|
-
showHelp ? /* @__PURE__ */ jsx9(HelpPane, { width: columns - sidebarWidth, height: paneHeight }) : /* @__PURE__ */ jsx9(
|
|
2755
|
+
showHelp ? /* @__PURE__ */ jsx9(HelpPane, { width: columns - sidebarWidth, height: paneHeight, offset: helpOffset }) : /* @__PURE__ */ jsx9(
|
|
2009
2756
|
MessagePane,
|
|
2010
2757
|
{
|
|
2011
2758
|
thread: active,
|
|
@@ -2035,8 +2782,41 @@ function ChatScreen({ store: store2, client }) {
|
|
|
2035
2782
|
footer: "\u2191\u2193 Enter \xB7 1-9 \xB7 Esc"
|
|
2036
2783
|
}
|
|
2037
2784
|
) : null,
|
|
2785
|
+
ownersOpen && ownersFrame ? /* @__PURE__ */ jsx9(
|
|
2786
|
+
ChoicePopup,
|
|
2787
|
+
{
|
|
2788
|
+
frame: ownersFrame,
|
|
2789
|
+
title: `Reactions \xB7 ${owners.length}`,
|
|
2790
|
+
cursor: Math.min(ownersCursor ?? 0, owners.length - 1),
|
|
2791
|
+
choices: owners.map((o) => ({
|
|
2792
|
+
label: `${o.name ?? `\u2026 ${o.uid.slice(-4)}`} ${o.icons.map(reactionIcon).join(" ")}`,
|
|
2793
|
+
color: o.mine ? "cyan" : void 0
|
|
2794
|
+
})),
|
|
2795
|
+
footer: "\u2191\u2193 scroll \xB7 Esc close"
|
|
2796
|
+
}
|
|
2797
|
+
) : null,
|
|
2038
2798
|
picker && reactionFrame ? /* @__PURE__ */ jsx9(ReactionPopup, { frame: reactionFrame, query: picker.query, cursor: picker.cursor, results: searchReactions(picker.query) }) : null
|
|
2039
2799
|
] }),
|
|
2800
|
+
searchBar && search ? /* @__PURE__ */ jsxs10(Text10, { wrap: "truncate-end", children: [
|
|
2801
|
+
/* @__PURE__ */ jsxs10(Text10, { color: "yellow", children: [
|
|
2802
|
+
"/ ",
|
|
2803
|
+
search.query
|
|
2804
|
+
] }),
|
|
2805
|
+
search.editing ? /* @__PURE__ */ jsx9(Text10, { inverse: true, children: " " }) : null,
|
|
2806
|
+
/* @__PURE__ */ jsxs10(Text10, { dimColor: true, children: [
|
|
2807
|
+
" ",
|
|
2808
|
+
search.query.trim() ? matches.length === 0 ? "no matches" : `${selectedMsg && matches.includes(selectedMsg.id) ? `${matches.indexOf(selectedMsg.id) + 1} of ` : ""}${matches.length} ${matches.length === 1 ? "match" : "matches"}` : "search this chat",
|
|
2809
|
+
search.editing ? " \xB7 Enter find \xB7 Esc cancel" : " \xB7 n older \xB7 N newer \xB7 Esc done"
|
|
2810
|
+
] })
|
|
2811
|
+
] }) : null,
|
|
2812
|
+
replyBar ? /* @__PURE__ */ jsxs10(Text10, { wrap: "truncate-end", children: [
|
|
2813
|
+
/* @__PURE__ */ jsxs10(Text10, { color: "cyan", children: [
|
|
2814
|
+
"\u21B3 replying to ",
|
|
2815
|
+
replyTo.isSelf ? "yourself" : replyTo.senderName,
|
|
2816
|
+
": "
|
|
2817
|
+
] }),
|
|
2818
|
+
/* @__PURE__ */ jsx9(Text10, { dimColor: true, children: replyTo.text.replace(/\s+/g, " ") })
|
|
2819
|
+
] }) : null,
|
|
2040
2820
|
pending ? /* @__PURE__ */ jsxs10(Text10, { wrap: "truncate-end", children: [
|
|
2041
2821
|
/* @__PURE__ */ jsxs10(Text10, { color: "cyan", children: [
|
|
2042
2822
|
"Send ",
|
|
@@ -2052,23 +2832,51 @@ function ChatScreen({ store: store2, client }) {
|
|
|
2052
2832
|
" ",
|
|
2053
2833
|
/* @__PURE__ */ jsx9(Text10, { dimColor: true, children: "Enter send \xB7 t send as text \xB7 Esc cancel" })
|
|
2054
2834
|
] }) : null,
|
|
2055
|
-
/* @__PURE__ */ jsx9(Box9, { display: pending ? "none" : "flex", children: /* @__PURE__ */ jsx9(
|
|
2056
|
-
|
|
2835
|
+
/* @__PURE__ */ jsx9(Box9, { display: pending ? "none" : "flex", children: /* @__PURE__ */ jsx9(
|
|
2836
|
+
Composer,
|
|
2837
|
+
{
|
|
2838
|
+
draftKey: state.activeKey,
|
|
2839
|
+
active: focus === "input" && !showHelp,
|
|
2840
|
+
enabled: !!active,
|
|
2841
|
+
error: sendError,
|
|
2842
|
+
width: columns,
|
|
2843
|
+
maxRows: 5,
|
|
2844
|
+
maxHeight: Math.max(1, rows - 6 - (replyBar ? 1 : 0) - (searchBar ? 1 : 0)),
|
|
2845
|
+
onHeight: setComposerRows,
|
|
2846
|
+
members,
|
|
2847
|
+
onMentionStart: loadMembers,
|
|
2848
|
+
onSuggesting: setMentionOpen,
|
|
2849
|
+
onSubmit: submit
|
|
2850
|
+
}
|
|
2851
|
+
) }),
|
|
2852
|
+
/* @__PURE__ */ jsx9(StatusBar, { connection: state.connection, selfName: state.selfName, focus, notify: state.notify, notifyGroups: state.notifyGroups, toast })
|
|
2057
2853
|
] });
|
|
2058
2854
|
}
|
|
2059
2855
|
|
|
2060
2856
|
// src/index.tsx
|
|
2061
2857
|
import { jsx as jsx10 } from "react/jsx-runtime";
|
|
2858
|
+
if (process.argv.includes("--version") || process.argv.includes("-v")) {
|
|
2859
|
+
console.log(package_default.version);
|
|
2860
|
+
process.exit(0);
|
|
2861
|
+
}
|
|
2062
2862
|
var store = new Store();
|
|
2063
|
-
var { notify } = loadSettings();
|
|
2863
|
+
var { notify, notifyGroups } = loadSettings();
|
|
2064
2864
|
if (notify && NOTIFY_MODES.includes(notify)) store.update((s) => ({ ...s, notify }));
|
|
2865
|
+
if (notifyGroups && GROUP_NOTIFY_MODES.includes(notifyGroups)) store.update((s) => ({ ...s, notifyGroups }));
|
|
2065
2866
|
var session = {};
|
|
2066
2867
|
var app = render(/* @__PURE__ */ jsx10(App, { store, onClient: (c) => session.client = c }), { alternateScreen: true });
|
|
2067
2868
|
var stopFocus = trackTerminalFocus((terminalFocused) => store.update((s) => ({ ...s, terminalFocused })));
|
|
2068
2869
|
var stopMouse = trackMouseWheel();
|
|
2870
|
+
ensureStateDir();
|
|
2871
|
+
setNotificationClick((key) => openChatCommand(key, stateDir));
|
|
2872
|
+
var stopOpenRequests = listenForOpenRequests(
|
|
2873
|
+
stateDir,
|
|
2874
|
+
(key) => store.update((s) => ({ ...s, openRequest: { key, seq: (s.openRequest?.seq ?? 0) + 1 } }))
|
|
2875
|
+
);
|
|
2069
2876
|
process.on("exit", () => {
|
|
2070
2877
|
stopFocus();
|
|
2071
2878
|
stopMouse();
|
|
2879
|
+
stopOpenRequests();
|
|
2072
2880
|
});
|
|
2073
2881
|
process.on("SIGINT", () => app.unmount());
|
|
2074
2882
|
process.on("SIGTERM", () => app.unmount());
|