Skip to content

Commit

Permalink
refactor(gnoweb): improve layouts and reduce code duplication (gnolan…
Browse files Browse the repository at this point in the history
…g#3569)

This PR aims to improve the HTML composability of gnoweb and avoid DRY
issues across templates and layouts. The changes include:

- Refactoring HTML components for better reusability.
- Reducing code duplication in views.
- Removing static data from the layout
- Enhancing code maintainability and readability.

---------

Signed-off-by: gfanton <[email protected]>
Co-authored-by: gfanton <[email protected]>
  • Loading branch information
2 people authored and stefann-01 committed Jan 24, 2025
1 parent 400c7cd commit 6e4a92a
Show file tree
Hide file tree
Showing 65 changed files with 1,171 additions and 952 deletions.
4 changes: 2 additions & 2 deletions gno.land/pkg/gnoweb/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ input_css := frontend/css/input.css
output_css := $(PUBLIC_DIR)/styles.css
tw_version := 3.4.14
tw_config_path := frontend/css/tx.config.js
templates_files := $(shell find . -iname '*.gohtml')
templates_files := $(shell find . -iname '*.html')

# static config
src_dir_static := frontend/static
Expand Down Expand Up @@ -79,7 +79,7 @@ dev:

# Go server in development mode
dev.gnoweb: generate
$(run_reflex) -s -r '.*\.go(html)?' -- \
$(run_reflex) -s -r '.*\.(go|html)' -- \
go run ../../cmd/gnoweb -assets-dir=${PUBLIC_DIR} -chainid=${CHAIN_ID} -remote=${DEV_REMOTE} \
2>&1 | $(run_logname) gnoweb

Expand Down
4 changes: 2 additions & 2 deletions gno.land/pkg/gnoweb/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func AliasAndRedirectMiddleware(next http.Handler, analytics bool) http.Handler
// Check if the request path matches a redirect
if newPath, ok := Redirects[r.URL.Path]; ok {
http.Redirect(w, r, newPath, http.StatusFound)
components.RenderRedirectComponent(w, components.RedirectData{
components.RedirectView(components.RedirectData{
To: newPath,
WithAnalytics: analytics,
})
}).Render(w)
return
}

Expand Down
4 changes: 2 additions & 2 deletions gno.land/pkg/gnoweb/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ func NewRouter(logger *slog.Logger, cfg *AppConfig) (http.Handler, error) {
if cfg.FaucetURL != "" {
mux.Handle("/faucet", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, cfg.FaucetURL, http.StatusFound)
components.RenderRedirectComponent(w, components.RedirectData{
components.RedirectView(components.RedirectData{
To: cfg.FaucetURL,
WithAnalytics: cfg.Analytics,
})
}).Render(w)
}))
}

Expand Down
18 changes: 0 additions & 18 deletions gno.land/pkg/gnoweb/components/breadcrumb.gohtml

This file was deleted.

35 changes: 35 additions & 0 deletions gno.land/pkg/gnoweb/components/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package components

import (
"io"
)

type Component interface {
Render(w io.Writer) error
}

type TemplateComponent struct {
name string
data any
}

func (c *TemplateComponent) Render(w io.Writer) error {
return tmpl.ExecuteTemplate(w, c.name, c.data)
}

func NewTemplateComponent(name string, data any) Component {
return &TemplateComponent{name: name, data: data}
}

type readerComponent struct {
io.Reader
}

func NewReaderComponent(reader io.Reader) Component {
return &readerComponent{reader}
}

func (c *readerComponent) Render(w io.Writer) (err error) {
_, err = io.Copy(w, c)
return err
}
15 changes: 0 additions & 15 deletions gno.land/pkg/gnoweb/components/directory.go

This file was deleted.

51 changes: 0 additions & 51 deletions gno.land/pkg/gnoweb/components/help.go

This file was deleted.

110 changes: 0 additions & 110 deletions gno.land/pkg/gnoweb/components/help.gohtml

This file was deleted.

47 changes: 0 additions & 47 deletions gno.land/pkg/gnoweb/components/index.go

This file was deleted.

Loading

0 comments on commit 6e4a92a

Please sign in to comment.