Skip to content

Conversation

@Ishankoradia
Copy link
Contributor

@Ishankoradia Ishankoradia commented Jun 27, 2025

This PR refactors the socket logic to use a common function/hook. And this hook now also does retry with exponential backoff of some sort.

Summary by CodeRabbit

  • New Features

    • Introduced a custom WebSocket connection feature with improved reconnection and error handling across connection forms.
  • Refactor

    • Updated connection, source, and destination forms to use the new custom WebSocket connection for more robust and consistent real-time communication.
  • Tests

    • Updated test files to mock and use the new custom WebSocket connection hook, maintaining test reliability.

@coderabbitai
Copy link

coderabbitai bot commented Jun 27, 2025

Walkthrough

A new custom React hook, useWebSocketConnection, was introduced to manage WebSocket connections with enhanced reconnection and error handling logic. Multiple components and test files were refactored to use this custom hook instead of the external react-use-websocket library directly. The new hook centralizes WebSocket management and exposes a consistent interface including methods to send messages, update the URL, and disconnect.

Changes

File(s) Change Summary
src/customHooks/useWebsocketConnection.ts Added new custom hook useWebSocketConnection with reconnection, error handling, and cleanup logic.
src/components/Connections/CreateConnectionForm.tsx
src/components/Destinations/DestinationForm.tsx
src/components/Sources/SourceForm.tsx
Replaced react-use-websocket with useWebSocketConnection; updated hook usage and error handling accordingly.
src/components/Destinations/tests/CreateDestinationForm.test.tsx
src/components/Destinations/tests/EditDestinationForm.test.tsx
src/components/Sources/tests/CreateSourceForm.test.tsx
src/components/Sources/tests/EditSourceForm.test.tsx
Updated WebSocket hook imports to use the custom hook; adjusted mocks in tests; no logic changes.

Sequence Diagram(s)

sequenceDiagram
    participant Component
    participant useWebSocketConnection
    participant react-use-websocket

    Component->>useWebSocketConnection: Call useWebSocketConnection(endpoint, options)
    useWebSocketConnection->>react-use-websocket: Establish WebSocket connection with options
    react-use-websocket-->>useWebSocketConnection: Provide sendJsonMessage, lastMessage, setSocketUrl
    useWebSocketConnection-->>Component: Expose sendJsonMessage, lastMessage, setSocketUrl, disconnect

    react-use-websocket-->>useWebSocketConnection: On error event
    useWebSocketConnection-->>Component: Call handleError or log error

    Component->>useWebSocketConnection: Call setSocketUrl or disconnect as needed
    useWebSocketConnection->>react-use-websocket: Update or close socket
Loading

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 426e518 and 7b9baad.

📒 Files selected for processing (1)
  • src/components/Sources/__tests__/CreateSourceForm.test.tsx (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/components/Sources/tests/CreateSourceForm.test.tsx
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: checks
✨ 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.
    • 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 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: 0

🧹 Nitpick comments (2)
src/customHooks/useWebsocketConnection.ts (1)

40-44: Consider adding dependency to cleanup effect.

The cleanup effect has an empty dependency array but calls disconnect() which references getWebSocket(). While this works for unmount cleanup, consider if any dependencies should be included for completeness.

  useEffect(() => {
    return () => {
      disconnect();
    };
-  }, []);
+  }, [getWebSocket]);
src/components/Connections/CreateConnectionForm.tsx (1)

15-15: Remove unused import.

The set import from react-hook-form is not used anywhere in the component and should be removed.

-import { Controller, set, useForm } from 'react-hook-form';
+import { Controller, useForm } from 'react-hook-form';
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 593522d and d49667b.

📒 Files selected for processing (8)
  • src/components/Connections/CreateConnectionForm.tsx (2 hunks)
  • src/components/Destinations/DestinationForm.tsx (2 hunks)
  • src/components/Destinations/__tests__/CreateDestinationForm.test.tsx (1 hunks)
  • src/components/Destinations/__tests__/EditDestinationForm.test.tsx (1 hunks)
  • src/components/Sources/SourceForm.tsx (2 hunks)
  • src/components/Sources/__tests__/CreateSourceForm.test.tsx (1 hunks)
  • src/components/Sources/__tests__/EditSourceForm.test.tsx (1 hunks)
  • src/customHooks/useWebsocketConnection.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (7)
