refactor(tui): move shared components (help bar, issue list, modals, etc.) into components folder. Also added kanban board view.
This commit is contained in:
231
pkg/tui/views/kanban/model.go
Normal file
231
pkg/tui/views/kanban/model.go
Normal file
@@ -0,0 +1,231 @@
|
||||
package kanban
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/LazyBachelor/LazyPM/internal/app"
|
||||
"github.com/LazyBachelor/LazyPM/internal/models"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/tui/components"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/tui/issues"
|
||||
"github.com/charmbracelet/bubbles/textarea"
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
type (
|
||||
Header = components.Header
|
||||
IssueList = components.IssueList
|
||||
IssueDetail = components.IssueDetail
|
||||
ListIssue = components.ListIssue
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
header Header
|
||||
todoList IssueList
|
||||
inProgList IssueList
|
||||
doneList IssueList
|
||||
issueDetail IssueDetail
|
||||
helpBar components.HelpBar
|
||||
keyMap KanbanKeyMap
|
||||
app *app.App
|
||||
width int
|
||||
height int
|
||||
|
||||
focusedColumn int // 0 = To Do, 1 = In Progress, 2 = Done
|
||||
focusOnDetail bool // true when detail pane is focused
|
||||
|
||||
editingTitle bool // true while we are editing a title
|
||||
titleInput textinput.Model
|
||||
editingIssueID string
|
||||
|
||||
editingDescription bool // true while editing a description
|
||||
descriptionInput textarea.Model
|
||||
editingDescIssueID string
|
||||
creatingIssue bool // true while creating a new issue
|
||||
createTitleInput textinput.Model
|
||||
|
||||
confirmingDelete bool // true while confirming a delete
|
||||
deleteConfirmID string
|
||||
deleteConfirmIndex int
|
||||
|
||||
choosingStatus bool // true while choosing a status
|
||||
statusIssueID string
|
||||
choosingPriority bool // true while choosing a priority
|
||||
priorityIssueID string
|
||||
choosingType bool // true while choosing a type
|
||||
typeIssueID string
|
||||
feedbackChan chan models.ValidationFeedback
|
||||
quitChan chan bool
|
||||
currentFeedback models.ValidationFeedback
|
||||
showComplete bool
|
||||
}
|
||||
|
||||
func NewDashboard(app *app.App, feedbackChan chan models.ValidationFeedback, quitChan chan bool) *Model {
|
||||
m := &Model{
|
||||
header: components.NewHeader("Kanban Board"),
|
||||
keyMap: defaultKanbanKeyMap,
|
||||
app: app,
|
||||
width: 80,
|
||||
height: 24,
|
||||
focusedColumn: 0,
|
||||
focusOnDetail: false,
|
||||
feedbackChan: feedbackChan,
|
||||
quitChan: quitChan,
|
||||
}
|
||||
|
||||
allIssues, _ := app.Issues.SearchIssues(context.Background(), "", models.IssueFilter{})
|
||||
todoIssues := components.StatusOnly(allIssues, models.StatusOpen)
|
||||
inProgIssues := components.StatusOnly(allIssues, models.StatusInProgress)
|
||||
doneIssues := components.StatusOnly(allIssues, models.StatusClosed)
|
||||
|
||||
m.todoList = components.NewIssueListFromIssues(app, todoIssues, 0, 0)
|
||||
m.inProgList = components.NewIssueListFromIssues(app, inProgIssues, 0, 0)
|
||||
m.doneList = components.NewIssueListFromIssues(app, doneIssues, 0, 0)
|
||||
m.issueDetail = components.NewIssueDetail()
|
||||
m.helpBar = components.NewHelpBar(components.ViewKanban)
|
||||
|
||||
inputs := components.NewIssueInputs()
|
||||
m.titleInput = inputs.Title
|
||||
m.createTitleInput = inputs.CreateTitle
|
||||
m.descriptionInput = inputs.Description
|
||||
|
||||
if selected := m.todoList.SelectedItem(); selected.ID != "" {
|
||||
m.issueDetail.SetIssue(selected.Issue)
|
||||
} else if selected := m.inProgList.SelectedItem(); selected.ID != "" {
|
||||
m.issueDetail.SetIssue(selected.Issue)
|
||||
} else if selected := m.doneList.SelectedItem(); selected.ID != "" {
|
||||
m.issueDetail.SetIssue(selected.Issue)
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *Model) startEditTitle(selected ListIssue) {
|
||||
m.editingTitle = true
|
||||
m.editingIssueID = selected.ID
|
||||
m.titleInput.SetValue(selected.Issue.Title)
|
||||
m.titleInput.CursorEnd()
|
||||
}
|
||||
|
||||
func (m *Model) startEditDescription(selected ListIssue) {
|
||||
m.editingDescription = true
|
||||
m.editingDescIssueID = selected.ID
|
||||
m.descriptionInput.SetValue(selected.Issue.Description)
|
||||
m.descriptionInput.CursorEnd()
|
||||
}
|
||||
|
||||
func (m *Model) startCreateIssue() {
|
||||
m.creatingIssue = true
|
||||
m.createTitleInput.SetValue("")
|
||||
m.createTitleInput.Reset()
|
||||
}
|
||||
|
||||
func (m *Model) startConfirmDelete(issueID string, index int) {
|
||||
m.confirmingDelete = true
|
||||
m.deleteConfirmID = issueID
|
||||
m.deleteConfirmIndex = index
|
||||
}
|
||||
|
||||
func (m *Model) startChooseStatus(selected ListIssue) {
|
||||
m.choosingStatus = true
|
||||
m.statusIssueID = selected.ID
|
||||
}
|
||||
|
||||
func (m *Model) startChoosePriority(selected ListIssue) {
|
||||
m.choosingPriority = true
|
||||
m.priorityIssueID = selected.ID
|
||||
}
|
||||
|
||||
func (m *Model) startChooseType(selected ListIssue) {
|
||||
m.choosingType = true
|
||||
m.typeIssueID = selected.ID
|
||||
}
|
||||
|
||||
func (m *Model) Init() tea.Cmd {
|
||||
return components.ListenForValidation(m.feedbackChan)
|
||||
}
|
||||
|
||||
// IsInModal returns true when a modal (edit, create, delete confirm, choose status/priority/type) is active.
|
||||
func (m *Model) IsInModal() bool {
|
||||
return m.editingTitle || m.creatingIssue || m.editingDescription ||
|
||||
m.choosingStatus || m.choosingPriority || m.confirmingDelete || m.choosingType
|
||||
}
|
||||
|
||||
func (m *Model) IsFocusedOnList() bool {
|
||||
return !m.focusOnDetail
|
||||
}
|
||||
|
||||
func (m *Model) IsFocusedOnDetail() bool {
|
||||
return m.focusOnDetail
|
||||
}
|
||||
|
||||
func (m *Model) FocusList() {
|
||||
m.focusOnDetail = false
|
||||
m.issueDetail.SetFocused(false)
|
||||
}
|
||||
|
||||
func (m *Model) FocusDetail() {
|
||||
m.focusOnDetail = true
|
||||
m.issueDetail.SetFocused(true)
|
||||
}
|
||||
|
||||
func (m *Model) ToggleFocus() {
|
||||
if m.IsFocusedOnList() {
|
||||
m.FocusDetail()
|
||||
} else {
|
||||
m.FocusList()
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Model) FocusedIssueList() *IssueList {
|
||||
switch m.focusedColumn {
|
||||
case 0:
|
||||
return &m.todoList
|
||||
case 1:
|
||||
return &m.inProgList
|
||||
case 2:
|
||||
return &m.doneList
|
||||
default:
|
||||
return &m.todoList
|
||||
}
|
||||
}
|
||||
|
||||
// updateDetailFromSelection updates the detail pane based on the currently
|
||||
// focused column's selected issue.
|
||||
func (m *Model) updateDetailFromSelection() {
|
||||
selected := m.FocusedIssueList().SelectedItem()
|
||||
if selected.ID != "" {
|
||||
m.issueDetail.SetIssue(selected.Issue)
|
||||
}
|
||||
}
|
||||
|
||||
// statusForColumn maps a board column index to a Status.
|
||||
func statusForColumn(col int) models.Status {
|
||||
switch col {
|
||||
case 0:
|
||||
return models.StatusOpen
|
||||
case 1:
|
||||
return models.StatusInProgress
|
||||
case 2:
|
||||
return models.StatusClosed
|
||||
default:
|
||||
return models.StatusOpen
|
||||
}
|
||||
}
|
||||
|
||||
// moveIssue moves the currently selected issue in the focused column horizontally
|
||||
// to an adjacent column by updating its status.
|
||||
func (m *Model) moveIssue(delta int) tea.Cmd {
|
||||
fl := m.FocusedIssueList()
|
||||
selected := fl.SelectedItem()
|
||||
if selected.ID == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
newCol := m.focusedColumn + delta
|
||||
if newCol < 0 || newCol > 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
newStatus := statusForColumn(newCol)
|
||||
return issues.UpdateIssueStatusCmd(m.app, selected.ID, string(newStatus))
|
||||
}
|
||||
Reference in New Issue
Block a user