Files
supermarket/internal/web/profile.go
T
davidandClaude Opus 5.5 a89121f1ec Redesign the UI on brain's look: sidebar with search, Overview, modules, grouped Settings
- Left sidebar as in brain: search at the top (press /), Overview,
  Inventory, the modules (Content with Inbox, Sources, Scheduled and
  Published; later phases greyed out), Settings at the bottom; a top bar
  with breadcrumbs, and a menu that slides in on narrow screens.
- brain's colour tokens, type, radius and shadows, with a dark theme that
  follows the system setting.
- Settings as grouped tabs: Profile; Social accounts (was Credentials, now
  keys and sign-in together per network); Core & Hermes; AI, which now
  holds the API keys too. Old addresses redirect.
- /search matches every word across posts, fetched items, the inventory,
  products and settings pages.
- Overview: a card per module, then the setup checklist, folded once done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 07:50:24 -07:00

206 lines
6.0 KiB
Go

package web
import (
"context"
"net/http"
"time"
"supermarket/internal/ai"
"supermarket/internal/profile"
)
type profileData struct {
P *profile.Profile
Products []profile.Product // existing plus one blank row
Websites []profile.Website // existing plus one blank row
Drafted bool
DraftURL string
NoAI bool // no AI provider yet: drafting from the website can't work
}
func withBlankRows(p *profile.Profile) profileData {
d := profileData{P: p}
d.Products = append(append([]profile.Product{}, p.Products...), profile.Product{})
d.Websites = append(append([]profile.Website{}, p.Websites...), profile.Website{})
return d
}
func (s *Server) profilePage(w http.ResponseWriter, r *http.Request) {
p, err := profile.Load(s.DB)
if err != nil {
s.serverError(w, err)
return
}
d := withBlankRows(p)
if len(p.Websites) > 0 {
d.DraftURL = p.Websites[0].URL
}
d.NoAI = !s.aiReady()
s.render(w, r, "profile", page{Title: "Company profile", Nav: "settings/profile", Data: d})
}
// profileFromForm reads the profile form. Repeated rows (products, websites)
// arrive as parallel lists, one value per row for each field.
func profileFromForm(r *http.Request) *profile.Profile {
f := r.PostForm
p := &profile.Profile{
Name: f.Get("name"),
Description: f.Get("description"),
Positioning: f.Get("positioning"),
Voice: f.Get("voice"),
Avoid: profile.Lines(f.Get("avoid")),
Keywords: profile.Lines(f.Get("keywords")),
Feeds: profile.Lines(f.Get("feeds")),
Targets: profile.Targets{
Industries: profile.Lines(f.Get("industries")),
CompanySizes: profile.Lines(f.Get("company_sizes")),
Roles: profile.Lines(f.Get("roles")),
Regions: profile.Lines(f.Get("regions")),
Problems: profile.Lines(f.Get("problems")),
BuyingSignals: profile.Lines(f.Get("buying_signals")),
Technologies: profile.Lines(f.Get("technologies")),
},
Accounts: profile.Accounts{
X: f.Get("account_x"),
LinkedIn: f.Get("account_linkedin"),
Reddit: f.Get("account_reddit"),
Subreddits: profile.Lines(f.Get("subreddits")),
},
}
at := func(key string, i int) string {
if v := f[key]; i < len(v) {
return v[i]
}
return ""
}
for i := range f["product_name"] {
p.Products = append(p.Products, profile.Product{
Name: at("product_name", i),
Description: at("product_description", i),
URL: at("product_url", i),
Pricing: at("product_pricing", i),
Benefits: profile.Lines(at("product_benefits", i)),
})
}
for i := range f["website_url"] {
p.Websites = append(p.Websites, profile.Website{
URL: at("website_url", i),
Repo: at("website_repo", i),
Branch: at("website_branch", i),
PlausibleSite: at("website_plausible", i),
SearchConsoleProperty: at("website_gsc", i),
BingSite: at("website_bing", i),
})
}
return p
}
func (s *Server) profileSave(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
p := profileFromForm(r)
p.Clean()
if !p.IsSet() {
s.render(w, r, "profile", page{Title: "Company profile", Nav: "settings/profile",
Error: "The company's name and description are required.", Data: withBlankRows(p)})
return
}
if err := profile.Save(s.DB, p); err != nil {
s.serverError(w, err)
return
}
redirect(w, r, "/settings/profile", "saved")
}
// profileDraft drafts the profile from a website and shows it in the form,
// unsaved. Fields the user already filled in keep their values.
func (s *Server) profileDraft(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
siteURL := r.PostForm.Get("draft_url")
current, err := profile.Load(s.DB)
if err != nil {
s.serverError(w, err)
return
}
show := func(p *profile.Profile, errMsg string, drafted bool) {
d := withBlankRows(p)
d.Drafted = drafted
d.DraftURL = siteURL
d.NoAI = !s.aiReady()
s.render(w, r, "profile", page{Title: "Company profile", Nav: "settings/profile", Error: errMsg, Data: d})
}
cfg, err := ai.LoadConfig(s.DB)
if err != nil {
s.serverError(w, err)
return
}
client, err := ai.New(cfg, s.Secrets)
if err != nil {
show(current, err.Error()+".", false)
return
}
ctx, cancel := context.WithTimeout(r.Context(), 4*time.Minute)
defer cancel()
draft, err := profile.Draft(ctx, client, siteURL)
if err != nil {
show(current, "Couldn't draft the profile: "+err.Error(), false)
return
}
show(merge(current, draft), "", true)
}
// merge keeps what the user already entered and fills the gaps from the draft.
func merge(cur, draft *profile.Profile) *profile.Profile {
out := *cur
pick := func(a, b string) string {
if a != "" {
return a
}
return b
}
pickList := func(a, b []string) []string {
if len(a) > 0 {
return a
}
return b
}
out.Name = pick(cur.Name, draft.Name)
out.Description = pick(cur.Description, draft.Description)
out.Positioning = pick(cur.Positioning, draft.Positioning)
out.Voice = pick(cur.Voice, draft.Voice)
out.Keywords = pickList(cur.Keywords, draft.Keywords)
if len(cur.Products) == 0 {
out.Products = draft.Products
}
if len(cur.Websites) == 0 {
out.Websites = draft.Websites
}
t, dt := &out.Targets, draft.Targets
t.Industries = pickList(t.Industries, dt.Industries)
t.CompanySizes = pickList(t.CompanySizes, dt.CompanySizes)
t.Roles = pickList(t.Roles, dt.Roles)
t.Regions = pickList(t.Regions, dt.Regions)
t.Problems = pickList(t.Problems, dt.Problems)
t.BuyingSignals = pickList(t.BuyingSignals, dt.BuyingSignals)
t.Technologies = pickList(t.Technologies, dt.Technologies)
out.Accounts.X = pick(cur.Accounts.X, draft.Accounts.X)
out.Accounts.LinkedIn = pick(cur.Accounts.LinkedIn, draft.Accounts.LinkedIn)
return &out
}
// aiReady reports whether an AI provider is set up.
func (s *Server) aiReady() bool {
cfg, err := ai.LoadConfig(s.DB)
if err != nil {
return false
}
_, err = ai.New(cfg, s.Secrets)
return err == nil
}