src/components/Destinations/DestinationForm.tsx (3)
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1550
File: src/components/Sources/__tests__/EditSourceForm.test.tsx:427-437
Timestamp: 2025-06-17T17:12:43.766Z
Learning: In src/components/Sources/__tests__/EditSourceForm.test.tsx, the test expectation for the host value 'localhostinvalid-host' in the connection failure test should be kept as-is, even though it appears to be a concatenation of default and input values.
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1556
File: src/components/Connections/Connections.tsx:692-701
Timestamp: 2025-06-11T08:52:45.594Z
Learning: In `src/components/Connections/Connections.tsx`, the intended behavior is for `filterConnections` to match the search term against connection name and source name only; destination name should not be included in the filter criteria.
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1550
File: src/helpers/connectorConfig/FormField.tsx:343-359
Timestamp: 2025-06-17T03:30:42.337Z
Learning: In the FormField component (src/helpers/connectorConfig/FormField.tsx), the addItem function for array fields only initializes explicit default values for top-level subFields, but nested required fields are correctly rendered dynamically in the UI at render time. This approach works properly for complex configurations like S3 buckets, as confirmed by user testing.
src/components/Destinations/__tests__/EditDestinationForm.test.tsx (2)
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1550
File: src/components/Sources/__tests__/EditSourceForm.test.tsx:427-437
Timestamp: 2025-06-17T17:12:43.766Z
Learning: In src/components/Sources/__tests__/EditSourceForm.test.tsx, the test expectation for the host value 'localhostinvalid-host' in the connection failure test should be kept as-is, even though it appears to be a concatenation of default and input values.
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1556
File: src/components/Connections/Connections.tsx:692-701
Timestamp: 2025-06-11T08:52:45.594Z
Learning: In `src/components/Connections/Connections.tsx`, the intended behavior is for `filterConnections` to match the search term against connection name and source name only; destination name should not be included in the filter criteria.
src/components/Destinations/__tests__/CreateDestinationForm.test.tsx (2)
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1550
File: src/components/Sources/__tests__/EditSourceForm.test.tsx:427-437
Timestamp: 2025-06-17T17:12:43.766Z
Learning: In src/components/Sources/__tests__/EditSourceForm.test.tsx, the test expectation for the host value 'localhostinvalid-host' in the connection failure test should be kept as-is, even though it appears to be a concatenation of default and input values.
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1556
File: src/components/Connections/Connections.tsx:692-701
Timestamp: 2025-06-11T08:52:45.594Z
Learning: In `src/components/Connections/Connections.tsx`, the intended behavior is for `filterConnections` to match the search term against connection name and source name only; destination name should not be included in the filter criteria.
src/components/Sources/__tests__/CreateSourceForm.test.tsx (2)
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1550
File: src/components/Sources/__tests__/EditSourceForm.test.tsx:427-437
Timestamp: 2025-06-17T17:12:43.766Z
Learning: In src/components/Sources/__tests__/EditSourceForm.test.tsx, the test expectation for the host value 'localhostinvalid-host' in the connection failure test should be kept as-is, even though it appears to be a concatenation of default and input values.
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1556
File: src/components/Connections/Connections.tsx:692-701
Timestamp: 2025-06-11T08:52:45.594Z
Learning: In `src/components/Connections/Connections.tsx`, the intended behavior is for `filterConnections` to match the search term against connection name and source name only; destination name should not be included in the filter criteria.
src/components/Sources/SourceForm.tsx (3)
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1550
File: src/components/Sources/__tests__/EditSourceForm.test.tsx:427-437
Timestamp: 2025-06-17T17:12:43.766Z
Learning: In src/components/Sources/__tests__/EditSourceForm.test.tsx, the test expectation for the host value 'localhostinvalid-host' in the connection failure test should be kept as-is, even though it appears to be a concatenation of default and input values.
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1556
File: src/components/Connections/Connections.tsx:692-701
Timestamp: 2025-06-11T08:52:45.594Z
Learning: In `src/components/Connections/Connections.tsx`, the intended behavior is for `filterConnections` to match the search term against connection name and source name only; destination name should not be included in the filter criteria.
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1550
File: src/helpers/connectorConfig/FormField.tsx:343-359
Timestamp: 2025-06-17T03:30:42.337Z
Learning: In the FormField component (src/helpers/connectorConfig/FormField.tsx), the addItem function for array fields only initializes explicit default values for top-level subFields, but nested required fields are correctly rendered dynamically in the UI at render time. This approach works properly for complex configurations like S3 buckets, as confirmed by user testing.
src/components/Connections/CreateConnectionForm.tsx (3)
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1550
File: src/components/Sources/__tests__/EditSourceForm.test.tsx:427-437
Timestamp: 2025-06-17T17:12:43.766Z
Learning: In src/components/Sources/__tests__/EditSourceForm.test.tsx, the test expectation for the host value 'localhostinvalid-host' in the connection failure test should be kept as-is, even though it appears to be a concatenation of default and input values.
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1556
File: src/components/Connections/Connections.tsx:692-701
Timestamp: 2025-06-11T08:52:45.594Z
Learning: In `src/components/Connections/Connections.tsx`, the intended behavior is for `filterConnections` to match the search term against connection name and source name only; destination name should not be included in the filter criteria.
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1550
File: src/helpers/connectorConfig/FormField.tsx:343-359
Timestamp: 2025-06-17T03:30:42.337Z
Learning: In the FormField component (src/helpers/connectorConfig/FormField.tsx), the addItem function for array fields only initializes explicit default values for top-level subFields, but nested required fields are correctly rendered dynamically in the UI at render time. This approach works properly for complex configurations like S3 buckets, as confirmed by user testing.
src/components/Sources/__tests__/EditSourceForm.test.tsx (1)
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1550
File: src/components/Sources/__tests__/EditSourceForm.test.tsx:427-437
Timestamp: 2025-06-17T17:12:43.766Z
Learning: In src/components/Sources/__tests__/EditSourceForm.test.tsx, the test expectation for the host value 'localhostinvalid-host' in the connection failure test should be kept as-is, even though it appears to be a concatenation of default and input values.
🧬 Code Graph Analysis (3)
src/components/Destinations/DestinationForm.tsx (1)
src/customHooks/useWebsocketConnection.ts (1)
  • useWebSocketConnection (9-47)
