Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor list-item to hold data and make list components to hold clickup obj #98

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions ui/components/folders-list/folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ type Model struct {
ctx *context.UserContext
log *log.Logger
ComponentId common.ComponentId
SelectedSpace string
SelectedFolder string
SelectedFolder clickup.Folder
folders []clickup.Folder
}

Expand All @@ -44,8 +43,7 @@ func InitialModel(ctx *context.UserContext, logger *log.Logger) Model {
ComponentId: ComponentId,
list: l,
ctx: ctx,
SelectedFolder: "",
SelectedSpace: ctx.Config.DefaultSpace,
SelectedFolder: clickup.Folder{},
folders: []clickup.Folder{},
log: log,
}
Expand All @@ -55,18 +53,18 @@ func (m *Model) syncList(folders []clickup.Folder) {
m.log.Info("Synchronizing list")
m.folders = folders

sre_index := 0
items := folderListToItems(folders)
itemsList := listitem.ItemListToBubblesItems(items)
items := NewListItem(folders)

for i, item := range items {
if item.Description() == m.ctx.Config.DefaultFolder {
sre_index = i
for _, item := range items {
i := item.(listitem.Item)
if i.Title() == m.ctx.Config.DefaultFolder {
m.SelectedFolder = i.Data().(clickup.Folder)
}
}

m.list.SetItems(itemsList)
m.list.Select(sre_index)
m.list.SetItems(items)
m.list.Select(0)
m.log.Info("List synchronized")
}

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
Expand All @@ -81,32 +79,32 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.log.Info("List is empty")
break
}
selectedFolder := listitem.BubblesItemToItem(m.list.SelectedItem()).Description()
m.log.Infof("Selected folder %s", selectedFolder)
selectedFolder := m.list.SelectedItem().(listitem.Item).Data().(clickup.Folder)
m.log.Info("Selected folder", "id", selectedFolder.Id, "name", selectedFolder.Name)
m.SelectedFolder = selectedFolder
return m, FolderChangeCmd(selectedFolder)
return m, FolderChangeCmd(selectedFolder.Id)

case "J", "shift+down":
m.list.CursorDown()
if m.list.SelectedItem() == nil {
m.log.Info("List is empty")
break
}
selectedFolder := listitem.BubblesItemToItem(m.list.SelectedItem()).Description()
m.log.Infof("Selected folder %s", selectedFolder)
selectedFolder := m.list.SelectedItem().(listitem.Item).Data().(clickup.Folder)
m.log.Info("Selected folder", "id", selectedFolder.Id, "name", selectedFolder.Name)
m.SelectedFolder = selectedFolder
return m, common.FolderPreviewCmd(selectedFolder)
return m, common.FolderPreviewCmd(selectedFolder.Id)

case "K", "shift+up":
m.list.CursorUp()
if m.list.SelectedItem() == nil {
m.log.Info("List is empty")
break
}
selectedFolder := listitem.BubblesItemToItem(m.list.SelectedItem()).Description()
m.log.Infof("Selected folder %s", selectedFolder)
selectedFolder := m.list.SelectedItem().(listitem.Item).Data().(clickup.Folder)
m.log.Info("Selected folder", "id", selectedFolder.Id, "name", selectedFolder.Name)
m.SelectedFolder = selectedFolder
return m, common.FolderPreviewCmd(selectedFolder)
return m, common.FolderPreviewCmd(selectedFolder.Id)
}
}

Expand All @@ -131,7 +129,6 @@ func (m *Model) SetSize(s common.Size) {

func (m *Model) SpaceChanged(id string) error {
m.log.Infof("Received: SpaceChangedMsg: %s", id)
m.SelectedSpace = id

folders, err := m.ctx.Api.GetFolders(id)
if err != nil {
Expand All @@ -141,3 +138,11 @@ func (m *Model) SpaceChanged(id string) error {
m.syncList(folders)
return nil
}

func NewListItem(items []clickup.Folder) []list.Item {
result := make([]list.Item, len(items))
for i, v := range items {
result[i] = listitem.NewItem(v.Name, v.Id, v)
}
return result
}
21 changes: 0 additions & 21 deletions ui/components/folders-list/utils.go

This file was deleted.

23 changes: 4 additions & 19 deletions ui/components/list-item/item.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
package listitem

import "github.com/charmbracelet/bubbles/list"

type Item struct {
title string
desc string
data interface{}
}

func NewItem(title, desc string) Item {
func NewItem(title, desc string, data interface{}) Item {
return Item{
title: title,
desc: desc,
data: data,
}
}

func (i Item) Title() string { return i.title }
func (i Item) Description() string { return i.desc }
func (i Item) FilterValue() string { return i.title }

func ItemListToBubblesItems(items []Item) []list.Item {
listItems := make([]list.Item, len(items))
for i, item := range items {
listItems[i] = ItemToBubblesItem(item)
}
return listItems
}

func ItemToBubblesItem(item Item) list.Item {
return list.Item(item)
}

func BubblesItemToItem(item list.Item) Item {
return item.(Item)
}
func (i Item) Data() interface{} { return i.data }
57 changes: 31 additions & 26 deletions ui/components/lists-list/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import (
const ComponentId = "viewLists"

type Model struct {
list list.Model
ctx *context.UserContext
log *log.Logger
ComponentId common.ComponentId
SelectedList string
SelectedFolder string
lists []clickup.List
list list.Model
ctx *context.UserContext
log *log.Logger
ComponentId common.ComponentId
SelectedList clickup.List
lists []clickup.List
}

func InitialModel(ctx *context.UserContext, logger *log.Logger) Model {
Expand All @@ -34,13 +33,12 @@ func InitialModel(ctx *context.UserContext, logger *log.Logger) Model {
log := logger.WithPrefix(logger.GetPrefix() + "/" + ComponentId)

return Model{
ComponentId: ComponentId,
list: l,
ctx: ctx,
SelectedFolder: "",
SelectedList: "",
lists: []clickup.List{},
log: log,
ComponentId: ComponentId,
list: l,
ctx: ctx,
SelectedList: clickup.List{},
lists: []clickup.List{},
log: log,
}
}

Expand All @@ -55,10 +53,9 @@ func (m *Model) syncList(lists []clickup.List) {
m.log.Info("Synchronizing list")
m.lists = lists

items := listsListToItems(lists)
itemsList := listitem.ItemListToBubblesItems(items)
items := NewListItem(lists)

m.list.SetItems(itemsList)
m.list.SetItems(items)
m.list.Select(0)
m.log.Info("List synchronized")
}
Expand All @@ -75,32 +72,32 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.log.Info("List is empty")
break
}
selectedList := listitem.BubblesItemToItem(m.list.SelectedItem()).Description()
m.log.Infof("Selected list %s", selectedList)
selectedList := m.list.SelectedItem().(listitem.Item).Data().(clickup.List)
m.log.Info("Selected list", "id", selectedList.Id, "name", selectedList.Name)
m.SelectedList = selectedList
return m, ListChangedCmd(m.SelectedList)
return m, ListChangedCmd(m.SelectedList.Id)

case "J", "shift+down":
m.list.CursorDown()
if m.list.SelectedItem() == nil {
m.log.Info("List is empty")
break
}
selectedList := listitem.BubblesItemToItem(m.list.SelectedItem()).Description()
m.log.Infof("Selected list %s", selectedList)
selectedList := m.list.SelectedItem().(listitem.Item).Data().(clickup.List)
m.log.Info("Selected list", "id", selectedList.Id, "name", selectedList.Name)
m.SelectedList = selectedList
return m, common.ListPreviewCmd(m.SelectedList)
return m, common.ListPreviewCmd(m.SelectedList.Id)

case "K", "shift+up":
m.list.CursorUp()
if m.list.SelectedItem() == nil {
m.log.Info("List is empty")
break
}
selectedList := listitem.BubblesItemToItem(m.list.SelectedItem()).Description()
m.log.Infof("Selected list %s", selectedList)
selectedList := m.list.SelectedItem().(listitem.Item).Data().(clickup.List)
m.log.Info("Selected list", "id", selectedList.Id, "name", selectedList.Name)
m.SelectedList = selectedList
return m, common.ListPreviewCmd(m.SelectedList)
return m, common.ListPreviewCmd(m.SelectedList.Id)
}
}

Expand Down Expand Up @@ -132,3 +129,11 @@ func (m *Model) SpaceChanged(id string) error {

return nil
}

func NewListItem(items []clickup.List) []list.Item {
result := make([]list.Item, len(items))
for i, v := range items {
result[i] = listitem.NewItem(v.Name, v.Id, v)
}
return result
}
21 changes: 0 additions & 21 deletions ui/components/lists-list/utils.go

This file was deleted.

Loading
Loading