Skip to content

Updated stats-x to use config for endpoints - #22970

Merged
9larsons merged 2 commits into
mainfrom
stats-x-use-config
Apr 21, 2025
Merged

9larsons merged 2 commits into
mainfrom
stats-x-use-config

Conversation

@9larsons

@9larsons 9larsons commented Apr 21, 2025 •

Copy link
Copy Markdown
Contributor

ref https://linear.app/ghost/issue/PROD-222

  • updated global data use of stats config
  • updated endpoint helper to use config to construct (support tb local; still needs changes for tb forward)

ref https://linear.app/ghost/issue/PROD-222
- updated global data use of stats config
- updated endpoint helper to use config to construct (support tb local; still needs changes for tb forward)
@coderabbitai

coderabbitai Bot commented Apr 21, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

This change refactors the handling and access of stats-related configuration across multiple modules in the application. The StatsConfig type is extracted and exported as a standalone interface, with optional fields for endpoints, tokens, IDs, and a nested local configuration. The function getStatEndpointUrl is updated to accept a StatsConfig object and conditionally construct the endpoint URL based on whether local mode is enabled. A new helper function, getToken, is introduced to select the appropriate token depending on the configuration mode. In the React components using stats data, access to configuration is streamlined: instead of accessing nested properties from a global data object, components now receive a dedicated statsConfig object and use the new helper functions for endpoint and token retrieval. These changes are reflected in the type definitions, context provider, and all affected components, without altering their core logic or exported signatures.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

apps/stats/src/config/stats-config.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-react-hooks".

