dmitri.shuralyov.com/app/changes/...

component: support explicit "open" state query

Previously, the default open tab could only be selected by not
providing the state query. Add support for selecting the default
open tab with an explicit ?state=open query too.

Similar to https://github.com/shurcooL/issuesapp/commit/0b82558264994ab993533eed0d2cfe83e145dbe5.
dmitshur committed 5 years ago commit dc30f1494c2111233691998b41a5624769f4f783
Collapse all
component/tabs.go
@@ -35,19 +35,19 @@ func (n ChangesNav) Render() []*html.Node {
	return []*html.Node{header}
}

// tabs renders the HTML nodes for <nav> element with tab header links.
func (n ChangesNav) tabs() []*html.Node {
	selectedTabName := n.Query.Get(n.StateQueryKey)
	selectedTabName := n.selectedTabName()
	var ns []*html.Node
	for i, tab := range []struct {
		Name      string // Tab name corresponds to its state filter query value.
		Component htmlg.Component
	}{
		// Note: The routing logic (i.e., exact tab Name values) is duplicated with tabStateFilter.
		//       Might want to try to factor it out into a common location (e.g., a route package or so).
		{Name: "", Component: OpenChangesTab{Count: n.OpenCount}},
		{Name: "open", Component: OpenChangesTab{Count: n.OpenCount}},
		{Name: "closed", Component: ClosedChangesTab{Count: n.ClosedCount}},
	} {
		tabURL := (&url.URL{
			Path:     n.Path,
			RawQuery: n.rawQuery(tab.Name),
@@ -68,14 +68,24 @@ func (n ChangesNav) tabs() []*html.Node {
		ns = append(ns, a)
	}
	return ns
}

const defaultTabName = "open"

func (n ChangesNav) selectedTabName() string {
	vs := n.Query[n.StateQueryKey]
	if len(vs) == 0 {
		return defaultTabName
	}
	return vs[0]
}

// rawQuery returns the raw query for a link pointing to tabName.
func (n ChangesNav) rawQuery(tabName string) string {
	q := n.Query
	if tabName == "" {
	if tabName == defaultTabName {
		q.Del(n.StateQueryKey)
		return q.Encode()
	}
	q.Set(n.StateQueryKey, tabName)
	return q.Encode()