fix(ui): go back on esc

Ayman Bagabas created

Fixes: https://github.com/charmbracelet/soft-serve/issues/415

Change summary

pkg/ssh/ui.go               |  4 +++-
pkg/ui/common/component.go  |  3 +++
pkg/ui/pages/repo/files.go  |  9 +++++++++
pkg/ui/pages/repo/log.go    | 10 ++++++++++
pkg/ui/pages/repo/readme.go |  5 +++++
pkg/ui/pages/repo/refs.go   |  5 +++++
pkg/ui/pages/repo/repo.go   | 12 ++++++++++++
pkg/ui/pages/repo/stash.go  |  5 +++++
8 files changed, 52 insertions(+), 1 deletion(-)

Detailed changes

pkg/ssh/ui.go 🔗

@@ -196,7 +196,9 @@ func (ui *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 					ui.common.Zone.Close()
 					return ui, tea.Quit
 				}
-			case ui.activePage == repoPage && key.Matches(msg, ui.common.KeyMap.Back):
+			case ui.activePage == repoPage &&
+				ui.pages[ui.activePage].(*repo.Repo).Path() == "" &&
+				key.Matches(msg, ui.common.KeyMap.Back):
 				ui.activePage = selectionPage
 				// Always show the footer on selection page.
 				ui.showFooter = true

pkg/ui/common/component.go 🔗

@@ -28,4 +28,7 @@ type TabComponent interface {
 
 	// TabName returns the name of the tab.
 	TabName() string
+
+	// Path returns the hierarchical path of the tab.
+	Path() string
 }

pkg/ui/pages/repo/files.go 🔗

@@ -105,6 +105,15 @@ func NewFiles(common common.Common) *Files {
 	return f
 }
 
+// Path implements common.TabComponent.
+func (f *Files) Path() string {
+	path := f.path
+	if path == "." {
+		return ""
+	}
+	return path
+}
+
 // TabName returns the tab name.
 func (f *Files) TabName() string {
 	return "Files"

pkg/ui/pages/repo/log.go 🔗

@@ -83,6 +83,16 @@ func NewLog(common common.Common) *Log {
 	return l
 }
 
+// Path implements common.TabComponent.
+func (l *Log) Path() string {
+	switch l.activeView {
+	case logViewCommits:
+		return ""
+	default:
+		return "diff" // XXX: this is a place holder and doesn't mean anything
+	}
+}
+
 // TabName returns the name of the tab.
 func (l *Log) TabName() string {
 	return "Commits"

pkg/ui/pages/repo/readme.go 🔗

@@ -45,6 +45,11 @@ func NewReadme(common common.Common) *Readme {
 	}
 }
 
+// Path implements common.TabComponent.
+func (r *Readme) Path() string {
+	return ""
+}
+
 // TabName returns the name of the tab.
 func (r *Readme) TabName() string {
 	return "Readme"

pkg/ui/pages/repo/refs.go 🔗

@@ -57,6 +57,11 @@ func NewRefs(common common.Common, refPrefix string) *Refs {
 	return r
 }
 
+// Path implements common.TabComponent.
+func (r *Refs) Path() string {
+	return ""
+}
+
 // TabName returns the name of the tab.
 func (r *Refs) TabName() string {
 	if r.refPrefix == git.RefsHeads {

pkg/ui/pages/repo/repo.go 🔗

@@ -104,6 +104,11 @@ func (r *Repo) SetSize(width, height int) {
 	}
 }
 
+// Path returns the current component path.
+func (r *Repo) Path() string {
+	return r.panes[r.activeTab].Path()
+}
+
 func (r *Repo) commonHelp() []key.Binding {
 	b := make([]key.Binding, 0)
 	back := r.common.KeyMap.Back
@@ -194,6 +199,13 @@ func (r *Repo) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 				}
 			}
 		}
+		switch msg := msg.(type) {
+		case tea.KeyMsg:
+			switch {
+			case key.Matches(msg, r.common.KeyMap.Back):
+				cmds = append(cmds, goBackCmd)
+			}
+		}
 	case CopyMsg:
 		txt := msg.Text
 		if cfg := r.common.Config(); cfg != nil {

pkg/ui/pages/repo/stash.go 🔗

@@ -64,6 +64,11 @@ func NewStash(common common.Common) *Stash {
 	}
 }
 
+// Path implements common.TabComponent.
+func (s *Stash) Path() string {
+	return ""
+}
+
 // TabName returns the name of the tab.
 func (s *Stash) TabName() string {
 	return "Stash"