@wopr-network/platform-ui-core 1.1.5 → 1.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -3,6 +3,8 @@ import {
|
|
|
3
3
|
clearChatHistory,
|
|
4
4
|
getSessionId,
|
|
5
5
|
loadChatHistory,
|
|
6
|
+
MAX_CHAT_HISTORY,
|
|
7
|
+
MAX_MESSAGE_CONTENT_LENGTH,
|
|
6
8
|
saveChatHistory,
|
|
7
9
|
} from "@/lib/chat/chat-store";
|
|
8
10
|
import type { ChatMessage } from "@/lib/chat/types";
|
|
@@ -84,4 +86,94 @@ describe("chat-store", () => {
|
|
|
84
86
|
expect(loadChatHistory()).toEqual([]);
|
|
85
87
|
});
|
|
86
88
|
});
|
|
89
|
+
|
|
90
|
+
describe("saveChatHistory limits", () => {
|
|
91
|
+
it("keeps only the last MAX_CHAT_HISTORY messages", () => {
|
|
92
|
+
const total = MAX_CHAT_HISTORY + 50;
|
|
93
|
+
const messages: ChatMessage[] = Array.from({ length: total }, (_, i) => ({
|
|
94
|
+
id: String(i),
|
|
95
|
+
role: "user" as const,
|
|
96
|
+
content: `msg ${i}`,
|
|
97
|
+
timestamp: 1000 + i,
|
|
98
|
+
}));
|
|
99
|
+
saveChatHistory(messages);
|
|
100
|
+
const loaded = loadChatHistory();
|
|
101
|
+
expect(loaded).toHaveLength(MAX_CHAT_HISTORY);
|
|
102
|
+
// Should keep the LAST MAX_CHAT_HISTORY (indices 50-149)
|
|
103
|
+
expect(loaded[0].id).toBe("50");
|
|
104
|
+
expect(loaded[MAX_CHAT_HISTORY - 1].id).toBe(String(total - 1));
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("truncates message content exceeding MAX_MESSAGE_CONTENT_LENGTH and appends ellipsis", () => {
|
|
108
|
+
const longContent = "x".repeat(MAX_MESSAGE_CONTENT_LENGTH + 1000);
|
|
109
|
+
const messages: ChatMessage[] = [
|
|
110
|
+
{ id: "1", role: "user", content: longContent, timestamp: 1000 },
|
|
111
|
+
];
|
|
112
|
+
saveChatHistory(messages);
|
|
113
|
+
const loaded = loadChatHistory();
|
|
114
|
+
expect(loaded).toHaveLength(1);
|
|
115
|
+
expect(loaded[0].content).toHaveLength(MAX_MESSAGE_CONTENT_LENGTH + 1);
|
|
116
|
+
expect(loaded[0].content).toBe(`${"x".repeat(MAX_MESSAGE_CONTENT_LENGTH)}…`);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("does not mutate the original messages array when saving", () => {
|
|
120
|
+
const longContent = "x".repeat(MAX_MESSAGE_CONTENT_LENGTH + 1000);
|
|
121
|
+
const messages: ChatMessage[] = [
|
|
122
|
+
{ id: "1", role: "user", content: longContent, timestamp: 1000 },
|
|
123
|
+
];
|
|
124
|
+
const originalContent = messages[0].content;
|
|
125
|
+
saveChatHistory(messages);
|
|
126
|
+
// localStorage copy is truncated; in-memory array must be unchanged
|
|
127
|
+
expect(messages[0].content).toBe(originalContent);
|
|
128
|
+
expect(messages[0].content).toHaveLength(MAX_MESSAGE_CONTENT_LENGTH + 1000);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("slices by Unicode codepoints so emoji are not split", () => {
|
|
132
|
+
// Each emoji is 2 UTF-16 code units but 1 codepoint.
|
|
133
|
+
// String.slice() cuts by code units; [...str].slice() cuts by codepoints.
|
|
134
|
+
// A string of MAX_MESSAGE_CONTENT_LENGTH emoji has 2*MAX code units.
|
|
135
|
+
// With .slice() the last emoji would be split; with spread it must not be.
|
|
136
|
+
const emoji = "\u{1F600}"; // 😀 — 2 code units
|
|
137
|
+
const longContent = emoji.repeat(MAX_MESSAGE_CONTENT_LENGTH + 100);
|
|
138
|
+
const messages: ChatMessage[] = [
|
|
139
|
+
{ id: "1", role: "user", content: longContent, timestamp: 1000 },
|
|
140
|
+
];
|
|
141
|
+
saveChatHistory(messages);
|
|
142
|
+
const loaded = loadChatHistory();
|
|
143
|
+
const stored = loaded[0].content;
|
|
144
|
+
// The stored content (minus ellipsis) must decode to valid codepoints only.
|
|
145
|
+
// If the last emoji were split, it would contain a lone surrogate which
|
|
146
|
+
// JSON.parse would either mangle or preserve as an invalid codepoint.
|
|
147
|
+
const withoutEllipsis = stored.slice(0, -1); // remove "…"
|
|
148
|
+
expect([...withoutEllipsis].every((ch) => ch === emoji)).toBe(true);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("does not truncate content at or below the limit", () => {
|
|
152
|
+
const exactContent = "y".repeat(MAX_MESSAGE_CONTENT_LENGTH);
|
|
153
|
+
const messages: ChatMessage[] = [
|
|
154
|
+
{ id: "1", role: "user", content: exactContent, timestamp: 1000 },
|
|
155
|
+
];
|
|
156
|
+
saveChatHistory(messages);
|
|
157
|
+
const loaded = loadChatHistory();
|
|
158
|
+
expect(loaded[0].content).toBe(exactContent);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("applies both message count and content length limits together", () => {
|
|
162
|
+
const total = MAX_CHAT_HISTORY + 10;
|
|
163
|
+
const messages: ChatMessage[] = Array.from({ length: total }, (_, i) => ({
|
|
164
|
+
id: String(i),
|
|
165
|
+
role: "bot" as const,
|
|
166
|
+
content: "z".repeat(MAX_MESSAGE_CONTENT_LENGTH + 1000),
|
|
167
|
+
timestamp: 1000 + i,
|
|
168
|
+
}));
|
|
169
|
+
saveChatHistory(messages);
|
|
170
|
+
const loaded = loadChatHistory();
|
|
171
|
+
expect(loaded).toHaveLength(MAX_CHAT_HISTORY);
|
|
172
|
+
expect(loaded[0].id).toBe("10");
|
|
173
|
+
for (const msg of loaded) {
|
|
174
|
+
expect(msg.content).toHaveLength(MAX_MESSAGE_CONTENT_LENGTH + 1);
|
|
175
|
+
expect(msg.content.endsWith("…")).toBe(true);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
});
|
|
87
179
|
});
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { render, screen, waitFor } from "@testing-library/react";
|
|
2
|
+
import userEvent from "@testing-library/user-event";
|
|
3
|
+
import type React from "react";
|
|
4
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
5
|
+
|
|
6
|
+
// Mock next/navigation
|
|
7
|
+
vi.mock("next/navigation", () => ({
|
|
8
|
+
useRouter: () => ({ push: vi.fn() }),
|
|
9
|
+
}));
|
|
10
|
+
|
|
11
|
+
// Mock framer-motion to avoid animation issues in tests
|
|
12
|
+
vi.mock("framer-motion", () => ({
|
|
13
|
+
motion: {
|
|
14
|
+
div: ({
|
|
15
|
+
children,
|
|
16
|
+
variants: _variants,
|
|
17
|
+
initial: _initial,
|
|
18
|
+
animate: _animate,
|
|
19
|
+
whileHover: _whileHover,
|
|
20
|
+
whileTap: _whileTap,
|
|
21
|
+
transition: _transition,
|
|
22
|
+
style,
|
|
23
|
+
className,
|
|
24
|
+
}: React.PropsWithChildren<{
|
|
25
|
+
variants?: unknown;
|
|
26
|
+
initial?: unknown;
|
|
27
|
+
animate?: unknown;
|
|
28
|
+
whileHover?: unknown;
|
|
29
|
+
whileTap?: unknown;
|
|
30
|
+
transition?: unknown;
|
|
31
|
+
style?: React.CSSProperties;
|
|
32
|
+
className?: string;
|
|
33
|
+
}>) => (
|
|
34
|
+
<div style={style} className={className}>
|
|
35
|
+
{children}
|
|
36
|
+
</div>
|
|
37
|
+
),
|
|
38
|
+
},
|
|
39
|
+
AnimatePresence: ({ children }: React.PropsWithChildren) => <>{children}</>,
|
|
40
|
+
}));
|
|
41
|
+
|
|
42
|
+
// Mock plugin setup chat hook
|
|
43
|
+
vi.mock("@/hooks/use-plugin-setup-chat", () => ({
|
|
44
|
+
usePluginSetupChat: () => ({
|
|
45
|
+
state: {
|
|
46
|
+
isOpen: false,
|
|
47
|
+
pluginName: "",
|
|
48
|
+
messages: [],
|
|
49
|
+
isConnected: false,
|
|
50
|
+
isTyping: false,
|
|
51
|
+
isComplete: false,
|
|
52
|
+
},
|
|
53
|
+
sendMessage: vi.fn(),
|
|
54
|
+
closeSetup: vi.fn(),
|
|
55
|
+
openSetup: vi.fn(),
|
|
56
|
+
}),
|
|
57
|
+
}));
|
|
58
|
+
|
|
59
|
+
// Mock brand config
|
|
60
|
+
vi.mock("@/lib/brand-config", () => ({
|
|
61
|
+
getBrandConfig: () => ({ productName: "TestProduct", brandName: "TestBrand" }),
|
|
62
|
+
}));
|
|
63
|
+
|
|
64
|
+
// Mock plugin-setup component
|
|
65
|
+
vi.mock("@/components/plugin-setup", () => ({
|
|
66
|
+
SetupChatPanel: () => null,
|
|
67
|
+
}));
|
|
68
|
+
|
|
69
|
+
const mockListMarketplacePlugins = vi.fn();
|
|
70
|
+
|
|
71
|
+
vi.mock("@/lib/marketplace-data", () => ({
|
|
72
|
+
listBots: vi.fn().mockResolvedValue([{ id: "bot-1", name: "Test Bot" }]),
|
|
73
|
+
listMarketplacePlugins: (...args: unknown[]) => mockListMarketplacePlugins(...args),
|
|
74
|
+
listInstalledPlugins: vi.fn().mockResolvedValue([]),
|
|
75
|
+
togglePluginEnabled: vi.fn(),
|
|
76
|
+
formatInstallCount: (n: number) => String(n),
|
|
77
|
+
hasHostedOption: () => false,
|
|
78
|
+
}));
|
|
79
|
+
|
|
80
|
+
import PluginsPage from "@/app/plugins/page";
|
|
81
|
+
|
|
82
|
+
describe("plugins catalog error state", () => {
|
|
83
|
+
beforeEach(() => {
|
|
84
|
+
vi.clearAllMocks();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("shows error message with retry when catalog load fails", async () => {
|
|
88
|
+
mockListMarketplacePlugins.mockRejectedValue(new Error("Network error"));
|
|
89
|
+
|
|
90
|
+
render(<PluginsPage />);
|
|
91
|
+
|
|
92
|
+
// Switch to the Catalog tab
|
|
93
|
+
const catalogTab = await screen.findByRole("tab", { name: /catalog/i });
|
|
94
|
+
await userEvent.click(catalogTab);
|
|
95
|
+
|
|
96
|
+
// Should show the error message
|
|
97
|
+
await waitFor(() => {
|
|
98
|
+
expect(screen.getByText(/CATALOG LOAD FAILED/i)).toBeInTheDocument();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// Should show the retry button
|
|
102
|
+
expect(screen.getByRole("button", { name: /retry/i })).toBeInTheDocument();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("retries loading when retry button is clicked", async () => {
|
|
106
|
+
const catalogPlugin = {
|
|
107
|
+
id: "plugin-1",
|
|
108
|
+
name: "Test Plugin",
|
|
109
|
+
description: "A test plugin",
|
|
110
|
+
version: "1.0.0",
|
|
111
|
+
color: "#ff0000",
|
|
112
|
+
tags: [],
|
|
113
|
+
capabilities: [],
|
|
114
|
+
installCount: 100,
|
|
115
|
+
author: "Test",
|
|
116
|
+
category: "integration",
|
|
117
|
+
configSchema: [],
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// First call fails, second succeeds
|
|
121
|
+
mockListMarketplacePlugins
|
|
122
|
+
.mockRejectedValueOnce(new Error("Network error"))
|
|
123
|
+
.mockResolvedValueOnce([catalogPlugin]);
|
|
124
|
+
|
|
125
|
+
render(<PluginsPage />);
|
|
126
|
+
|
|
127
|
+
// Switch to the Catalog tab
|
|
128
|
+
const catalogTab = await screen.findByRole("tab", { name: /catalog/i });
|
|
129
|
+
await userEvent.click(catalogTab);
|
|
130
|
+
|
|
131
|
+
// Wait for error state
|
|
132
|
+
await waitFor(() => {
|
|
133
|
+
expect(screen.getByText(/CATALOG LOAD FAILED/i)).toBeInTheDocument();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// Click retry
|
|
137
|
+
const retryButton = screen.getByRole("button", { name: /retry/i });
|
|
138
|
+
await userEvent.click(retryButton);
|
|
139
|
+
|
|
140
|
+
// After retry, the error should be gone and plugin visible in catalog tab (no tab reset)
|
|
141
|
+
await waitFor(() => {
|
|
142
|
+
expect(screen.getByText("Test Plugin")).toBeInTheDocument();
|
|
143
|
+
});
|
|
144
|
+
expect(screen.queryByText(/CATALOG LOAD FAILED/i)).not.toBeInTheDocument();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("shows error banner and retry button when catalog error occurs", async () => {
|
|
148
|
+
// This test validates the corrected rendering condition:
|
|
149
|
+
// The error banner renders whenever catalogError=true, not only when catalogTotal===0.
|
|
150
|
+
// The previous condition (catalogError && catalogTotal === 0) would hide the error
|
|
151
|
+
// if a prior catalog existed — this test verifies the simpler, correct condition.
|
|
152
|
+
mockListMarketplacePlugins.mockRejectedValue(new Error("Network error"));
|
|
153
|
+
|
|
154
|
+
render(<PluginsPage />);
|
|
155
|
+
|
|
156
|
+
const catalogTab = await screen.findByRole("tab", { name: /catalog/i });
|
|
157
|
+
await userEvent.click(catalogTab);
|
|
158
|
+
|
|
159
|
+
// Error banner shown
|
|
160
|
+
await waitFor(() => {
|
|
161
|
+
expect(screen.getByText(/CATALOG LOAD FAILED/i)).toBeInTheDocument();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// Retry button visible (not hidden by incorrect catalogTotal condition)
|
|
165
|
+
expect(screen.getByRole("button", { name: /retry/i })).toBeInTheDocument();
|
|
166
|
+
|
|
167
|
+
// No empty-state message rendered at the same time
|
|
168
|
+
expect(screen.queryByText(/NO MATCHING PLUGINS FOUND/i)).not.toBeInTheDocument();
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("retry does not reset active tab to Installed", async () => {
|
|
172
|
+
const catalogPlugin = {
|
|
173
|
+
id: "plugin-1",
|
|
174
|
+
name: "Test Plugin",
|
|
175
|
+
description: "A test plugin",
|
|
176
|
+
version: "1.0.0",
|
|
177
|
+
color: "#ff0000",
|
|
178
|
+
tags: [],
|
|
179
|
+
capabilities: [],
|
|
180
|
+
installCount: 100,
|
|
181
|
+
author: "Test",
|
|
182
|
+
category: "integration",
|
|
183
|
+
configSchema: [],
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// First call fails, second succeeds
|
|
187
|
+
mockListMarketplacePlugins
|
|
188
|
+
.mockRejectedValueOnce(new Error("Network error"))
|
|
189
|
+
.mockResolvedValueOnce([catalogPlugin]);
|
|
190
|
+
|
|
191
|
+
render(<PluginsPage />);
|
|
192
|
+
|
|
193
|
+
// Switch to the Catalog tab
|
|
194
|
+
const catalogTab = await screen.findByRole("tab", { name: /catalog/i });
|
|
195
|
+
await userEvent.click(catalogTab);
|
|
196
|
+
|
|
197
|
+
// Wait for error state
|
|
198
|
+
await waitFor(() => {
|
|
199
|
+
expect(screen.getByText(/CATALOG LOAD FAILED/i)).toBeInTheDocument();
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// Click retry
|
|
203
|
+
const retryButton = screen.getByRole("button", { name: /retry/i });
|
|
204
|
+
await userEvent.click(retryButton);
|
|
205
|
+
|
|
206
|
+
// After retry succeeds, the Catalog tab content should be visible
|
|
207
|
+
// WITHOUT having to re-click the tab (no tab reset)
|
|
208
|
+
await waitFor(() => {
|
|
209
|
+
expect(screen.getByText("Test Plugin")).toBeInTheDocument();
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// The active tab should still be Catalog — verify the catalog content is visible
|
|
213
|
+
// without re-clicking the tab (i.e., no tab reset occurred)
|
|
214
|
+
expect(screen.queryByText(/CATALOG LOAD FAILED/i)).not.toBeInTheDocument();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("shows empty state (not error) when catalog loads with zero plugins", async () => {
|
|
218
|
+
mockListMarketplacePlugins.mockResolvedValue([]);
|
|
219
|
+
|
|
220
|
+
render(<PluginsPage />);
|
|
221
|
+
|
|
222
|
+
// Switch to the Catalog tab
|
|
223
|
+
const catalogTab = await screen.findByRole("tab", { name: /catalog/i });
|
|
224
|
+
await userEvent.click(catalogTab);
|
|
225
|
+
|
|
226
|
+
// Should show the normal empty state, not the error state
|
|
227
|
+
await waitFor(() => {
|
|
228
|
+
expect(screen.getByText(/NO MATCHING PLUGINS FOUND/i)).toBeInTheDocument();
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
expect(screen.queryByText(/CATALOG LOAD FAILED/i)).not.toBeInTheDocument();
|
|
232
|
+
});
|
|
233
|
+
});
|
package/src/app/plugins/page.tsx
CHANGED
|
@@ -82,6 +82,8 @@ export default function PluginsPage() {
|
|
|
82
82
|
const [toggling, setToggling] = useState<string | null>(null);
|
|
83
83
|
const togglingRef = useRef<string | null>(null);
|
|
84
84
|
const [toggleError, setToggleError] = useState<string | null>(null);
|
|
85
|
+
const [catalogError, setCatalogError] = useState(false);
|
|
86
|
+
const [catalogLoading, setCatalogLoading] = useState(false);
|
|
85
87
|
const [installedPage, setInstalledPage] = useState(1);
|
|
86
88
|
const [catalogPage, setCatalogPage] = useState(1);
|
|
87
89
|
|
|
@@ -114,15 +116,24 @@ export default function PluginsPage() {
|
|
|
114
116
|
});
|
|
115
117
|
}, []);
|
|
116
118
|
|
|
117
|
-
const loadCatalog = useCallback(async () => {
|
|
118
|
-
|
|
119
|
+
const loadCatalog = useCallback(async (isRetry = false) => {
|
|
120
|
+
setCatalogError(false);
|
|
121
|
+
if (isRetry) {
|
|
122
|
+
setCatalogLoading(true);
|
|
123
|
+
} else {
|
|
124
|
+
setLoading(true);
|
|
125
|
+
}
|
|
119
126
|
try {
|
|
120
127
|
const data = await listMarketplacePlugins();
|
|
121
128
|
setCatalog(data);
|
|
122
129
|
} catch {
|
|
123
|
-
|
|
130
|
+
setCatalogError(true);
|
|
124
131
|
} finally {
|
|
125
|
-
|
|
132
|
+
if (isRetry) {
|
|
133
|
+
setCatalogLoading(false);
|
|
134
|
+
} else {
|
|
135
|
+
setLoading(false);
|
|
136
|
+
}
|
|
126
137
|
}
|
|
127
138
|
}, []);
|
|
128
139
|
|
|
@@ -446,7 +457,43 @@ export default function PluginsPage() {
|
|
|
446
457
|
className="max-w-sm bg-black/50 border-terminal/30 placeholder:text-terminal/30 focus-visible:border-terminal focus-visible:ring-terminal/20"
|
|
447
458
|
/>
|
|
448
459
|
|
|
449
|
-
{
|
|
460
|
+
{catalogLoading ? (
|
|
461
|
+
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
|
|
462
|
+
{Array.from({ length: 6 }, (_, n) => `csk-${n}`).map((skId) => (
|
|
463
|
+
<Card key={skId}>
|
|
464
|
+
<CardHeader>
|
|
465
|
+
<div className="flex items-start gap-3">
|
|
466
|
+
<Skeleton className="h-10 w-10 rounded-lg" />
|
|
467
|
+
<div className="flex-1 space-y-2">
|
|
468
|
+
<Skeleton className="h-5 w-28" />
|
|
469
|
+
<Skeleton className="h-4 w-full" />
|
|
470
|
+
</div>
|
|
471
|
+
</div>
|
|
472
|
+
</CardHeader>
|
|
473
|
+
<CardContent>
|
|
474
|
+
<div className="flex items-center justify-between">
|
|
475
|
+
<Skeleton className="h-3 w-24" />
|
|
476
|
+
<Skeleton className="h-5 w-10 rounded-full" />
|
|
477
|
+
</div>
|
|
478
|
+
</CardContent>
|
|
479
|
+
</Card>
|
|
480
|
+
))}
|
|
481
|
+
</div>
|
|
482
|
+
) : catalogError ? (
|
|
483
|
+
<div className="flex h-40 flex-col items-center justify-center gap-3 rounded-sm border border-dashed border-red-500/25 bg-red-500/5">
|
|
484
|
+
<p className="font-mono text-sm text-red-500">
|
|
485
|
+
> CATALOG LOAD FAILED. CHECK CONNECTION AND RETRY.
|
|
486
|
+
</p>
|
|
487
|
+
<Button
|
|
488
|
+
variant="outline"
|
|
489
|
+
size="sm"
|
|
490
|
+
className="border-red-500/30 text-red-500 hover:bg-red-500/10"
|
|
491
|
+
onClick={() => loadCatalog(true)}
|
|
492
|
+
>
|
|
493
|
+
Retry
|
|
494
|
+
</Button>
|
|
495
|
+
</div>
|
|
496
|
+
) : catalogTotal === 0 ? (
|
|
450
497
|
<div className="flex h-40 items-center justify-center rounded-sm border border-dashed border-terminal/20">
|
|
451
498
|
<p className="font-mono text-sm text-terminal/60">> NO MATCHING PLUGINS FOUND.</p>
|
|
452
499
|
</div>
|
|
@@ -163,7 +163,7 @@ export function FieldQR({ field, value: _value, onChange, error, botId }: FieldQ
|
|
|
163
163
|
|
|
164
164
|
{/* bg-white is intentional -- QR codes require white background for scanability */}
|
|
165
165
|
<div className="h-40 w-40 rounded-sm bg-white p-3 min-[375px]:h-48 min-[375px]:w-48">
|
|
166
|
-
{/* biome-ignore lint/performance/noImgElement: QR
|
|
166
|
+
{/* biome-ignore lint/performance/noImgElement: QR PNG is a base64 data URI from the API — next/image does not support data: URIs (cannot optimize, resize, or lazy-load inline blobs). Raw <img> is the only option here. */}
|
|
167
167
|
<img
|
|
168
168
|
src={qrPng}
|
|
169
169
|
alt="Scan this QR code with your phone"
|
|
@@ -205,7 +205,7 @@ export function FieldQR({ field, value: _value, onChange, error, botId }: FieldQ
|
|
|
205
205
|
{/* bg-white is intentional -- QR codes require white background for scanability */}
|
|
206
206
|
<div className="relative h-40 w-40 rounded-sm bg-white p-3 min-[375px]:h-48 min-[375px]:w-48">
|
|
207
207
|
{qrPng && (
|
|
208
|
-
// biome-ignore lint/performance/noImgElement:
|
|
208
|
+
// biome-ignore lint/performance/noImgElement: base64 data URI — see comment above for rationale
|
|
209
209
|
<img src={qrPng} alt="Expired QR code" className="h-full w-full opacity-40" />
|
|
210
210
|
)}
|
|
211
211
|
{/* Dark overlay */}
|
|
@@ -5,6 +5,12 @@ import type { ChatMessage } from "./types";
|
|
|
5
5
|
const HISTORY_KEY = storageKey("chat-history");
|
|
6
6
|
const SESSION_KEY = storageKey("chat-session");
|
|
7
7
|
|
|
8
|
+
/** Maximum number of messages persisted to localStorage. */
|
|
9
|
+
export const MAX_CHAT_HISTORY = 100;
|
|
10
|
+
|
|
11
|
+
/** Maximum character length for a single message's content field. */
|
|
12
|
+
export const MAX_MESSAGE_CONTENT_LENGTH = 4_000;
|
|
13
|
+
|
|
8
14
|
const ChatMessageSchema = z.object({
|
|
9
15
|
id: z.string(),
|
|
10
16
|
role: z.enum(["user", "bot", "event"]),
|
|
@@ -46,9 +52,15 @@ export function loadChatHistory(): ChatMessage[] {
|
|
|
46
52
|
export function saveChatHistory(messages: ChatMessage[]): void {
|
|
47
53
|
if (typeof window === "undefined") return;
|
|
48
54
|
try {
|
|
49
|
-
|
|
55
|
+
const trimmed = messages.slice(-MAX_CHAT_HISTORY).map((msg) => {
|
|
56
|
+
const codepoints = [...msg.content];
|
|
57
|
+
return codepoints.length > MAX_MESSAGE_CONTENT_LENGTH
|
|
58
|
+
? { ...msg, content: `${codepoints.slice(0, MAX_MESSAGE_CONTENT_LENGTH).join("")}…` }
|
|
59
|
+
: msg;
|
|
60
|
+
});
|
|
61
|
+
localStorage.setItem(HISTORY_KEY, JSON.stringify(trimmed));
|
|
50
62
|
} catch {
|
|
51
|
-
// ignore
|
|
63
|
+
// ignore — quota exceeded or private browsing
|
|
52
64
|
}
|
|
53
65
|
}
|
|
54
66
|
|