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

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-alpha7",
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 = {
@@ -234,59 +234,93 @@ export default class CommentPopup extends LightningElement {
234
234
  JSON.stringify(payload, null, 2)
235
235
  );
236
236
 
237
- // LOCAL STORAGE IMPLEMENTATION (Temporary until backend is ready)
237
+ // Try API first, fallback to localStorage
238
238
  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
- })
239
+ const response = await fetch(
240
+ "https://cx-helper-engine-1-114ef038858c.herokuapp.com/post-comment",
241
+ {
242
+ method: "POST",
243
+ headers: {
244
+ "Content-Type": "application/json"
245
+ },
246
+ body: JSON.stringify(payload)
247
+ }
253
248
  );
254
- } catch (error) {
255
- console.error("Error saving comment to local storage:", error);
256
- throw error;
257
- }
258
249
 
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
- }
250
+ if (response.ok) {
251
+ const responseData = await response.json();
252
+ console.log(
253
+ "Comment posted successfully via API:",
254
+ responseData
255
+ );
273
256
 
274
- const responseData = await response.json();
275
- console.log('Comment posted successfully:', responseData);
257
+ // Refresh comments after successful post
258
+ await this.fetchComments();
259
+
260
+ // Dispatch custom event
261
+ this.dispatchEvent(
262
+ new CustomEvent("commentadded", {
263
+ detail: {
264
+ ...payload.comment,
265
+ id: responseData.id // Include any ID returned by the API
266
+ }
267
+ })
268
+ );
269
+ } else {
270
+ console.warn(
271
+ `API call failed (${response.status}): ${response.statusText}, falling back to localStorage`
272
+ );
273
+ // Fallback to localStorage if API fails
274
+ await this.saveCommentToLocalStorage(payload);
275
+ console.log(
276
+ "Comment saved to local storage successfully (fallback)"
277
+ );
276
278
 
277
- // Refresh comments after successful post
278
- await this.fetchComments();
279
+ // Refresh comments after successful save
280
+ await this.fetchComments();
281
+
282
+ // Dispatch custom event
283
+ this.dispatchEvent(
284
+ new CustomEvent("commentadded", {
285
+ detail: {
286
+ ...payload.comment,
287
+ id: Date.now().toString() // Generate temporary ID
288
+ }
289
+ })
290
+ );
291
+ }
292
+ } catch (error) {
293
+ console.error(
294
+ "Error posting comment via API, falling back to localStorage:",
295
+ error
296
+ );
297
+ // Fallback to localStorage if API call throws an error
298
+ try {
299
+ await this.saveCommentToLocalStorage(payload);
300
+ console.log(
301
+ "Comment saved to local storage successfully (fallback)"
302
+ );
279
303
 
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
- */
304
+ // Refresh comments after successful save
305
+ await this.fetchComments();
306
+
307
+ // Dispatch custom event
308
+ this.dispatchEvent(
309
+ new CustomEvent("commentadded", {
310
+ detail: {
311
+ ...payload.comment,
312
+ id: Date.now().toString() // Generate temporary ID
313
+ }
314
+ })
315
+ );
316
+ } catch (localStorageError) {
317
+ console.error(
318
+ "Error saving comment to local storage:",
319
+ localStorageError
320
+ );
321
+ throw localStorageError;
322
+ }
323
+ }
290
324
  }
291
325
 
292
326
  private async saveCommentToLocalStorage(payload: ApiCommentPayload) {
@@ -376,36 +410,58 @@ export default class CommentPopup extends LightningElement {
376
410
  this.apiError = "";
377
411
 
378
412
  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
- /*
413
+ // API IMPLEMENTATION - Try to fetch from /get-comments endpoint
386
414
  const params = new URLSearchParams({
387
415
  branch: this._currentBranch
388
416
  });
389
417
 
390
- console.log('Fetching comments with params:', params.toString());
418
+ console.log("Fetching comments with params:", params.toString());
391
419
 
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
- }
420
+ const response = await fetch(
421
+ `https://cx-helper-engine-1-114ef038858c.herokuapp.com/get-comments?${params.toString()}`
422
+ );
423
+
424
+ if (response.ok) {
425
+ const data: ApiCommentResponse = await response.json();
426
+ console.log("Fetched comments from API:", data);
397
427
 
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
- */
428
+ // Find comments for this specific file path and heading title
429
+ const comments = this.extractCommentsFromApiResponse(data);
430
+ this.comments = comments;
431
+ } else {
432
+ console.warn(
433
+ `API call failed (${response.status}): ${response.statusText}, falling back to localStorage`
434
+ );
435
+ // Fallback to localStorage if API fails
436
+ const comments = await this.getCommentsFromLocalStorage();
437
+ console.log(
438
+ "Fetched comments from localStorage (fallback):",
439
+ comments
440
+ );
441
+ this.comments = comments;
442
+ }
405
443
  } catch (error) {
406
- console.error("Error fetching comments:", error);
407
- this.apiError = "Failed to load comments. Please try again later.";
408
- this.comments = [];
444
+ console.error(
445
+ "Error fetching comments from API, falling back to localStorage:",
446
+ error
447
+ );
448
+ // Fallback to localStorage if API call throws an error
449
+ try {
450
+ const comments = await this.getCommentsFromLocalStorage();
451
+ console.log(
452
+ "Fetched comments from localStorage (fallback):",
453
+ comments
454
+ );
455
+ this.comments = comments;
456
+ } catch (localStorageError) {
457
+ console.error(
458
+ "Error fetching comments from localStorage:",
459
+ localStorageError
460
+ );
461
+ this.apiError =
462
+ "Failed to load comments. Please try again later.";
463
+ this.comments = [];
464
+ }
409
465
  } finally {
410
466
  this.isLoading = false;
411
467
  }