(The package "eslint-plugin-react-hooks" was not found when loaded as a Node module from the directory "/apps/stats".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-react-hooks@latest --save-dev

The plugin "eslint-plugin-react-hooks" was referenced from the config file in "apps/stats/.eslintrc.cjs".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

apps/stats/src/providers/GlobalDataProvider.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-react-hooks".

(The package "eslint-plugin-react-hooks" was not found when loaded as a Node module from the directory "/apps/stats".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-react-hooks@latest --save-dev

The plugin "eslint-plugin-react-hooks" was referenced from the config file in "apps/stats/.eslintrc.cjs".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

apps/stats/src/views/Stats/components/WebKpis.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-react-hooks".

(The package "eslint-plugin-react-hooks" was not found when loaded as a Node module from the directory "/apps/stats".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-react-hooks@latest --save-dev

The plugin "eslint-plugin-react-hooks" was referenced from the config file in "apps/stats/.eslintrc.cjs".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

  • 3 others

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
  • @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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@9larsons
9larsons enabled auto-merge (squash) April 21, 2025 19:49

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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 (1)
apps/stats/src/providers/GlobalDataProvider.tsx (1)

38-38: Consider improving the type assertion approach

The complex type assertion chain is potentially brittle and could lead to runtime errors if the actual structure from useBrowseConfig() changes.

Consider defining proper interface extensions or using a type guard function instead:

- const config = useBrowseConfig() as unknown as { data: Config & { config: { stats?: StatsConfig } } | null, isLoading: boolean, error: Error | null };
+ interface ConfigWithStats extends Config {
+   config?: {
+     stats?: StatsConfig;
+   };
+ }
+ 
+ interface ConfigResponseWithStats {
+   data: ConfigWithStats | null;
+   isLoading: boolean;
+   error: Error | null;
+ }
+ 
+ const config = useBrowseConfig() as unknown as ConfigResponseWithStats;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 81fa59f and ff5f84e.

📒 Files selected for processing (6)
  • apps/stats/src/config/stats-config.ts (1 hunks)
  • apps/stats/src/providers/GlobalDataProvider.tsx (3 hunks)
  • apps/stats/src/views/Stats/Locations.tsx (2 hunks)
  • apps/stats/src/views/Stats/Sources.tsx (2 hunks)
  • apps/stats/src/views/Stats/Web.tsx (1 hunks)
  • apps/stats/src/views/Stats/components/WebKpis.tsx (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (4)
apps/stats/src/config/stats-config.ts (2)
apps/stats/src/providers/GlobalDataProvider.tsx (1)
  • StatsConfig (6-15)
ghost/core/core/frontend/helpers/ghost_head.js (1)
  • endpoint (165-165)
apps/stats/src/views/Stats/Web.tsx (5)
apps/stats/src/providers/GlobalDataProvider.tsx (1)
  • useGlobalData (29-35)
apps/stats/src/utils/chart-helpers.ts (1)
  • getRangeDates (42-47)
apps/stats/src/utils/data-formatters.ts (1)
  • formatQueryDate (3-5)
apps/stats/src/views/Stats/components/AudienceSelect.tsx (1)
  • getAudienceQueryParam (17-31)
apps/stats/src/config/stats-config.ts (2)
  • getStatEndpointUrl (5-13)
  • getToken (15-17)
apps/stats/src/providers/GlobalDataProvider.tsx (1)
apps/admin-x-framework/src/api/config.ts (2)
  • Config (7-62)
  • useBrowseConfig (72-75)
apps/stats/src/views/Stats/Locations.tsx (5)
apps/stats/src/providers/GlobalDataProvider.tsx (1)
  • useGlobalData (29-35)
apps/stats/src/utils/chart-helpers.ts (1)
  • getRangeDates (42-47)
apps/stats/src/utils/data-formatters.ts (1)
  • formatQueryDate (3-5)
apps/stats/src/views/Stats/components/AudienceSelect.tsx (1)
  • getAudienceQueryParam (17-31)
apps/stats/src/config/stats-config.ts (2)
  • getStatEndpointUrl (5-13)
  • getToken (15-17)
🔇 Additional comments (22)
apps/stats/src/views/Stats/Locations.tsx (4)

15-15: Updated import to include getToken function.

Good addition to import the getToken function alongside getStatEndpointUrl to support the refactored token handling.


33-33: Streamlined configuration access with direct statsConfig usage.

The destructuring now directly accesses statsConfig instead of using nested configData?.config.stats, which improves code clarity.


39-39: Updated site_uuid parameter to use statsConfig.id.

Clean replacement of the nested property access with direct statsConfig?.id reference.


47-49: Refactored endpoint and token handling.

The component now passes the entire statsConfig object to the helper functions instead of accessing nested properties. This implementation correctly uses the new helper functions to handle conditional endpoint construction and token selection.

apps/stats/src/config/stats-config.ts (3)

1-2: Good import of StatsConfig interface.

Importing the StatsConfig interface from the provider ensures type consistency across the application.


5-13: Well-implemented endpoint URL construction with local mode support.

The refactored function now accepts a StatsConfig object rather than separate parameters, which simplifies the API. The conditional logic correctly handles local mode by:

  1. Returning early if config is missing
  2. Using local endpoint without version suffix when local mode is enabled
  3. Using main endpoint with version suffix otherwise

The implementation makes good use of optional chaining operators to handle potential undefined values.


15-17: Effective token selection helper function.

The new getToken function cleanly centralizes the logic for selecting between local and main tokens based on configuration. This is a good abstraction that simplifies token handling across components.

apps/stats/src/views/Stats/Web.tsx (4)

11-11: Updated import to include token helper.

Correctly importing the new getToken function alongside the existing getStatEndpointUrl.


16-16: Simplified configuration access pattern.

Good update to directly destructure statsConfig from the global data context instead of using nested property access.


21-21: Updated site_uuid parameter to use statsConfig.id.

Consistent with the pattern used in other components, using direct property access.


29-31: Refactored endpoint and token handling.

This update correctly uses the helper functions with the entire statsConfig object, which aligns with the broader refactoring efforts for configuration handling.

apps/stats/src/views/Stats/components/WebKpis.tsx (4)

8-8: Updated import to include token helper.

Correct addition of the getToken import alongside getStatEndpointUrl.


42-42: Simplified configuration access.

Good change to use direct statsConfig access instead of nested property destructuring.


48-48: Updated site_uuid parameter consistently.

This change maintains consistency with the other components by using statsConfig?.id.


56-58: Refactored endpoint and token handling with helper functions.

The implementation correctly uses the new helper functions, passing the entire statsConfig object which simplifies the code and supports the local/remote mode switching.

apps/stats/src/providers/GlobalDataProvider.tsx (3)

5-15: Good extraction of the StatsConfig interface

The extraction of StatsConfig as a standalone interface is a positive change that improves the maintainability of the code. Making all fields optional is appropriate for a configuration object, and the nested structure allows for clear separation between standard and local configurations.


17-20: Clean separation of statsConfig from data in context type

Separating statsConfig from the general data object in the context type is a good architectural decision. It makes the stats configuration directly accessible to consumers without requiring them to navigate through nested properties.


52-54: Clean exposure of statsConfig in context value

Providing statsConfig directly in the context value is a good improvement that aligns with the interface changes above. This simplifies access for consuming components and makes the code more readable.

apps/stats/src/views/Stats/Sources.tsx (4)

11-11: Good addition of getToken import

Adding the getToken import alongside getStatEndpointUrl properly supports the refactoring of token retrieval logic.


35-35: Improved access to statsConfig

Accessing statsConfig directly from useGlobalData() instead of through nested destructuring makes the code cleaner and more direct. This change is consistent with the updates in the GlobalDataProvider.


39-41: Clean implementation of statsConfig access

Using statsConfig?.id directly for the site_uuid parameter is more readable and maintainable than accessing it through nested properties. The optional chaining operator is appropriate for handling potential undefined values.


47-50: Well-implemented helper function usage

The updated useQuery implementation properly leverages the new helper functions. Passing the entire statsConfig object to getStatEndpointUrl and using getToken(statsConfig) centralizes endpoint and token retrieval logic, making it more maintainable.

@9larsons
9larsons merged commit ae93688 into main Apr 21, 2025
@9larsons
9larsons deleted the stats-x-use-config branch April 21, 2025 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant