From c6da6d96c1718d7e45bf796cb4fb88276fee4581 Mon Sep 17 00:00:00 2001 From: Christ Date: Sat, 15 Aug 2026 01:57:40 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=B6=E6=AE=B5=E4=B8=83=EF=BC=9A=E5=AE=8C?= =?UTF-8?q?=E6=88=90Chat=20React=20feature=E5=8C=96=E4=B8=8E=E4=BC=9A?= =?UTF-8?q?=E8=AF=9D=E6=B6=88=E6=81=AF=E8=BE=B9=E7=95=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .commentlint/frontend-baseline.json | 2456 ++++++++--------- docs/architecture/refactoring-master-plan.md | 4 +- frontend/app/features/chat/api.ts | 12 + frontend/app/features/chat/hooks.ts | 491 ++++ frontend/app/features/chat/state.test.ts | 30 + frontend/app/features/chat/state.ts | 50 + frontend/app/features/chat/types.ts | 49 + frontend/chatFeature.test.ts | 18 +- frontend/components/Chat.tsx | 384 +-- frontend/services/api.test.ts | 23 + frontend/services/api.ts | 18 +- .../webui/static/assets/index-7RF419ck.js | 20 - .../webui/static/assets/index-CstHnrBD.css | 1 - .../webui/static/assets/index-DFFQFH9n.css | 1 + .../webui/static/assets/index-DMOAPZ9K.js | 20 + internal/webui/static/index.html | 4 +- 16 files changed, 1913 insertions(+), 1668 deletions(-) create mode 100644 frontend/app/features/chat/api.ts create mode 100644 frontend/app/features/chat/hooks.ts create mode 100644 frontend/app/features/chat/state.test.ts create mode 100644 frontend/app/features/chat/state.ts create mode 100644 frontend/app/features/chat/types.ts delete mode 100644 internal/webui/static/assets/index-7RF419ck.js delete mode 100644 internal/webui/static/assets/index-CstHnrBD.css create mode 100644 internal/webui/static/assets/index-DFFQFH9n.css create mode 100644 internal/webui/static/assets/index-DMOAPZ9K.js diff --git a/.commentlint/frontend-baseline.json b/.commentlint/frontend-baseline.json index 817135b..7e0afa4 100644 --- a/.commentlint/frontend-baseline.json +++ b/.commentlint/frontend-baseline.json @@ -323,6 +323,870 @@ "kind": "function-expression", "name": "anonymous" }, + { + "file": "app/features/chat/hooks.ts", + "line": 140, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 141, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 144, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 145, + "kind": "variable", + "name": "sequence" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 147, + "kind": "variable", + "name": "controller" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 150, + "kind": "variable", + "name": "page" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 152, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 153, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 154, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 156, + "kind": "variable", + "name": "error" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 162, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 163, + "kind": "variable", + "name": "controller" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 164, + "kind": "variable", + "name": "load" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 167, + "kind": "variable", + "name": "[details, statuses]" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 171, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 171, + "kind": "variable", + "name": "withRuntime" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 176, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 176, + "kind": "variable", + "name": "enabled" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 177, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 177, + "kind": "variable", + "name": "sessionPages" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 180, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 181, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 182, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 183, + "kind": "variable", + "name": "stored" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 184, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 184, + "kind": "variable", + "name": "first" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 186, + "kind": "variable", + "name": "loadError" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 193, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 196, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 197, + "kind": "variable", + "name": "disposed" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 198, + "kind": "variable", + "name": "timer" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 199, + "kind": "variable", + "name": "controller" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 200, + "kind": "variable", + "name": "poll" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 203, + "kind": "variable", + "name": "statuses" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 204, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 204, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 216, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 223, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 226, + "kind": "variable", + "name": "sessions" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 227, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 227, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 230, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 236, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 241, + "kind": "variable", + "name": "sequence" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 243, + "kind": "variable", + "name": "controller" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 249, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 254, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 254, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 255, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 255, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 256, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 258, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 261, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 264, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 269, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 274, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 274, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 282, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 284, + "kind": "variable", + "name": "container" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 285, + "kind": "variable", + "name": "previousHeight" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 286, + "kind": "variable", + "name": "sequence" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 287, + "kind": "variable", + "name": "controller" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 292, + "kind": "variable", + "name": "oldestID" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 293, + "kind": "variable", + "name": "page" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 295, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 298, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 301, + "kind": "variable", + "name": "loadError" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 309, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 310, + "kind": "variable", + "name": "disposed" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 311, + "kind": "variable", + "name": "reconnectTimer" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 312, + "kind": "variable", + "name": "retry" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 313, + "kind": "variable", + "name": "socket" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 314, + "kind": "variable", + "name": "connect" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 317, + "kind": "variable", + "name": "protocol" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 319, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 320, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 322, + "kind": "variable", + "name": "payload" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 323, + "kind": "variable", + "name": "message" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 325, + "kind": "variable", + "name": "accountID" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 326, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 327, + "kind": "variable", + "name": "rows" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 328, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 328, + "kind": "variable", + "name": "found" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 333, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 338, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 341, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 348, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 351, + "kind": "variable", + "name": "delay" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 354, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 357, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 365, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 366, + "kind": "variable", + "name": "container" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 368, + "kind": "variable", + "name": "distanceFromBottom" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 372, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 373, + "kind": "variable", + "name": "contextChanged" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 376, + "kind": "variable", + "name": "container" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 385, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 385, + "kind": "variable", + "name": "activeAccount" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 386, + "kind": "variable", + "name": "activeSessions" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 387, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 387, + "kind": "variable", + "name": "selectedSession" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 388, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 388, + "kind": "variable", + "name": "filteredSessions" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 389, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 389, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 389, + "kind": "variable", + "name": "unreadForAccount" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 392, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 394, + "kind": "variable", + "name": "sequence" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 396, + "kind": "variable", + "name": "controller" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 398, + "kind": "variable", + "name": "accountID" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 402, + "kind": "variable", + "name": "page" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 404, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 405, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 406, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 407, + "kind": "variable", + "name": "loadError" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 415, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 417, + "kind": "variable", + "name": "sequence" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 419, + "kind": "variable", + "name": "controller" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 424, + "kind": "variable", + "name": "result" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 428, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 429, + "kind": "variable", + "name": "sendError" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 440, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 442, + "kind": "variable", + "name": "sequence" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 444, + "kind": "variable", + "name": "controller" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 449, + "kind": "variable", + "name": "result" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 452, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 453, + "kind": "variable", + "name": "sendError" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 467, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 468, + "kind": "variable", + "name": "text" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 474, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/hooks.ts", + "line": 480, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/state.test.ts", + "line": 25, + "kind": "variable", + "name": "controller" + }, + { + "file": "app/features/chat/state.ts", + "line": 10, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/state.ts", + "line": 16, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/state.ts", + "line": 16, + "kind": "variable", + "name": "keys" + }, + { + "file": "app/features/chat/state.ts", + "line": 17, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/state.ts", + "line": 22, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/state.ts", + "line": 22, + "kind": "variable", + "name": "index" + }, + { + "file": "app/features/chat/state.ts", + "line": 24, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "app/features/chat/state.ts", + "line": 38, + "kind": "variable", + "name": "date" + }, + { + "file": "app/features/chat/state.ts", + "line": 39, + "kind": "variable", + "name": "today" + }, + { + "file": "app/features/chat/state.ts", + "line": 48, + "kind": "variable", + "name": "date" + }, + { + "file": "app/features/chat/state.ts", + "line": 5, + "kind": "variable", + "name": "keyword" + }, + { + "file": "app/features/chat/state.ts", + "line": 6, + "kind": "function-expression", + "name": "anonymous" + }, { "file": "app/features/dashboard/hooks.ts", "line": 100, @@ -933,7 +1797,7 @@ "file": "chatFeature.test.ts", "line": 26, "kind": "variable", - "name": "chat" + "name": "chatHook" }, { "file": "chatFeature.test.ts", @@ -949,28 +1813,40 @@ }, { "file": "chatFeature.test.ts", - "line": 40, - "kind": "function-expression", - "name": "anonymous" + "line": 33, + "kind": "variable", + "name": "chatHook" }, { "file": "chatFeature.test.ts", "line": 41, - "kind": "variable", - "name": "chat" - }, - { - "file": "chatFeature.test.ts", - "line": 46, "kind": "function-expression", "name": "anonymous" }, { "file": "chatFeature.test.ts", - "line": 47, + "line": 42, "kind": "variable", "name": "chat" }, + { + "file": "chatFeature.test.ts", + "line": 47, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "chatFeature.test.ts", + "line": 48, + "kind": "variable", + "name": "chat" + }, + { + "file": "chatFeature.test.ts", + "line": 49, + "kind": "variable", + "name": "chatHook" + }, { "file": "chatFeature.test.ts", "line": 5, @@ -979,13 +1855,13 @@ }, { "file": "chatFeature.test.ts", - "line": 55, + "line": 57, "kind": "function-expression", "name": "anonymous" }, { "file": "chatFeature.test.ts", - "line": 56, + "line": 58, "kind": "variable", "name": "sidebar" }, @@ -2851,55 +3727,7 @@ }, { "file": "components/Chat.tsx", - "line": 101, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 104, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 105, - "kind": "variable", - "name": "disposed" - }, - { - "file": "components/Chat.tsx", - "line": 106, - "kind": "variable", - "name": "timer" - }, - { - "file": "components/Chat.tsx", - "line": 107, - "kind": "variable", - "name": "controller" - }, - { - "file": "components/Chat.tsx", - "line": 108, - "kind": "variable", - "name": "poll" - }, - { - "file": "components/Chat.tsx", - "line": 111, - "kind": "variable", - "name": "statuses" - }, - { - "file": "components/Chat.tsx", - "line": 113, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 113, + "line": 122, "kind": "function-expression", "name": "anonymous" }, @@ -2911,97 +3739,19 @@ }, { "file": "components/Chat.tsx", - "line": 134, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 137, + "line": 128, "kind": "variable", - "name": "sessions" + "name": "outgoing" }, { "file": "components/Chat.tsx", - "line": 138, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 138, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 141, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 144, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 149, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 15, + "line": 129, "kind": "variable", - "name": "accountStorageKey" + "name": "system" }, { "file": "components/Chat.tsx", - "line": 154, - "kind": "variable", - "name": "controller" - }, - { - "file": "components/Chat.tsx", - "line": 159, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 164, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 164, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 166, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 168, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 17, - "kind": "variable", - "name": "formatClock" - }, - { - "file": "components/Chat.tsx", - "line": 171, + "line": 169, "kind": "function-expression", "name": "anonymous" }, @@ -3013,39 +3763,51 @@ }, { "file": "components/Chat.tsx", - "line": 176, + "line": 173, "kind": "function-expression", "name": "anonymous" }, { "file": "components/Chat.tsx", - "line": 179, - "kind": "variable", - "name": "loadOlderMessages" + "line": 177, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "components/Chat.tsx", + "line": 177, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "components/Chat.tsx", + "line": 177, + "kind": "function-expression", + "name": "anonymous" }, { "file": "components/Chat.tsx", "line": 181, - "kind": "variable", - "name": "container" + "kind": "function-expression", + "name": "anonymous" }, { "file": "components/Chat.tsx", "line": 182, - "kind": "variable", - "name": "previousHeight" + "kind": "function-expression", + "name": "anonymous" }, { "file": "components/Chat.tsx", - "line": 187, - "kind": "variable", - "name": "oldestID" + "line": 185, + "kind": "function-expression", + "name": "anonymous" }, { "file": "components/Chat.tsx", - "line": 188, - "kind": "variable", - "name": "page" + "line": 186, + "kind": "function-expression", + "name": "anonymous" }, { "file": "components/Chat.tsx", @@ -3053,795 +3815,39 @@ "kind": "function-expression", "name": "anonymous" }, - { - "file": "components/Chat.tsx", - "line": 19, - "kind": "variable", - "name": "date" - }, - { - "file": "components/Chat.tsx", - "line": 190, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 190, - "kind": "variable", - "name": "keys" - }, - { - "file": "components/Chat.tsx", - "line": 191, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 195, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 198, - "kind": "variable", - "name": "loadError" - }, - { - "file": "components/Chat.tsx", - "line": 20, - "kind": "variable", - "name": "today" - }, - { - "file": "components/Chat.tsx", - "line": 206, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 207, - "kind": "variable", - "name": "disposed" - }, - { - "file": "components/Chat.tsx", - "line": 208, - "kind": "variable", - "name": "reconnectTimer" - }, - { - "file": "components/Chat.tsx", - "line": 209, - "kind": "variable", - "name": "retry" - }, - { - "file": "components/Chat.tsx", - "line": 210, - "kind": "variable", - "name": "socket" - }, - { - "file": "components/Chat.tsx", - "line": 211, - "kind": "variable", - "name": "connect" - }, - { - "file": "components/Chat.tsx", - "line": 214, - "kind": "variable", - "name": "protocol" - }, - { - "file": "components/Chat.tsx", - "line": 216, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 217, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 219, - "kind": "variable", - "name": "payload" - }, - { - "file": "components/Chat.tsx", - "line": 220, - "kind": "variable", - "name": "message" - }, - { - "file": "components/Chat.tsx", - "line": 222, - "kind": "variable", - "name": "accountID" - }, - { - "file": "components/Chat.tsx", - "line": 223, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 224, - "kind": "variable", - "name": "rows" - }, - { - "file": "components/Chat.tsx", - "line": 225, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 225, - "kind": "variable", - "name": "found" - }, - { - "file": "components/Chat.tsx", - "line": 232, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 238, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 242, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 243, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 243, - "kind": "variable", - "name": "index" - }, - { - "file": "components/Chat.tsx", - "line": 244, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 253, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 256, - "kind": "variable", - "name": "delay" - }, - { - "file": "components/Chat.tsx", - "line": 259, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 262, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 269, - "kind": "variable", - "name": "handleMessageScroll" - }, - { - "file": "components/Chat.tsx", - "line": 27, - "kind": "variable", - "name": "messageTime" - }, - { - "file": "components/Chat.tsx", - "line": 270, - "kind": "variable", - "name": "container" - }, - { - "file": "components/Chat.tsx", - "line": 272, - "kind": "variable", - "name": "distanceFromBottom" - }, - { - "file": "components/Chat.tsx", - "line": 276, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 277, - "kind": "variable", - "name": "contextChanged" - }, - { - "file": "components/Chat.tsx", - "line": 28, - "kind": "variable", - "name": "date" - }, - { - "file": "components/Chat.tsx", - "line": 282, - "kind": "variable", - "name": "container" - }, - { - "file": "components/Chat.tsx", - "line": 292, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 292, - "kind": "variable", - "name": "activeAccount" - }, - { - "file": "components/Chat.tsx", - "line": 293, - "kind": "variable", - "name": "activeSessions" - }, - { - "file": "components/Chat.tsx", - "line": 294, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 294, - "kind": "variable", - "name": "selectedSession" - }, - { - "file": "components/Chat.tsx", - "line": 295, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 295, - "kind": "variable", - "name": "filteredSessions" - }, - { - "file": "components/Chat.tsx", - "line": 296, - "kind": "variable", - "name": "keyword" - }, - { - "file": "components/Chat.tsx", - "line": 297, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 301, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 305, - "kind": "variable", - "name": "unreadForAccount" - }, - { - "file": "components/Chat.tsx", - "line": 306, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 308, - "kind": "variable", - "name": "loadMoreContacts" - }, - { - "file": "components/Chat.tsx", - "line": 313, - "kind": "variable", - "name": "page" - }, - { - "file": "components/Chat.tsx", - "line": 314, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 315, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 316, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 317, - "kind": "variable", - "name": "loadError" - }, - { - "file": "components/Chat.tsx", - "line": 32, - "kind": "variable", - "name": "Chat" - }, - { - "file": "components/Chat.tsx", - "line": 322, - "kind": "variable", - "name": "handleSend" - }, - { - "file": "components/Chat.tsx", - "line": 323, - "kind": "variable", - "name": "text" - }, - { - "file": "components/Chat.tsx", - "line": 328, - "kind": "variable", - "name": "result" - }, - { - "file": "components/Chat.tsx", - "line": 33, - "kind": "variable", - "name": "[accounts, setAccounts]" - }, - { - "file": "components/Chat.tsx", - "line": 334, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 334, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 335, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 337, - "kind": "variable", - "name": "sendError" - }, - { - "file": "components/Chat.tsx", - "line": 34, - "kind": "variable", - "name": "[activeAccountID, setActiveAccountID]" - }, - { - "file": "components/Chat.tsx", - "line": 345, - "kind": "variable", - "name": "handleImage" - }, - { - "file": "components/Chat.tsx", - "line": 35, - "kind": "variable", - "name": "[sessionsByAccount, setSessionsByAccount]" - }, - { - "file": "components/Chat.tsx", - "line": 350, - "kind": "variable", - "name": "result" - }, - { - "file": "components/Chat.tsx", - "line": 355, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 355, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 356, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 358, - "kind": "variable", - "name": "sendError" - }, { "file": "components/Chat.tsx", "line": 36, - "kind": "variable", - "name": "[activeChatID, setActiveChatID]" + "kind": "function-expression", + "name": "anonymous" }, { "file": "components/Chat.tsx", "line": 37, "kind": "variable", - "name": "[messages, setMessages]" + "name": "active" }, { "file": "components/Chat.tsx", "line": 38, "kind": "variable", - "name": "[search, setSearch]" - }, - { - "file": "components/Chat.tsx", - "line": 382, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 383, - "kind": "variable", - "name": "active" - }, - { - "file": "components/Chat.tsx", - "line": 384, - "kind": "variable", "name": "unread" }, - { - "file": "components/Chat.tsx", - "line": 385, - "kind": "variable", - "name": "online" - }, - { - "file": "components/Chat.tsx", - "line": 387, - "kind": "function-expression", - "name": "anonymous" - }, { "file": "components/Chat.tsx", "line": 39, "kind": "variable", - "name": "[unreadOnly, setUnreadOnly]" - }, - { - "file": "components/Chat.tsx", - "line": 40, - "kind": "variable", - "name": "[draft, setDraft]" + "name": "online" }, { "file": "components/Chat.tsx", "line": 41, - "kind": "variable", - "name": "[loading, setLoading]" - }, - { - "file": "components/Chat.tsx", - "line": 410, "kind": "function-expression", "name": "anonymous" }, { "file": "components/Chat.tsx", - "line": 414, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 414, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 418, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 42, - "kind": "variable", - "name": "[messagesLoading, setMessagesLoading]" - }, - { - "file": "components/Chat.tsx", - "line": 424, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 425, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 43, - "kind": "variable", - "name": "[olderLoading, setOlderLoading]" - }, - { - "file": "components/Chat.tsx", - "line": 44, - "kind": "variable", - "name": "[hasOlder, setHasOlder]" - }, - { - "file": "components/Chat.tsx", - "line": 445, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 45, - "kind": "variable", - "name": "[historyCursor, setHistoryCursor]" - }, - { - "file": "components/Chat.tsx", - "line": 46, - "kind": "variable", - "name": "[contactCursors, setContactCursors]" - }, - { - "file": "components/Chat.tsx", - "line": 468, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 47, - "kind": "variable", - "name": "[hasMoreContacts, setHasMoreContacts]" - }, - { - "file": "components/Chat.tsx", - "line": 473, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 474, - "kind": "variable", - "name": "outgoing" - }, - { - "file": "components/Chat.tsx", - "line": 475, - "kind": "variable", - "name": "system" - }, - { - "file": "components/Chat.tsx", - "line": 48, - "kind": "variable", - "name": "[contactsLoading, setContactsLoading]" - }, - { - "file": "components/Chat.tsx", - "line": 49, - "kind": "variable", - "name": "[emojiOpen, setEmojiOpen]" - }, - { - "file": "components/Chat.tsx", - "line": 50, - "kind": "variable", - "name": "[sending, setSending]" - }, - { - "file": "components/Chat.tsx", - "line": 51, - "kind": "variable", - "name": "[error, setError]" - }, - { - "file": "components/Chat.tsx", - "line": 519, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 519, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 52, - "kind": "variable", - "name": "[liveState, setLiveState]" - }, - { - "file": "components/Chat.tsx", - "line": 523, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 523, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 523, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 527, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 528, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 53, - "kind": "variable", - "name": "activeAccountRef" - }, - { - "file": "components/Chat.tsx", - "line": 532, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 533, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 536, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 54, - "kind": "variable", - "name": "activeChatRef" - }, - { - "file": "components/Chat.tsx", - "line": 55, - "kind": "variable", - "name": "scrollRef" - }, - { - "file": "components/Chat.tsx", - "line": 56, - "kind": "variable", - "name": "scrollContextRef" - }, - { - "file": "components/Chat.tsx", - "line": 57, - "kind": "variable", - "name": "shouldScrollToBottomRef" - }, - { - "file": "components/Chat.tsx", - "line": 58, - "kind": "variable", - "name": "skipNextMessageScrollRef" - }, - { - "file": "components/Chat.tsx", - "line": 59, - "kind": "variable", - "name": "imageInputRef" - }, - { - "file": "components/Chat.tsx", - "line": 60, - "kind": "variable", - "name": "refreshedAccountsRef" - }, - { - "file": "components/Chat.tsx", - "line": 62, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 63, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 65, - "kind": "variable", - "name": "reloadSessions" - }, - { - "file": "components/Chat.tsx", - "line": 66, - "kind": "variable", - "name": "page" - }, - { - "file": "components/Chat.tsx", - "line": 67, + "line": 64, "kind": "function-expression", "name": "anonymous" }, @@ -3853,112 +3859,34 @@ }, { "file": "components/Chat.tsx", - "line": 69, + "line": 68, "kind": "function-expression", "name": "anonymous" }, { "file": "components/Chat.tsx", - "line": 73, + "line": 72, "kind": "function-expression", "name": "anonymous" }, - { - "file": "components/Chat.tsx", - "line": 74, - "kind": "variable", - "name": "cancelled" - }, - { - "file": "components/Chat.tsx", - "line": 75, - "kind": "variable", - "name": "load" - }, { "file": "components/Chat.tsx", "line": 78, - "kind": "variable", - "name": "[details, statuses]" - }, - { - "file": "components/Chat.tsx", - "line": 79, "kind": "function-expression", "name": "anonymous" }, { "file": "components/Chat.tsx", "line": 79, - "kind": "variable", - "name": "withRuntime" - }, - { - "file": "components/Chat.tsx", - "line": 84, "kind": "function-expression", "name": "anonymous" }, { "file": "components/Chat.tsx", - "line": 84, - "kind": "variable", - "name": "enabled" - }, - { - "file": "components/Chat.tsx", - "line": 85, + "line": 99, "kind": "function-expression", "name": "anonymous" }, - { - "file": "components/Chat.tsx", - "line": 85, - "kind": "variable", - "name": "sessionPages" - }, - { - "file": "components/Chat.tsx", - "line": 88, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 89, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 90, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 91, - "kind": "variable", - "name": "stored" - }, - { - "file": "components/Chat.tsx", - "line": 92, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "components/Chat.tsx", - "line": 92, - "kind": "variable", - "name": "first" - }, - { - "file": "components/Chat.tsx", - "line": 94, - "kind": "variable", - "name": "loadError" - }, { "file": "components/Dashboard.test.tsx", "line": 10, @@ -9175,19 +9103,19 @@ }, { "file": "services/api.test.ts", - "line": 109, + "line": 112, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 110, + "line": 113, "kind": "variable", "name": "fetchMock" }, { "file": "services/api.test.ts", - "line": 118, + "line": 119, "kind": "function-expression", "name": "anonymous" }, @@ -9195,31 +9123,25 @@ "file": "services/api.test.ts", "line": 120, "kind": "variable", - "name": "result" + "name": "fetchMock" }, { "file": "services/api.test.ts", - "line": 124, - "kind": "variable", - "name": "jsonResponse" - }, - { - "file": "services/api.test.ts", - "line": 129, + "line": 122, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 130, - "kind": "variable", - "name": "fetchMock" + "line": 132, + "kind": "function-expression", + "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 135, + "line": 133, "kind": "variable", - "name": "result" + "name": "fetchMock" }, { "file": "services/api.test.ts", @@ -9229,82 +9151,88 @@ }, { "file": "services/api.test.ts", - "line": 145, + "line": 143, "kind": "variable", "name": "result" }, { "file": "services/api.test.ts", - "line": 149, + "line": 147, + "kind": "variable", + "name": "jsonResponse" + }, + { + "file": "services/api.test.ts", + "line": 152, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 163, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 183, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 203, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 223, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 224, + "line": 153, "kind": "variable", "name": "fetchMock" }, { "file": "services/api.test.ts", - "line": 234, + "line": 158, "kind": "variable", "name": "result" }, + { + "file": "services/api.test.ts", + "line": 164, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 168, + "kind": "variable", + "name": "result" + }, + { + "file": "services/api.test.ts", + "line": 172, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 186, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 206, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 226, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 246, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 247, + "kind": "variable", + "name": "fetchMock" + }, { "file": "services/api.test.ts", "line": 257, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 258, - "kind": "variable", - "name": "fetchMock" - }, - { - "file": "services/api.test.ts", - "line": 263, "kind": "variable", "name": "result" }, - { - "file": "services/api.test.ts", - "line": 272, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 273, - "kind": "variable", - "name": "fetchMock" - }, { "file": "services/api.test.ts", "line": 280, @@ -9313,88 +9241,94 @@ }, { "file": "services/api.test.ts", - "line": 282, + "line": 281, + "kind": "variable", + "name": "fetchMock" + }, + { + "file": "services/api.test.ts", + "line": 286, "kind": "variable", "name": "result" }, { "file": "services/api.test.ts", - "line": 286, + "line": 295, "kind": "function-expression", "name": "anonymous" }, - { - "file": "services/api.test.ts", - "line": 287, - "kind": "variable", - "name": "fetchMock" - }, { "file": "services/api.test.ts", "line": 296, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 297, "kind": "variable", "name": "fetchMock" }, { "file": "services/api.test.ts", - "line": 306, + "line": 303, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 307, + "line": 305, + "kind": "variable", + "name": "result" + }, + { + "file": "services/api.test.ts", + "line": 309, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 310, "kind": "variable", "name": "fetchMock" }, { "file": "services/api.test.ts", - "line": 318, + "line": 319, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 320, + "kind": "variable", + "name": "fetchMock" + }, + { + "file": "services/api.test.ts", + "line": 329, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 330, + "kind": "variable", + "name": "fetchMock" + }, + { + "file": "services/api.test.ts", + "line": 341, "kind": "variable", "name": "body" }, { "file": "services/api.test.ts", - "line": 322, + "line": 345, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 337, + "line": 360, "kind": "variable", "name": "items" }, - { - "file": "services/api.test.ts", - "line": 352, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 353, - "kind": "variable", - "name": "fetchMock" - }, - { - "file": "services/api.test.ts", - "line": 364, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 370, - "kind": "variable", - "name": "settings" - }, { "file": "services/api.test.ts", "line": 375, @@ -9415,39 +9349,51 @@ }, { "file": "services/api.test.ts", - "line": 388, + "line": 393, + "kind": "variable", + "name": "settings" + }, + { + "file": "services/api.test.ts", + "line": 398, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 388, + "line": 399, "kind": "variable", "name": "fetchMock" }, { "file": "services/api.test.ts", - "line": 414, + "line": 410, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 415, + "line": 411, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 411, "kind": "variable", "name": "fetchMock" }, { "file": "services/api.test.ts", - "line": 429, + "line": 437, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 443, + "line": 438, "kind": "variable", - "name": "accounts" + "name": "fetchMock" }, { "file": "services/api.test.ts", @@ -9457,49 +9403,37 @@ }, { "file": "services/api.test.ts", - "line": 454, + "line": 452, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 455, + "line": 466, + "kind": "variable", + "name": "accounts" + }, + { + "file": "services/api.test.ts", + "line": 477, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 478, "kind": "variable", "name": "fetchMock" }, { "file": "services/api.test.ts", - "line": 469, + "line": 492, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 470, - "kind": "variable", - "name": "fetchMock" - }, - { - "file": "services/api.test.ts", - "line": 481, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 482, - "kind": "variable", - "name": "fetchMock" - }, - { - "file": "services/api.test.ts", - "line": 495, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 496, + "line": 493, "kind": "variable", "name": "fetchMock" }, @@ -9509,6 +9443,18 @@ "kind": "function-expression", "name": "anonymous" }, + { + "file": "services/api.test.ts", + "line": 504, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 505, + "kind": "variable", + "name": "fetchMock" + }, { "file": "services/api.test.ts", "line": 51, @@ -9517,49 +9463,43 @@ }, { "file": "services/api.test.ts", - "line": 513, - "kind": "variable", - "name": "status" - }, - { - "file": "services/api.test.ts", - "line": 521, + "line": 518, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 544, - "kind": "variable", - "name": "rules" - }, - { - "file": "services/api.test.ts", - "line": 558, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 559, + "line": 519, "kind": "variable", "name": "fetchMock" }, { "file": "services/api.test.ts", - "line": 568, + "line": 536, "kind": "variable", - "name": "rules" + "name": "status" }, { "file": "services/api.test.ts", - "line": 580, + "line": 544, "kind": "function-expression", "name": "anonymous" }, + { + "file": "services/api.test.ts", + "line": 567, + "kind": "variable", + "name": "rules" + }, { "file": "services/api.test.ts", "line": 581, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 582, "kind": "variable", "name": "fetchMock" }, @@ -9571,15 +9511,9 @@ }, { "file": "services/api.test.ts", - "line": 596, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 597, + "line": 591, "kind": "variable", - "name": "fetchMock" + "name": "rules" }, { "file": "services/api.test.ts", @@ -9589,28 +9523,34 @@ }, { "file": "services/api.test.ts", - "line": 606, + "line": 603, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 607, + "line": 604, "kind": "variable", "name": "fetchMock" }, - { - "file": "services/api.test.ts", - "line": 618, - "kind": "function-expression", - "name": "anonymous" - }, { "file": "services/api.test.ts", "line": 619, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 620, "kind": "variable", "name": "fetchMock" }, + { + "file": "services/api.test.ts", + "line": 629, + "kind": "function-expression", + "name": "anonymous" + }, { "file": "services/api.test.ts", "line": 63, @@ -9619,76 +9559,88 @@ }, { "file": "services/api.test.ts", - "line": 634, + "line": 630, "kind": "variable", - "name": "body" + "name": "fetchMock" + }, + { + "file": "services/api.test.ts", + "line": 641, + "kind": "function-expression", + "name": "anonymous" }, { "file": "services/api.test.ts", "line": 642, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 643, "kind": "variable", "name": "fetchMock" }, { "file": "services/api.test.ts", - "line": 654, + "line": 657, "kind": "variable", "name": "body" }, { "file": "services/api.test.ts", - "line": 661, + "line": 665, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 662, + "line": 666, "kind": "variable", "name": "fetchMock" }, - { - "file": "services/api.test.ts", - "line": 676, - "kind": "function-expression", - "name": "anonymous" - }, { "file": "services/api.test.ts", "line": 677, "kind": "variable", - "name": "fetchMock" - }, - { - "file": "services/api.test.ts", - "line": 698, - "kind": "variable", "name": "body" }, { "file": "services/api.test.ts", - "line": 714, + "line": 684, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 715, + "line": 685, "kind": "variable", "name": "fetchMock" }, { "file": "services/api.test.ts", - "line": 743, + "line": 699, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 700, + "kind": "variable", + "name": "fetchMock" + }, + { + "file": "services/api.test.ts", + "line": 721, "kind": "variable", "name": "body" }, + { + "file": "services/api.test.ts", + "line": 737, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 738, + "kind": "variable", + "name": "fetchMock" + }, { "file": "services/api.test.ts", "line": 75, @@ -9697,94 +9649,76 @@ }, { "file": "services/api.test.ts", - "line": 76, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 76, - "kind": "variable", - "name": "fetchMock" - }, - { - "file": "services/api.test.ts", - "line": 767, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 768, - "kind": "variable", - "name": "fetchMock" - }, - { - "file": "services/api.test.ts", - "line": 780, + "line": 766, "kind": "variable", "name": "body" }, { "file": "services/api.test.ts", - "line": 781, + "line": 790, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 781, + "line": 791, + "kind": "variable", + "name": "fetchMock" + }, + { + "file": "services/api.test.ts", + "line": 803, + "kind": "variable", + "name": "body" + }, + { + "file": "services/api.test.ts", + "line": 804, + "kind": "function-expression", + "name": "anonymous" + }, + { + "file": "services/api.test.ts", + "line": 804, "kind": "member", "name": "action_type" }, { "file": "services/api.test.ts", - "line": 789, + "line": 812, "kind": "function-expression", "name": "anonymous" }, { "file": "services/api.test.ts", - "line": 790, + "line": 813, "kind": "variable", "name": "fetchMock" }, { "file": "services/api.test.ts", - "line": 807, + "line": 830, "kind": "variable", "name": "body" }, { "file": "services/api.test.ts", - "line": 89, + "line": 98, "kind": "function-expression", "name": "anonymous" }, - { - "file": "services/api.test.ts", - "line": 90, - "kind": "variable", - "name": "fetchMock" - }, - { - "file": "services/api.test.ts", - "line": 96, - "kind": "function-expression", - "name": "anonymous" - }, - { - "file": "services/api.test.ts", - "line": 97, - "kind": "variable", - "name": "fetchMock" - }, { "file": "services/api.test.ts", "line": 99, "kind": "function-expression", "name": "anonymous" }, + { + "file": "services/api.test.ts", + "line": 99, + "kind": "variable", + "name": "fetchMock" + }, { "file": "services/api.ts", "line": 100, diff --git a/docs/architecture/refactoring-master-plan.md b/docs/architecture/refactoring-master-plan.md index 6092010..4c81cd8 100644 --- a/docs/architecture/refactoring-master-plan.md +++ b/docs/architecture/refactoring-master-plan.md @@ -177,7 +177,8 @@ app shell / routes - 已完成阶段 7 第六个 PR 切片“React Notifications feature 化与渠道/事件绑定行为边界”:按 `app/features/notifications` 提取通知渠道/SMTP API 适配层、静态渠道配置、数据与动作 Hook、渠道列表、事件绑定选择器、渠道编辑弹窗和 SMTP 面板;渠道与 SMTP 请求支持取消和代次门禁,表单支持渠道字段/独立 SMTP 校验、保存失败重试和过期响应保护;更新架构字符串测试并新增状态/API 取消信号测试,保留旧页面入口和 API 路径;前端类型检查、全量测试、注释检查和构建门禁通过,合并为一个可回滚提交; - 已完成阶段 7 第七个 PR 切片“React Dashboard feature 化与统计/趋势查询行为边界”:按 `app/features/dashboard` 提取 Dashboard API 适配层、统计数据 Hook、趋势/排行派生状态和请求边界;概览、趋势和有效订单支持并行加载、刷新取消、失败重试与过期响应保护;新增统计派生数据、API 取消信号和请求代次测试,保留旧页面入口和 API 路径;前端类型检查、全量测试、注释检查和构建门禁通过并合并为一个可回滚提交; - 已完成阶段 7 第八个 PR 切片“React Settings feature 化与系统配置校验边界”:按 `app/features/settings` 提取系统配置/模型/凭据 API 适配层、配置常量、表单状态和敏感字段校验;配置读取与保存支持并行加载、请求取消、失败重试和过期响应保护,登录凭据提交保留现有校验与重登录语义;新增配置裁剪、凭据校验、API 取消信号和请求代次测试,保留旧页面入口和 API 路径; -- 阶段 7 下一 PR 切片为“React Chat feature 化与会话消息行为边界”:提取聊天 API、会话/消息分页 Hook 和消息发送状态,补充会话切换取消、分页过期响应和发送失败重试测试; +- 已完成阶段 7 第九个 PR 切片“React Chat feature 化与会话消息行为边界”:按 `app/features/chat` 提取聊天 API、会话筛选/消息合并状态、账号/会话/消息分页 Hook;会话切换、联系人分页和消息加载支持取消与请求代次保护,文本/图片发送支持失败重试,WebSocket 和滚动语义保持不变;新增会话筛选、消息去重、分页过期响应、发送取消与 API 取消信号测试,保留旧页面入口和 API 路径; +- 阶段 7 下一 PR 切片为“React AccountAutomation feature 化与任务设置行为边界”:提取账号任务设置/执行 API、任务表单状态和运行结果面板,补充账号切换取消、重复执行阻断和失败重试测试; - 禁止跳过当前入口直接开始 Engine、Automation 或 DB 的大规模拆分。 ## 6. 阶段 0:治理文档与强约束 @@ -604,3 +605,4 @@ npm --prefix frontend run build | 2026-08-15 | 完成阶段 7 第六个 PR 切片“React Notifications feature 化与渠道/事件绑定行为边界” | 按 `app/features/notifications` 提取通知渠道/SMTP API 适配层、静态渠道配置、数据与动作 Hook、渠道列表、事件绑定选择器、渠道编辑弹窗和 SMTP 面板;渠道与 SMTP 请求支持取消和代次门禁,表单支持渠道字段/独立 SMTP 校验、保存失败重试和过期响应保护;更新架构字符串测试并新增状态/API 取消信号测试,保留旧页面入口和 API 路径;前端类型检查、全量测试、注释检查和构建门禁通过并合并为一个可回滚提交 | 阶段 7:React Dashboard feature 化与统计/趋势查询行为边界 | | 2026-08-15 | 完成阶段 7 第七个 PR 切片“React Dashboard feature 化与统计/趋势查询行为边界” | 按 `app/features/dashboard` 提取 Dashboard API 适配层、统计数据 Hook、趋势/排行派生状态和请求边界;概览、趋势和有效订单支持并行加载、刷新取消、失败重试与过期响应保护;新增统计派生数据、API 取消信号和请求代次测试,保留旧页面入口和 API 路径;前端类型检查、全量测试、注释检查和构建门禁通过并合并为一个可回滚提交 | 阶段 7:React Settings feature 化与系统配置校验边界 | | 2026-08-15 | 完成阶段 7 第八个 PR 切片“React Settings feature 化与系统配置校验边界” | 按 `app/features/settings` 提取系统配置/模型/凭据 API 适配层、配置常量、表单状态和敏感字段校验;配置读取与保存支持并行加载、请求取消、失败重试和过期响应保护,登录凭据提交保留现有校验与重登录语义;新增配置裁剪、凭据校验、API 取消信号和请求代次测试,保留旧页面入口和 API 路径 | 阶段 7:React Chat feature 化与会话消息行为边界 | +| 2026-08-15 | 完成阶段 7 第九个 PR 切片“React Chat feature 化与会话消息行为边界” | 按 `app/features/chat` 提取聊天 API、会话筛选/消息合并状态、账号/会话/消息分页 Hook;会话切换、联系人分页和消息加载支持取消与请求代次保护,文本/图片发送支持失败重试,WebSocket 和滚动语义保持不变;新增会话筛选、消息去重、分页过期响应、发送取消与 API 取消信号测试,保留旧页面入口和 API 路径 | 阶段 7:React AccountAutomation feature 化与任务设置行为边界 | diff --git a/frontend/app/features/chat/api.ts b/frontend/app/features/chat/api.ts new file mode 100644 index 0000000..e6ddefd --- /dev/null +++ b/frontend/app/features/chat/api.ts @@ -0,0 +1,12 @@ +/** Chat feature 使用的会话、消息和账号运行状态 API。 */ +export { + getAccountDetails, + getAccountRuntimeStatuses, + getChatMessagePage, + getChatMessages, + getChatSessionPage, + getChatSessions, + markChatRead, + sendChatImage, + sendChatMessage, +} from '../../../services/api'; diff --git a/frontend/app/features/chat/hooks.ts b/frontend/app/features/chat/hooks.ts new file mode 100644 index 0000000..be573d8 --- /dev/null +++ b/frontend/app/features/chat/hooks.ts @@ -0,0 +1,491 @@ +import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'; +import type React from 'react'; +import type { AccountDetail, ChatMessage, ChatSession } from '../../../types'; +import { emojiURL, renderXianyuText, xianyuEmojis } from '../../../chatEmojis'; +import { getAccountDetails, getAccountRuntimeStatuses, getChatMessagePage, getChatSessionPage, markChatRead, sendChatImage, sendChatMessage } from './api'; +import { filterChatSessions, formatClock, isChatAbortError, isCurrentChatRequest, mergeLiveMessage, mergeOlderMessages, messageTime } from './state'; +import type { ChatFeatureState, ChatLiveState, SessionsByAccount } from './types'; + +/** Chat Hook 对外暴露的状态、引用和交互动作。 */ +export type UseChatResult = ChatFeatureState & { + /** 当前选中的会话 ID。 */ + activeChatID: string; + /** 当前账号过滤后的会话。 */ + filteredSessions: ChatSession[]; + /** 消息滚动容器引用。 */ + scrollRef: React.MutableRefObject; + /** 图片文件输入引用。 */ + imageInputRef: React.MutableRefObject; + /** 更新当前账号。 */ + setActiveAccountID: React.Dispatch>; + /** 更新当前会话。 */ + setActiveChatID: React.Dispatch>; + /** 更新搜索文本。 */ + setSearch: React.Dispatch>; + /** 更新未读筛选。 */ + setUnreadOnly: React.Dispatch>; + /** 更新消息草稿。 */ + setDraft: React.Dispatch>; + /** 更新表情选择器状态。 */ + setEmojiOpen: React.Dispatch>; + /** 刷新当前账号会话。 */ + reloadSessions: (accountID: string) => Promise; + /** 加载更早的联系人。 */ + loadMoreContacts: () => Promise; + /** 加载更早的消息。 */ + loadOlderMessages: () => Promise; + /** 根据滚动位置更新自动滚动策略。 */ + handleMessageScroll: () => void; + /** 发送文本消息。 */ + handleSend: () => Promise; + /** 发送图片消息。 */ + handleImage: (file?: File) => Promise; + /** 重试最近一次失败发送。 */ + retrySend: () => Promise; + /** 是否存在可重试的发送动作。 */ + retryAvailable: boolean; + /** 列出指定账号的未读总数。 */ + unreadForAccount: (accountID: string) => number; + /** 表情资源导出,保持页面兼容入口。 */ + emojiURL: typeof emojiURL; + /** 闲鱼表情列表导出,保持页面兼容入口。 */ + xianyuEmojis: typeof xianyuEmojis; + /** 闲鱼文本渲染器导出,保持页面兼容入口。 */ + renderXianyuText: typeof renderXianyuText; + /** 时间格式化函数导出,保持页面兼容入口。 */ + formatClock: typeof formatClock; + /** 消息时间格式化函数导出,保持页面兼容入口。 */ + messageTime: typeof messageTime; +}; + +/** 统一管理聊天账号、会话、消息分页、实时连接和发送重试状态。 */ +export const useChat = (): UseChatResult => { + // accounts 保存启用账号及其运行状态。 + const [accounts, setAccounts] = useState([]); + // activeAccountID 保存当前选中的账号。 + const [activeAccountID, setActiveAccountID] = useState(''); + // sessionsByAccount 按账号隔离会话列表。 + const [sessionsByAccount, setSessionsByAccount] = useState({}); + // activeChatID 保存当前选中的会话。 + const [activeChatID, setActiveChatID] = useState(''); + // messages 保存当前会话消息。 + const [messages, setMessages] = useState([]); + // search 保存会话搜索文本。 + const [search, setSearch] = useState(''); + // unreadOnly 控制是否仅展示未读会话。 + const [unreadOnly, setUnreadOnly] = useState(false); + // draft 保存待发送文本。 + const [draft, setDraft] = useState(''); + // loading 表示聊天初始数据加载状态。 + const [loading, setLoading] = useState(true); + // messagesLoading 表示当前会话消息加载状态。 + const [messagesLoading, setMessagesLoading] = useState(false); + // olderLoading 表示历史消息分页状态。 + const [olderLoading, setOlderLoading] = useState(false); + // hasOlder 表示当前会话是否还有历史消息。 + const [hasOlder, setHasOlder] = useState(false); + // historyCursor 保存历史消息分页游标。 + const [historyCursor, setHistoryCursor] = useState(); + // contactCursors 保存各账号联系人分页游标。 + const [contactCursors, setContactCursors] = useState>({}); + // hasMoreContacts 保存各账号是否还有联系人。 + const [hasMoreContacts, setHasMoreContacts] = useState>({}); + // contactsLoading 表示联系人分页状态。 + const [contactsLoading, setContactsLoading] = useState(false); + // emojiOpen 控制表情选择器显示。 + const [emojiOpen, setEmojiOpen] = useState(false); + // sending 表示当前是否正在发送消息。 + const [sending, setSending] = useState(false); + // error 保存聊天页面最近错误。 + const [error, setError] = useState(''); + // liveState 保存 WebSocket 连接状态。 + const [liveState, setLiveState] = useState('connecting'); + // retryText 保存最近失败的文本消息。 + const [retryText, setRetryText] = useState(null); + // retryImage 保存最近失败的图片消息。 + const [retryImage, setRetryImage] = useState(null); + // activeAccountRef 供实时回调读取最新账号。 + const activeAccountRef = useRef(''); + // activeChatRef 供实时回调读取最新会话。 + const activeChatRef = useRef(''); + // scrollRef 指向消息滚动容器。 + const scrollRef = useRef(null); + // scrollContextRef 保存滚动上下文。 + const scrollContextRef = useRef({ accountID: '', chatID: '' }); + // shouldScrollToBottomRef 控制新消息是否自动滚到底部。 + const shouldScrollToBottomRef = useRef(true); + // skipNextMessageScrollRef 防止加载历史消息后跳到底部。 + const skipNextMessageScrollRef = useRef(false); + // imageInputRef 指向图片文件输入框。 + const imageInputRef = useRef(null); + // refreshedAccountsRef 防止同一账号重复刷新联系人。 + const refreshedAccountsRef = useRef(new Set()); + // sessionSequence 隔离联系人刷新请求。 + const sessionSequence = useRef(0); + // sessionController 保存当前联系人请求控制器。 + const sessionController = useRef(null); + // messageSequence 隔离会话切换产生的旧消息响应。 + const messageSequence = useRef(0); + // messageController 保存当前消息请求控制器。 + const messageController = useRef(null); + // contactSequence 隔离联系人分页产生的旧响应。 + const contactSequence = useRef(0); + // contactController 保存当前联系人分页控制器。 + const contactController = useRef(null); + // sendSequence 隔离账号或会话切换产生的旧发送响应。 + const sendSequence = useRef(0); + // sendController 保存当前消息发送控制器。 + const sendController = useRef(null); + + useEffect(() => { activeAccountRef.current = activeAccountID; }, [activeAccountID]); + useEffect(() => { activeChatRef.current = activeChatID; }, [activeChatID]); + + /** 刷新指定账号的联系人列表,并丢弃过期响应。 */ + const reloadSessions = useCallback(async (accountID: string): Promise => { + const sequence = ++sessionSequence.current; + sessionController.current?.abort(); + const controller = new AbortController(); + sessionController.current = controller; + try { + const page = await getChatSessionPage(accountID, undefined, { signal: controller.signal }, true); + if (!isCurrentChatRequest(sessionSequence.current, sequence, controller.signal)) return []; + setSessionsByAccount(current => ({ ...current, [accountID]: page.sessions })); + setContactCursors(current => ({ ...current, [accountID]: page.next_cursor })); + setHasMoreContacts(current => ({ ...current, [accountID]: page.has_more })); + return page.sessions; + } catch (error) { + if (isCurrentChatRequest(sessionSequence.current, sequence, controller.signal) && !isChatAbortError(error)) setError(error instanceof Error ? error.message : '同步会话失败'); + return []; + } + }, []); + + useEffect(() => { + const controller = new AbortController(); + const load = async (): Promise => { + setLoading(true); + try { + const [details, statuses] = await Promise.all([ + getAccountDetails({ signal: controller.signal }), + getAccountRuntimeStatuses({ signal: controller.signal }), + ]); + const withRuntime = details.map(account => ({ + ...account, + runtime_state: statuses[account.id]?.state || (account.enabled ? 'connecting' : 'disabled'), + runtime_connected: statuses[account.id]?.connected === true, + })); + const enabled = withRuntime.filter(account => account.enabled); + const sessionPages = await Promise.all(enabled.map(async account => [account.id, await getChatSessionPage(account.id, undefined, { signal: controller.signal })] as const)); + if (controller.signal.aborted) return; + setAccounts(enabled); + setSessionsByAccount(Object.fromEntries(sessionPages.map(([id, page]) => [id, page.sessions]))); + setContactCursors(Object.fromEntries(sessionPages.map(([id, page]) => [id, page.next_cursor]))); + setHasMoreContacts(Object.fromEntries(sessionPages.map(([id, page]) => [id, page.has_more]))); + const stored = window.localStorage.getItem('ydisks.chat.account.v1') || ''; + const first = enabled.some(account => account.id === stored) ? stored : enabled[0]?.id || ''; + setActiveAccountID(first); + } catch (loadError) { + if (!controller.signal.aborted) setError(loadError instanceof Error ? loadError.message : '加载聊天数据失败'); + } finally { + if (!controller.signal.aborted) setLoading(false); + } + }; + void load(); + return () => controller.abort(); + }, []); + + useEffect(() => { + let disposed = false; + let timer = 0; + let controller: AbortController | null = null; + const poll = async (): Promise => { + controller = new AbortController(); + try { + const statuses = await getAccountRuntimeStatuses({ signal: controller.signal, timeoutMs: 10_000 }); + if (!disposed) setAccounts(current => current.map(account => ({ + ...account, + runtime_state: statuses[account.id]?.state || account.runtime_state, + runtime_connected: statuses[account.id]?.connected ?? account.runtime_connected, + }))); + } catch { + // WebSocket 拥有独立的可见状态,短暂轮询失败不清除已加载会话。 + } finally { + if (!disposed) timer = window.setTimeout(poll, 3_000); + } + }; + timer = window.setTimeout(poll, 3_000); + return () => { + disposed = true; + window.clearTimeout(timer); + controller?.abort(); + }; + }, []); + + useEffect(() => { + if (!activeAccountID) return; + window.localStorage.setItem('ydisks.chat.account.v1', activeAccountID); + const sessions = sessionsByAccount[activeAccountID] || []; + setActiveChatID(current => sessions.some(session => session.chat_id === current) ? current : sessions[0]?.chat_id || ''); + }, [activeAccountID, sessionsByAccount]); + + useEffect(() => { + if (!activeAccountID || refreshedAccountsRef.current.has(activeAccountID)) return; + refreshedAccountsRef.current.add(activeAccountID); + void reloadSessions(activeAccountID); + }, [activeAccountID, reloadSessions]); + + useEffect(() => { + if (!activeAccountID || !activeChatID) { + setMessages([]); + return; + } + const sequence = ++messageSequence.current; + messageController.current?.abort(); + const controller = new AbortController(); + messageController.current = controller; + setMessagesLoading(true); + void Promise.all([ + getChatMessagePage(activeAccountID, activeChatID, undefined, undefined, { signal: controller.signal }), + markChatRead(activeAccountID, activeChatID, { signal: controller.signal }), + ]).then(([page]) => { + if (!isCurrentChatRequest(messageSequence.current, sequence, controller.signal)) return; + setMessages(page.messages); + setHasOlder(page.has_more); + setHistoryCursor(page.next_cursor); + if (page.session) setSessionsByAccount(current => ({ ...current, [activeAccountID]: (current[activeAccountID] || []).map(session => session.chat_id === page.session?.chat_id ? page.session! : session) })); + setSessionsByAccount(current => ({ ...current, [activeAccountID]: (current[activeAccountID] || []).map(session => session.chat_id === activeChatID ? { ...session, unread_count: 0 } : session) })); + }).catch(loadError => { + if (isCurrentChatRequest(messageSequence.current, sequence, controller.signal) && !isChatAbortError(loadError)) setError(loadError instanceof Error ? loadError.message : '加载消息失败'); + }).finally(() => { + if (isCurrentChatRequest(messageSequence.current, sequence, controller.signal)) setMessagesLoading(false); + }); + return () => controller.abort(); + }, [activeAccountID, activeChatID]); + + useEffect(() => { + contactController.current?.abort(); + contactSequence.current += 1; + }, [activeAccountID]); + + useEffect(() => { + sendController.current?.abort(); + sendSequence.current += 1; + }, [activeAccountID, activeChatID]); + + useEffect(() => () => { + sessionController.current?.abort(); + messageController.current?.abort(); + contactController.current?.abort(); + sendController.current?.abort(); + }, []); + + /** 加载当前会话更早消息并保持滚动位置。 */ + const loadOlderMessages = useCallback(async (): Promise => { + if (!activeAccountID || !activeChatID || olderLoading || !hasOlder) return; + const container = scrollRef.current; + const previousHeight = container?.scrollHeight || 0; + const sequence = messageSequence.current; + const controller = new AbortController(); + skipNextMessageScrollRef.current = true; + setOlderLoading(true); + setError(''); + try { + const oldestID = messages[0]?.id; + const page = await getChatMessagePage(activeAccountID, activeChatID, historyCursor, oldestID, { signal: controller.signal }); + if (!isCurrentChatRequest(messageSequence.current, sequence, controller.signal)) return; + setMessages(current => mergeOlderMessages(current, page.messages)); + setHasOlder(page.has_more); + setHistoryCursor(page.next_cursor); + requestAnimationFrame(() => { + if (container) container.scrollTop += container.scrollHeight - previousHeight; + }); + } catch (loadError) { + skipNextMessageScrollRef.current = false; + if (!isChatAbortError(loadError)) setError(loadError instanceof Error ? loadError.message : '加载历史消息失败'); + } finally { + setOlderLoading(false); + } + }, [activeAccountID, activeChatID, hasOlder, historyCursor, messages, olderLoading]); + + useEffect(() => { + let disposed = false; + let reconnectTimer = 0; + let retry = 0; + let socket: WebSocket | null = null; + const connect = (): void => { + if (disposed) return; + setLiveState('connecting'); + const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; + socket = new WebSocket(`${protocol}//${window.location.host}/api/v1/chat/ws`); + socket.onopen = () => { retry = 0; setLiveState('online'); }; + socket.onmessage = event => { + try { + const payload = JSON.parse(event.data); + const message = payload.message as ChatMessage | undefined; + if (!message) return; + const accountID = message.account_id; + setSessionsByAccount(current => { + const rows = current[accountID] || []; + const found = rows.some(row => row.chat_id === message.chat_id); + if (!found) { + void reloadSessions(accountID); + return current; + } + return { ...current, [accountID]: rows.map(row => row.chat_id === message.chat_id ? { + ...row, + last_message: message.content, + last_message_at: message.sent_at, + unread_count: message.direction === 'incoming' && (activeAccountRef.current !== accountID || activeChatRef.current !== message.chat_id) ? row.unread_count + 1 : row.unread_count, + } : row).sort((a, b) => b.last_message_at - a.last_message_at) }; + }); + if (activeAccountRef.current === accountID && activeChatRef.current === message.chat_id) { + setMessages(current => mergeLiveMessage(current, message)); + if (message.direction === 'incoming') void markChatRead(accountID, message.chat_id); + } + } catch { + // 忽略非聊天格式的 WebSocket 帧,后续 REST 查询会恢复权威状态。 + } + }; + socket.onclose = () => { + if (disposed) return; + setLiveState('offline'); + const delay = Math.min(15_000, 1_000 * 2 ** Math.min(retry++, 4)); + reconnectTimer = window.setTimeout(connect, delay); + }; + socket.onerror = () => socket?.close(); + }; + connect(); + return () => { + disposed = true; + window.clearTimeout(reconnectTimer); + socket?.close(); + }; + }, [reloadSessions]); + + /** 根据滚动位置决定新消息是否自动滚到底部。 */ + const handleMessageScroll = useCallback(() => { + const container = scrollRef.current; + if (!container) return; + const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight; + shouldScrollToBottomRef.current = distanceFromBottom <= 48; + }, []); + + useLayoutEffect(() => { + const contextChanged = scrollContextRef.current.accountID !== activeAccountID || scrollContextRef.current.chatID !== activeChatID; + scrollContextRef.current = { accountID: activeAccountID, chatID: activeChatID }; + if (contextChanged) shouldScrollToBottomRef.current = true; + const container = scrollRef.current; + if (!container) return; + if (skipNextMessageScrollRef.current) { + skipNextMessageScrollRef.current = false; + return; + } + if (messagesLoading || shouldScrollToBottomRef.current) container.scrollTop = container.scrollHeight; + }, [activeAccountID, activeChatID, messages, messagesLoading]); + + const activeAccount = accounts.find(account => account.id === activeAccountID); + const activeSessions = sessionsByAccount[activeAccountID] || []; + const selectedSession = activeSessions.find(session => session.chat_id === activeChatID); + const filteredSessions = useMemo(() => filterChatSessions(activeSessions, search, unreadOnly), [activeSessions, search, unreadOnly]); + const unreadForAccount = useCallback((accountID: string) => (sessionsByAccount[accountID] || []).reduce((sum, session) => sum + session.unread_count, 0), [sessionsByAccount]); + + /** 加载当前账号下一页联系人。 */ + const loadMoreContacts = useCallback(async (): Promise => { + if (!activeAccountID || contactsLoading || !hasMoreContacts[activeAccountID]) return; + const sequence = ++contactSequence.current; + contactController.current?.abort(); + const controller = new AbortController(); + contactController.current = controller; + const accountID = activeAccountID; + setContactsLoading(true); + setError(''); + try { + const page = await getChatSessionPage(accountID, contactCursors[accountID], { signal: controller.signal }, true); + if (!isCurrentChatRequest(contactSequence.current, sequence, controller.signal)) return; + setSessionsByAccount(current => ({ ...current, [accountID]: page.sessions })); + setContactCursors(current => ({ ...current, [accountID]: page.next_cursor })); + setHasMoreContacts(current => ({ ...current, [accountID]: page.has_more })); + } catch (loadError) { + if (isCurrentChatRequest(contactSequence.current, sequence, controller.signal) && !isChatAbortError(loadError)) setError(loadError instanceof Error ? loadError.message : '加载历史联系人失败'); + } finally { + if (isCurrentChatRequest(contactSequence.current, sequence, controller.signal)) setContactsLoading(false); + } + }, [activeAccountID, contactCursors, contactsLoading, hasMoreContacts]); + + /** 发送文本消息并记录失败重试数据。 */ + const sendText = useCallback(async (text: string, rememberRetry: boolean): Promise => { + if (!selectedSession || !activeAccountID || sending) return; + const sequence = ++sendSequence.current; + sendController.current?.abort(); + const controller = new AbortController(); + sendController.current = controller; + setSending(true); + setError(''); + try { + const result = await sendChatMessage({ account_id: activeAccountID, chat_id: selectedSession.chat_id, buyer_id: selectedSession.buyer_id, buyer_name: selectedSession.buyer_name, item_id: selectedSession.item_id, item_title: selectedSession.item_title, text }, { signal: controller.signal }); + if (!isCurrentChatRequest(sendSequence.current, sequence, controller.signal)) return; + setDraft(''); + setRetryText(null); + setMessages(current => mergeLiveMessage(current, result.message)); + } catch (sendError) { + if (isCurrentChatRequest(sendSequence.current, sequence, controller.signal)) { + if (rememberRetry) setRetryText(text); + if (!isChatAbortError(sendError)) setError(sendError instanceof Error ? sendError.message : '消息发送失败'); + } + } finally { + if (isCurrentChatRequest(sendSequence.current, sequence, controller.signal)) setSending(false); + } + }, [activeAccountID, selectedSession, sending]); + + /** 发送图片消息并记录失败重试数据。 */ + const sendImage = useCallback(async (file: File, rememberRetry: boolean): Promise => { + if (!selectedSession || !activeAccountID || sending) return; + const sequence = ++sendSequence.current; + sendController.current?.abort(); + const controller = new AbortController(); + sendController.current = controller; + setSending(true); + setError(''); + try { + const result = await sendChatImage({ account_id: activeAccountID, chat_id: selectedSession.chat_id, buyer_id: selectedSession.buyer_id, buyer_name: selectedSession.buyer_name, buyer_avatar_url: selectedSession.buyer_avatar_url, item_id: selectedSession.item_id, item_title: selectedSession.item_title, image: file }, { signal: controller.signal }); + if (!isCurrentChatRequest(sendSequence.current, sequence, controller.signal)) return; + setRetryImage(null); + setMessages(current => mergeLiveMessage(current, result.message)); + } catch (sendError) { + if (isCurrentChatRequest(sendSequence.current, sequence, controller.signal)) { + if (rememberRetry) setRetryImage(file); + if (!isChatAbortError(sendError)) setError(sendError instanceof Error ? sendError.message : '图片发送失败'); + } + } finally { + if (isCurrentChatRequest(sendSequence.current, sequence, controller.signal)) { + setSending(false); + if (imageInputRef.current) imageInputRef.current.value = ''; + } + } + }, [activeAccountID, selectedSession, sending]); + + /** 处理文本发送按钮和 Enter 快捷键。 */ + const handleSend = useCallback(async (): Promise => { + const text = draft.trim(); + if (!text || !selectedSession || !activeAccountID || sending) return; + await sendText(text, true); + }, [activeAccountID, draft, selectedSession, sendText, sending]); + + /** 处理图片选择并发送。 */ + const handleImage = useCallback(async (file?: File): Promise => { + if (!file || !selectedSession || !activeAccountID || sending) return; + await sendImage(file, true); + }, [activeAccountID, selectedSession, sendImage, sending]); + + /** 重试最近一次失败的文本或图片发送。 */ + const retrySend = useCallback(async (): Promise => { + if (retryText) await sendText(retryText, false); + else if (retryImage) await sendImage(retryImage, false); + }, [retryImage, retryText, sendImage, sendText]); + + return { + accounts, activeAccountID, activeSessions, selectedSession, activeAccount, messages, search, unreadOnly, draft, loading, messagesLoading, olderLoading, hasOlder, contactsLoading, hasMoreContacts: hasMoreContacts[activeAccountID] === true, emojiOpen, sending, error, liveState, + activeChatID, filteredSessions, scrollRef, imageInputRef, setActiveAccountID, setActiveChatID, setSearch, setUnreadOnly, setDraft, setEmojiOpen, + reloadSessions, loadMoreContacts, loadOlderMessages, handleMessageScroll, handleSend, handleImage, retrySend, retryAvailable: Boolean(retryText || retryImage), unreadForAccount, + emojiURL, xianyuEmojis, renderXianyuText, formatClock, messageTime, + }; +}; diff --git a/frontend/app/features/chat/state.test.ts b/frontend/app/features/chat/state.test.ts new file mode 100644 index 0000000..8578173 --- /dev/null +++ b/frontend/app/features/chat/state.test.ts @@ -0,0 +1,30 @@ +import { expect, test } from 'vitest'; +import type { ChatMessage, ChatSession } from '../../../types'; +import { filterChatSessions, isCurrentChatRequest, mergeLiveMessage, mergeOlderMessages } from './state'; + +// sessionFixture 是覆盖搜索、未读筛选和联系人隔离的最小会话数据。 +const sessionFixture: ChatSession[] = [ + { account_id: 'a1', chat_id: 'c1', buyer_id: 'b1', buyer_name: '张三', item_title: '测试商品', last_message: '你好', last_message_at: 1, unread_count: 2 }, + { account_id: 'a1', chat_id: 'c2', buyer_id: 'b2', buyer_name: '李四', item_title: '另一个商品', last_message: '已发货', last_message_at: 2, unread_count: 0 }, +]; + +// messageFixture 是覆盖消息去重和实时替换的最小消息数据。 +const messageFixture: ChatMessage = { id: 1, account_id: 'a1', chat_id: 'c1', message_key: 'm1', direction: 'incoming', sender_id: 'b1', sender_name: '张三', message_type: 'text', content: '旧消息', status: 'received', sent_at: 1 }; + +test('Chat 会话筛选和历史消息合并保持账号内顺序', + // 会话状态测试验证搜索、未读筛选和历史消息去重语义。 + () => { + expect(filterChatSessions(sessionFixture, '张三', false)).toHaveLength(1); + expect(filterChatSessions(sessionFixture, '', true)).toEqual([sessionFixture[0]]); + expect(mergeOlderMessages([messageFixture], [{ ...messageFixture, id: 0, message_key: 'm0', content: '更早' }])).toHaveLength(2); + }); +test('Chat 实时消息替换同键记录并拒绝过期请求', + // 请求边界测试验证实时回执不会产生重复消息,切换会话后的旧请求不能写入。 + () => { + const controller = new AbortController(); + expect(mergeLiveMessage([messageFixture], { ...messageFixture, content: '新消息' })[0].content).toBe('新消息'); + expect(isCurrentChatRequest(3, 3, controller.signal)).toBe(true); + expect(isCurrentChatRequest(2, 3, controller.signal)).toBe(false); + controller.abort(); + expect(isCurrentChatRequest(3, 3, controller.signal)).toBe(false); + }); diff --git a/frontend/app/features/chat/state.ts b/frontend/app/features/chat/state.ts new file mode 100644 index 0000000..2331507 --- /dev/null +++ b/frontend/app/features/chat/state.ts @@ -0,0 +1,50 @@ +import type { ChatMessage, ChatSession } from '../../../types'; + +/** 按搜索条件筛选会话列表。 */ +export const filterChatSessions = (sessions: ChatSession[], search: string, unreadOnly: boolean): ChatSession[] => { + const keyword = search.trim().toLowerCase(); + return sessions.filter(session => { + if (unreadOnly && session.unread_count <= 0) return false; + if (!keyword) return true; + return [session.buyer_name, session.buyer_id, session.item_title, session.last_message] + .some(value => (value || '').toLowerCase().includes(keyword)); + }); +}; + +/** 合并历史消息并按消息键去重。 */ +export const mergeOlderMessages = (current: ChatMessage[], older: ChatMessage[]): ChatMessage[] => { + const keys = new Set(current.map(message => message.message_key)); + return [...older.filter(message => !keys.has(message.message_key)), ...current]; +}; + +/** 合并实时消息并替换同消息键的临时记录。 */ +export const mergeLiveMessage = (current: ChatMessage[], incoming: ChatMessage): ChatMessage[] => { + const index = current.findIndex(message => message.message_key === incoming.message_key); + if (index < 0) return [...current, incoming]; + return current.map((message, currentIndex) => currentIndex === index ? incoming : message); +}; + +/** 判断 Chat 请求响应是否仍属于当前账号和会话。 */ +export const isCurrentChatRequest = (currentSequence: number, requestSequence: number, signal: AbortSignal): boolean => ( + currentSequence === requestSequence && !signal.aborted +); + +/** 判断错误是否来自请求主动取消。 */ +export const isChatAbortError = (error: unknown): boolean => error instanceof Error && error.message === '请求已取消'; + +/** 将聊天时间戳格式化为列表时间。 */ +export const formatClock = (value: number): string => { + if (!value) return ''; + const date = new Date(value < 10_000_000_000 ? value * 1000 : value); + const today = new Date(); + if (date.toDateString() === today.toDateString()) { + return date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', hour12: false }); + } + return date.toLocaleDateString('zh-CN', { month: '2-digit', day: '2-digit' }); +}; + +/** 将聊天时间戳格式化为消息详情时间。 */ +export const messageTime = (value: number): string => { + const date = new Date(value < 10_000_000_000 ? value * 1000 : value); + return date.toLocaleString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false }); +}; diff --git a/frontend/app/features/chat/types.ts b/frontend/app/features/chat/types.ts new file mode 100644 index 0000000..2969bbb --- /dev/null +++ b/frontend/app/features/chat/types.ts @@ -0,0 +1,49 @@ +import type { AccountDetail, ChatMessage, ChatSession } from '../../../types'; + +/** 按账号保存会话列表。 */ +export type SessionsByAccount = Record; + +/** Chat 实时连接状态。 */ +export type ChatLiveState = 'connecting' | 'online' | 'offline'; + +/** Chat Hook 的状态与交互能力。 */ +export type ChatFeatureState = { + /** 当前启用账号列表。 */ + accounts: AccountDetail[]; + /** 当前选中的账号 ID。 */ + activeAccountID: string; + /** 当前账号的会话列表。 */ + activeSessions: ChatSession[]; + /** 当前选中的会话。 */ + selectedSession?: ChatSession; + /** 当前选中的账号。 */ + activeAccount?: AccountDetail; + /** 当前会话消息。 */ + messages: ChatMessage[]; + /** 会话搜索文本。 */ + search: string; + /** 是否只显示未读会话。 */ + unreadOnly: boolean; + /** 消息输入草稿。 */ + draft: string; + /** 初始数据加载状态。 */ + loading: boolean; + /** 当前会话消息加载状态。 */ + messagesLoading: boolean; + /** 历史消息加载状态。 */ + olderLoading: boolean; + /** 是否还有更早消息。 */ + hasOlder: boolean; + /** 历史联系人加载状态。 */ + contactsLoading: boolean; + /** 当前账号是否还有更多联系人。 */ + hasMoreContacts: boolean; + /** 表情选择器展开状态。 */ + emojiOpen: boolean; + /** 消息发送状态。 */ + sending: boolean; + /** 当前错误信息。 */ + error: string; + /** WebSocket 实时连接状态。 */ + liveState: ChatLiveState; +}; diff --git a/frontend/chatFeature.test.ts b/frontend/chatFeature.test.ts index e6b793f..3da25fe 100644 --- a/frontend/chatFeature.test.ts +++ b/frontend/chatFeature.test.ts @@ -23,18 +23,19 @@ describe('online chat UI contract', () => { }); test('frontend connects only to the application chat websocket', () => { - const chat = source('components/Chat.tsx'); - expect(chat).toContain('/api/v1/chat/ws'); - expect(chat).not.toContain('wss-goofish.dingtalk.com'); + const chatHook = source('app/features/chat/hooks.ts'); + expect(chatHook).toContain('/api/v1/chat/ws'); + expect(chatHook).not.toContain('wss-goofish.dingtalk.com'); }); test('renders peer/self identity and verified media capabilities', () => { const chat = source('components/Chat.tsx'); + const chatHook = source('app/features/chat/hooks.ts'); expect(chat).toContain('selectedSession.buyer_avatar_url'); expect(chat).toContain('activeAccount?.avatar_url'); expect(chat).toContain("message.message_type === 'image'"); expect(chat).toContain("message.message_type === 'video'"); - expect(chat).toContain('sendChatImage'); + expect(chatHook).toContain('sendChatImage'); }); test('renders official notices as neutral system messages', () => { @@ -45,11 +46,12 @@ describe('online chat UI contract', () => { test('keeps the active chat at the bottom when new messages arrive', () => { const chat = source('components/Chat.tsx'); - expect(chat).toContain('shouldScrollToBottomRef'); - expect(chat).toContain('skipNextMessageScrollRef'); + const chatHook = source('app/features/chat/hooks.ts'); + expect(chatHook).toContain('shouldScrollToBottomRef'); + expect(chatHook).toContain('skipNextMessageScrollRef'); expect(chat).toContain('onScroll={handleMessageScroll}'); - expect(chat).toContain('container.scrollHeight - container.scrollTop - container.clientHeight'); - expect(chat).toContain('[activeAccountID, activeChatID, messages, messagesLoading]'); + expect(chatHook).toContain('container.scrollHeight - container.scrollTop - container.clientHeight'); + expect(chatHook).toContain('[activeAccountID, activeChatID, messages, messagesLoading]'); }); test('sidebar exposes collapse control and chat primary navigation', () => { diff --git a/frontend/components/Chat.tsx b/frontend/components/Chat.tsx index 67b20c9..82eae25 100644 --- a/frontend/components/Chat.tsx +++ b/frontend/components/Chat.tsx @@ -1,367 +1,21 @@ -import React, { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'; +import React from 'react'; import { AlertCircle, Check, CheckCheck, ImagePlus, Loader2, MessageCircleMore, RefreshCw, Search, Send, Smile, UserRound, Wifi, WifiOff, } from 'lucide-react'; -import { AccountDetail, ChatMessage, ChatSession } from '../types'; -import { - getAccountDetails, getAccountRuntimeStatuses, getChatMessagePage, getChatMessages, getChatSessionPage, getChatSessions, - markChatRead, sendChatImage, sendChatMessage, -} from '../services/api'; -import { emojiURL, renderXianyuText, xianyuEmojis } from '../chatEmojis'; - -type SessionsByAccount = Record; - -const accountStorageKey = 'ydisks.chat.account.v1'; - -const formatClock = (value: number): string => { - if (!value) return ''; - const date = new Date(value < 10_000_000_000 ? value * 1000 : value); - const today = new Date(); - if (date.toDateString() === today.toDateString()) { - return date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', hour12: false }); - } - return date.toLocaleDateString('zh-CN', { month: '2-digit', day: '2-digit' }); -}; - -const messageTime = (value: number): string => { - const date = new Date(value < 10_000_000_000 ? value * 1000 : value); - return date.toLocaleString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false }); -}; +import { useChat } from '../app/features/chat/hooks'; +// Chat 展示实时会话、消息分页和消息发送界面。 const Chat: React.FC = () => { - const [accounts, setAccounts] = useState([]); - const [activeAccountID, setActiveAccountID] = useState(''); - const [sessionsByAccount, setSessionsByAccount] = useState({}); - const [activeChatID, setActiveChatID] = useState(''); - const [messages, setMessages] = useState([]); - const [search, setSearch] = useState(''); - const [unreadOnly, setUnreadOnly] = useState(false); - const [draft, setDraft] = useState(''); - const [loading, setLoading] = useState(true); - const [messagesLoading, setMessagesLoading] = useState(false); - const [olderLoading, setOlderLoading] = useState(false); - const [hasOlder, setHasOlder] = useState(false); - const [historyCursor, setHistoryCursor] = useState(); - const [contactCursors, setContactCursors] = useState>({}); - const [hasMoreContacts, setHasMoreContacts] = useState>({}); - const [contactsLoading, setContactsLoading] = useState(false); - const [emojiOpen, setEmojiOpen] = useState(false); - const [sending, setSending] = useState(false); - const [error, setError] = useState(''); - const [liveState, setLiveState] = useState<'connecting' | 'online' | 'offline'>('connecting'); - const activeAccountRef = useRef(''); - const activeChatRef = useRef(''); - const scrollRef = useRef(null); - const scrollContextRef = useRef({ accountID: '', chatID: '' }); - const shouldScrollToBottomRef = useRef(true); - const skipNextMessageScrollRef = useRef(false); - const imageInputRef = useRef(null); - const refreshedAccountsRef = useRef(new Set()); - - useEffect(() => { activeAccountRef.current = activeAccountID; }, [activeAccountID]); - useEffect(() => { activeChatRef.current = activeChatID; }, [activeChatID]); - - const reloadSessions = async (accountID: string) => { - const page = await getChatSessionPage(accountID, undefined, undefined, true); - setSessionsByAccount(current => ({ ...current, [accountID]: page.sessions })); - setContactCursors(current => ({ ...current, [accountID]: page.next_cursor })); - setHasMoreContacts(current => ({ ...current, [accountID]: page.has_more })); - return page.sessions; - }; - - useEffect(() => { - let cancelled = false; - const load = async () => { - setLoading(true); - try { - const [details, statuses] = await Promise.all([getAccountDetails(), getAccountRuntimeStatuses()]); - const withRuntime = details.map(account => ({ - ...account, - runtime_state: statuses[account.id]?.state || (account.enabled ? 'connecting' : 'disabled'), - runtime_connected: statuses[account.id]?.connected === true, - })); - const enabled = withRuntime.filter(account => account.enabled); - const sessionPages = await Promise.all(enabled.map(async account => [account.id, await getChatSessionPage(account.id)] as const)); - if (cancelled) return; - setAccounts(enabled); - setSessionsByAccount(Object.fromEntries(sessionPages.map(([id, page]) => [id, page.sessions]))); - setContactCursors(Object.fromEntries(sessionPages.map(([id, page]) => [id, page.next_cursor]))); - setHasMoreContacts(Object.fromEntries(sessionPages.map(([id, page]) => [id, page.has_more]))); - const stored = window.localStorage.getItem(accountStorageKey) || ''; - const first = enabled.some(account => account.id === stored) ? stored : enabled[0]?.id || ''; - setActiveAccountID(first); - } catch (loadError) { - if (!cancelled) setError(loadError instanceof Error ? loadError.message : '加载聊天数据失败'); - } finally { - if (!cancelled) setLoading(false); - } - }; - void load(); - return () => { cancelled = true; }; - }, []); - - useEffect(() => { - let disposed = false; - let timer = 0; - let controller: AbortController | null = null; - const poll = async () => { - controller = new AbortController(); - try { - const statuses = await getAccountRuntimeStatuses({ signal: controller.signal, timeoutMs: 10_000 }); - if (!disposed) { - setAccounts(current => current.map(account => ({ - ...account, - runtime_state: statuses[account.id]?.state || account.runtime_state, - runtime_connected: statuses[account.id]?.connected ?? account.runtime_connected, - }))); - } - } catch { - // The app WebSocket has its own visible state; a transient status poll - // failure does not invalidate the loaded conversations. - } finally { - if (!disposed) timer = window.setTimeout(poll, 3_000); - } - }; - timer = window.setTimeout(poll, 3_000); - return () => { - disposed = true; - window.clearTimeout(timer); - controller?.abort(); - }; - }, []); - - useEffect(() => { - if (!activeAccountID) return; - window.localStorage.setItem(accountStorageKey, activeAccountID); - const sessions = sessionsByAccount[activeAccountID] || []; - setActiveChatID(current => sessions.some(session => session.chat_id === current) ? current : sessions[0]?.chat_id || ''); - }, [activeAccountID, sessionsByAccount]); - - useEffect(() => { - if (!activeAccountID || refreshedAccountsRef.current.has(activeAccountID)) return; - refreshedAccountsRef.current.add(activeAccountID); - void reloadSessions(activeAccountID).catch(loadError => { - setError(loadError instanceof Error ? loadError.message : '同步会话失败'); - }); - }, [activeAccountID]); - - useEffect(() => { - if (!activeAccountID || !activeChatID) { - setMessages([]); - return; - } - const controller = new AbortController(); - setMessagesLoading(true); - void Promise.all([ - getChatMessagePage(activeAccountID, activeChatID, undefined, undefined, { signal: controller.signal }), - markChatRead(activeAccountID, activeChatID), - ]).then(([page]) => { - setMessages(page.messages); - setHasOlder(page.has_more); - setHistoryCursor(page.next_cursor); - if (page.session) { - setSessionsByAccount(current => ({ ...current, [activeAccountID]: (current[activeAccountID] || []).map(session => session.chat_id === page.session?.chat_id ? page.session! : session) })); - } - setSessionsByAccount(current => ({ - ...current, - [activeAccountID]: (current[activeAccountID] || []).map(session => - session.chat_id === activeChatID ? { ...session, unread_count: 0 } : session), - })); - }).catch(loadError => { - if (!controller.signal.aborted) setError(loadError instanceof Error ? loadError.message : '加载消息失败'); - }).finally(() => { - if (!controller.signal.aborted) setMessagesLoading(false); - }); - return () => controller.abort(); - }, [activeAccountID, activeChatID]); - - const loadOlderMessages = async () => { - if (!activeAccountID || !activeChatID || olderLoading || !hasOlder) return; - const container = scrollRef.current; - const previousHeight = container?.scrollHeight || 0; - skipNextMessageScrollRef.current = true; - setOlderLoading(true); - setError(''); - try { - const oldestID = messages[0]?.id; - const page = await getChatMessagePage(activeAccountID, activeChatID, historyCursor, oldestID); - setMessages(current => { - const keys = new Set(current.map(message => message.message_key)); - return [...page.messages.filter(message => !keys.has(message.message_key)), ...current]; - }); - setHasOlder(page.has_more); - setHistoryCursor(page.next_cursor); - requestAnimationFrame(() => { - if (container) container.scrollTop += container.scrollHeight - previousHeight; - }); - } catch (loadError) { - skipNextMessageScrollRef.current = false; - setError(loadError instanceof Error ? loadError.message : '加载历史消息失败'); - } finally { - setOlderLoading(false); - } - }; - - useEffect(() => { - let disposed = false; - let reconnectTimer = 0; - let retry = 0; - let socket: WebSocket | null = null; - const connect = () => { - if (disposed) return; - setLiveState('connecting'); - const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; - socket = new WebSocket(`${protocol}//${window.location.host}/api/v1/chat/ws`); - socket.onopen = () => { retry = 0; setLiveState('online'); }; - socket.onmessage = event => { - try { - const payload = JSON.parse(event.data); - const message = payload.message as ChatMessage | undefined; - if (!message) return; - const accountID = message.account_id; - setSessionsByAccount(current => { - const rows = current[accountID] || []; - const found = rows.some(row => row.chat_id === message.chat_id); - if (!found) { - void reloadSessions(accountID); - return current; - } - return { - ...current, - [accountID]: rows.map(row => row.chat_id === message.chat_id ? { - ...row, - last_message: message.content, - last_message_at: message.sent_at, - unread_count: message.direction === 'incoming' && (activeAccountRef.current !== accountID || activeChatRef.current !== message.chat_id) - ? row.unread_count + 1 : row.unread_count, - } : row).sort((a, b) => b.last_message_at - a.last_message_at), - }; - }); - if (activeAccountRef.current === accountID && activeChatRef.current === message.chat_id) { - setMessages(current => { - const index = current.findIndex(item => item.message_key === message.message_key); - if (index >= 0) return current.map((item, i) => i === index ? message : item); - return [...current, message]; - }); - if (message.direction === 'incoming') void markChatRead(accountID, message.chat_id); - } - } catch { - // Ignore malformed non-chat frames and recover from authoritative REST state. - } - }; - socket.onclose = () => { - if (disposed) return; - setLiveState('offline'); - const delay = Math.min(15_000, 1_000 * 2 ** Math.min(retry++, 4)); - reconnectTimer = window.setTimeout(connect, delay); - }; - socket.onerror = () => socket?.close(); - }; - connect(); - return () => { - disposed = true; - window.clearTimeout(reconnectTimer); - socket?.close(); - }; - }, []); - - const handleMessageScroll = () => { - const container = scrollRef.current; - if (!container) return; - const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight; - shouldScrollToBottomRef.current = distanceFromBottom <= 48; - }; - - useLayoutEffect(() => { - const contextChanged = scrollContextRef.current.accountID !== activeAccountID - || scrollContextRef.current.chatID !== activeChatID; - scrollContextRef.current = { accountID: activeAccountID, chatID: activeChatID }; - if (contextChanged) shouldScrollToBottomRef.current = true; - - const container = scrollRef.current; - if (!container) return; - - if (skipNextMessageScrollRef.current) { - skipNextMessageScrollRef.current = false; - return; - } - if (messagesLoading || shouldScrollToBottomRef.current) container.scrollTop = container.scrollHeight; - }, [activeAccountID, activeChatID, messages, messagesLoading]); - - const activeAccount = accounts.find(account => account.id === activeAccountID); - const activeSessions = sessionsByAccount[activeAccountID] || []; - const selectedSession = activeSessions.find(session => session.chat_id === activeChatID); - const filteredSessions = useMemo(() => { - const keyword = search.trim().toLowerCase(); - return activeSessions.filter(session => { - if (unreadOnly && session.unread_count <= 0) return false; - if (!keyword) return true; - return [session.buyer_name, session.buyer_id, session.item_title, session.last_message] - .some(value => (value || '').toLowerCase().includes(keyword)); - }); - }, [activeSessions, search, unreadOnly]); - - const unreadForAccount = (accountID: string) => - (sessionsByAccount[accountID] || []).reduce((sum, session) => sum + session.unread_count, 0); - - const loadMoreContacts = async () => { - if (!activeAccountID || contactsLoading || !hasMoreContacts[activeAccountID]) return; - setContactsLoading(true); - setError(''); - try { - const page = await getChatSessionPage(activeAccountID, contactCursors[activeAccountID], undefined, true); - setSessionsByAccount(current => ({ ...current, [activeAccountID]: page.sessions })); - setContactCursors(current => ({ ...current, [activeAccountID]: page.next_cursor })); - setHasMoreContacts(current => ({ ...current, [activeAccountID]: page.has_more })); - } catch (loadError) { - setError(loadError instanceof Error ? loadError.message : '加载历史联系人失败'); - } finally { setContactsLoading(false); } - }; - - const handleSend = async () => { - const text = draft.trim(); - if (!text || !selectedSession || !activeAccountID || sending) return; - setSending(true); - setError(''); - try { - const result = await sendChatMessage({ - account_id: activeAccountID, chat_id: selectedSession.chat_id, buyer_id: selectedSession.buyer_id, - buyer_name: selectedSession.buyer_name, item_id: selectedSession.item_id, - item_title: selectedSession.item_title, text, - }); - setDraft(''); - setMessages(current => current.some(item => item.message_key === result.message.message_key) - ? current.map(item => item.message_key === result.message.message_key ? result.message : item) - : [...current, result.message]); - } catch (sendError) { - setError(sendError instanceof Error ? sendError.message : '消息发送失败'); - void getChatMessages(activeAccountID, selectedSession.chat_id).then(setMessages); - } finally { - setSending(false); - } - }; - - const handleImage = async (file?: File) => { - if (!file || !selectedSession || !activeAccountID || sending) return; - setSending(true); - setError(''); - try { - const result = await sendChatImage({ - account_id: activeAccountID, chat_id: selectedSession.chat_id, buyer_id: selectedSession.buyer_id, - buyer_name: selectedSession.buyer_name, buyer_avatar_url: selectedSession.buyer_avatar_url, - item_id: selectedSession.item_id, item_title: selectedSession.item_title, image: file, - }); - setMessages(current => current.some(item => item.message_key === result.message.message_key) - ? current.map(item => item.message_key === result.message.message_key ? result.message : item) - : [...current, result.message]); - } catch (sendError) { - setError(sendError instanceof Error ? sendError.message : '图片发送失败'); - } finally { - setSending(false); - if (imageInputRef.current) imageInputRef.current.value = ''; - } - }; + // chatState 是 Chat feature Hook 提供的状态、引用和交互动作。 + const { + accounts, activeAccountID, activeChatID, activeAccount, selectedSession, filteredSessions, + messages, search, unreadOnly, draft, loading, messagesLoading, olderLoading, hasOlder, contactsLoading, + hasMoreContacts, emojiOpen, sending, error, liveState, scrollRef, imageInputRef, setActiveAccountID, + setActiveChatID, setSearch, setUnreadOnly, setDraft, setEmojiOpen, reloadSessions, loadMoreContacts, + loadOlderMessages, handleMessageScroll, handleSend, handleImage, retrySend, retryAvailable, + unreadForAccount, emojiURL, xianyuEmojis, renderXianyuText, formatClock, messageTime, + } = useChat(); if (loading) return
; @@ -441,7 +95,7 @@ const Chat: React.FC = () => { ))} {filteredSessions.length === 0 &&
当前账号暂无匹配会话
} - {hasMoreContacts[activeAccountID] && !search && !unreadOnly &&
+ {hasMoreContacts && !search && !unreadOnly &&
}
-
{outgoing ? (activeAccount?.nickname || activeAccount?.remark || '我') : (selectedSession.buyer_name || message.sender_name || selectedSession.buyer_id)}
+
{outgoing ? (activeAccount?.nickname || activeAccount?.remark || '我') : (selectedSession.buyer_name || message.sender_name || selectedSession.buyer_id)}
{message.message_type === 'image' ? ( 聊天图片 @@ -512,7 +166,7 @@ const Chat: React.FC = () => { })} }
- {error &&
{error}
} + {error &&
{error}{retryAvailable && }
}
@@ -520,21 +174,19 @@ const Chat: React.FC = () => { {emojiOpen &&
全部表情
- {xianyuEmojis.map(([name,file]) => )} + {xianyuEmojis.map(([name, file]) => )}
}
void handleImage(event.target.files?.[0])} /> - +