Files
glance/internal/widget/twitch-channels.go
fawn 0681a04607 Add custom sorting to the twitch channels widget
this allows the channels to be sorted as defined in the config while
still keeping the live channels on the top. the wording could probably
be improved, "custom" is too broad but i'm not sure what else to call
it.
2024-05-18 03:07:34 +03:00

53 lines
1.2 KiB
Go

package widget
import (
"context"
"html/template"
"time"
"github.com/glanceapp/glance/internal/assets"
"github.com/glanceapp/glance/internal/feed"
)
type TwitchChannels struct {
widgetBase `yaml:",inline"`
ChannelsRequest []string `yaml:"channels"`
Channels []feed.TwitchChannel `yaml:"-"`
CollapseAfter int `yaml:"collapse-after"`
SortBy string `yaml:"sort-by"`
}
func (widget *TwitchChannels) Initialize() error {
widget.withTitle("Twitch Channels").withCacheDuration(time.Minute * 10)
if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
widget.CollapseAfter = 5
}
if widget.SortBy != "viewers" && widget.SortBy != "custom" {
widget.SortBy = "viewers"
}
return nil
}
func (widget *TwitchChannels) Update(ctx context.Context) {
channels, err := feed.FetchChannelsFromTwitch(widget.ChannelsRequest)
if !widget.canContinueUpdateAfterHandlingErr(err) {
return
}
if widget.SortBy == "viewers" {
channels.SortByViewers()
} else if widget.SortBy == "custom" {
channels.SortByLive()
}
widget.Channels = channels
}
func (widget *TwitchChannels) Render() template.HTML {
return widget.render(widget, assets.TwitchChannelsTemplate)
}