Skip to content

fix(ui): refetch environments when window regains focus or reconnects#4774

Merged
markphelps merged 4 commits into
v2from
markphelps/fix-stale-env-pr-picker
Oct 1, 2025
Merged

fix(ui): refetch environments when window regains focus or reconnects#4774
markphelps merged 4 commits into
v2from
markphelps/fix-stale-env-pr-picker

Conversation

@markphelps

@markphelps markphelps commented Sep 30, 2025

Copy link
Copy Markdown
Collaborator

Summary

Fixes the issue where deleted/merged branches and closed pull requests were not being removed from the environment picker until the user performed a hard refresh.

The root cause was twofold:

  1. RTK Query's default caching behavior doesn't automatically refetch data when the browser tab regains focus or when network reconnects
  2. While the backend polls upstream every 30 seconds to detect changes (added in fix(environments): remove deleted branches from UI when synced from Git #4770), the UI wasn't polling to sync with those backend updates

This meant stale environment data persisted in the UI even after the backend detected and removed deleted branches.

Changes

  • Modified ui/src/app/environments/environmentsApi.ts: Added refetchOnFocus: true and refetchOnReconnect: true to the environmentsApi configuration to enable automatic refetching on focus/reconnect events
  • Modified ui/src/app/Layout.tsx:
    • Added refetchOnMountOrArgChange: true to ensure fresh data on component mount
    • Added pollingInterval: 30000 to poll every 30 seconds, matching the backend Git fetch interval
  • Modified ui/src/components/environments/EnvironmentNamespaceSwitcher.tsx:
    • Added conditional pollingInterval: open ? 30000 : 0 to poll for namespace changes only when the switcher popover is open (resource optimization)
  • Modified ui/src/components/environments/branches/BranchActionsDropdown.tsx: Added refetchOnMountOrArgChange: true to useListBranchEnvironmentsQuery to keep branch data fresh

Behavior

Screenshot 2025-10-01 at 5 16 57 PM

^ here you can see several of the polling requests

Now the environment list automatically refetches when:

  • The user switches back to the Flipt browser tab (refetchOnFocus)
  • The network connection is restored (refetchOnReconnect)
  • Every 30 seconds via polling (syncs with backend Git fetch interval)

This ensures deleted branches and closed/merged PRs are removed from the environment picker within 30 seconds without requiring a manual page refresh.

Timeline for upstream branch deletions:

  • Immediately if you switch back to the Flipt tab after deletion
  • Within 30 seconds if you leave the tab open continuously
  • Immediately if your network reconnects after deletion

Related

Fixes #4770 (comment)

The environment picker was showing stale data for deleted/merged branches
and closed pull requests until the user performed a hard refresh. This
occurred because RTK Query's default caching behavior doesn't automatically
refetch data when the browser tab regains focus or when network reconnects.

Changes:
- Added refetchOnFocus and refetchOnReconnect to environmentsApi config
- Added refetchOnMountOrArgChange to useListEnvironmentsQuery in Layout
- Added refetchOnMountOrArgChange to useListBranchEnvironmentsQuery in BranchActionsDropdown

Now when users switch back to the Flipt tab or reconnect to the network,
the environment list will automatically refetch, removing any deleted
branches or closed PRs from the picker without requiring a manual refresh.

Fixes #4770

Signed-off-by: Mark Phelps <[email protected]>
@markphelps markphelps added the v2 Flipt v2 label Sep 30, 2025
@codecov

codecov Bot commented Sep 30, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.50%. Comparing base (c6fe648) to head (69b92d1).
⚠️ Report is 1 commits behind head on v2.

Additional details and impacted files
@@            Coverage Diff             @@
##               v2    #4774      +/-   ##
==========================================
- Coverage   58.55%   58.50%   -0.05%     
==========================================
  Files         135      135              
  Lines       16840    16840              
==========================================
- Hits         9860     9853       -7     
- Misses       6294     6300       +6     
- Partials      686      687       +1     
Flag Coverage Δ
integrationtests 33.76% <ø> (-0.17%) ⬇️
unittests 49.13% <ø> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@markphelps
markphelps requested a review from Copilot October 1, 2025 14:25

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

This PR fixes an issue where deleted branches and closed pull requests were not being removed from the environment picker until a hard refresh was performed. The solution implements automatic refetching of environment data using RTK Query's built-in capabilities.

  • Configures RTK Query to automatically refetch data when the browser tab regains focus or network reconnects
  • Adds refetch-on-mount behavior to ensure fresh data when components mount
  • Ensures the environment picker always displays current data without manual refresh

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
ui/src/app/environments/environmentsApi.ts Adds global refetch configuration for focus and reconnect events
ui/src/app/Layout.tsx Enables refetch on mount for main environment list
ui/src/components/environments/branches/BranchActionsDropdown.tsx Enables refetch on mount for branch environment queries

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

The environment picker was showing stale data for deleted/merged branches
and closed pull requests until the user performed a hard refresh. This
occurred because:
1. RTK Query's default caching doesn't automatically refetch on focus/reconnect
2. The backend polls upstream every 30s, but the UI wasn't polling for updates

Changes:
- Added refetchOnFocus and refetchOnReconnect to environmentsApi config
- Added refetchOnMountOrArgChange to useListEnvironmentsQuery in Layout
- Added 30s pollingInterval to useListEnvironmentsQuery to match backend fetch interval
- Added refetchOnMountOrArgChange to useListBranchEnvironmentsQuery in BranchActionsDropdown
- Added conditional 30s polling to namespace query when environment switcher is open

Now the UI automatically refetches environments when:
- The browser tab regains focus (refetchOnFocus)
- Network reconnects (refetchOnReconnect)
- Every 30 seconds via polling (matching backend Git fetch interval)

This ensures deleted branches and closed PRs are removed from the environment
picker within 30 seconds without requiring a manual page refresh.

Fixes #4770

Signed-off-by: Mark Phelps <[email protected]>
@markphelps
markphelps force-pushed the markphelps/fix-stale-env-pr-picker branch from a9127d3 to c870585 Compare October 1, 2025 20:42
@markphelps
markphelps marked this pull request as ready for review October 1, 2025 21:17
@markphelps
markphelps requested a review from a team as a code owner October 1, 2025 21:17
@markphelps
markphelps requested a review from erka October 1, 2025 21:17
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Oct 1, 2025
@dosubot

dosubot Bot commented Oct 1, 2025

Copy link
Copy Markdown

Related Documentation

Checked 4 published document(s). No updates required.

You have 1 draft document(s). Publish docs to keep them always up-to-date

How did I do? Any feedback?  Join Discord

@erka erka 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.

The git sync is another thing which impacts this. It could add some extra time in the worst case.

@markphelps

Copy link
Copy Markdown
Collaborator Author

Yeah def the timing could be off max 1m I think

@markphelps markphelps added the automerge Used by Kodiak bot to automerge PRs label Oct 1, 2025
@markphelps
markphelps merged commit ac46bc1 into v2 Oct 1, 2025
44 of 47 checks passed
@markphelps
markphelps deleted the markphelps/fix-stale-env-pr-picker branch October 1, 2025 23:28
@github-project-automation github-project-automation Bot moved this to Done in Flipt V2 Oct 1, 2025
@dosubot

dosubot Bot commented Oct 1, 2025

Copy link
Copy Markdown

Documentation Updates

Checked 4 published document(s). No updates required.

You have 1 draft document(s). Publish docs to keep them always up-to-date

How did I do? Any feedback?  Join Discord

ptejasvini pushed a commit to ptejasvini/flipt that referenced this pull request Oct 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge Used by Kodiak bot to automerge PRs size:S This PR changes 10-29 lines, ignoring generated files. v2 Flipt v2

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants