Skip to content

[ENG-524] Optimized Encounter ID search by removing redundant React state#16488

Open
NikhilA8606 wants to merge 2 commits into
developfrom
ENG-524-audit-react-state-usage-in-encounter-id-search
Open

[ENG-524] Optimized Encounter ID search by removing redundant React state#16488
NikhilA8606 wants to merge 2 commits into
developfrom
ENG-524-audit-react-state-usage-in-encounter-id-search

Conversation

@NikhilA8606

@NikhilA8606 NikhilA8606 commented Jun 30, 2026

Copy link
Copy Markdown
Member

Proposed Changes

  • Removed searchWith state because it was duplicating information already available in qParams.
  • Removed activeSearchOptionIndex since Command component now handles the selected item styling automatically.
  • Removed searchInputRef because PopoverAnchor and Radix handle focus management without needing focus() calls
  • Removed custom keyboard navigation and use Command inbuilt keyboard navigation
Screen.Recording.2026-07-01.at.1.10.58.AM.mov

Tagging: @ohcnetwork/care-fe-code-reviewers

Merge Checklist

  • Add specs that demonstrate the bug or test the new feature.
  • Update product documentation.
  • Ensure that UI text is placed in I18n files.
  • Prepare a screenshot or demo video for the changelog entry and attach it to the issue.
  • Request peer reviews.
  • Complete QA on mobile devices.
  • Complete QA on desktop devices.
  • Add or update Playwright tests for related changes

Summary by CodeRabbit

  • New Features

    • Updated the patient search UI to use a streamlined, menu-based selection flow.
    • Search mode now automatically follows the selected query type, keeping the input and results aligned.
  • Bug Fixes

    • Improved keyboard interactions with clearer Enter/Escape behavior and simplified selection handling.

@NikhilA8606
NikhilA8606 requested review from a team and Copilot June 30, 2026 19:43
@netlify

netlify Bot commented Jun 30, 2026

Copy link
Copy Markdown

Deploy Preview for care-ohc ready!

Name Link
🔨 Latest commit 725852a
🔍 Latest deploy log https://app.netlify.com/projects/care-ohc/deploys/6a44c56416e8db0008104ba2
😎 Deploy Preview https://deploy-preview-16488.preview.ohc.network
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

EncounterList’s patient search now derives its mode from qParams.external_identifier, syncs search text from query changes, and updates search params directly. The custom input and arrow-key navigation were replaced with a CommandInput inside a Popover, with simpler Escape and Enter handling.

Changes

Search UI Refactor

Layer / File(s) Summary
Search mode derivation from qParams
src/pages/Encounters/EncounterList.tsx
Local searchWith state is removed; search mode is now computed from qParams.external_identifier, the syncing effect only updates searchText, and handleSearchWithChange simply updates the query and closes the popover.
Command/Popover search input
src/pages/Encounters/EncounterList.tsx
Imports are updated to include CommandInput; the old text input with active-index keyboard navigation is replaced by a Command/CommandInput rendered inside a Popover, with CommandItem selection and simplified Escape/Enter handling.

Possibly related PRs

  • ohcnetwork/care_fe#16437: Also modifies EncounterList.tsx to derive patient search mode between "name" and "external_identifier" from query params and replaces the search input with a Popover + Command-based control.
  • ohcnetwork/care_fe#16438: Also touches EncounterList.tsx search UI behavior and layout around the same control.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change: simplifying Encounter ID search by removing redundant React state.
Description check ✅ Passed It covers the proposed changes and merge checklist well, though the required Fixes #issue_number line is missing.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ENG-524-audit-react-state-usage-in-encounter-id-search

Comment @coderabbitai help to get the list of available commands.

@NikhilA8606 NikhilA8606 changed the title ENG-524 Audit React state usage in Encounter ID search [ENG-524] Optimized Encounter ID search by removing redundant React state Jun 30, 2026
@NikhilA8606

NikhilA8606 commented Jun 30, 2026

Copy link
Copy Markdown
Member Author

ENG-524

@greptile-apps

greptile-apps Bot commented Jun 30, 2026

Copy link
Copy Markdown

