@salesforcedevs/docs-components 0.0.30-chat → 0.0.32-chat
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
|
@@ -117,11 +117,18 @@ body.chat-closed .global-header {
|
|
|
117
117
|
border-bottom: 1px solid #e0e0e0;
|
|
118
118
|
display: flex;
|
|
119
119
|
align-items: center;
|
|
120
|
-
|
|
120
|
+
justify-content: space-between;
|
|
121
121
|
flex-shrink: 0;
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
.chat-
|
|
124
|
+
.chat-header-actions {
|
|
125
|
+
display: flex;
|
|
126
|
+
align-items: center;
|
|
127
|
+
gap: 8px;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.chat-close-button,
|
|
131
|
+
.chat-clear-button {
|
|
125
132
|
width: 32px;
|
|
126
133
|
height: 32px;
|
|
127
134
|
border: none;
|
|
@@ -135,12 +142,19 @@ body.chat-closed .global-header {
|
|
|
135
142
|
color: #666666;
|
|
136
143
|
}
|
|
137
144
|
|
|
138
|
-
.chat-close-button:hover
|
|
145
|
+
.chat-close-button:hover,
|
|
146
|
+
.chat-clear-button:hover {
|
|
139
147
|
background-color: #e9ecef;
|
|
140
148
|
color: #333333;
|
|
141
149
|
}
|
|
142
150
|
|
|
143
|
-
.
|
|
151
|
+
.chat-clear-button:hover {
|
|
152
|
+
background-color: #fee;
|
|
153
|
+
color: #dc3545;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
.close-icon,
|
|
157
|
+
.clear-icon {
|
|
144
158
|
width: 18px;
|
|
145
159
|
height: 18px;
|
|
146
160
|
}
|
|
@@ -17,15 +17,27 @@
|
|
|
17
17
|
<div class={chatContainerClass}>
|
|
18
18
|
<div class="chat-header">
|
|
19
19
|
<h3 class="chat-title">{title}</h3>
|
|
20
|
-
<
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
20
|
+
<div class="chat-header-actions">
|
|
21
|
+
<button
|
|
22
|
+
class="chat-clear-button"
|
|
23
|
+
onclick={handleClearClick}
|
|
24
|
+
aria-label="Clear chat messages"
|
|
25
|
+
title="Clear chat messages"
|
|
26
|
+
>
|
|
27
|
+
<svg class="clear-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor">
|
|
28
|
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"/>
|
|
29
|
+
</svg>
|
|
30
|
+
</button>
|
|
31
|
+
<button
|
|
32
|
+
class="chat-close-button"
|
|
33
|
+
onclick={handleCloseClick}
|
|
34
|
+
aria-label="Close chat"
|
|
35
|
+
>
|
|
36
|
+
<svg class="close-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor">
|
|
37
|
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
|
|
38
|
+
</svg>
|
|
39
|
+
</button>
|
|
40
|
+
</div>
|
|
29
41
|
</div>
|
|
30
42
|
|
|
31
43
|
<div class="chat-messages">
|
|
@@ -52,10 +52,22 @@ export default class Chat extends LightningElement {
|
|
|
52
52
|
private _showTimestamp: boolean = false;
|
|
53
53
|
private _isOpen: boolean = false;
|
|
54
54
|
private messageIdCounter: number = 0;
|
|
55
|
+
|
|
56
|
+
// localStorage keys for persisting messages and open state
|
|
57
|
+
private static readonly STORAGE_KEY = 'doc-chat-messages';
|
|
58
|
+
private static readonly OPEN_STATE_KEY = 'doc-chat-should-open';
|
|
55
59
|
|
|
56
60
|
connectedCallback() {
|
|
57
|
-
//
|
|
58
|
-
this.
|
|
61
|
+
// Load existing messages from localStorage
|
|
62
|
+
this.loadMessages();
|
|
63
|
+
|
|
64
|
+
// Add welcome message only if no existing messages
|
|
65
|
+
if (this.messages.length === 0) {
|
|
66
|
+
this.addMessage("Hello! How can I help you today?", "assistant");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Check if chat should be opened after reload
|
|
70
|
+
this.checkAndOpenAfterReload();
|
|
59
71
|
|
|
60
72
|
// Ensure body has proper class state
|
|
61
73
|
this.updateBodyClass();
|
|
@@ -104,6 +116,7 @@ export default class Chat extends LightningElement {
|
|
|
104
116
|
formattedTime: this.formatTimestamp(timestamp)
|
|
105
117
|
};
|
|
106
118
|
this.messages = [...this.messages, message];
|
|
119
|
+
this.saveMessages();
|
|
107
120
|
this.scrollToBottom();
|
|
108
121
|
}
|
|
109
122
|
|
|
@@ -128,6 +141,77 @@ export default class Chat extends LightningElement {
|
|
|
128
141
|
}
|
|
129
142
|
}
|
|
130
143
|
|
|
144
|
+
private saveMessages() {
|
|
145
|
+
try {
|
|
146
|
+
const messagesToSave = this.messages.map(msg => ({
|
|
147
|
+
id: msg.id,
|
|
148
|
+
text: msg.text,
|
|
149
|
+
timestamp: msg.timestamp.toISOString(),
|
|
150
|
+
sender: msg.sender,
|
|
151
|
+
formattedTime: msg.formattedTime
|
|
152
|
+
}));
|
|
153
|
+
localStorage.setItem(Chat.STORAGE_KEY, JSON.stringify(messagesToSave));
|
|
154
|
+
} catch (error) {
|
|
155
|
+
console.warn('Failed to save messages to localStorage:', error);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private loadMessages() {
|
|
160
|
+
try {
|
|
161
|
+
const savedMessages = localStorage.getItem(Chat.STORAGE_KEY);
|
|
162
|
+
if (savedMessages) {
|
|
163
|
+
const parsedMessages = JSON.parse(savedMessages);
|
|
164
|
+
this.messages = parsedMessages.map((msg: any) => ({
|
|
165
|
+
id: msg.id,
|
|
166
|
+
text: msg.text,
|
|
167
|
+
timestamp: new Date(msg.timestamp),
|
|
168
|
+
sender: msg.sender,
|
|
169
|
+
formattedTime: msg.formattedTime
|
|
170
|
+
}));
|
|
171
|
+
|
|
172
|
+
// Update message counter to avoid ID conflicts
|
|
173
|
+
const lastId = this.messages.length > 0 ?
|
|
174
|
+
Math.max(...this.messages.map(m => parseInt(m.id.replace('msg-', '')) || 0)) : 0;
|
|
175
|
+
this.messageIdCounter = lastId + 1;
|
|
176
|
+
}
|
|
177
|
+
} catch (error) {
|
|
178
|
+
console.warn('Failed to load messages from localStorage:', error);
|
|
179
|
+
this.messages = [];
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private clearMessages() {
|
|
184
|
+
try {
|
|
185
|
+
localStorage.removeItem(Chat.STORAGE_KEY);
|
|
186
|
+
this.messages = [];
|
|
187
|
+
this.messageIdCounter = 0;
|
|
188
|
+
// Add welcome message after clearing
|
|
189
|
+
this.addMessage("Hello! How can I help you today?", "assistant");
|
|
190
|
+
} catch (error) {
|
|
191
|
+
console.warn('Failed to clear messages from localStorage:', error);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private checkAndOpenAfterReload() {
|
|
196
|
+
try {
|
|
197
|
+
const shouldOpen = localStorage.getItem(Chat.OPEN_STATE_KEY);
|
|
198
|
+
if (shouldOpen === 'true') {
|
|
199
|
+
// Clear the flag
|
|
200
|
+
localStorage.removeItem(Chat.OPEN_STATE_KEY);
|
|
201
|
+
// Open the chat
|
|
202
|
+
this.isOpen = true;
|
|
203
|
+
this.updateBodyClass();
|
|
204
|
+
|
|
205
|
+
// Dispatch custom event to notify parent components
|
|
206
|
+
this.dispatchEvent(new CustomEvent('chatopened', {
|
|
207
|
+
detail: { opened: true }
|
|
208
|
+
}));
|
|
209
|
+
}
|
|
210
|
+
} catch (error) {
|
|
211
|
+
console.warn('Failed to check open state from localStorage:', error);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
131
215
|
handleInputChange(event: Event) {
|
|
132
216
|
const target = event.target as HTMLInputElement;
|
|
133
217
|
this.currentMessage = target.value;
|
|
@@ -205,24 +289,37 @@ export default class Chat extends LightningElement {
|
|
|
205
289
|
}));
|
|
206
290
|
}
|
|
207
291
|
|
|
208
|
-
|
|
209
|
-
this.
|
|
210
|
-
this.updateBodyClass();
|
|
292
|
+
handleClearClick() {
|
|
293
|
+
this.clearMessages();
|
|
211
294
|
|
|
212
295
|
// Dispatch custom event to notify parent components
|
|
213
|
-
this.dispatchEvent(new CustomEvent('
|
|
214
|
-
detail: {
|
|
296
|
+
this.dispatchEvent(new CustomEvent('chatcleared', {
|
|
297
|
+
detail: { cleared: true }
|
|
215
298
|
}));
|
|
216
299
|
}
|
|
217
300
|
|
|
301
|
+
handleOpenClick() {
|
|
302
|
+
try {
|
|
303
|
+
// Set flag to open chat after reload
|
|
304
|
+
localStorage.setItem(Chat.OPEN_STATE_KEY, 'true');
|
|
305
|
+
} catch (error) {
|
|
306
|
+
console.warn('Failed to set open state in localStorage:', error);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Hard reload the page to clear cache when opening chat
|
|
310
|
+
window.location.reload();
|
|
311
|
+
}
|
|
312
|
+
|
|
218
313
|
openChat() {
|
|
219
|
-
|
|
220
|
-
|
|
314
|
+
try {
|
|
315
|
+
// Set flag to open chat after reload
|
|
316
|
+
localStorage.setItem(Chat.OPEN_STATE_KEY, 'true');
|
|
317
|
+
} catch (error) {
|
|
318
|
+
console.warn('Failed to set open state in localStorage:', error);
|
|
319
|
+
}
|
|
221
320
|
|
|
222
|
-
//
|
|
223
|
-
|
|
224
|
-
detail: { opened: true }
|
|
225
|
-
}));
|
|
321
|
+
// Hard reload the page to clear cache when opening chat
|
|
322
|
+
window.location.reload();
|
|
226
323
|
}
|
|
227
324
|
|
|
228
325
|
closeChat() {
|