Repository navigation
Conversation
WalkthroughThis change introduces a new feature flag system for the posts analytics application. A custom React hook ( Possibly related PRs
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/posts/src/routes.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/posts".) 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/posts/.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/posts/src/hooks/withFeatureFlag.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/posts".) 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/posts/.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/posts/src/hooks/useFeatureFlag.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/posts".) 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/posts/.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 ⚡️ Faster reviews with caching
Enjoy the performance boost—your workflow just got faster. ✨ 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:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. 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: 1
🧹 Nitpick comments (2)
apps/posts/src/hooks/useFeatureFlag.tsx (1)
17-18: Consider adding error handling for JSON parsingThe JSON parsing could potentially throw an error if the labs setting contains malformed JSON. Consider adding a try-catch block to handle this gracefully.
- const labsJSON = getSettingValue<string>(settings, 'labs') || '{}'; - const labs = JSON.parse(labsJSON); + const labsJSON = getSettingValue<string>(settings, 'labs') || '{}'; + let labs = {}; + try { + labs = JSON.parse(labsJSON); + } catch (e) { + console.error('Failed to parse labs settings:', e); + }apps/posts/src/hooks/withFeatureFlag.tsx (1)
31-42: Consider enhancing the loading placeholder.The loading state is currently an empty div with a comment. Consider adding a more meaningful loading indicator or skeleton UI to improve user experience during loading.
- <div>{/* Loading placeholder */}</div> + <div aria-label="Loading content" className="skeleton-loading"> + {/* Skeleton UI elements can go here */} + </div>
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
apps/posts/src/hooks/useFeatureFlag.tsx(1 hunks)apps/posts/src/hooks/withFeatureFlag.tsx(1 hunks)apps/posts/src/providers/GlobalDataProvider.tsx(1 hunks)apps/posts/src/routes.tsx(2 hunks)apps/posts/src/views/PostAnalytics/Newsletter.tsx(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
apps/posts/src/hooks/useFeatureFlag.tsx (1)
apps/posts/src/providers/GlobalDataProvider.tsx (1)
useGlobalData(20-26)
apps/posts/src/hooks/withFeatureFlag.tsx (1)
apps/posts/src/hooks/useFeatureFlag.tsx (1)
useFeatureFlag(13-47)
🔇 Additional comments (9)
apps/posts/src/providers/GlobalDataProvider.tsx (1)
36-38: Good approach to centralized loading and error handling!This is a solid implementation that properly tracks loading and error states across both config and settings requests. It ensures that the component waits for all data to be available before rendering, preventing premature actions that depend on settings.
apps/posts/src/views/PostAnalytics/Newsletter.tsx (1)
72-72: Clean simplification of loading state logicSimplifying the loading state to only depend on newsletter stats loading is appropriate now that the feature flag logic has been moved to the HOC.
apps/posts/src/routes.tsx (2)
10-11: Good implementation of feature flag protection at route levelWrapping the Newsletter component with the feature flag HOC at the route level is a clean approach that properly separates concerns and ensures the component isn't rendered until the feature flag is checked.
37-37: LGTM - Properly uses the protected componentThe route now correctly uses the feature flag-protected component instead of the raw Newsletter component.
apps/posts/src/hooks/useFeatureFlag.tsx (1)
13-47: Well-implemented feature flag hook with proper loading handlingThis hook correctly handles the loading state to prevent premature redirects, which addresses the core issue described in the PR. The implementation effectively guards against the race condition where redirection happens before settings are loaded.
apps/posts/src/hooks/withFeatureFlag.tsx (4)
7-15: Well-documented Higher-Order Component.The JSDoc comments are thorough and clearly explain the purpose and parameters of the HOC, which will help other developers understand how to use it.
16-21: Good use of TypeScript generics for type safety.The generic type parameter
P extends objectensures that props are properly typed and passed through to the wrapped component, maintaining type safety throughout.
22-28: Clean handling of redirect state.The code appropriately checks for the redirect case first and returns early if needed, following good control flow practices.
48-49: Good practice setting displayName for debugging.Setting a descriptive display name with fallbacks helps with debugging and component identification in React DevTools.
no refs ## Evidence that the component is unused `PostAnalyticsView` is not exported by the private Admin package, and repository-wide searches for both its symbol and module path find no importer. Admin-only Knip reported `apps/admin/src/posts/analytics/components/post-analytics-view.tsx` as an unused file before this removal and stopped reporting it afterward. The Admin build uses explicit routes and entry points; no dynamic import or glob references this path. ## History and intentionality Documented facts: - ad52284 introduced the component as the loading/empty/content wrapper in the first React post-analytics skeleton. #22978 explains that the work began a better breakdown of post statistics, initially focused on web analytics for one post. - 54d993b replaced that static wrapper in the main page with real post and traffic data. #22986 says this made the previously static skeleton usable for beta users. - 26de9ed later reused the component only as the loading screen for a duplicated `withFeatureFlag` wrapper. #23270 documents that the wrapper prevented the Newsletter tab from redirecting before the alpha setting had loaded, and explicitly called the duplication temporary. - #23029 documents the rollout model: `trafficAnalytics` selected beta users, while `trafficAnalyticsAlpha` selected features still under development for that beta. - f0a33b8 intentionally moved Newsletter analytics out of alpha. Its diff replaced the protected route with the direct `Newsletter` route and made the sidebar links unconditional; #23294 states that purpose directly. Production therefore stopped reaching the wrapper, and consequently `PostAnalyticsView`, on May 12, 2025. Given the documented flag semantics, this was an intentional promotion to the analytics beta rather than an accidental bypass. - fb0790b later restored the alpha flag for other design work but did not restore Newsletter's route guard, corroborating that Newsletter's promotion was deliberate. - 566f8a9 removed the now-unreachable wrapper and its tests. #29686 identifies those files as duplicated dead code with no live importers. That deletion removed the component's last source or test importer on July 30, 2026. Inference: leaving `PostAnalyticsView` behind in that last cleanup was an incomplete leaf cleanup. No historical source explicitly describes the omission, but the dead-code PR's stated intent, the current zero-reference result, and Knip's independent unused-file report support that conclusion. ## Removal Deleted the 33-line orphaned component. It had no dedicated tests, export, dependency, configuration, or documentation to remove. Its `EmptyStatView` and `PostAnalyticsContent` dependencies remain used by live analytics screens. ## Verification - `pnpm exec knip --workspace @tryghost/admin --include files --reporter compact --no-progress` (the removed file is absent; four pre-existing acceptance setup/config entries remain) - `pnpm --dir apps/admin lint` - `pnpm --dir apps/admin typecheck` - `pnpm --dir apps/admin test:unit` (135 files, 1,604 tests) - `pnpm nx run @tryghost/admin:build` - `pnpm nx run-many -t test --exclude @tryghost/e2e --exclude ghost-admin --exclude @tryghost/koenig-lexical` (35 projects, including 8,000 Ghost tests) `pnpm check` completed repository-wide lint and documentation checks, but its aggregate test phase could not run Koenig Lexical's browser suite in this environment: Chromium was denied Mach port registration and the configured Firefox executable is absent. The broader test run above excludes only that environment-blocked package and passed.
ref https://linear.app/ghost/issue/PROD-1672/post-analytics-newsletter-tab-fails-to-load-frequently
The Newsletter tab in the Post Analytics view was prematurely redirecting because the
trafficAnalyticsAlphaflag hadn't been loaded yet. The redirect was occurring before the settings were loaded from the server, so unless thepostsapp had already been loaded and cached the settings, the Newsletter tab would redirect to the/web/route. This/web/route doesn't really exist, so it shows the "Loading interrupted" error.This follows the same pattern we have in the
statsapp using awithFeatureFlaghigher order component to put the whole route behind the flag, and critically it waits for the settings to load before redirecting.Note: this duplicates the
withFeatureFlaganduseFeatureFlaghook fromstatsintoposts. We should either merge these apps together, or most these intoadmin-x-frameworkto remove the duplication.