Greptile Summary

This PR simplifies the encounter search control by removing three pieces of redundant local state (searchWith, activeSearchOptionIndex, searchInputRef) and replacing the custom <input> + manual keyboard handler with cmdk's CommandInput. The search mode is now derived directly from qParams rather than mirrored in a parallel state variable, which eliminates a class of sync bugs.

  • searchWith is converted from a useState to a derived constant computed from qParams.external_identifier, and the useEffect that kept them in sync is removed.
  • The custom ArrowUp/ArrowDown/Enter keyboard navigation is deleted in favour of cmdk's built-in item navigation; the Command wrapper now owns focus management, and PopoverTrigger asChild replaces the manual searchInputRef.current?.focus() call.
  • searchQueryParams is now a trivial passthrough of qParams values (with empty-string-to-undefined coercion) rather than a conditional build from local state.

Confidence Score: 5/5

Safe to merge — the refactoring removes state duplication without changing any data-fetching logic or committed-search behaviour.

The change is a focused state-reduction refactor on a single UI component. The query key and API call parameters remain driven by qParams, so no search results are affected. The two observations flagged are a minor post-Escape keyboard edge-case and a now-trivial intermediate variable — neither alters correctness of the feature.

No files require special attention.

Important Files Changed

Filename Overview
src/pages/Encounters/EncounterList.tsx Removes redundant searchWith state and activeSearchOptionIndex, derives search mode directly from qParams, and replaces the custom <input> + manual keyboard navigation with cmdk's CommandInput. One keyboard edge-case regression and a now-trivial intermediate variable are noted.

Reviews (2): Last reviewed commit: "Copilot suggestions" | Re-trigger Greptile

Comment thread src/pages/Encounters/EncounterList.tsx Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the Encounter list “search by Encounter ID / patient name” UI to reduce duplicated React state and rely more on the Command component’s built-in selection/keyboard behavior, keeping search type derived from query params.

Changes:

  • Removes duplicated state (searchWith, activeSearchOptionIndex, searchInputRef) and simplifies syncing searchText from qParams.
  • Replaces custom keyboard navigation/styling with CommandInput + CommandItem selection behavior.
  • Restructures the search-type popover anchoring and open/close interactions.

Comment thread src/pages/Encounters/EncounterList.tsx Outdated
Comment thread src/pages/Encounters/EncounterList.tsx Outdated
Comment on lines +578 to 605
<Command
shouldFilter={false}
className="overflow-visible bg-transparent"
>
<PopoverTrigger asChild>
<div className="relative">
<input
ref={searchInputRef}
<Popover
open={searchOptionsOpen && showSearchOptions}
onOpenChange={setSearchOptionsOpen}
>
<PopoverAnchor asChild>
<CommandInput
value={searchText}
onChange={(event) => {
const value = event.target.value;
onValueChange={(value) => {
setSearchText(value);
setSearchOptionsOpen(value.trim() !== "");
setActiveSearchOptionIndex(0);
}}
onFocus={() => {
if (showSearchOptions) {
setSearchOptionsOpen(true);
onClick={() => {
if (showSearchOptions) setSearchOptionsOpen(true);
}}
onKeyDown={(event) => {
if (event.key === "Escape") {
setSearchOptionsOpen(false);
return;
}
if (event.key === "Enter" && !searchOptionsOpen) {
event.preventDefault();
updateSearchQuery(searchWith, searchText);
}
}}

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/pages/Encounters/EncounterList.tsx`:
- Around line 196-201: Keep the API search params derived only from committed
qParams, not from the draft searchText state. In EncounterList, update
searchQueryParams (and any callers such as the useEffect and the
data-fetching/query-key logic) so the request payload only uses
qParams.external_identifier/qParams.name and other committed filter values; do
not let unsubmitted searchText affect the query when filters or pagination
change. Use the existing searchWith, searchQueryParams, and qParams symbols to
keep the URL and fetched results in sync.
- Around line 587-607: The search field in EncounterList’s CommandInput relies
only on placeholder text, so it needs an explicit accessible name. Update the
CommandInput usage to add an aria-label that clearly identifies the search
control, while keeping the existing placeholder and handlers in place. Use the
CommandInput component in EncounterList as the target for this accessibility
fix.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 8efbc7a6-1099-4c85-8837-7b471e101996

📥 Commits

Reviewing files that changed from the base of the PR and between f8a2f09 and 7b0686b.

📒 Files selected for processing (1)
  • src/pages/Encounters/EncounterList.tsx

Comment thread src/pages/Encounters/EncounterList.tsx
Comment thread src/pages/Encounters/EncounterList.tsx
@github-actions

github-actions Bot commented Jun 30, 2026

Copy link
Copy Markdown

🎭 Playwright Test Results

Status: ✅ Passed
Test Shards: 3

Metric Count
Total Tests 328
✅ Passed 328
❌ Failed 0
⏭️ Skipped 0

📊 Detailed results are available in the playwright-final-report artifact.

Run: #9855

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying care-preview with  Cloudflare Pages  Cloudflare Pages

Latest commit: 725852a
Status: ✅  Deploy successful!
Preview URL: https://c202cdbb.care-preview-a7w.pages.dev
Branch Preview URL: https://eng-524-audit-react-state-us.care-preview-a7w.pages.dev

View logs

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
src/pages/Encounters/EncounterList.tsx (2)

615-641: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Keep the active cmdk item synced to searchWith src/pages/Encounters/EncounterList.tsx:615-641 — this Command is uncontrolled, so cmdk keeps the first item active by default. When the popover is open, Enter can select name even while external_identifier is active. Pass the current mode as value so the highlighted item matches the active search mode.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/pages/Encounters/EncounterList.tsx` around lines 615 - 641, The Command
menu in EncounterList is uncontrolled, so cmdk can keep the first item active
even when a different search mode is selected. Update the Command setup around
searchWithOptions, CommandList, and CommandItem so the current searchWith state
is passed as the Command value, keeping the highlighted item synchronized with
searchWith and ensuring Enter selects the active mode rather than the first
option.

586-609: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Keep the input click from closing the open options list. PopoverTrigger asChild forwards Radix’s toggle behavior to CommandInput, so clicking inside the already-open field to move the caret closes the list and focus alone won’t reopen it. Move the trigger off the input or ignore trigger clicks while the popover is open.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/pages/Encounters/EncounterList.tsx` around lines 586 - 609, Keep the
search input from toggling the popover closed when it is already open. In
EncounterList’s CommandInput/PopoverTrigger wiring, Radix’s asChild trigger
behavior is causing clicks on the focused field to close the options list, so
change the interaction so the input is not the active trigger or clicks are
ignored while searchOptionsOpen is true. Make sure the fix preserves typing,
caret movement, and the existing setSearchOptionsOpen/showSearchOptions behavior
around CommandInput and PopoverTrigger.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/pages/Encounters/EncounterList.tsx`:
- Around line 615-641: The Command menu in EncounterList is uncontrolled, so
cmdk can keep the first item active even when a different search mode is
selected. Update the Command setup around searchWithOptions, CommandList, and
CommandItem so the current searchWith state is passed as the Command value,
keeping the highlighted item synchronized with searchWith and ensuring Enter
selects the active mode rather than the first option.
- Around line 586-609: Keep the search input from toggling the popover closed
when it is already open. In EncounterList’s CommandInput/PopoverTrigger wiring,
Radix’s asChild trigger behavior is causing clicks on the focused field to close
the options list, so change the interaction so the input is not the active
trigger or clicks are ignored while searchOptionsOpen is true. Make sure the fix
preserves typing, caret movement, and the existing
setSearchOptionsOpen/showSearchOptions behavior around CommandInput and
PopoverTrigger.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 7727e180-9ace-4992-9c20-fec2ca98b3af

📥 Commits

Reviewing files that changed from the base of the PR and between 7b0686b and 725852a.

📒 Files selected for processing (1)
  • src/pages/Encounters/EncounterList.tsx

Comment thread src/pages/Encounters/EncounterList.tsx
Comment thread src/pages/Encounters/EncounterList.tsx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants