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: reload ui data #125

Merged
merged 5 commits into from
Jun 3, 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
24 changes: 12 additions & 12 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (m *Api) GetSpaces(teamId string) ([]clickup.Space, error) {
return m.getSpaces(true, teamId)
}

func (m *Api) syncSpaces(teamId string) ([]clickup.Space, error) {
func (m *Api) SyncSpaces(teamId string) ([]clickup.Space, error) {
return m.getSpaces(false, teamId)
}

Expand Down Expand Up @@ -115,7 +115,7 @@ func (m *Api) GetTeams() ([]clickup.Team, error) {
return m.getTeams(true)
}

func (m *Api) syncTeams() ([]clickup.Team, error) {
func (m *Api) SyncTeams() ([]clickup.Team, error) {
return m.getTeams(false)
}

Expand All @@ -138,7 +138,7 @@ func (m *Api) GetFolders(spaceId string) ([]clickup.Folder, error) {
return m.getFolders(true, spaceId)
}

func (m *Api) syncFolders(spaceId string) ([]clickup.Folder, error) {
func (m *Api) SyncFolders(spaceId string) ([]clickup.Folder, error) {
return m.getFolders(false, spaceId)
}

Expand All @@ -157,11 +157,11 @@ func (m *Api) getFolders(cached bool, spaceId string) ([]clickup.Folder, error)
return data, nil
}

func (m *Api) GetListsFromFolder(folderId string) ([]clickup.List, error) {
func (m *Api) GetLists(folderId string) ([]clickup.List, error) {
return m.getListsFromFolder(true, folderId)
}

func (m *Api) syncListsFromFolder(folderId string) ([]clickup.List, error) {
func (m *Api) SyncLists(folderId string) ([]clickup.List, error) {
return m.getListsFromFolder(false, folderId)
}

Expand Down Expand Up @@ -373,7 +373,7 @@ func (m *Api) GetList(listId string) (clickup.List, error) {
return m.getList(true, listId)
}

func (m *Api) syncList(listId string) (clickup.List, error) {
func (m *Api) SyncList(listId string) (clickup.List, error) {
return m.getList(false, listId)
}

Expand Down Expand Up @@ -412,15 +412,15 @@ func (m *Api) Sync() error {

switch entry.Namespace {
case CacheNamespaceTeams:
_, err = m.syncTeams()
_, err = m.SyncTeams()
case CacheNamespaceSpaces:
_, err = m.syncSpaces(key)
_, err = m.SyncSpaces(key)
case CacheNamespaceFolders:
_, err = m.syncFolders(key)
case CacheNamespaceListsFolder:
_, err = m.syncListsFromFolder(key)
_, err = m.SyncFolders(key)
case CacheNamespaceLists:
_, err = m.syncList(key)
_, err = m.SyncList(key)
case CacheNamespaceListsFolder:
_, err = m.SyncLists(key)
case CacheNamespaceViewsWorkspace:
_, err = m.syncViewsFromWorkspace(key)
case CacheNamespaceViewsSpace:
Expand Down
24 changes: 24 additions & 0 deletions ui/common/commands.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package common

import (
"time"

tea "github.com/charmbracelet/bubbletea"
)

Expand Down Expand Up @@ -91,3 +93,25 @@ func ErrCmd(err ErrMsg) tea.Cmd {
return err
}
}

type UITickMsg int64

func (m UITickMsg) Tick() tea.Cmd {
return func() tea.Msg {
return m
}
}

func UITickCmd(ts int64) tea.Cmd {
return func() tea.Msg {
return UITickMsg(time.Now().Unix() + ts)
}
}

type RefreshMsg string

func RefreshCmd() tea.Cmd {
return func() tea.Msg {
return RefreshMsg("")
}
}
4 changes: 2 additions & 2 deletions ui/components/folders-list/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func InitialModel(ctx *context.UserContext, logger *log.Logger) Model {
}
}

func (m *Model) syncList(folders []clickup.Folder) {
func (m *Model) SetList(folders []clickup.Folder) {
m.log.Info("Synchronizing list")
m.folders = folders

Expand Down Expand Up @@ -110,7 +110,7 @@ func (m *Model) SpaceChanged(id string) error {
return err
}

m.syncList(folders)
m.SetList(folders)
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions ui/components/lists-list/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (m Model) KeyMap() KeyMap {
return m.keyMap
}

func (m *Model) syncList(lists []clickup.List) {
func (m *Model) SetList(lists []clickup.List) {
m.log.Info("Synchronizing list")
m.lists = lists

Expand Down Expand Up @@ -96,11 +96,11 @@ func (m *Model) SetSize(size common.Size) {
}

func (m *Model) FolderChanged(id string) error {
lists, err := m.ctx.Api.Clickup.GetListsFromFolder(id)
lists, err := m.ctx.Api.GetLists(id)
if err != nil {
return err
}
m.syncList(lists)
m.SetList(lists)

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions ui/components/spaces-list/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func InitialModel(ctx *context.UserContext, logger *log.Logger) Model {
}
}

func (m *Model) syncList(spaces []clickup.Space) {
func (m *Model) SetList(spaces []clickup.Space) {
m.log.Info("Synchronizing list...")
m.spaces = spaces

Expand Down Expand Up @@ -110,7 +110,7 @@ func (m *Model) WorkspaceChanged(id string) error {
return err
}

m.syncList(spaces)
m.SetList(spaces)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions ui/components/workspaces-list/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func InitialModel(ctx *context.UserContext, logger *log.Logger) Model {
}
}

func (m *Model) syncList(workspaces []clickup.Workspace) {
func (m *Model) SetList(workspaces []clickup.Workspace) {
m.log.Info("Synchronizing list...")
m.workspaces = workspaces

Expand Down Expand Up @@ -127,7 +127,7 @@ func (m *Model) InitWorkspaces() error {
}

m.Selected = workspaces[0]
m.syncList(workspaces)
m.SetList(workspaces)

return nil
}
Expand Down
18 changes: 18 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ui

import (
"strings"
"time"

"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -13,6 +14,10 @@ import (
"github.com/prgrs/clickup/ui/widgets/help"
)

const (
refreshInterval = 3
)

type Model struct {
ctx *context.UserContext
log *log.Logger
Expand Down Expand Up @@ -74,6 +79,18 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
"height", msg.Height)
m.ctx.WindowSize.Set(msg.Width, msg.Height)
return m, tea.Batch(cmds...)

case common.UITickMsg:
ts := int64(msg)
if time.Now().Unix() > ts {
m.log.Debug("Fire refresh tick")
return m, tea.Batch(
common.UITickCmd(refreshInterval),
common.RefreshCmd(),
)
}

return m, msg.Tick()
}

cmds = append(cmds,
Expand Down Expand Up @@ -134,5 +151,6 @@ func (m Model) Init() tea.Cmd {
return tea.Batch(
m.viewCompact.Init(),
m.dialogHelp.Init(),
common.UITickCmd(refreshInterval),
)
}
15 changes: 6 additions & 9 deletions ui/views/compact/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,11 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
m.widgetViewsTabs.Path = m.widgetNavigator.GetPath()
}

cmd = m.widgetNavigator.Update(msg)
cmds = append(cmds, cmd)

cmd = m.widgetViewsTabs.Update(msg)
cmds = append(cmds, cmd)

cmd = m.widgetTasks.Update(msg)
cmds = append(cmds, cmd)
cmds = append(cmds,
m.widgetNavigator.Update(msg),
m.widgetViewsTabs.Update(msg),
m.widgetTasks.Update(msg),
)

return tea.Batch(cmds...)
}
Expand Down Expand Up @@ -283,7 +280,7 @@ func (m *Model) reloadTasks(viewId string) error {
}

func (m *Model) handleWorkspaceChangePreview(id string) tea.Cmd {
views, err := m.ctx.Api.GetViewsFromWorkspace(id)
views, err := m.ctx.Api.GetViewsFromWorkspace(id) // TODO: should fetch from the list
if err != nil {
return common.ErrCmd(err)
}
Expand Down
9 changes: 4 additions & 5 deletions ui/widgets/navigator/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package navigator
import tea "github.com/charmbracelet/bubbletea"

func (m *Model) handleKeys(msg tea.KeyMsg) tea.Cmd {
var cmd tea.Cmd
var cmds []tea.Cmd

switch keypress := msg.String(); keypress {
Expand All @@ -19,10 +18,11 @@ func (m *Model) handleKeys(msg tea.KeyMsg) tea.Cmd {
m.state = m.componentFoldersList.Id()
}

cmds = append(cmds, cmd)
return tea.Batch(cmds...)
return nil
}

var cmd tea.Cmd

switch m.state {
case m.componentWorkspacesList.Id():
cmd = m.componentWorkspacesList.Update(msg)
Expand All @@ -34,6 +34,5 @@ func (m *Model) handleKeys(msg tea.KeyMsg) tea.Cmd {
cmd = m.componentListsList.Update(msg)
}

cmds = append(cmds, cmd)
return tea.Batch(cmds...)
return tea.Batch(append(cmds, cmd)...)
}
60 changes: 60 additions & 0 deletions ui/widgets/navigator/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
spaceslist "github.com/prgrs/clickup/ui/components/spaces-list"
workspaceslist "github.com/prgrs/clickup/ui/components/workspaces-list"
"github.com/prgrs/clickup/ui/context"
"golang.org/x/sync/errgroup"
)

const id = "navigator"
Expand All @@ -40,6 +41,10 @@ func (m Model) Id() common.Id {
return m.id
}

func (m Model) State() common.Id {
return m.state
}

// TODO: refactor
func (m *Model) SetWorksapce(workspace clickup.Workspace) {
m.componentWorkspacesList.Selected = workspace
Expand Down Expand Up @@ -174,6 +179,61 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
id := string(msg)
m.log.Infof("Received: ListChangedMsg: %s", id)
cmds = append(cmds, common.ListChangeCmd(id))

case common.RefreshMsg:
m.log.Debug("Received: common.RefreshMsg")

errgroup := new(errgroup.Group)

errgroup.Go(func() error {
t, err := m.ctx.Api.SyncTeams()
if err != nil {
return err
}
m.componentWorkspacesList.SetList(t)
return nil
})

errgroup.Go(func() error {
id := m.componentWorkspacesList.Selected.Id
if id != "" {
t, err := m.ctx.Api.SyncSpaces(id)
if err != nil {
return err
}
m.componentSpacesList.SetList(t)
}
return nil
})

errgroup.Go(func() error {
id := m.componentSpacesList.Selected.Id
if id != "" {
t, err := m.ctx.Api.SyncFolders(id)
if err != nil {
return err
}
m.componentFoldersList.SetList(t)
}
return nil
})

errgroup.Go(func() error {
id := m.componentFoldersList.Selected.Id
if id != "" {
t, err := m.ctx.Api.SyncLists(id)
if err != nil {
return err
}
m.componentListsList.SetList(t)
}
return nil
})

err := errgroup.Wait()
if err != nil {
return common.ErrCmd(err)
}
}

cmds = append(cmds,
Expand Down
Loading
Loading