src/components/Sources/SourceForm.tsx (1)
src/customHooks/useWebsocketConnection.ts (1)
  • useWebSocketConnection (9-47)
src/components/Connections/CreateConnectionForm.tsx (1)
src/customHooks/useWebsocketConnection.ts (1)
  • useWebSocketConnection (9-47)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: checks
🔇 Additional comments (12)
src/customHooks/useWebsocketConnection.ts (2)

1-47: Well-implemented WebSocket abstraction with proper retry logic.

The custom hook effectively centralizes WebSocket management with exponential backoff retry mechanism. The implementation looks solid overall.


20-20: Verify the exponential backoff calculation.

The exponential backoff formula looks correct, but let's confirm the sequence:

  • Attempt 0: Math.min(1000 * 2^0, 30000) = 1000ms
  • Attempt 1: Math.min(1000 * 2^1, 30000) = 2000ms
  • Attempt 2: Math.min(1000 * 2^2, 30000) = 4000ms
  • And so on, capped at 30 seconds
#!/bin/bash
# Verify the exponential backoff calculation produces expected intervals
python3 -c "
import math
for attempt in range(6):
    interval = min(1000 * math.pow(2, attempt), 30000)
    print(f'Attempt {attempt}: {interval}ms ({interval/1000}s)')
"
src/components/Sources/__tests__/CreateSourceForm.test.tsx (1)

6-6: Import change correctly implements the WebSocket refactor.

The import has been properly updated to use the custom hook while maintaining interface compatibility through aliasing. The test logic remains unchanged, which demonstrates good abstraction.

src/components/Sources/__tests__/EditSourceForm.test.tsx (1)

7-7: Consistent refactor implementation across test files.

The import change follows the same pattern as other test files in this refactor, properly switching to the custom hook while maintaining interface compatibility.

src/components/Destinations/__tests__/CreateDestinationForm.test.tsx (1)

7-7: Consistent application of WebSocket refactor to destination tests.

The import change maintains the same pattern used across all test files in this refactor, ensuring consistent adoption of the custom WebSocket hook.

src/components/Destinations/__tests__/EditDestinationForm.test.tsx (1)

7-7: Completes the comprehensive WebSocket refactor across test files.

This final import change ensures all test files consistently use the custom WebSocket hook, completing the refactor objective of centralizing WebSocket connection management.

src/components/Destinations/DestinationForm.tsx (2)

13-13: LGTM: Import updated to use custom WebSocket hook.

The import change to the custom useWebSocketConnection hook is part of the centralization effort to standardize WebSocket usage across components.


