Skip to content

Commit

Permalink
feat: open selected tasks in the web browser (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
prgres authored May 20, 2024
1 parent 716412c commit 57a9d7b
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 59 deletions.
123 changes: 67 additions & 56 deletions ui/components/table-tasks/taskstable.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@ type Model struct {
log *log.Logger
ctx *context.UserContext
ComponentId common.ComponentId
requiredColsKeys []string
columns []table.Column
requiredCols []table.Column
columns []Column
columnsVisible []Column
columnsHidden []Column
table table.Model
size common.Size
SelectedTaskIndex int
autoColumns bool
Focused bool
Hidden bool
ifBorders bool
}

type Column struct {
table.Column
Hidden bool
}

func (m Model) GetTasks() []clickup.Task {
return m.tasks
}
Expand Down Expand Up @@ -110,30 +114,42 @@ func (m Model) KeyMap() help.KeyMap {
}

func InitialModel(ctx *context.UserContext, logger *log.Logger) Model {
columns := []table.Column{}

requiredCols := []table.Column{
table.NewFlexColumn("name", "Name", 70),
table.NewFlexColumn("status", "Status", 5).
WithStyle(
lipgloss.NewStyle().Align(lipgloss.Center),
),
// TODO: do better
columnsHidden := []Column{
{
Column: table.NewFlexColumn("url", "url", 0),
Hidden: true,
},
}
columnsVisible := []Column{
{
Column: table.NewFlexColumn("name", "Name", 70),
Hidden: false,
},
{
Column: table.NewFlexColumn("status", "Status", 5).
WithStyle(lipgloss.NewStyle().Align(lipgloss.Center)),
Hidden: false,
},
}
columns := append(columnsVisible, columnsHidden...)

tableColumns := make([]table.Column, len(columnsVisible))
for i := range columnsVisible {
tableColumns[i] = columns[i].Column
}
requiredColsKeys := []string{"name", "status"}

size := common.Size{
Width: 0,
Height: 0,
}

columns = append(columns, requiredCols...)

tableKeyMap := table.DefaultKeyMap()
tableKeyMap.RowSelectToggle = key.NewBinding(
key.WithKeys(" "),
)

t := table.New(columns).
t := table.New(tableColumns).
WithKeyMap(tableKeyMap).
WithTargetWidth(size.Width).
SelectableRows(true).
Expand All @@ -152,26 +168,33 @@ func InitialModel(ctx *context.UserContext, logger *log.Logger) Model {
log := logger.WithPrefix(logger.GetPrefix() + "/" + ComponentId)

return Model{
ComponentId: ComponentId,
ctx: ctx,
table: t,
columns: columns,
requiredCols: requiredCols,
requiredColsKeys: requiredColsKeys,
tasks: []clickup.Task{},
autoColumns: false,
size: size,
Focused: false,
Hidden: false,
log: log,
ifBorders: true,
ComponentId: ComponentId,
ctx: ctx,
table: t,
columns: columns,
columnsVisible: columnsVisible,
columnsHidden: columnsHidden,
tasks: []clickup.Task{},
size: size,
Focused: false,
Hidden: false,
log: log,
ifBorders: true,
}
}

func (m *Model) GetVisibleColumnsKey() []string {
return m.getColumnsKey(m.columnsVisible)
}

func (m *Model) GetColumnsKey() []string {
r := make([]string, len(m.columns))
for i, c := range m.columns {
r[i] = c.Key()
return m.getColumnsKey(m.columns)
}

func (m *Model) getColumnsKey(cols []Column) []string {
r := make([]string, len(cols))
for i := range cols {
r[i] = cols[i].Key()
}

return r
Expand Down Expand Up @@ -212,6 +235,18 @@ func (m Model) GetHighlightedTask() *clickup.Task {
return &m.tasks[index]
}

func (m Model) GetSelectedTasks() []*clickup.Task {
rows := m.table.SelectedRows()
tasks := make([]*clickup.Task, len(rows))

for i := range rows {
task := rowToTask(rows[i], m.GetColumnsKey())
tasks[i] = &task
}

return tasks
}

func (m Model) TotalRows() int {
return m.table.TotalRows()
}
Expand Down Expand Up @@ -250,24 +285,6 @@ func (m Model) TabChanged(tabId string) (Model, tea.Cmd) {

m.log.Infof("Received TabChangedMsg: %s", tabId)

columns := []table.Column{}
columns = append(columns, m.requiredCols...)

// if m.autoColumns {
// tab := viewtabs.Tab(msg)
// for _, field := range view.Columns.Fields {
// if field.Field == "name" || field.Field == "status" { // TODO: check if in requiredCols
// continue
// }
// columns = append(columns, table.Column{
// Title: field.Field,
// Width: 30,
// })
// }
// }

m.log.Infof("Columns: %d", len(columns))
m.setColumns(columns)
m.SetTasks(m.tasks)

if len(m.tasks) != 0 { // TODO: store tasks list in var
Expand All @@ -280,12 +297,6 @@ func (m Model) TabChanged(tabId string) (Model, tea.Cmd) {
return m, tea.Batch(cmds...)
}

func (m *Model) setColumns(columns []table.Column) {
m.columns = columns
m.table = m.table.
WithColumns(m.columns)
}

func (m *Model) SetTasks(tasks []clickup.Task) {
m.tasks = tasks
items := taskListToRows(tasks, m.GetColumnsKey())
Expand Down
36 changes: 35 additions & 1 deletion ui/components/table-tasks/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,40 @@ func taskToRow(task clickup.Task, columns []string) table.Row {
values[column] = task.Status.Status
case "name":
values[column] = task.Name
case "url":
values[column] = task.Url
// After migration from charm to evertras/bubble-table I temporary removed all columns
// except "status" and "name" since they are not supported yet. See autoColumns feature
// case "assignee":
// values = append(values, task.GetAssignees())
// case "list":
// values = append(values, task.List.String())
// case "tags":
// values = append(values, task.GetTags())
// case "folder":
// values = append(values, task.Folder.String())
// case "space":
// values = append(values, task.Space.Id)
// case "id":
// values = append(values, task.Id)
}
}

return table.NewRow(table.RowData(values))
}

func rowToTask(row table.Row, columns []string) clickup.Task {
var task clickup.Task
data := row.Data

for _, column := range columns {
switch column {
case "status":
task.Status.Status = data[column].(string)
case "name":
task.Name = data[column].(string)
case "url":
task.Url = data[column].(string)
// After migration from charm to evertras/bubble-table I temporary removed all columns
// except "status" and "name" since they are not supported yet. See autoColumns feature
// case "assignee":
Expand All @@ -40,5 +74,5 @@ func taskToRow(task clickup.Task, columns []string) table.Row {
}
}

return table.NewRow(table.RowData(values))
return task
}
20 changes: 18 additions & 2 deletions ui/widgets/tasks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,17 @@ func InitialModel(ctx *context.UserContext, logger *log.Logger) Model {
}

type KeyMap struct {
OpenTicketInWebBrowser key.Binding
ToggleSidebar key.Binding
OpenTicketInWebBrowserBatch key.Binding
OpenTicketInWebBrowser key.Binding
ToggleSidebar key.Binding
}

func DefaultKeyMap() KeyMap {
return KeyMap{
OpenTicketInWebBrowserBatch: key.NewBinding(
key.WithKeys("U"),
key.WithHelp("U", "batch open in web browser"),
),
OpenTicketInWebBrowser: key.NewBinding(
key.WithKeys("u"),
key.WithHelp("u", "open in web browser"),
Expand Down Expand Up @@ -131,11 +136,22 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keyMap.OpenTicketInWebBrowserBatch):
tasks := m.componenetTasksTable.GetSelectedTasks()
for _, task := range tasks {
m.log.Debug("Opening task in the web browser", "url", task.Url)
if err := common.OpenUrlInWebBrowser(task.Url); err != nil {
m.log.Fatal(err)
}
}

case key.Matches(msg, m.keyMap.OpenTicketInWebBrowser):
task := m.componenetTasksTable.GetHighlightedTask()
m.log.Debug("Opening task in the web browser", "url", task.Url)
if err := common.OpenUrlInWebBrowser(task.Url); err != nil {
m.log.Fatal(err)
}

case key.Matches(msg, m.keyMap.ToggleSidebar):
m.componenetTasksSidebar = m.componenetTasksSidebar.SetHidden(!m.componenetTasksSidebar.GetHidden())
}
Expand Down

0 comments on commit 57a9d7b

Please sign in to comment.