Files
netbird/client/ui/services/update.go
Zoltan Papp 9d8eb76746 [client/ui] Replace update event fan-out with typed UpdateState API
The auto-update feature was driven by two narrow Wails events
(netbird:update:available and :progress) plus a SystemEvent-metadata
iteration on the React side. Both surfaces had to know the daemon
metadata schema (new_version_available, enforced, progress_window),
and the frontend had no pull endpoint to seed its state on mount.

Extract the state machine into a new client/ui/updater package, mirroring
how i18n and preferences are split between domain logic and a thin
services facade. The package owns the State type, the metadata-key
parsing, the mutex-guarded Holder, and the single netbird:update:state
event. services.Update keeps the daemon RPCs (Trigger, GetInstallerResult,
Quit) and gains GetState as a Wails pull endpoint.

Tray-side update behaviour moves out of tray.go into a dedicated
trayUpdater (tray_update.go): owns its menu item, OS notification,
click handler, and the /update window opener triggered by the
daemon's progress_window:show. tray.go drops three callbacks and four
fields, and reads hasUpdate through the updater.

Frontend ClientVersionContext now seeds from Update.GetState() and
subscribes to netbird:update:state; the status.events iteration and
metadata-key string literals are gone. UpdateAvailableBanner renders
only for the enforced && !installing branch and labels its action
"Install now"; UpdateVersionCard splits the install vs. download
branches by Enforced so the disabled flow routes to GitHub.
2026-05-15 13:31:17 +02:00

84 lines
2.3 KiB
Go

//go:build !android && !ios && !freebsd && !js
package services
import (
"context"
"time"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/netbirdio/netbird/client/proto"
"github.com/netbirdio/netbird/client/ui/updater"
)
// UpdateResult mirrors TriggerUpdateResponse: Success false carries an error
// message in ErrorMsg.
type UpdateResult struct {
Success bool `json:"success"`
ErrorMsg string `json:"errorMsg"`
}
// Update is the Wails-bound facade over the daemon's update RPCs and the
// updater.Holder cached state. The state machine, metadata schema, and
// push event live in client/ui/updater — this file exists only to give
// the binding generator a service type with the context.Context-first
// signatures it expects.
type Update struct {
conn DaemonConn
holder *updater.Holder
}
func NewUpdate(conn DaemonConn, holder *updater.Holder) *Update {
return &Update{conn: conn, holder: holder}
}
// GetState returns the latest update.State snapshot. The frontend calls
// this once on mount, then subscribes to updater.EventStateChanged for
// live updates.
func (s *Update) GetState() updater.State {
return s.holder.Get()
}
// Quit asks the host application to exit. The /update page calls this once
// the daemon-side installer has reported success, mirroring the legacy
// Fyne UI's app.Quit() in showInstallerResult. Schedules the actual exit
// off the calling goroutine so the JS-side caller's response can return
// before the runtime tears down.
func (s *Update) Quit() {
go func() {
time.Sleep(100 * time.Millisecond)
application.Get().Quit()
}()
}
func (s *Update) Trigger(ctx context.Context) (UpdateResult, error) {
cli, err := s.conn.Client()
if err != nil {
return UpdateResult{}, err
}
resp, err := cli.TriggerUpdate(ctx, &proto.TriggerUpdateRequest{})
if err != nil {
return UpdateResult{}, err
}
return UpdateResult{
Success: resp.GetSuccess(),
ErrorMsg: resp.GetErrorMsg(),
}, nil
}
func (s *Update) GetInstallerResult(ctx context.Context) (UpdateResult, error) {
cli, err := s.conn.Client()
if err != nil {
return UpdateResult{}, err
}
resp, err := cli.GetInstallerResult(ctx, &proto.InstallerResultRequest{})
if err != nil {
return UpdateResult{}, err
}
return UpdateResult{
Success: resp.GetSuccess(),
ErrorMsg: resp.GetErrorMsg(),
}, nil
}