Eric Bower
·
17 Dec 24
chat.go
1package chat
2
3import (
4 tea "github.com/charmbracelet/bubbletea"
5 "github.com/picosh/pico/tui/common"
6 "github.com/picosh/pico/tui/pages"
7)
8
9var maxWidth = 55
10
11type focus int
12
13const (
14 focusNone = iota
15 focusChat
16)
17
18type Model struct {
19 shared *common.SharedModel
20 focus focus
21}
22
23func NewModel(shrd *common.SharedModel) Model {
24 return Model{
25 shared: shrd,
26 focus: focusChat,
27 }
28}
29
30func (m Model) Init() tea.Cmd {
31 return nil
32}
33
34func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
35 switch msg := msg.(type) {
36 case tea.KeyMsg:
37 switch msg.String() {
38 case "q", "esc":
39 return m, pages.Navigate(pages.MenuPage)
40 case "tab":
41 if m.focus == focusNone {
42 m.focus = focusChat
43 } else {
44 m.focus = focusNone
45 }
46 case "enter":
47 if m.focus == focusChat {
48 return m, m.gotoChat()
49 }
50 }
51 }
52 return m, nil
53}
54
55func (m Model) View() string {
56 return m.analyticsView()
57}
58
59func (m Model) analyticsView() string {
60 banner := `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.
61
62If you haven't configured your pico+ account with our IRC bouncer, the guide is here:
63
64 https://pico.sh/bouncer
65
66If you want to quickly chat with us on IRC without pico+, go to the web chat:
67
68 https://web.libera.chat/gamja?autojoin=#pico.sh`
69
70 str := ""
71 hasPlus := m.shared.PlusFeatureFlag != nil
72 if hasPlus {
73 hasFocus := m.focus == focusChat
74 str += banner + "\n\nLet's Chat! " + common.OKButtonView(m.shared.Styles, hasFocus, true)
75 } else {
76 str += banner + "\n\n" + m.shared.Styles.Error.SetString("Our IRC Bouncer is only available to pico+ users.").String()
77 }
78
79 return m.shared.Styles.RoundedBorder.Width(maxWidth).SetString(str).String()
80}
81
82func (m Model) gotoChat() tea.Cmd {
83 return LoadChat(m.shared)
84}