github.com/kyleu/dbaudit@v0.0.2-0.20240321155047-ff2f2c940496/app/controller/clib/themecatalog.go (about)

     1  // Package clib - Content managed by Project Forge, see [projectforge.md] for details.
     2  package clib
     3  
     4  import (
     5  	"fmt"
     6  	"net/http"
     7  	"strings"
     8  
     9  	"github.com/muesli/gamut"
    10  	"github.com/pkg/errors"
    11  	"github.com/samber/lo"
    12  
    13  	"github.com/kyleu/dbaudit/app"
    14  	"github.com/kyleu/dbaudit/app/controller"
    15  	"github.com/kyleu/dbaudit/app/controller/cutil"
    16  	"github.com/kyleu/dbaudit/app/lib/telemetry"
    17  	"github.com/kyleu/dbaudit/app/lib/theme"
    18  	"github.com/kyleu/dbaudit/app/util"
    19  	"github.com/kyleu/dbaudit/views"
    20  	"github.com/kyleu/dbaudit/views/vtheme"
    21  )
    22  
    23  func ThemeColor(w http.ResponseWriter, r *http.Request) {
    24  	controller.Act("theme.color", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
    25  		col, err := cutil.RCRequiredString(r, "color", false)
    26  		if err != nil {
    27  			return "", err
    28  		}
    29  		col = strings.ToLower(col)
    30  		if !strings.HasPrefix(col, "#") {
    31  			col = "#" + col
    32  		}
    33  		th := theme.ColorTheme(col, gamut.Hex(col))
    34  		ps.SetTitleAndData(fmt.Sprintf("[%s] Theme", col), th)
    35  		ps.DefaultNavIcon = themeIcon
    36  		return controller.Render(w, r, as, &vtheme.Edit{Theme: th, Icon: "app"}, ps, "Themes||/theme", col)
    37  	})
    38  }
    39  
    40  func ThemeColorEdit(w http.ResponseWriter, r *http.Request) {
    41  	controller.Act("theme.color.edit", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
    42  		color := r.URL.Query().Get("color")
    43  		if color == "" {
    44  			return "", errors.New("must provide color in query string")
    45  		}
    46  		if !strings.HasPrefix(color, "#") {
    47  			return "", errors.New("provided color must be a hex string")
    48  		}
    49  		t := theme.ColorTheme(strings.TrimPrefix(color, "#"), gamut.Hex(color))
    50  		ps.SetTitleAndData("Edit theme colors ["+t.Key+"]", t)
    51  		ps.DefaultNavIcon = themeIcon
    52  		page := &vtheme.Edit{Theme: t, Icon: "app", Exists: as.Themes.FileExists(t.Key)}
    53  		return controller.Render(w, r, as, page, ps, "Themes||/theme", t.Key)
    54  	})
    55  }
    56  
    57  func ThemePalette(w http.ResponseWriter, r *http.Request) {
    58  	controller.Act("theme.palette", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
    59  		pal, err := cutil.RCRequiredString(r, "palette", false)
    60  		if err != nil {
    61  			return "", err
    62  		}
    63  		_, span, _ := telemetry.StartSpan(ps.Context, "theme:load", ps.Logger)
    64  		thms, err := theme.PaletteThemes(pal)
    65  		span.Complete()
    66  		if err != nil {
    67  			return "", err
    68  		}
    69  		ps.SetTitleAndData(fmt.Sprintf("[%s] Themes", pal), thms)
    70  		if r.URL.Query().Get("t") == "go" {
    71  			ps.Data = strings.Join(lo.Map(thms, func(t *theme.Theme, _ int) string {
    72  				return t.ToGo()
    73  			}), util.StringDefaultLinebreak)
    74  			return controller.Render(w, r, as, &views.Debug{}, ps, "Themes")
    75  		}
    76  		ps.DefaultNavIcon = themeIcon
    77  		return controller.Render(w, r, as, &vtheme.Add{Palette: pal, Themes: thms}, ps, "Themes||/theme", "Palette")
    78  	})
    79  }
    80  
    81  func ThemePaletteEdit(w http.ResponseWriter, r *http.Request) {
    82  	controller.Act("theme.palette.edit", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
    83  		palette, err := cutil.RCRequiredString(r, "palette", false)
    84  		if err != nil {
    85  			return "", err
    86  		}
    87  		key, err := cutil.RCRequiredString(r, "theme", false)
    88  		if err != nil {
    89  			return "", err
    90  		}
    91  		if key == theme.Default.Key {
    92  			return controller.FlashAndRedir(false, "Unable to edit default theme", "/theme", w, ps)
    93  		}
    94  		themes, err := theme.PaletteThemes(palette)
    95  		if err != nil {
    96  			return "", err
    97  		}
    98  		t := themes.Get(key)
    99  		if t == nil {
   100  			return "", errors.Errorf("invalid theme [%s] for palette [%s]", key, palette)
   101  		}
   102  		ps.SetTitleAndData("Edit theme palette ["+t.Key+"]", t)
   103  		ps.DefaultNavIcon = themeIcon
   104  		return controller.Render(w, r, as, &vtheme.Edit{Theme: t, Icon: "app"}, ps, "Themes||/theme", t.Key)
   105  	})
   106  }