Private
Public Access
1
0

Tweaks/cleanups to conversation management in tui

- Pass around message/conversation values instead of pointers where it
makes more sense, and store values instead of pointers in the globally
(within the TUI) shared `App` (pointers provide no utility here).

- Split conversation persistence into separate conversation/message
  saving stages
This commit is contained in:
2024-10-25 16:57:15 +00:00
parent 07c96082e7
commit ec21a02ec0
9 changed files with 116 additions and 86 deletions

View File

@@ -21,14 +21,14 @@ type (
// sent when conversation list is loaded
msgConversationsLoaded conversation.ConversationList
// sent when a single conversation is loaded
msgConversationLoaded *conversation.Conversation
msgConversationLoaded conversation.Conversation
// sent when a conversation is deleted
msgConversationDeleted struct{}
)
type Model struct {
App *model.AppModel
width int
App *model.AppModel
width int
height int
cursor int
@@ -151,14 +151,14 @@ func (m *Model) Update(msg tea.Msg) (shared.ViewModel, tea.Cmd) {
m.content.SetContent(m.renderConversationList())
case msgConversationLoaded:
m.App.ClearConversation()
m.App.Conversation = msg
m.App.Conversation = conversation.Conversation(msg)
cmds = append(cmds, func() tea.Msg {
return shared.MsgViewChange(shared.ViewChat)
})
case bubbles.MsgConfirmPromptAnswered:
m.confirmPrompt.Blur()
if msg.Value {
conv, ok := msg.Payload.(conversation.Conversation)
conv, ok := msg.Payload.(conversation.ConversationListItem)
if ok {
cmds = append(cmds, m.deleteConversation(conv))
}
@@ -198,13 +198,13 @@ func (m *Model) loadConversation(conversationID uint) tea.Cmd {
if err != nil {
return shared.AsMsgError(fmt.Errorf("Could not load conversation %d: %v", conversationID, err))
}
return msgConversationLoaded(conversation)
return msgConversationLoaded(*conversation)
}
}
func (m *Model) deleteConversation(conv conversation.Conversation) tea.Cmd {
func (m *Model) deleteConversation(conv conversation.ConversationListItem) tea.Cmd {
return func() tea.Msg {
err := m.App.Ctx.Conversations.DeleteConversation(&conv)
err := m.App.Ctx.Conversations.DeleteConversationById(conv.ID)
if err != nil {
return shared.AsMsgError(fmt.Errorf("Could not delete conversation: %v", err))
}