Files
netbird/client/ui/tray_update.go
Zoltán Papp 2cdc6ef1c6 ui: split tray.go into feature files, rename Peers service to DaemonFeed
The 1542-line tray.go grew into a 14-feature kitchen sink. Split it
into feature-coherent same-package siblings, give the daemon-stream
service a name that matches what it actually does, and trim the
cargo-cult context.WithCancel pattern from click handlers.

File layout (tray.go: 1542 → ~470 lines):
  - tray_status.go    onStatusEvent / applyStatus / status indicator
  - tray_icon.go      applyIcon / iconForState (tray icon painting)
  - tray_events.go    onSystemEvent + eventTitle / titleCase, plus a
                      shouldSkipSystemEvent helper that names the
                      three "daemon notification we don't surface"
                      filters
  - tray_session.go   session-expiry row + warning notification flow +
                      handleSessionExpired (moved from tray.go)
  - tray_profiles.go  loadConfig / loadProfiles / switchProfile
  - tray_exitnodes.go exit-node submenu (rebuild / refresh / toggle)

Mutex split: the kitchen-sink t.mu becomes four domain-scoped mutexes
so a long-running gRPC call in one domain can't block status-push
readers in another:
  - statusMu        connected / lastStatus / lastDaemonVersion /
                    lastNetworksRevision / pendingConnectLogin
  - sessionMu       sessionExpiresAt (read by the 30s ticker,
                    written by applySessionExpiry on every status push)
  - profileMu       activeProfile / activeUsername /
                    notificationsEnabled / switchCancel
  - exitNodesMu     row cache (read in reapplyMenuState's Repaint copy)
  - exitNodesRebuildMu  serialises ListNetworks + submenu rebuild +
                        SetMenu (already separate, kept)

Service rename: the "Peers" service handled the daemon's full
SubscribeStatus snapshot (peers, daemon version, management/signal
link state, networks revision, SSO deadline) plus the SubscribeEvents
notification stream and the profile-switch suppression filter. Peers
was a misleading name for a daemon-stream fan-out service. Rename to
DaemonFeed in services/, profileswitcher's stored reference, the
TrayServices struct, main.go wiring, and every doc comment that
referenced it. peers.go → daemon_feed.go. The Status.Peers field
itself (the peer list in the snapshot) is unchanged.

Event constant renames (wire strings unchanged so the frontend keeps
working without regenerating bindings beyond the rename):
  - EventStatus → EventStatusSnapshot
    Payload is a full Status struct (daemon-wide snapshot), not just
    a state-change ping — name the value-shape.
  - EventSystem → EventDaemonNotification
    Payload is a daemon SystemEvent meant to drive an OS toast or a
    Recent Events row. "System" was too generic; "Notification"
    matches what consumers do with it.

Concurrency fixes:
  - WaitExtendAuthSession now preempts a previous in-flight wait
    via the existing SetWaitCancel/CancelWait infrastructure on
    PendingFlow, the same pattern WaitSSOLogin uses. The previous
    waiter exits with codes.Canceled; the authsession service
    translates that to ExtendResult{Preempted: true} so the tray
    and the about-to-expire dialog stay silent on the losing flow
    instead of showing a false-failure toast. Without this, both
    a tray "Extend now" click and a dialog "Stay connected" click
    on the same deadline started two parallel IdP polls, and
    whichever lost the device-code check painted a bogus error.
  - mgmClient.ExtendAuthSession drops the dead backoff retry loop.
    The loop only retried on codes.Canceled, but the inner mgmCtx
    was derived from context.Background() and never cancelled, so
    every real error went straight to backoff.Permanent on the
    first attempt. Replace with a single
    context.WithTimeout(c.ctx, ConnectTimeout) call; daemon
    shutdown now interrupts the RPC and behaviour on real errors
    is unchanged.

Click-handler hygiene: six call sites used the cargo-cult
context.WithCancel(context.Background()) + defer cancel() pattern
without ever calling cancel() externally. Replace with
context.Background() directly (loadConfig, loadProfiles,
runExtendSession, dismissSessionWarning, handleConnect's Up,
handleDisconnect's Down). The one site that genuinely needs the
cancel — switchProfile, which stores it in t.switchCancel so
handleDisconnect can preempt the switch — keeps WithCancel.

Helper extraction: shouldSkipSystemEvent groups the three
"daemon notification we drop on the floor" checks
(new_version_available metadata, progress_window metadata, the
::/0 partner of an exit-node default-route event) behind a single
named predicate. Each had a comment explaining why; collecting
them moves the rationale into the helper docstring and shrinks
onSystemEvent to a router.
2026-05-28 21:26:57 +02:00

213 lines
6.2 KiB
Go

//go:build !android && !ios && !freebsd && !js
package main
import (
"context"
"sync"
"time"
log "github.com/sirupsen/logrus"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/services/notifications"
"github.com/netbirdio/netbird/client/ui/services"
"github.com/netbirdio/netbird/client/ui/updater"
)
// trayUpdater owns every piece of tray UI that reacts to the auto-update
// feature: the "Download latest / Install version X" menu item, the
// EventUpdateState subscription, the click that either opens the GitHub
// releases page or triggers the in-window installer flow, the OS
// notification for a freshly announced version, and the bring-window-forward
// call when the daemon enters force-install. Composed inside Tray; never
// used standalone.
type trayUpdater struct {
app *application.App
window *application.WebviewWindow
update *services.Update
notifier *notifications.NotificationService
loc *Localizer
// onIconChange is invoked whenever the "update available" flag
// transitions, so the tray can repaint its icon (the small badge
// overlay differs between has-update / no-update).
onIconChange func()
mu sync.Mutex
item *application.MenuItem
state updater.State
notifiedVersion string // last version we surfaced as an OS notification
progressWindowOpen bool // last installing value we acted on
}
func newTrayUpdater(app *application.App, window *application.WebviewWindow, update *services.Update, notifier *notifications.NotificationService, loc *Localizer, onIconChange func()) *trayUpdater {
u := &trayUpdater{
app: app,
window: window,
update: update,
notifier: notifier,
loc: loc,
onIconChange: onIconChange,
}
app.Event.On(updater.EventStateChanged, u.onStateEvent)
// Seed from the cached state so we don't miss an event that fired
// before NewTray finished wiring (DaemonFeed.Watch starts after tray
// construction today, but treat that as an implementation detail).
u.state = update.GetState()
return u
}
// attach (re)binds the menu item the tray builds for us. Called every time
// Tray.buildMenu runs — initial menu construction and language switches.
// The menu item's OnClick handler is owned by the caller; this method only
// configures label and visibility from the cached state.
func (u *trayUpdater) attach(item *application.MenuItem) {
u.mu.Lock()
u.item = item
state := u.state
u.mu.Unlock()
u.refreshMenuItem(state)
}
// hasUpdate reports whether the tray should paint the "update available"
// icon variant. Read by Tray.iconForState during applyIcon.
func (u *trayUpdater) hasUpdate() bool {
u.mu.Lock()
defer u.mu.Unlock()
return u.state.Available
}
// applyLanguage re-renders the menu item label from the cached state, used
// after Tray.applyLanguage rebuilds the menu with a fresh locale.
func (u *trayUpdater) applyLanguage() {
u.mu.Lock()
state := u.state
u.mu.Unlock()
u.refreshMenuItem(state)
}
// handleClick runs when the user clicks the tray update entry. Branch 1
// (Enforced=false) opens the GitHub releases page in the browser; Branch 2
// (Enforced=true) surfaces the in-window /update progress page and asks
// the daemon to start the installer.
func (u *trayUpdater) handleClick() {
u.mu.Lock()
state := u.state
u.mu.Unlock()
if !state.Enforced {
_ = u.app.Browser.OpenURL(urlGitHubReleases)
return
}
u.openProgressWindow(state.Version)
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if _, err := u.update.Trigger(ctx); err != nil {
log.Errorf("trigger update: %v", err)
}
}()
}
func (u *trayUpdater) onStateEvent(ev *application.CustomEvent) {
st, ok := ev.Data.(updater.State)
if !ok {
log.Warnf("update state event payload not UpdateState: %T", ev.Data)
return
}
u.applyState(st)
}
// applyState diffs the incoming UpdateState against the cached copy and
// drives every side effect: icon repaint, menu label/visibility, OS
// notification on a newly-announced version, /update window on install
// entry.
func (u *trayUpdater) applyState(st updater.State) {
u.mu.Lock()
prev := u.state
u.state = st
sendNotify := st.Available && st.Version != "" && st.Version != u.notifiedVersion
if sendNotify {
u.notifiedVersion = st.Version
}
showWindow := st.Installing && !u.progressWindowOpen
if showWindow {
u.progressWindowOpen = true
} else if !st.Installing {
u.progressWindowOpen = false
}
u.mu.Unlock()
u.refreshMenuItem(st)
if prev.Available != st.Available && u.onIconChange != nil {
u.onIconChange()
}
if sendNotify {
u.sendUpdateNotification(st)
}
if showWindow {
u.openProgressWindow(st.Version)
}
}
// refreshMenuItem updates the menu item's label and visibility from the
// given state. Called from applyState (event-driven), attach (menu rebuild)
// and applyLanguage (locale switch) — all three converge on the same shape.
func (u *trayUpdater) refreshMenuItem(st updater.State) {
u.mu.Lock()
item := u.item
u.mu.Unlock()
if item == nil {
return
}
if !st.Available {
item.SetHidden(true)
return
}
if st.Enforced {
item.SetLabel(u.loc.T("tray.menu.installVersion", "version", st.Version))
} else {
item.SetLabel(u.loc.T("tray.menu.downloadLatest"))
}
item.SetHidden(false)
}
func (u *trayUpdater) sendUpdateNotification(st updater.State) {
if u.notifier == nil {
return
}
body := u.loc.T("notify.update.body", "version", st.Version)
if st.Enforced {
body += u.loc.T("notify.update.enforcedSuffix")
}
if err := u.notifier.SendNotification(notifications.NotificationOptions{
ID: notifyIDUpdatePrefix + st.Version,
Title: u.loc.T("notify.update.title"),
Body: body,
}); err != nil {
log.Debugf("send update notification: %v", err)
}
}
// openProgressWindow points the main window at the /update progress page
// and brings it forward. Used both when the user clicks an enforced-update
// menu entry (Branch 2) and when the daemon flips Installing to true on
// its own (Branch 3, force install).
func (u *trayUpdater) openProgressWindow(version string) {
if u.window == nil {
return
}
url := "/#/update"
if version != "" {
url += "?version=" + version
}
u.window.SetURL(url)
u.window.Show()
u.window.Focus()
}