@salesforcedevs/docs-components 0.0.29-chat → 0.0.31-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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforcedevs/docs-components",
3
- "version": "0.0.29-chat",
3
+ "version": "0.0.31-chat",
4
4
  "description": "Docs Lightning web components for DSC",
5
5
  "license": "MIT",
6
6
  "main": "index.js",
@@ -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
- gap: 12px;
120
+ justify-content: space-between;
121
121
  flex-shrink: 0;
122
122
  }
123
123
 
124
- .chat-close-button {
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
- .close-icon {
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
- <button
21
- class="chat-close-button"
22
- onclick={handleCloseClick}
23
- aria-label="Close chat"
24
- >
25
- <svg class="close-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor">
26
- <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
27
- </svg>
28
- </button>
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,18 @@ 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 key for persisting messages
57
+ private static readonly STORAGE_KEY = 'doc-chat-messages';
55
58
 
56
59
  connectedCallback() {
57
- // Add a welcome message
58
- this.addMessage("Hello! How can I help you today?", "assistant");
60
+ // Load existing messages from localStorage
61
+ this.loadMessages();
62
+
63
+ // Add welcome message only if no existing messages
64
+ if (this.messages.length === 0) {
65
+ this.addMessage("Hello! How can I help you today?", "assistant");
66
+ }
59
67
 
60
68
  // Ensure body has proper class state
61
69
  this.updateBodyClass();
@@ -104,6 +112,7 @@ export default class Chat extends LightningElement {
104
112
  formattedTime: this.formatTimestamp(timestamp)
105
113
  };
106
114
  this.messages = [...this.messages, message];
115
+ this.saveMessages();
107
116
  this.scrollToBottom();
108
117
  }
109
118
 
@@ -128,6 +137,57 @@ export default class Chat extends LightningElement {
128
137
  }
129
138
  }
130
139
 
140
+ private saveMessages() {
141
+ try {
142
+ const messagesToSave = this.messages.map(msg => ({
143
+ id: msg.id,
144
+ text: msg.text,
145
+ timestamp: msg.timestamp.toISOString(),
146
+ sender: msg.sender,
147
+ formattedTime: msg.formattedTime
148
+ }));
149
+ localStorage.setItem(Chat.STORAGE_KEY, JSON.stringify(messagesToSave));
150
+ } catch (error) {
151
+ console.warn('Failed to save messages to localStorage:', error);
152
+ }
153
+ }
154
+
155
+ private loadMessages() {
156
+ try {
157
+ const savedMessages = localStorage.getItem(Chat.STORAGE_KEY);
158
+ if (savedMessages) {
159
+ const parsedMessages = JSON.parse(savedMessages);
160
+ this.messages = parsedMessages.map((msg: any) => ({
161
+ id: msg.id,
162
+ text: msg.text,
163
+ timestamp: new Date(msg.timestamp),
164
+ sender: msg.sender,
165
+ formattedTime: msg.formattedTime
166
+ }));
167
+
168
+ // Update message counter to avoid ID conflicts
169
+ const lastId = this.messages.length > 0 ?
170
+ Math.max(...this.messages.map(m => parseInt(m.id.replace('msg-', '')) || 0)) : 0;
171
+ this.messageIdCounter = lastId + 1;
172
+ }
173
+ } catch (error) {
174
+ console.warn('Failed to load messages from localStorage:', error);
175
+ this.messages = [];
176
+ }
177
+ }
178
+
179
+ private clearMessages() {
180
+ try {
181
+ localStorage.removeItem(Chat.STORAGE_KEY);
182
+ this.messages = [];
183
+ this.messageIdCounter = 0;
184
+ // Add welcome message after clearing
185
+ this.addMessage("Hello! How can I help you today?", "assistant");
186
+ } catch (error) {
187
+ console.warn('Failed to clear messages from localStorage:', error);
188
+ }
189
+ }
190
+
131
191
  handleInputChange(event: Event) {
132
192
  const target = event.target as HTMLInputElement;
133
193
  this.currentMessage = target.value;
@@ -205,6 +265,15 @@ export default class Chat extends LightningElement {
205
265
  }));
206
266
  }
207
267
 
268
+ handleClearClick() {
269
+ this.clearMessages();
270
+
271
+ // Dispatch custom event to notify parent components
272
+ this.dispatchEvent(new CustomEvent('chatcleared', {
273
+ detail: { cleared: true }
274
+ }));
275
+ }
276
+
208
277
  handleOpenClick() {
209
278
  this.isOpen = true;
210
279
  this.updateBodyClass();
@@ -65,11 +65,4 @@
65
65
  </div>
66
66
  </div>
67
67
  </div>
68
-
69
- <!-- Fixed positioned chat component -->
70
- <doc-chat
71
- title="Smart Agent"
72
- placeholder="Type your message..."
73
- assistant-name="Assistant"
74
- ></doc-chat>
75
68
  </template>