feat: persist sidebar state
This commit is contained in:
parent
61eaa268ab
commit
de0e87cd71
4 changed files with 36 additions and 2 deletions
21
internal/middleware/sidebar.go
Normal file
21
internal/middleware/sidebar.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.juancwu.dev/juancwu/budgit/internal/ctxkeys"
|
||||
)
|
||||
|
||||
// WithSidebarState reads the sidebar_state cookie and adds the collapsed
|
||||
// state to the request context so templates can render the sidebar in the
|
||||
// correct initial state.
|
||||
func WithSidebarState(next http.Handler) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
collapsed := false
|
||||
if c, err := r.Cookie("sidebar_state"); err == nil {
|
||||
collapsed = c.Value == "false"
|
||||
}
|
||||
ctx := ctxkeys.WithSidebarCollapsed(r.Context(), collapsed)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue