refine navigation and loading enteries

This commit is contained in:
Travis Shears 2025-10-02 20:09:57 +02:00
parent 99548e67b7
commit 8414414f98
10 changed files with 148 additions and 41 deletions

View file

@ -15,18 +15,14 @@ type EntryListPageModel struct {
type SelectActionToTake struct{ ActionToTake Action }
func InitialEntryListPageModel() EntryListPageModel {
func initialEntryListPageModel() EntryListPageModel {
return EntryListPageModel{
cursor: 0,
actionToTake: Read,
}
}
func (m EntryListPageModel) InitEntryListPage() tea.Cmd {
return nil
}
func (m EntryListPageModel) Update(msg tea.Msg, ctx *context) (EntryListPageModel, tea.Cmd) {
func (m EntryListPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryListPageModel, tea.Cmd) {
switch msg := msg.(type) {
case SelectActionToTake:
m.actionToTake = msg.ActionToTake
@ -34,6 +30,9 @@ func (m EntryListPageModel) Update(msg tea.Msg, ctx *context) (EntryListPageMode
m.entries = msg.Logs
return m, nil
case tea.KeyMsg:
if !active {
return m, nil
}
switch msg.String() {
case "up", "k":
if m.cursor > 0 {
@ -43,33 +42,27 @@ func (m EntryListPageModel) Update(msg tea.Msg, ctx *context) (EntryListPageMode
if m.cursor < len(actions)-1 {
m.cursor++
}
case "left", "h":
cmd := func() tea.Msg {
return SwitchPages{Page: ActionList}
}
return m, cmd
case "enter", " ":
id := m.entries[m.cursor].ID
rev := m.entries[m.cursor].Rev
switch m.actionToTake {
case Delete:
return m, gemlog.DeleteGemlogCMD(ctx.config, id)
}
delCmd := gemlog.DeleteGemlogCMD(ctx.config, id, rev)
loadCmd := gemlog.LoadGemlogsCMD(ctx.config)
navCmd := func() tea.Msg {
return SwitchPages{Page: ActionList}
}
// action := actions[m.cursor]
// switch action {
// case Write:
// return m, gemlog.WritePostCMD(ctx.config)
// case Read:
// m.ui.page = EntryList
// m.ui.entryListPage.cursor = 0
// m.ui.entryListPage.actionToTake = Read
// return m, gemlog.LoadGemlogCMD(ctx.config)
// case Edit:
// m.ui.page = EntryList
// m.ui.entryListPage.cursor = 0
// m.ui.entryListPage.actionToTake = Edit
// return m, gemlog.LoadGemlogCMD(ctx.config)
// case Delete:
// m.ui.page = EntryList
// m.ui.entryListPage.cursor = 0
// m.ui.entryListPage.actionToTake = Delete
// return m, gemlog.LoadGemlogCMD(m.context.config)
// }
return m, tea.Sequence(delCmd, loadCmd, navCmd)
case Read:
return m, gemlog.LoadGemlogCMD(ctx.config, id)
}
}
}
@ -85,5 +78,7 @@ func (m EntryListPageModel) View() string {
}
s += fmt.Sprintf("%s %s : %s\n", cursor, entry.Date, entry.Slug)
}
s += "\n\n Press h or left arrow to go back"
return s
}