@salesforcedevs/docs-components 1.17.0-hack-alpha6 → 1.17.0-hack-alpha8

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": "1.17.0-hack-alpha6",
3
+ "version": "1.17.0-hack-alpha8",
4
4
  "description": "Docs Lightning web components for DSC",
5
5
  "license": "MIT",
6
6
  "main": "index.js",
@@ -4,6 +4,8 @@ A Lightning Web Component that allows users to leave comments on documentation s
4
4
 
5
5
  ## Features
6
6
 
7
+ - ✅ **API Integration**: Now actively tries to fetch from `/get-comments` endpoint
8
+ - ✅ **Fallback Support**: Falls back to localStorage if API is unavailable
7
9
  - ✅ **New API Format**: Supports branch-based comment fetching
8
10
  - ✅ **Pre-loaded Comments**: Comments are fetched when component is rendered, not when popup opens
9
11
  - ✅ **Local Storage**: Comments persist across page refreshes during development
@@ -188,29 +190,55 @@ The component automatically creates a development UI when running on localhost.
188
190
  - Clearing all comments
189
191
  - Viewing statistics
190
192
 
191
- ## Migration to Backend API
193
+ ## API Integration Status
192
194
 
193
- When the backend API is ready, follow these steps to migrate from localStorage:
195
+ The component now actively tries to use the backend API with localStorage as a fallback:
194
196
 
195
- 1. **Uncomment API calls**: In `commentPopup.ts`, uncomment the API implementation sections
196
- 2. **Comment localStorage calls**: Comment out the localStorage implementation sections
197
- 3. **Update API endpoints**: Ensure the API endpoints match your backend
198
- 4. **Test API integration**: Use the development tools to test API calls
197
+ ### Current Behavior
199
198
 
200
- ### Example Migration
199
+ 1. **Fetch Comments**: First tries `/get-comments?branch={branch}`, falls back to localStorage if API fails
200
+ 2. **Post Comments**: First tries `/post-comment`, falls back to localStorage if API fails
201
+ 3. **Error Handling**: Graceful degradation with console warnings for debugging
202
+ 4. **Backward Compatibility**: localStorage continues to work for development and offline scenarios
203
+
204
+ ### API Endpoints
205
+
206
+ - **GET** `/get-comments?branch={branch}` - Fetch all comments for a branch
207
+ - **POST** `/post-comment` - Add a new comment
208
+
209
+ ### Fallback Strategy
201
210
 
202
211
  ```typescript
203
- // Before (localStorage)
204
- await this.saveCommentToLocalStorage(payload);
205
-
206
- // After (API)
207
- const response = await fetch("/post-comment", {
208
- method: "POST",
209
- headers: {
210
- "Content-Type": "application/json"
211
- },
212
- body: JSON.stringify(payload)
213
- });
212
+ // Fetch comments with fallback
213
+ try {
214
+ const response = await fetch(`/get-comments?${params.toString()}`);
215
+ if (response.ok) {
216
+ // Use API response
217
+ const data = await response.json();
218
+ this.comments = this.extractCommentsFromApiResponse(data);
219
+ } else {
220
+ // Fallback to localStorage
221
+ this.comments = await this.getCommentsFromLocalStorage();
222
+ }
223
+ } catch (error) {
224
+ // Fallback to localStorage on network error
225
+ this.comments = await this.getCommentsFromLocalStorage();
226
+ }
227
+ ```
228
+
229
+ ### Testing API Integration
230
+
231
+ Use the development tools to test API integration:
232
+
233
+ ```javascript
234
+ // Test API response format
235
+ CommentDevHelper.testApiResponseFormat();
236
+
237
+ // Simulate API call for a specific branch
238
+ CommentDevHelper.simulateApiCall("main");
239
+
240
+ // Check if API is available
241
+ CommentDevHelper.checkApiAvailability();
214
242
  ```
215
243
 
216
244
  ## Testing
@@ -27,7 +27,8 @@ if (typeof window !== "undefined") {
27
27
  addComment,
28
28
  getCommentsForBranch,
29
29
  fetchCommentsForBranch,
30
- extractCommentsFromApiResponse
30
+ extractCommentsFromApiResponse,
31
+ checkApiAvailability
31
32
  };
32
33
  }
33
34
 
@@ -301,6 +302,64 @@ export async function simulateApiCall(
301
302
  }
302
303
  }
303
304
 
