workspaces добавлено
This commit is contained in:
134
tg_bot/screens/workspace_menu.go
Normal file
134
tg_bot/screens/workspace_menu.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package screens
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/NicoNex/echotron/v3"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_bot/bot"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_bot/screens/ui"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type WorkspaceMenu struct {
|
||||
CurrentPage int
|
||||
BackState bot.State
|
||||
}
|
||||
|
||||
const msgWorkspaceEmpty = `
|
||||
У вас пока нет рабочих пространств.
|
||||
`
|
||||
|
||||
const workspacesPerPage = 6
|
||||
|
||||
func (s *WorkspaceMenu) Enter(b *bot.Bot, mode bot.RenderMode) {
|
||||
workspaces, err := b.Backend.GetWorkspaces(context.Background(), b.Session.JWT)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to get workspaces for workspace menu")
|
||||
b.SendNew("❌ Не удалось загрузить рабочие пространства", Keyboard(Row(Button("← Назад", "back"))))
|
||||
return
|
||||
}
|
||||
|
||||
if len(workspaces) == 0 {
|
||||
kb := Keyboard(Row(Button("← Назад", "back")))
|
||||
b.Render(msgWorkspaceEmpty, kb, mode)
|
||||
return
|
||||
}
|
||||
|
||||
var allButtons []echotron.InlineKeyboardButton
|
||||
for _, ws := range workspaces {
|
||||
label := ws.Name
|
||||
if ws.ID == b.Session.WorkspaceID {
|
||||
label = "● " + label
|
||||
}
|
||||
allButtons = append(allButtons, Button(label, fmt.Sprintf("workspace_select:%s", ws.ID)))
|
||||
}
|
||||
|
||||
elementRows := ui.BuildElementRows(ui.ElementLayoutConfig{
|
||||
CurrentPage: s.CurrentPage,
|
||||
ItemsPerPage: workspacesPerPage,
|
||||
ItemsPerRow: 2,
|
||||
}, allButtons)
|
||||
|
||||
var buttons [][]echotron.InlineKeyboardButton
|
||||
buttons = append(buttons, elementRows...)
|
||||
|
||||
if navRow := ui.BuildNavigationRow(ui.PaginationConfig{
|
||||
CurrentPage: s.CurrentPage,
|
||||
TotalItems: len(allButtons),
|
||||
ItemsPerPage: workspacesPerPage,
|
||||
}); navRow != nil {
|
||||
buttons = append(buttons, navRow)
|
||||
}
|
||||
|
||||
buttons = append(buttons, Row(Button("← Назад", "back")))
|
||||
|
||||
totalPages := ui.CalculatePages(len(allButtons), workspacesPerPage)
|
||||
text := fmt.Sprintf(`<b>Рабочие пространства</b>%s
|
||||
|
||||
Выберите текущее рабочее пространство.`, ui.FormatPageInfo(s.CurrentPage, totalPages))
|
||||
|
||||
b.Render(text, Keyboard(buttons...), mode)
|
||||
}
|
||||
|
||||
func (s *WorkspaceMenu) HandleCallback(b *bot.Bot, u *echotron.Update) {
|
||||
if u.CallbackQuery == nil || u.CallbackQuery.Data == "" {
|
||||
return
|
||||
}
|
||||
|
||||
data := u.CallbackQuery.Data
|
||||
|
||||
switch {
|
||||
case data == "prev":
|
||||
if s.CurrentPage > 0 {
|
||||
s.CurrentPage--
|
||||
}
|
||||
s.Enter(b, bot.EditMessage)
|
||||
|
||||
case data == "next":
|
||||
s.CurrentPage++
|
||||
s.Enter(b, bot.EditMessage)
|
||||
|
||||
case data == "back":
|
||||
if s.BackState != nil {
|
||||
b.SetState(s.BackState, bot.EditMessage)
|
||||
return
|
||||
}
|
||||
b.SetState(&MainMenu{}, bot.EditMessage)
|
||||
|
||||
case strings.HasPrefix(data, "workspace_select:"):
|
||||
parts := strings.Split(data, ":")
|
||||
if len(parts) != 2 {
|
||||
return
|
||||
}
|
||||
workspaceID := parts[1]
|
||||
workspaces, err := b.Backend.GetWorkspaces(context.Background(), b.Session.JWT)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to get workspaces for selection")
|
||||
b.SendNew("❌ Не удалось загрузить рабочие пространства", Keyboard(Row(Button("← Назад", "back"))))
|
||||
return
|
||||
}
|
||||
for _, ws := range workspaces {
|
||||
if ws.ID == workspaceID {
|
||||
b.Session.WorkspaceID = ws.ID
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if s.BackState != nil {
|
||||
b.SetState(s.BackState, bot.EditMessage)
|
||||
return
|
||||
}
|
||||
b.SetState(&MainMenu{}, bot.EditMessage)
|
||||
|
||||
default:
|
||||
s.Enter(b, bot.NewMessage)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *WorkspaceMenu) HandleMessage(_ *bot.Bot, _ *echotron.Update) {}
|
||||
|
||||
func (s *WorkspaceMenu) Handle(_ *bot.Bot, _ *echotron.Update) {}
|
||||
|
||||
func (s *WorkspaceMenu) Exit() {}
|
||||
Reference in New Issue
Block a user