repos / pico

pico services mono repo
git clone https://github.com/picosh/pico.git

pico / pkg / tui
Antonio Mika  ·  2025-03-12

chat.go

 1package tui
 2
 3import (
 4	"git.sr.ht/~rockorager/vaxis"
 5	"git.sr.ht/~rockorager/vaxis/vxfw"
 6	"git.sr.ht/~rockorager/vaxis/vxfw/button"
 7	"git.sr.ht/~rockorager/vaxis/vxfw/richtext"
 8	"git.sr.ht/~rockorager/vaxis/vxfw/text"
 9)
10
11type ChatPage struct {
12	shared *SharedModel
13	btn    *button.Button
14}
15
16func NewChatPage(shrd *SharedModel) *ChatPage {
17	btn := button.New("chat!", func() (vxfw.Command, error) { return nil, nil })
18	btn.Style = button.StyleSet{
19		Default: vaxis.Style{Background: oj, Foreground: black},
20	}
21	return &ChatPage{shared: shrd, btn: btn}
22}
23
24func (m *ChatPage) Footer() []Shortcut {
25	short := []Shortcut{
26		{Shortcut: "enter", Text: "chat"},
27	}
28	return short
29}
30
31func (m *ChatPage) hasAccess() bool {
32	if m.shared.PlusFeatureFlag != nil && m.shared.PlusFeatureFlag.IsValid() {
33		return true
34	}
35
36	if m.shared.BouncerFeatureFlag != nil && m.shared.BouncerFeatureFlag.IsValid() {
37		return true
38	}
39
40	return false
41}
42
43func (m *ChatPage) HandleEvent(ev vaxis.Event, phase vxfw.EventPhase) (vxfw.Command, error) {
44	switch msg := ev.(type) {
45	case PageIn:
46		return vxfw.FocusWidgetCmd(m), nil
47	case vaxis.Key:
48		if msg.Matches(vaxis.KeyEnter) {
49			_ = m.shared.App.Suspend()
50			loadChat(m.shared)
51			_ = m.shared.App.Resume()
52			return vxfw.QuitCmd{}, nil
53		}
54	}
55
56	return nil, nil
57}
58
59func (m *ChatPage) Draw(ctx vxfw.DrawContext) (vxfw.Surface, error) {
60	segs := []vaxis.Segment{
61		{Text: "We provide a managed IRC bouncer for pico+ users.  When you click the button we will open our TUI chat with your user authenticated automatically.\n\n"},
62		{Text: "If you haven't configured your pico+ account with our IRC bouncer, the guide is here:\n\n  "},
63		{Text: "https://pico.sh/bouncer", Style: vaxis.Style{Hyperlink: "https://pico.sh/bouncer"}},
64		{Text: "\n\nIf you want to quickly chat with us on IRC without pico+, go to the web chat:\n\n  "},
65		{Text: "https://web.libera.chat/gamja?autojoin=#pico.sh", Style: vaxis.Style{Hyperlink: "https://web.libera.chat/gamja?autojoin=#pico.sh"}},
66	}
67	txt, _ := richtext.New(segs).Draw(ctx)
68
69	surfs := []vxfw.Surface{txt}
70	if m.hasAccess() {
71		btnSurf, _ := m.btn.Draw(vxfw.DrawContext{
72			Characters: ctx.Characters,
73			Max:        vxfw.Size{Width: 7, Height: 1},
74		})
75		surfs = append(surfs, btnSurf)
76	} else {
77		t := text.New("Our IRC Bouncer is only available to pico+ users.")
78		t.Style = vaxis.Style{Foreground: red}
79		ss, _ := t.Draw(ctx)
80		surfs = append(surfs, ss)
81	}
82	stack := NewGroupStack(surfs)
83	stack.Gap = 1
84
85	brd := NewBorder(stack)
86	brd.Label = "irc chat"
87	surf, _ := brd.Draw(ctx)
88
89	root := vxfw.NewSurface(ctx.Max.Width, ctx.Max.Height, m)
90	root.AddChild(0, 0, surf)
91	return root, nil
92}