diff --git a/internal/handler/book.go b/internal/handler/book.go index a8bbcb3c..3f80ee42 100644 --- a/internal/handler/book.go +++ b/internal/handler/book.go @@ -54,7 +54,8 @@ type bookPageData struct { HostName string HostInitial string AvatarURL string - Hosts []hostDisplay // faces for the info panel (1 = single, >1 = group stack) + Hosts []hostDisplay // faces for the info panel (1 = single, >1 = group stack); capped at maxHostFaces + HostsMore int // >0 when the stack was capped: hosts not drawn, shown as a "+N" chip HostsLabel string // "Alex, Sam & 2 others" for the group case // SoleHostName is the host's name when this event type has exactly one, and "" when // it has several. It is what lets an empty day read "Alex has no available times on @@ -160,6 +161,29 @@ func (h *Handler) displayHosts(ctx context.Context, etID, mode string) []hostDis return out } +// maxHostFaces caps the host face stack in the info panel. The panel is 240px +// wide with 1.5rem padding (192px of content); each face is 56px and the stack +// overlaps by 16px, so four circles already fill 176px. Drawing every face of a +// six-host team overflowed the panel and spilled into the calendar (#166). Three +// faces plus a "+N" chip matches the label, which names at most three hosts. +const maxHostFaces = 3 + +// capHostFaces returns at most limit faces for drawing. +func capHostFaces(hosts []hostDisplay, limit int) []hostDisplay { + if limit <= 0 || len(hosts) <= limit { + return hosts + } + return hosts[:limit] +} + +// hostsOverflow counts the hosts capHostFaces left out, for the "+N" chip. +func hostsOverflow(hosts []hostDisplay, limit int) int { + if limit <= 0 || len(hosts) <= limit { + return 0 + } + return len(hosts) - limit +} + // hostsLabel renders a group host list as "Alex, Sam & 2 others" (first names), or // "Alex, Sam y 2 más" in Spanish. The separator and conjunction are locale keys, not // hardcoded punctuation — translating only the trailing noun (as this used to) produces @@ -522,7 +546,8 @@ func (h *Handler) BookPage(w http.ResponseWriter, r *http.Request) { HostName: hosts[0].Name, HostInitial: hosts[0].Initial, AvatarURL: hosts[0].AvatarURL, - Hosts: hosts, + Hosts: capHostFaces(hosts, maxHostFaces), + HostsMore: hostsOverflow(hosts, maxHostFaces), HostsLabel: hostsLabel(hosts, loc), SoleHostName: soleHostName(hosts), MinNoticeLabel: noticeLabel(minNotice, loc), diff --git a/internal/handler/embed.js b/internal/handler/embed.js index af5d4ea4..e45944a3 100644 --- a/internal/handler/embed.js +++ b/internal/handler/embed.js @@ -22,6 +22,8 @@ var BASE = SELF ? new URL(SELF.src).origin : window.location.origin; var TZ = (Intl.DateTimeFormat().resolvedOptions().timeZone) || 'UTC'; + // Max host faces drawn in the info panel; mirrors maxHostFaces in book.go. + var MAX_HOST_FACES = 3; var STEP_BP = 560; // below this width → step-flow (one view at a time) // i18n — the server resolves locale from the browser's own Accept-Language header @@ -289,13 +291,19 @@ hosts = sel.host_ids.map(function (id) { return hm[id]; }).filter(Boolean); } if (!hosts || !hosts.length) hosts = (this.info.hosts && this.info.hosts.length) ? this.info.hosts : []; - var faceKids = hosts.map(function (host, i) { - var z = (hosts.length - i) * 10; + // Cap the face stack so it cannot overflow the info panel into the calendar (#166). + // Keep in step with maxHostFaces in book.go and book.html's copy. + var shown = hosts.slice(0, MAX_HOST_FACES); + var faceKids = shown.map(function (host, i) { + var z = (shown.length - i) * 10; var inner = host.avatar_url ? el('img', { class: 'avatar-img', src: host.avatar_url, alt: host.name || '' }) : el('span', { class: 'avatar-initials', text: ((host.name || '?')[0] || '?').toUpperCase() }); return el('span', { class: 'face', style: 'z-index:' + z }, [inner]); }); + if (hosts.length > shown.length) { + faceKids.push(el('span', { class: 'face face-more', style: 'z-index:100', text: '+' + (hosts.length - shown.length) })); + } // info-head = avatar + title (host name + event name). On compact widths the // avatar centers against this title only; meta + description sit below, indented // to line up under the title. On desktop these wrappers are plain blocks, so the diff --git a/internal/handler/host_faces_book_test.go b/internal/handler/host_faces_book_test.go new file mode 100644 index 00000000..c420ed56 --- /dev/null +++ b/internal/handler/host_faces_book_test.go @@ -0,0 +1,68 @@ +package handler_test + +import ( + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +// The info panel is 240px wide with 1.5rem padding (192px of content). Each face +// is 56px and the stack overlaps by 16px, so six hosts is 256px — it spilled into +// the calendar column (#166). The page must draw at most three faces plus a "+N" +// chip, matching the label which already names only three hosts. +func TestBookPageCapsHostFacesAtThree(t *testing.T) { + h, database, key, ownerID := setupWorkspaceWithDB(t) + slug, _ := seedEventTypeHTTP(t, h, key) + + parts := []string{ + fmt.Sprintf(`{"user_id":%q,"role":"required","priority":0}`, ownerID), + } + for i := 2; i <= 6; i++ { + hostID := fmt.Sprintf("u%d", i) + if _, err := database.Exec( + `INSERT INTO users (id,email,name,iana_timezone,is_admin) VALUES (?,?,?, 'UTC',0)`, + hostID, hostID+"@example.com", "Host "+hostID); err != nil { + t.Fatalf("seed %s: %v", hostID, err) + } + parts = append(parts, fmt.Sprintf(`{"user_id":%q,"role":"required","priority":%d}`, hostID, i-1)) + } + if rec := putHosts(t, h, slug, key, `{"hosts":[`+strings.Join(parts, ",")+`]}`); rec.Code != http.StatusOK { + t.Fatalf("set hosts: %d — %s", rec.Code, rec.Body.String()) + } + + req := httptest.NewRequest(http.MethodGet, "/book/"+slug, nil) + req.SetPathValue("slug", slug) + rec := httptest.NewRecorder() + h.BookPage(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("book page: %d", rec.Code) + } + + block := hostFacesBlock(t, rec.Body.String()) + if got := strings.Count(block, `class="face"`); got != 3 { + t.Errorf("rendered %d avatar faces; want 3", got) + } + if !strings.Contains(block, `class="face face-more"`) { + t.Error("missing the \"+N\" overflow chip for 6 hosts") + } + if !strings.Contains(block, ">+3") { + t.Error("overflow chip should read +3 for 6 hosts") + } +} + +// hostFacesBlock returns the rendered #host-faces element only — the page's inline +// script contains the same class names in template literals. +func hostFacesBlock(t *testing.T, page string) string { + t.Helper() + start := strings.Index(page, `id="host-faces"`) + if start < 0 { + t.Fatal("no #host-faces element in rendered page") + } + end := strings.Index(page[start:], "") + if end < 0 { + t.Fatal("unterminated #host-faces element") + } + return page[start : start+end] +} diff --git a/internal/handler/host_faces_internal_test.go b/internal/handler/host_faces_internal_test.go new file mode 100644 index 00000000..6a93ffa3 --- /dev/null +++ b/internal/handler/host_faces_internal_test.go @@ -0,0 +1,41 @@ +package handler + +import ( + "testing" +) + +// mkHosts builds n placeholder faces. +func mkHosts(n int) []hostDisplay { + out := make([]hostDisplay, n) + for i := range out { + name := string(rune('A' + i)) + out[i] = hostDisplay{Name: name, Initial: name, Z: (n - i) * 10} + } + return out +} + +func TestCapHostFaces(t *testing.T) { + cases := []struct { + name string + n int + wantFaces int + wantOverflow int + }{ + {"none", 0, 0, 0}, + {"single", 1, 1, 0}, + {"at the limit", maxHostFaces, maxHostFaces, 0}, + {"one over", maxHostFaces + 1, maxHostFaces, 1}, + {"six hosts (issue #166)", 6, maxHostFaces, 3}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + hosts := mkHosts(tc.n) + if got := capHostFaces(hosts, maxHostFaces); len(got) != tc.wantFaces { + t.Errorf("capHostFaces len = %d; want %d", len(got), tc.wantFaces) + } + if got := hostsOverflow(hosts, maxHostFaces); got != tc.wantOverflow { + t.Errorf("hostsOverflow = %d; want %d", got, tc.wantOverflow) + } + }) + } +} diff --git a/internal/handler/templates/book.html b/internal/handler/templates/book.html index 618936b9..583489f5 100644 --- a/internal/handler/templates/book.html +++ b/internal/handler/templates/book.html @@ -130,6 +130,7 @@ {{end}} {{end}} + {{if .HostsMore}}+{{.HostsMore}}{{end}}
{{.HostsLabel}}