Refactor (packages/web/src/components/Share.tsx): Function with many returns - #21
Open
ahmaddpathan wants to merge 2 commits into
Open
Refactor (packages/web/src/components/Share.tsx): Function with many returns#21ahmaddpathan wants to merge 2 commits into
ahmaddpathan wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
P1B: Starter Task: Refactoring PR
1. Issue
Link to the associated GitHub issue:
#7
Full path to the refactored file:
packages/web/src/components/Share.tsx
What do you think this file does?
This file appears to implement the UI for viewing a shared OpenCode session. It manages live session updates through a WebSocket connection and renders session messages, connection status, usage statistics and related UI state.
What is the scope of your refactoring within that file?
The refactoring is limited to
getStatusText(), which converts the current connection status into the appropriate display message. I changed its return structure without changing the behavior of the largerSharecomponent.Which Qlty‑reported issue did you address?
Qlty reported
Function with many returns (count = 6): getStatusTextat line 24 ofpackages/web/src/components/Share.tsx.2. Refactoring
How did the specific issue you chose impact the codebase’s maintainability?
The function had six separate return paths, which fragmented its control flow and could make future changes to connection-status handling easier to overlook. A single return point makes the result of each branch easier to follow consistently.
What changes did you make to resolve the issue?
I replaced the individual returns inside the
switchwith assignments to a localtextvariable and added one return at the end of the function. I preserved all existing status mappings, including the custom error-message fallback, and exported the function so its behavior could be tested directly.How do your changes improve maintainability? Did you consider alternatives?
The refactor gives the function one consistent exit point while preserving its existing switch-based structure and behavior. I considered replacing the switch with a lookup object, but the
errorcase has special fallback behavior, so retaining the switch required fewer behavioural changes and kept the refactor focused.3. Validation
How did you validate that the change is correct?
I reran Qlty and confirmed that the
getStatusTextmany-returns smell is no longer reported. I also added targeted Bun tests covering all normal connection statuses, a supplied error message, and the default error fallback; all three tests pass locally, and LCOV confirms that the refactored lines are executed.Attach a screenshot of the test coverage showing the lines were executed by the tests.
Attach a screenshot showing the tests that cover the change passing during CI
Attach a screenshot of
qlty smells --no-snippets <full/path/to/file.ts>showing fewer reported issues after the changes.Before Refactoring:

After Refactoring:

MORE SCREENSHOTS
1. New Tests Written to Check Code Changes
Note: The tests cover all possible six cases/branches in the function. All tests pass after refactoring. The 3 tests cover successful signals, error message and default case. No tests were previously given for the refactored function.
2.
bun testOutputNote:
bun testcommand executed successfully in the terminal for the relevant file and all written tests passed.3.
bun lintOutputNote:
bun lintcommand executed successfully in the terminal for the relevant file and no errors/warnings were shown.