~bigbes/sr-ht-dolt

ref: 74d2612eac643b9e6e038e6dd1a3dda9b7940519 sr-ht-dolt/web/handlers_view.go -rw-r--r-- 2.5 KiB
74d2612e — Eugene Blikh fix(db): map repository_path_key to ErrNameTaken 25 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package web

import (
	"errors"
	"net/http"

	"github.com/go-chi/chi/v5"

	"sourcecraft.dev/bigbes/sr-ht-dolt/browse"
	"sourcecraft.dev/bigbes/sr-ht-dolt/core"
)

// handleView renders a specialized alternative view of a repository. The view
// is selected by the {view} URL slug and must both exist in a.views and Apply
// to the tables at the resolved ref; otherwise the request is a 404 (the view
// simply does not exist for this repo). The generic table browser remains
// reachable via the tree/table routes regardless.
//
// The ref is taken from ?ref= (default: the repo's default branch). The view's
// own Template() is rendered with a fixed envelope: the chrome, the repo, the
// ref and branch list, the applicable view tabs, and the opaque value the view
// produced from Build (as .Data).
func (a *app) handleView(w http.ResponseWriter, r *http.Request) {
	repo, _, _, ok := a.loadRepoForBrowse(w, r)
	if !ok {
		return
	}
	sess, ok := a.openBrowse(w, r, repo)
	if !ok {
		return
	}
	defer sess.Close()

	ref := r.URL.Query().Get("ref")
	if ref == "" {
		branches, err := sess.Branches(r.Context())
		if err != nil {
			http.Error(w, "failed to list branches", http.StatusInternalServerError)
			return
		}
		ref = browse.DefaultBranch(branches)
	}

	tables, err := sess.Tables(r.Context(), ref)
	if err != nil {
		if errors.Is(err, browse.ErrRefNotFound) {
			a.notFound(w, r)
			return
		}
		http.Error(w, "failed to read tables", http.StatusInternalServerError)
		return
	}

	// Find the requested view; an unknown slug or a view that does not fingerprint
	// these tables is reported as not found — the view does not exist here.
	slug := chi.URLParam(r, "view")
	var view View
	for _, v := range a.views {
		if v.Name() == slug {
			view = v
			break
		}
	}
	if view == nil || !view.Applies(tables) {
		a.notFound(w, r)
		return
	}

	data, err := view.Build(r.Context(), sess, repo, ref, r.URL.Query())
	if err != nil {
		http.Error(w, "failed to build view", http.StatusInternalServerError)
		return
	}

	// Re-list branches for the chrome/tab bar. Cheap and keeps the ref selector
	// consistent whether or not ?ref= was supplied.
	branches, _ := sess.Branches(r.Context())

	page := struct {
		basePage
		Repo     *core.Repo
		Ref      string
		Branches []browse.Branch
		Views    []View
		Data     any
	}{
		basePage: a.newBasePage(r, view.Label()+" — "+repo.OwnerName+"/"+repo.Name),
		Repo:     repo,
		Ref:      ref,
		Branches: branches,
		Views:    applicableViews(a.views, tables),
		Data:     data,
	}
	a.render(w, http.StatusOK, view.Template(), page)
}