From 617f0980d81f7de78ccdef273d030ea66561bcfa Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Thu, 10 Dec 2020 19:26:24 -0500 Subject: [PATCH] Handle stash failures --- ui/pager.go | 8 ++------ ui/stash.go | 8 +++++++- ui/ui.go | 29 +++++++++++++++++++++-------- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/ui/pager.go b/ui/pager.go index b243cc5..0f4a9c6 100644 --- a/ui/pager.go +++ b/ui/pager.go @@ -49,10 +49,6 @@ var ( type contentRenderedMsg string type noteSavedMsg *charm.Markdown -type stashSuccessMsg markdown -type stashErrMsg struct{ err error } - -func (s stashErrMsg) Error() string { return s.err.Error() } type pagerState int @@ -316,8 +312,8 @@ func (m pagerModel) update(msg tea.Msg) (pagerModel, tea.Cmd) { m.stashedDocument = &md } - case stashErrMsg: - // TODO + case stashFailMsg: + delete(m.general.filesStashed, msg.markdown.localID) case statusMessageTimeoutMsg: m.state = pagerStateBrowse diff --git a/ui/stash.go b/ui/stash.go index cc6102b..eb41753 100644 --- a/ui/stash.go +++ b/ui/stash.go @@ -572,7 +572,6 @@ func (m stashModel) update(msg tea.Msg) (stashModel, tea.Cmd) { } } - // Something was stashed. Add it to the stash listing. case stashSuccessMsg: md := markdown(msg) m.addMarkdowns(&md) @@ -582,6 +581,9 @@ func (m stashModel) update(msg tea.Msg) (stashModel, tea.Cmd) { } cmds = append(cmds, m.newStatusMessage("Stashed!")) + case stashFailMsg: + cmds = append(cmds, m.newStatusMessage("Couldn’t stash :(")) + case statusMessageTimeoutMsg: if applicationContext(msg) == stashContext { m.hideStatusMessage() @@ -727,6 +729,10 @@ func (m *stashModel) handleDocumentBrowsing(msg tea.Msg) tea.Cmd { md := m.selectedMarkdown() + if !stashableDocTypes.Contains(md.markdownType) { + break + } + if _, alreadyStashed := m.general.filesStashed[md.localID]; alreadyStashed { cmds = append(cmds, m.newStatusMessage("Already stashed")) break diff --git a/ui/ui.go b/ui/ui.go index a417d3a..b5542af 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -93,6 +93,13 @@ type stashLoadErrMsg struct{ err error } type gotNewsMsg []*charm.Markdown type statusMessageTimeoutMsg applicationContext type newsLoadErrMsg struct{ err error } +type stashSuccessMsg markdown +type stashFailMsg struct { + err error + markdown markdown +} + +//type stashErrMsg struct{ err error } func (e errMsg) Error() string { return e.err.Error() } func (e errMsg) Unwrap() error { return e.err } @@ -422,9 +429,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds = append(cmds, findNextLocalFile(m)) case stashSuccessMsg: - // Something was stashed. Update the stash listing but don't run an - // actual update on the stash since we don't want to trigger the status - // message and generally don't want any other effects. + // Something was stashed outside the file listing view. Update the + // stash listing but don't run an actual update on the stash since we + // don't want to trigger the status message and generally don't want + // any other effects. if m.state == stateShowDocument { md := markdown(msg) m.stash.addMarkdowns(&md) @@ -435,6 +443,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } + case stashFailMsg: + delete(m.general.filesStashed, msg.markdown.localID) + case filteredMarkdownMsg: if m.state == stateShowDocument { newStashModel, cmd := m.stash.update(msg) @@ -653,7 +664,7 @@ func stashDocument(cc *charm.Client, md markdown) tea.Cmd { if debug { log.Println("error stashing document:", err) } - return stashErrMsg{err} + return stashFailMsg{err, md} } } @@ -669,21 +680,23 @@ func stashDocument(cc *charm.Client, md markdown) tea.Cmd { if debug { log.Println("error loading document body for stashing:", err) } - return stashErrMsg{err} + return stashFailMsg{err, md} } md.Body = string(data) case NewsDoc: newMD, err := loadMarkdownFromCharm(cc, md.ID, md.markdownType) if err != nil { - return stashErrMsg{err} + return stashFailMsg{err, md} } md.Body = newMD.Body default: + err := fmt.Errorf("user is attempting to stash an unsupported markdown type: %s", md.markdownType) if debug { - log.Printf("user is attempting to stash an unsupported markdown type: %s", md.markdownType) + log.Println(err) } + return stashFailMsg{err, md} } } @@ -698,7 +711,7 @@ func stashDocument(cc *charm.Client, md markdown) tea.Cmd { if debug { log.Println("error stashing document:", err) } - return stashErrMsg{err} + return stashFailMsg{err, md} } // The server sends the whole stashed document back, but we really just