Files
MujicaUI/error_message.go
T

73 lines
2.3 KiB
Go

package mujica
import (
"fmt"
"log/slog"
"math"
"time"
"github.com/egoist/mygo/ui"
"mujicaui/icons"
"mujicaui/theme"
)
// ErrorMessageOptions configure an ErrorMessage.
type ErrorMessageOptions struct {
// Title sums the failure up; empty uses the built-in "Something went wrong".
Title string
// RetryAt is when a rate limit lifts; until then retry waits with a countdown.
RetryAt time.Time
}
// ErrorMessageResult is the error's element and a retry the user asked for.
type ErrorMessageResult struct {
Element *ui.Element
retried bool
}
// Retried reports a press of retry.
func (r ErrorMessageResult) Retried() bool { return r.retried }
// ErrorMessage shows a failed reply with the original error text, selectable, and retry.
func ErrorMessage(c *ui.Context, text string, opts ErrorMessageOptions) ErrorMessageResult {
if text == "" {
panic("mujica: ErrorMessage needs the error text")
}
k := Tokens(c)
u := Density(c).Unit()
title := opts.Title
if title == "" {
title = msg(c, "error_message.title")
}
wait := opts.RetryAt.Sub(c.Now())
var r ErrorMessageResult
r.Element = ui.Column(c).Gap(u*2).Padding(u*3, u*4).MaxWidth(560).Radius(theme.CardRadius).
Background(k.Surface).Border(theme.BorderWidth, k.Danger).Role(ui.RoleGroup).Label(title)
disabled := r.Element.IsDisabled()
r.Element.Children(func() {
ui.Row(c).Gap(u * 2).AlignItems(ui.Center).Children(func() {
ui.Icon(c, icons.Must("circle-alert")).Size(16, 16).Shrink(0).TextColor(k.Danger)
ui.Text(c, title).FontWeight(600).TextColor(k.Danger)
})
ui.Text(c, text).Selectable().Font(theme.MonoFont).FontSize(FontSize(c, theme.CaptionSize)).TextColor(k.Text)
ui.Row(c).Gap(u * 3).AlignItems(ui.Center).Children(func() {
label := msg(c, "error_message.retry")
if wait > 0 {
secs := int(math.Ceil(wait.Seconds()))
label = fmt.Sprintf(msg(c, "error_message.wait"), secs)
// Wake when the shown second changes.
c.After(wait - time.Duration(secs-1)*time.Second)
}
if Button(c, label, ButtonOptions{Variant: Secondary, Icon: icons.Must("refresh-cw"), Disabled: wait > 0}).Clicked() && !disabled {
r.retried = true
slog.Debug("mujica: error_message retry")
}
if wait > 0 {
ui.Text(c, msg(c, "error_message.limited")).FontSize(FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted)
}
})
})
return r
}