Skip to content

PCH: Fix issue with potential duplicate API calls in settings and smart links#3095

Merged
vaurdan merged 2 commits intodevelopfrom
fix/potential-duplicated-requests
Jan 27, 2025
Merged

PCH: Fix issue with potential duplicate API calls in settings and smart links#3095
vaurdan merged 2 commits intodevelopfrom
fix/potential-duplicated-requests

Conversation

@vaurdan
Copy link
Contributor

@vaurdan vaurdan commented Jan 24, 2025

Description

While working on the Traffic Boost feature, I noticed a re-rendering issue that was causing a bunch of duplicated requests in a row to the editor-sidebar settings endpoint. By checking if the data has actually changed, we can only call the endpoint if there are changes in the settings.

I also noticed a double call to the Smart Links /get endpoint. This was due to the initialization useEffect running twice. I added a safeguard ref to track if the component was already initialized, so it only calls the /get endpoint once.

Motivation and context

  • Improve the reliability of PCH and Parse.ly plugin.
  • Save resources and improve performance by avoiding needless API calls.

How has this been tested?

  • Tested locally on two environments: one where the issue was noticeable, and other where the issue wasn't present. With this solution, none of the environments suffer from this problem.

Summary by CodeRabbit

Release Notes

  • Performance Improvements

    • Enhanced settings saving mechanism to prevent unnecessary API calls
    • Optimized Smart Linking component initialization process
  • Code Maintenance

    • Refactored settings hook implementation
    • Removed deprecated Excerpt Suggestions settings endpoint
  • Stability

    • Improved component initialization logic to prevent duplicate operations
    • Streamlined state management for settings and component references

@vaurdan vaurdan added Changelog: Fixed PR to be added under the changelog's "Fixed" section Feature: PCI Ticket/PR related to Content Intelligence labels Jan 24, 2025
@vaurdan vaurdan added this to the 3.18.0 milestone Jan 24, 2025
@vaurdan vaurdan self-assigned this Jan 24, 2025
@vaurdan vaurdan requested a review from a team as a code owner January 24, 2025 16:45
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 24, 2025

📝 Walkthrough

Walkthrough

This pull request introduces modifications to the settings management and initialization processes in the Content Helper. The changes focus on optimizing the useSettings hook, improving the useSaveSettings function's efficiency, and adding an initialization reference to prevent redundant operations in the Smart Linking component. Additionally, the PR removes an API endpoint class related to Excerpt Suggestions settings.

Changes

File Change Summary
src/content-helper/common/settings/provider.tsx - Converted useSettings hook from arrow function to standard function
- Added previousData ref in useSaveSettings
- Implemented optimization to prevent unnecessary API calls by comparing settings data
src/content-helper/editor-sidebar/smart-linking/component.tsx - Added initializationRef to manage component initialization
- Modified useEffect to prevent re-initialization of smart links
src/rest-api/settings/class-endpoint-excerpt-suggestions-settings.php - Completely removed the endpoint settings class for Excerpt Suggestions

Sequence Diagram

sequenceDiagram
    participant Component as Smart Linking Component
    participant InitRef as initializationRef
    participant Effect as useEffect

    Component->>InitRef: Initialize with false
    Component->>Effect: Trigger initialization
    Effect->>InitRef: Check current value
    alt Not yet initialized
        Effect->>Component: Perform initialization
        Effect->>InitRef: Set to true
    else Already initialized
        Effect->>Effect: Return early
    end
Loading

Possibly related PRs

Suggested labels

Type: Maintenance

Suggested reviewers

  • acicovic

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
src/content-helper/common/settings/provider.tsx (2)

91-91: Good use of useRef for tracking previous settings.
Storing the initial data in a ref helps avoid unintended re-renders. Ensure the @since annotation reflects this addition if a doc block is introduced in the future.


97-99: Early return on first render is valid.
This prevents initial unnecessary API calls and ensures data is set properly before proceeding. Doc blocks around this logic might be updated to mention skipping the first render explicitly.

src/content-helper/editor-sidebar/smart-linking/component.tsx (1)

194-194: Initial local ref for initialization state.
initializationRef ensures the logic only runs once. Consider adding an inline comment with a @since tag if this is part of a bigger feature change.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9e0ba09 and e611d7d.

⛔ Files ignored due to path filters (4)
  • build/content-helper/dashboard-widget.asset.php is excluded by !build/**
  • build/content-helper/dashboard-widget.js is excluded by !build/**
  • build/content-helper/editor-sidebar.asset.php is excluded by !build/**
  • build/content-helper/editor-sidebar.js is excluded by !build/**
📒 Files selected for processing (3)
  • src/content-helper/common/settings/provider.tsx (2 hunks)
  • src/content-helper/editor-sidebar/smart-linking/component.tsx (4 hunks)
  • src/rest-api/settings/class-endpoint-excerpt-suggestions-settings.php (0 hunks)
💤 Files with no reviewable changes (1)
  • src/rest-api/settings/class-endpoint-excerpt-suggestions-settings.php
🧰 Additional context used
📓 Path-based instructions (2)
src/content-helper/common/settings/provider.tsx (1)

Pattern **/*.{js,ts,tsx,jsx}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the code to ensure it is well-structured and adheres to best practices.
  • Verify compliance with WordPress coding standards.
  • Ensure the code is well-documented.
  • Check for security vulnerabilities and confirm the code is secure.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate JSDoc comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Confirm every JSDoc comment includes a @SInCE tag indicating the next version of the plugin to include the code.
  • Guarantee compatibility with the latest version of WordPress, avoiding deprecated functions or features."
src/content-helper/editor-sidebar/smart-linking/component.tsx (1)

Pattern **/*.{js,ts,tsx,jsx}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the code to ensure it is well-structured and adheres to best practices.
  • Verify compliance with WordPress coding standards.
  • Ensure the code is well-documented.
  • Check for security vulnerabilities and confirm the code is secure.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate JSDoc comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Confirm every JSDoc comment includes a @SInCE tag indicating the next version of the plugin to include the code.
  • Guarantee compatibility with the latest version of WordPress, avoiding deprecated functions or features."
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: E2E against WordPress latest
🔇 Additional comments (8)
src/content-helper/common/settings/provider.tsx (4)

62-68: Switching to a standard function declaration is fine.
This change is acceptable and consistent with the existing codebase style.


101-102: Efficient comparison for unchanged data.
Skipping the save when data has not changed aligns with the PR goal of reducing duplicate requests.


106-107: Updating previousData after changes is sound.
Properly synchronizing previousData closes the loop on preventing unnecessary updates.


Line range hint 246-248: Dependency array includes relevant state.
Adding permissions.SmartLinking, ready, postId, and initializationRef ensures the effect only re-runs when logically necessary. Monitor for unintended rerenders or missing dependencies.

Also applies to: 252-252

src/content-helper/editor-sidebar/smart-linking/component.tsx (4)

9-9: Ref imports are appropriate.
Bringing in useRef (and other hooks) is consistent with WordPress and React usage.


211-214: Guard clause prevents repeated initialization.
This avoids multiple calls to the same endpoint. The inline comment properly ends with a period.


215-216: Setting initializationRef indicates successful initialization.
This simple flag approach is clear and avoids repeated triggers.


246-248: Added dependencies in effect.
Including initializationRef in the dependency array is crucial. Validate that changes in these dependencies won’t cause unwanted re-initializations.

Also applies to: 252-252

Copy link
Collaborator

@acicovic acicovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@vaurdan vaurdan merged commit 13db281 into develop Jan 27, 2025
40 checks passed
@vaurdan vaurdan deleted the fix/potential-duplicated-requests branch January 27, 2025 10:45
github-actions bot added a commit that referenced this pull request Jan 27, 2025
…-requests PCH: Fix issue with potential duplicate API calls in settings and smart links" (13db281)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: Fixed PR to be added under the changelog's "Fixed" section Feature: PCI Ticket/PR related to Content Intelligence

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants