6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-07-05 09:31:25 +00:00
prologic-saltyim/internal/pwa/components/appupdate.go

57 lines
1.4 KiB
Go
Raw Normal View History

2022-03-23 04:06:56 +00:00
package components
import (
"github.com/maxence-charriere/go-app/v9/pkg/app"
"github.com/mlctrez/goapp-mdc/pkg/banner"
"github.com/mlctrez/goapp-mdc/pkg/button"
2022-03-23 14:09:38 +00:00
"github.com/mlctrez/goapp-mdc/pkg/icon"
2022-03-23 04:06:56 +00:00
)
// AppUpdateBanner prompts the user to update the application when the version changes
type AppUpdateBanner struct {
app.Compo
bnr *banner.Banner
}
func (d *AppUpdateBanner) Render() app.UI {
if d.bnr == nil {
d.bnr = &banner.Banner{
Id: "appUpdateBanner", Fixed: true, Centered: true,
Text: "A new version is available, would you like to install?",
}
d.bnr.Buttons = d.bannerButtons()
}
return d.bnr
}
func (d *AppUpdateBanner) bannerButtons() []app.UI {
2022-03-23 14:09:38 +00:00
primary := &button.Button{
Id: "updateBannerYes", Label: "yes",
Icon: string(icon.MIUpdate),
2022-03-23 04:06:56 +00:00
Banner: true, BannerAction: "primary"}
2022-03-23 14:09:38 +00:00
secondary := &button.Button{
Id: "updateBannerNo", Label: "later",
Icon: string(icon.MIWatchLater),
2022-03-23 04:06:56 +00:00
Banner: true, BannerAction: "secondary"}
return []app.UI{primary, secondary}
}
func (d *AppUpdateBanner) onBannerClose(ctx app.Context, reason string) {
switch reason {
case "primary": // Yes button
ctx.Reload()
case "secondary": // Later button
// This could SetState for a future time to ask
}
}
func (d *AppUpdateBanner) OnMount(ctx app.Context) {
d.bnr.OnClose(ctx, d.onBannerClose)
}
func (d *AppUpdateBanner) OnAppUpdate(ctx app.Context) {
if ctx.AppUpdateAvailable() {
d.bnr.ActionOpen(ctx)
}
}