53-53: LGTM: Hook usage properly simplified.

The change removes the explicit error handling options and relies on the custom hook's default error handling (console.error). The returned interface (sendJsonMessage, lastMessage) remains consistent with the original implementation.

src/components/Sources/SourceForm.tsx (2)

12-12: LGTM: Import updated to use centralized WebSocket hook.

The import change aligns with the refactor to use the custom useWebSocketConnection hook across all components.


84-84: LGTM: Enhanced hook usage with dynamic URL management.

The change properly utilizes the custom hook's setSocketUrl functionality, allowing dynamic WebSocket URL updates. This is a good improvement over static URL management.

src/components/Connections/CreateConnectionForm.tsx (2)

23-23: LGTM: Import updated to use custom WebSocket hook.

The import change maintains consistency with the WebSocket refactoring across other components.


217-222: LGTM: Hook usage properly maintains error handling.

The change correctly uses the custom hook's handleError option to preserve the existing error handling behavior while benefiting from the centralized retry logic and connection management.

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: 1

🧹 Nitpick comments (1)
src/components/Sources/__tests__/CreateSourceForm.test.tsx (1)

182-190: Remove debug console logs and improve test clarity.

The console.log statements appear to be leftover debugging code and should be removed for cleaner test output.

-      console.log('generateWebsocketUrl calls:', generateWebsocketUrlMock.mock.calls);
-      console.log('setSocketUrl calls:', setSocketUrlMock.mock.calls);
-
       expect(generateWebsocketUrlMock).toHaveBeenCalledWith(
         'airbyte/source/check_connection',
         expect.objectContaining({ user: { email: 'a' } })
       );
-
       expect(setSocketUrlMock).toHaveBeenCalledWith(socketUrlEnpoint);
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 9970092 and 426e518.

📒 Files selected for processing (1)
  • src/components/Sources/__tests__/CreateSourceForm.test.tsx (5 hunks)
🧰 Additional context used
🧠 Learnings (1)
src/components/Sources/__tests__/CreateSourceForm.test.tsx (1)
Learnt from: himanshudube97
PR: DalgoT4D/webapp#1550
File: src/components/Sources/__tests__/EditSourceForm.test.tsx:427-437
Timestamp: 2025-06-17T17:12:43.766Z
Learning: In src/components/Sources/__tests__/EditSourceForm.test.tsx, the test expectation for the host value 'localhostinvalid-host' in the connection failure test should be kept as-is, even though it appears to be a concatenation of default and input values.
🧬 Code Graph Analysis (1)
src/components/Sources/__tests__/CreateSourceForm.test.tsx (1)
src/customHooks/useWebsocketConnection.ts (1)
  • useWebSocketConnection (9-47)
🪛 GitHub Check: checks
src/components/Sources/__tests__/CreateSourceForm.test.tsx

[failure] 103-103:
Require statement not part of import statement

🪛 ESLint
src/components/Sources/__tests__/CreateSourceForm.test.tsx

[error] 103-103: Require statement not part of import statement.

(@typescript-eslint/no-var-requires)

🪛 GitHub Actions: Dalgo CI
src/components/Sources/__tests__/CreateSourceForm.test.tsx

[error] 103-103: ESLint: Require statement not part of import statement (@typescript-eslint/no-var-requires)

🔇 Additional comments (6)
src/components/Sources/__tests__/CreateSourceForm.test.tsx (6)

6-6: LGTM: Import updated for custom hook.

The import has been correctly updated to use the new custom hook as part of the WebSocket refactor.


15-15: LGTM: Added helper function import.

The import of generateWebsocketUrl is necessary for the new WebSocket architecture.


27-30: LGTM: Mock setup updated correctly.

The Jest mock has been properly updated to mock the named export useWebSocketConnection instead of the external package.


84-87: LGTM: Helper function mock added.

The mock for generateWebsocketUrl is properly set up to support the new testing approach.


106-111: LGTM: Mock return value matches new hook interface.

The mock return value correctly includes all the methods exposed by the useWebSocketConnection hook: sendJsonMessage, lastMessage, disconnect, and setSocketUrl.


177-191: No changes required; test aligns with component behavior.
The CreateSourceForm test correctly mocks generateWebsocketUrl and useWebSocketConnection and asserts that setSocketUrl is called with the value returned by generateWebsocketUrl, matching the SourceForm’s useEffect logic.

@Ishankoradia
Copy link
Contributor Author

working on the test cases

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