Skip to content

Conversation

@vitorguima
Copy link
Contributor

@vitorguima vitorguima commented Jul 28, 2025

This PR introduces two key changes to improve the reliability of GraphQL subscriptions when the app transitions between background and foreground states:

  1. New Hook: useAppStateSubscription
    Added a reusable hook that listens to app state changes using React Native’s AppState API. Executes a provided callback (onAppResume) when the app returns to the foreground (e.g., after being backgrounded or locked).

Useful for re-subscribing to real-time data or refetching stale content.

Example:

AppStateSubscription(() => {
  refetch({ id: chatRoom.id }, { fetchPolicy: 'store-and-network' })
})
  1. Improved useMessagesListSubscription Hook
    Enhanced the subscription lifecycle by subscribing and unsubscribing based on the app's active/inactive state.
    Ensures real-time updates are re-established automatically after app resumes from the background or device unlock.
    Prevents missed events or stale subscriptions due to suspended WebSocket connections on iOS.

Summary by CodeRabbit

  • New Features

    • Introduced a new hook to detect when the app returns to the foreground.
  • Improvements

    • Enhanced message subscription logic to better handle app state transitions, providing more reliable updates when the app becomes active or inactive.
  • Chores

    • Updated several internal dependencies across packages to their latest versions.
    • Incremented package versions and updated changelogs for improved tracking.

@changeset-bot
Copy link

changeset-bot bot commented Jul 28, 2025

⚠️ No Changeset found

Latest commit: 1610978

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link

coderabbitai bot commented Jul 28, 2025

Warning

Rate limit exceeded

@vitorguima has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 4 minutes and 15 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 9cb0032 and ff2b681.

📒 Files selected for processing (1)
  • packages/graphql/package.json (2 hunks)

Walkthrough

This update introduces a new hook, useAppStateSubscription, in the utilities package to handle app state transitions. The components package refactors its subscription logic to leverage this hook, improving responsiveness to app state changes. Multiple package versions and their changelogs are updated to reflect dependency upgrades and minor enhancements.

Changes

Cohort / File(s) Change Summary
Authentication Package Version & Changelog
packages/authentication/CHANGELOG.md, packages/authentication/package.json
Updated package version to 5.0.2 and changelog to document dependency upgrade (@baseapp-frontend/utils to 4.0.1). Replaced catalog dependency placeholders with explicit version numbers. No functional changes.
Components Package Version & Changelog
packages/components/CHANGELOG.md, packages/components/package.json
Updated version to 1.2.8 and changelog to note improved subscription logic and multiple dependency upgrades. Replaced catalog references with explicit versions.
Components Subscription Logic
packages/components/modules/messages/native/graphql/subscriptions/useMessagesListSubscription.tsx
Refactored subscription management: extracted subscribe/unsubscribe callbacks, integrated useAppStateSubscription to refresh subscriptions on app state change, and improved modularity. No changes to exported function signatures.
Design System Version & Changelog
packages/design-system/CHANGELOG.md, packages/design-system/package.json
Updated version to 1.0.21 and changelog to document dependency upgrade (@baseapp-frontend/utils to 4.0.1). Replaced catalog references with explicit versions. No functional changes.
GraphQL Package Version & Changelog
packages/graphql/CHANGELOG.md, packages/graphql/package.json
Updated version to 1.3.3 and changelog for dependency upgrades (@baseapp-frontend/utils and @baseapp-frontend/authentication). Replaced catalog references with explicit versions. No functional changes.
Provider Package Version & Changelog
packages/provider/CHANGELOG.md, packages/provider/package.json
Updated version to 2.0.15 and changelog for dependency upgrade (@baseapp-frontend/utils to 4.0.1). Replaced catalog references with explicit versions. No functional changes.
Utils Hook Addition
packages/utils/hooks/useAppStateSubscription/index.ts
Added new hook useAppStateSubscription to trigger a callback when the app resumes from background/inactive state. Replaced catalog references with explicit versions in package.json.
Utils Package Version & Changelog
packages/utils/CHANGELOG.md, packages/utils/package.json
Updated version to 4.0.1 and changelog to document new hook addition. Replaced catalog references with explicit versions.
Wagtail Package Version & Changelog
packages/wagtail/CHANGELOG.md, packages/wagtail/package.json
Updated version to 1.0.35 and changelog for dependency upgrades (@baseapp-frontend/utils, @baseapp-frontend/design-system, @baseapp-frontend/graphql). Replaced catalog references with explicit versions. No functional changes.
Config, ESLint Plugin, and Test Packages
packages/config/package.json, packages/eslint-plugin/package.json, packages/test/package.json
Replaced catalog references in devDependencies and dependencies with explicit version numbers for eslint, prettier, testing libraries, Storybook, and related tooling. No functional changes.

Sequence Diagram(s)

sequenceDiagram
    participant App
    participant useAppStateSubscription
    participant useMessagesListSubscription

    App->>useMessagesListSubscription: Mounts component
    useMessagesListSubscription->>useAppStateSubscription: Registers onAppResume callback
    useAppStateSubscription-->>useMessagesListSubscription: Notifies on app resume
    useMessagesListSubscription->>useMessagesListSubscription: Calls subscribe/unsubscribe as needed
    App-->>useMessagesListSubscription: Unmounts component
    useMessagesListSubscription->>useAppStateSubscription: Cleans up subscription
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • BA: restore catalogs #216: Reverses catalog reference replacements in @baseapp-frontend/authentication package.json, directly related to dependency version management in this package.
  • BA-2012: stable versions #166: Stabilizes dependency versions by replacing catalog references with fixed versions across multiple packages, including @baseapp-frontend/authentication.
  • (No third PR strongly related at code level found.)

Suggested labels

approved

Suggested reviewers

  • anicioalexandre
  • priscilladeroode
  • deboracosilveira
  • nossila

Poem

In a warren of code, a rabbit hops with glee,
New hooks for app state, so nimble and free!
Subscriptions now dance when the app comes alive,
With changelogs and versions, our packages thrive.
Dependency carrots all fresh and new—
Reviewers, come nibble, there’s little to chew!
🐇✨

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/react-native-subscriptions-improvement

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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need 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)

  • @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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests 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.

Copy link

@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: 3

🧹 Nitpick comments (5)
packages/utils/package.json (1)

4-4: Consider using semantic versioning correctly for the new feature.

Adding a new hook (useAppStateSubscription) is a backward-compatible feature addition that should warrant a minor version bump (4.1.0) rather than a patch version bump (4.0.1). Patch versions are typically reserved for bug fixes.

packages/utils/CHANGELOG.md (1)

5-7: Semantic versioning inconsistency between changelog and version number.

The changelog correctly categorizes the new hook as "Minor Changes", but the version number is 4.0.1 (patch) instead of 4.1.0 (minor). The categorization in the changelog is correct - adding new features should be minor version bumps.

Consider updating the version to 4.1.0 to match the "Minor Changes" categorization in the changelog.

packages/utils/hooks/useAppStateSubscription/index.ts (1)

22-22: Consider optimizing useEffect dependencies to prevent unnecessary re-subscriptions.

The current implementation will recreate the subscription whenever onAppResume changes, which could happen frequently if the parent component doesn't memoize the callback. Consider using useCallback in the consuming components or add a ref pattern to avoid unnecessary subscription churn.

Example optimization using useRef:

export const useAppStateSubscription = (onAppResume: () => void) => {
  const appState = useRef<AppStateStatus>(AppState.currentState)
+ const onAppResumeRef = useRef(onAppResume)
+ 
+ useEffect(() => {
+   onAppResumeRef.current = onAppResume
+ })

  useEffect(() => {
    const handleAppStateChange = (nextAppState: AppStateStatus) => {
      if (appState.current.match(/inactive|background/) && nextAppState === 'active') {
-       onAppResume()
+       onAppResumeRef.current()
      }
      appState.current = nextAppState
    }

    const stateChangeSubscription = AppState.addEventListener('change', handleAppStateChange)
    return () => {
      stateChangeSubscription.remove()
    }
- }, [onAppResume])
+ }, [])
packages/components/modules/messages/native/graphql/subscriptions/useMessagesListSubscription.tsx (2)

35-39: Well-structured subscription callback with minor improvement opportunity.

The subscribe callback is well-implemented with proper guard clauses and memory leak prevention. The logic correctly disposes existing subscriptions before creating new ones.

Consider simplifying the dispose call since Disposable.dispose should always exist:

-    disposableRef.current?.dispose?.()
+    disposableRef.current?.dispose()

41-44: Clean unsubscribe implementation.

The unsubscribe callback is well-structured with appropriate dependencies and proper cleanup logic.

Same minor improvement as in the subscribe function:

-    disposableRef.current?.dispose?.()
+    disposableRef.current?.dispose()
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ba018f5 and 2afc628.

📒 Files selected for processing (16)
  • packages/authentication/CHANGELOG.md (1 hunks)
  • packages/authentication/package.json (1 hunks)
  • packages/components/CHANGELOG.md (1 hunks)
  • packages/components/modules/messages/native/graphql/subscriptions/useMessagesListSubscription.tsx (3 hunks)
  • packages/components/package.json (1 hunks)
  • packages/design-system/CHANGELOG.md (1 hunks)
  • packages/design-system/package.json (1 hunks)
  • packages/graphql/CHANGELOG.md (1 hunks)
  • packages/graphql/package.json (1 hunks)
  • packages/provider/CHANGELOG.md (1 hunks)
  • packages/provider/package.json (1 hunks)
  • packages/utils/CHANGELOG.md (1 hunks)
  • packages/utils/hooks/useAppStateSubscription/index.ts (1 hunks)
  • packages/utils/package.json (1 hunks)
  • packages/wagtail/CHANGELOG.md (1 hunks)
  • packages/wagtail/package.json (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/components/modules/messages/native/graphql/subscriptions/useMessagesListSubscription.tsx (1)
packages/utils/hooks/useAppStateSubscription/index.ts (1)
  • useAppStateSubscription (7-23)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (13)
packages/utils/hooks/useAppStateSubscription/index.ts (1)

7-23: LGTM! Solid implementation with one optimization opportunity.

The hook correctly implements app state monitoring for React Native with proper cleanup and accurate state transition detection.

packages/graphql/package.json (1)

4-4: LGTM! Appropriate patch version bump for dependency updates.

The patch version increment is semantically correct for a dependency-only update with no API changes to this package.

packages/wagtail/package.json (1)

4-4: Version bump looks good – please confirm matching CHANGELOG entry.

version was incremented to 1.0.35, which is fine sem-ver-wise.
Double-check that packages/wagtail/CHANGELOG.md (not shown here) contains a corresponding 1.0.35 section so consumers can track what changed.

packages/authentication/package.json (1)

4-4: Patch version increment is correct.

5.0.1 → 5.0.2 follows sem-ver for the dependency bump to utils. No further action needed if the CHANGELOG already reflects this.

packages/components/package.json (1)

4-4: Minor patch bump acknowledged.

1.2.7 → 1.2.8 aligns with the small hook refactor. Ensure the dist artefacts are rebuilt before publishing.

packages/provider/CHANGELOG.md (1)

3-9: Semantic-version mismatch between CHANGELOG and actual change.

A new heading 3.0.0 is introduced, yet the notes list only a patch dependency bump.
Either:

  1. Rename the section to 2.0.15 (or the next patch), or
  2. If a breaking change really exists, bump packages/provider/package.json to 3.0.0.

Publishing with mismatched versions will confuse consumers and automation.

packages/authentication/CHANGELOG.md (1)

3-9: Changelog entry looks good

Patch-level bump only updates a dependency – the versioning and wording are consistent.

packages/graphql/CHANGELOG.md (1)

3-10: Changelog entry consistent

Patch bump with dependency updates only – no issues spotted.

packages/wagtail/CHANGELOG.md (1)

3-10: Changelog entry consistent

New major version (2.0.0) lists only dependency bumps; acceptable if other breaking changes occurred elsewhere in the repo. No structural issues detected.

Ensure Wagtail actually contains breaking changes; if not, consider minor/patch bump instead of major.

packages/components/CHANGELOG.md (1)

3-16: Changelog entry reads well

Minor version increment matches functional refactor; dependency bumps recorded.

packages/components/modules/messages/native/graphql/subscriptions/useMessagesListSubscription.tsx (3)

3-3: LGTM: Import statement is correct.

The import for useAppStateSubscription is properly structured and aligns with the package architecture described in the PR objectives.


46-53: Excellent refactoring of focus effect logic.

The extraction of subscription logic into reusable callbacks improves code organization and maintainability while preserving the original behavior.


55-57: Perfect implementation of app state subscription enhancement.

This addition directly addresses the PR objectives by ensuring subscriptions are refreshed when the app returns to the foreground. The integration with the existing subscribe callback maintains consistency and simplicity.

@vitorguima vitorguima force-pushed the feature/react-native-subscriptions-improvement branch 2 times, most recently from 32ac684 to a65ad4c Compare July 28, 2025 16:17
@vitorguima vitorguima force-pushed the feature/react-native-subscriptions-improvement branch from 63d52b9 to 1610978 Compare July 28, 2025 16:23
@vitorguima vitorguima force-pushed the feature/react-native-subscriptions-improvement branch 2 times, most recently from 9cb0032 to 1610978 Compare July 28, 2025 19:01
@sonarqubecloud
Copy link

@vitorguima vitorguima merged commit d994082 into master Jul 28, 2025
12 checks passed
@vitorguima vitorguima deleted the feature/react-native-subscriptions-improvement branch July 28, 2025 19:15
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.

3 participants