305
+ /**
306
+ * Check if the API endpoints are available
307
+ */
308
+ export async function checkApiAvailability(): Promise<void> {
309
+ console.log("=== Checking API Availability ===");
310
+
311
+ try {
312
+ // Test GET endpoint
313
+ const getResponse = await fetch("/get-comments?branch=test");
314
+ console.log(
315
+ `GET /get-comments status: ${getResponse.status} ${getResponse.statusText}`
316
+ );
317
+
318
+ if (getResponse.ok) {
319
+ console.log("✅ GET /get-comments is available");
320
+ } else {
321
+ console.log("❌ GET /get-comments is not available");
322
+ }
323
+
324
+ // Test POST endpoint (with a test payload)
325
+ const testPayload = {
326
+ branch: "test",
327
+ file_path: "test.md",
328
+ heading_title: "Test",
329
+ start_line: "1",
330
+ end_line: "10",
331
+ comment: {
332
+ comment_text: "Test comment",
333
+ email: "test@example.com",
334
+ timestamp: new Date().toISOString()
335
+ }
336
+ };
337
+
338
+ const postResponse = await fetch("/post-comment", {
339
+ method: "POST",
340
+ headers: {
341
+ "Content-Type": "application/json"
342
+ },
343
+ body: JSON.stringify(testPayload)
344
+ });
345
+
346
+ console.log(
347
+ `POST /post-comment status: ${postResponse.status} ${postResponse.statusText}`
348
+ );
349
+
350
+ if (postResponse.ok) {
351
+ console.log("✅ POST /post-comment is available");
352
+ } else {
353
+ console.log("❌ POST /post-comment is not available");
354
+ }
355
+
356
+ console.log("=== API Availability Check Complete ===");
357
+ } catch (error) {
358
+ console.error("❌ API availability check failed:", error);
359
+ console.log("This is expected if the backend is not running");
360
+ }
361
+ }
362
+
304
363
  // Add the helper functions to the global CommentDevHelper
