Repository navigation
Updated stats-x to use config for endpoints - #22970
Conversation
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)
WalkthroughThis change refactors the handling and access of stats-related configuration across multiple modules in the application. The 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
apps/stats/src/config/stats-config.tsOops! 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: 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.tsxOops! 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: 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.tsxOops! 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: 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.
Tip ⚡💬 Agentic Chat (Pro Plan, General Availability)
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
apps/stats/src/providers/GlobalDataProvider.tsx (1)
38-38: Consider improving the type assertion approachThe 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
📒 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 includegetTokenfunction.Good addition to import the
getTokenfunction alongsidegetStatEndpointUrlto support the refactored token handling.
33-33: Streamlined configuration access with directstatsConfigusage.The destructuring now directly accesses
statsConfiginstead of using nestedconfigData?.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?.idreference.
47-49: Refactored endpoint and token handling.The component now passes the entire
statsConfigobject 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
StatsConfiginterface 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
StatsConfigobject rather than separate parameters, which simplifies the API. The conditional logic correctly handles local mode by:
- Returning early if config is missing
- Using local endpoint without version suffix when local mode is enabled
- 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
getTokenfunction 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
getTokenfunction alongside the existinggetStatEndpointUrl.
16-16: Simplified configuration access pattern.Good update to directly destructure
statsConfigfrom 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
statsConfigobject, 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
getTokenimport alongsidegetStatEndpointUrl.
42-42: Simplified configuration access.Good change to use direct
statsConfigaccess 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
statsConfigobject 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 interfaceThe extraction of
StatsConfigas 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 typeSeparating
statsConfigfrom the generaldataobject 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 valueProviding
statsConfigdirectly 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 importAdding the
getTokenimport alongsidegetStatEndpointUrlproperly supports the refactoring of token retrieval logic.
35-35: Improved access to statsConfigAccessing
statsConfigdirectly fromuseGlobalData()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 accessUsing
statsConfig?.iddirectly for thesite_uuidparameter 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 usageThe updated
useQueryimplementation properly leverages the new helper functions. Passing the entirestatsConfigobject togetStatEndpointUrland usinggetToken(statsConfig)centralizes endpoint and token retrieval logic, making it more maintainable.
ref https://linear.app/ghost/issue/PROD-222