305
364
  if (typeof window !== "undefined") {
306
365
  (window as any).CommentDevHelper = {
@@ -33,6 +33,7 @@ interface ApiCommentResponse {
33
33
 
34
34
  // Local storage key for comments
35
35
  const COMMENTS_STORAGE_KEY = "dsc_comments";
36
+ const API_URL = "https://cx-helper-engine-2-356c8dd6c7cc.herokuapp.com";
36
37
 
37
38
  export default class CommentPopup extends LightningElement {
38
39
  iconSymbol = "chat";
@@ -234,59 +235,90 @@ export default class CommentPopup extends LightningElement {
234
235
  JSON.stringify(payload, null, 2)
235
236
  );
236
237
 
237
- // LOCAL STORAGE IMPLEMENTATION (Temporary until backend is ready)
238
+ // Try API first, fallback to localStorage
238
239
  try {
239
- await this.saveCommentToLocalStorage(payload);
240
- console.log("Comment saved to local storage successfully");
241
-
242
- // Refresh comments after successful save
243
- await this.fetchComments();
244
-
245
- // Dispatch custom event
246
- this.dispatchEvent(
247
- new CustomEvent("commentadded", {
248
- detail: {
249
- ...payload.comment,
250
- id: Date.now().toString() // Generate temporary ID
251
- }
252
- })
253
- );
254
- } catch (error) {
255
- console.error("Error saving comment to local storage:", error);
256
- throw error;
257
- }
240
+ const response = await fetch(`${API_URL}/post-comment`, {
241
+ method: "POST",
242
+ headers: {
243
+ "Content-Type": "application/json"
244
+ },
245
+ body: JSON.stringify(payload)
246
+ });
258
247
 
259
- // API IMPLEMENTATION (Commented until backend is ready)
260
- /*
261
- const response = await fetch('/post-comment', {
262
- method: 'POST',
263
- headers: {
264
- 'Content-Type': 'application/json'
265
- },
266
- body: JSON.stringify(payload)
267
- });
268
-
269
- if (!response.ok) {
270
- const errorText = await response.text();
271
- throw new Error(`HTTP error! status: ${response.status}, message: ${errorText}`);
272
- }
248
+ if (response.ok) {
249
+ const responseData = await response.json();
250
+ console.log(
251
+ "Comment posted successfully via API:",
252
+ responseData
253
+ );
273
254
 
274
- const responseData = await response.json();
275
- console.log('Comment posted successfully:', responseData);
255
+ // Refresh comments after successful post
256
+ await this.fetchComments();
257
+
258
+ // Dispatch custom event
259
+ this.dispatchEvent(
260
+ new CustomEvent("commentadded", {
261
+ detail: {
262
+ ...payload.comment,
263
+ id: responseData.id // Include any ID returned by the API
264
+ }
265
+ })
266
+ );
267
+ } else {
268
+ console.warn(
269
+ `API call failed (${response.status}): ${response.statusText}, falling back to localStorage`
270
+ );
271
+ // Fallback to localStorage if API fails
272
+ await this.saveCommentToLocalStorage(payload);
273
+ console.log(
274
+ "Comment saved to local storage successfully (fallback)"
275
+ );
276
276
 
277
- // Refresh comments after successful post
278
- await this.fetchComments();
277
+ // Refresh comments after successful save
278
+ await this.fetchComments();
279
+
280
+ // Dispatch custom event
281
+ this.dispatchEvent(
282
+ new CustomEvent("commentadded", {
283
+ detail: {
284
+ ...payload.comment,
285
+ id: Date.now().toString() // Generate temporary ID
286
+ }
287
+ })
288
+ );
289
+ }
290
+ } catch (error) {
291
+ console.error(
292
+ "Error posting comment via API, falling back to localStorage:",
293
+ error
294
+ );
295
+ // Fallback to localStorage if API call throws an error
296
+ try {
297
+ await this.saveCommentToLocalStorage(payload);
298
+ console.log(
299
+ "Comment saved to local storage successfully (fallback)"
300
+ );
279
301
 
280
- // Dispatch custom event
281
- this.dispatchEvent(
282
- new CustomEvent("commentadded", {
283
- detail: {
284
- ...payload.comment,
285
- id: responseData.id // Include any ID returned by the API
286
- }
287
- })
288
- );
289
- */
302
+ // Refresh comments after successful save
303
+ await this.fetchComments();
304
+
305
+ // Dispatch custom event
306
+ this.dispatchEvent(
307
+ new CustomEvent("commentadded", {
308
+ detail: {
309
+ ...payload.comment,
310
+ id: Date.now().toString() // Generate temporary ID
311
+ }
312
+ })
313
+ );
314
+ } catch (localStorageError) {
315
+ console.error(
316
+ "Error saving comment to local storage:",
317
+ localStorageError
318
+ );
319
+ throw localStorageError;
320
+ }
321
+ }
290
322
  }
291
323
 
292
324
  private async saveCommentToLocalStorage(payload: ApiCommentPayload) {
@@ -376,36 +408,58 @@ export default class CommentPopup extends LightningElement {
376
408
  this.apiError = "";
377
409
 
378
410
  try {
379
- // LOCAL STORAGE IMPLEMENTATION (Temporary until backend is ready)
380
- const comments = await this.getCommentsFromLocalStorage();
381
- console.log("Fetched comments from localStorage:", comments);
382
- this.comments = comments;
383
-
384
- // API IMPLEMENTATION (Commented until backend is ready)
385
- /*
411
+ // API IMPLEMENTATION - Try to fetch from /get-comments endpoint
386
412
  const params = new URLSearchParams({
387
413
  branch: this._currentBranch
388
414
  });
389
415
 
390
- console.log('Fetching comments with params:', params.toString());
416
+ console.log("Fetching comments with params:", params.toString());
391
417
 
392
- const response = await fetch(`/get-comments?${params.toString()}`);
393
-
394
- if (!response.ok) {
395
- throw new Error(`Failed to fetch comments: ${response.status} ${response.statusText}`);
396
- }
418
+ const response = await fetch(
419
+ `${API_URL}/get-comments?${params.toString()}`
420
+ );
421
+
422
+ if (response.ok) {
423
+ const data: ApiCommentResponse = await response.json();
424
+ console.log("Fetched comments from API:", data);
397
425
 
398
- const data: ApiCommentResponse = await response.json();
399
- console.log('Fetched comments from API:', data);
400
-
401
- // Find comments for this specific file path and heading title
402
- const comments = this.extractCommentsFromApiResponse(data);
403
- this.comments = comments;
404
- */
426
+ // Find comments for this specific file path and heading title
427
+ const comments = this.extractCommentsFromApiResponse(data);
428
+ this.comments = comments;
429
+ } else {
430
+ console.warn(
431
+ `API call failed (${response.status}): ${response.statusText}, falling back to localStorage`
432
+ );
433
+ // Fallback to localStorage if API fails
434
+ const comments = await this.getCommentsFromLocalStorage();
435
+ console.log(
436
+ "Fetched comments from localStorage (fallback):",
437
+ comments
438
+ );
439
+ this.comments = comments;
440
+ }
405
441
  } catch (error) {
406
- console.error("Error fetching comments:", error);
407
- this.apiError = "Failed to load comments. Please try again later.";
408
- this.comments = [];
442
+ console.error(
443
+ "Error fetching comments from API, falling back to localStorage:",
444
+ error
445
+ );
446
+ // Fallback to localStorage if API call throws an error
447
+ try {
448
+ const comments = await this.getCommentsFromLocalStorage();
449
+ console.log(
450
+ "Fetched comments from localStorage (fallback):",
451
+ comments
452
+ );
453
+ this.comments = comments;
454
+ } catch (localStorageError) {
455
+ console.error(
456
+ "Error fetching comments from localStorage:",
457
+ localStorageError
458
+ );
459
+ this.apiError =
460
+ "Failed to load comments. Please try again later.";
461
+ this.comments = [];
462
+ }
409
463
  } finally {
410
464
  this.isLoading = false;
411
465
  }