Consolidate post header/menu system into shared infrastructure
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m29s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m29s
Replace duplicated _post_header_html, _oob_header_html, and header-child components across blog/events/market/errors with shared sexpr components (~post-label, ~page-cart-badge, ~oob-header, ~header-child, ~error-content) and shared Python helpers (post_header_html, oob_header_html, header_child_html, error_content_html). App-specific logic (blog container-nav wrapping, admin cog, events calendar links) preserved via thin wrappers. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -83,8 +83,7 @@ async def _rich_error_page(errnum: str, message: str, image: str | None = None)
|
||||
return None
|
||||
|
||||
try:
|
||||
from shared.sexp.jinja_bridge import render
|
||||
from shared.sexp.helpers import full_page, call_url
|
||||
from shared.sexp.helpers import full_page
|
||||
|
||||
# Build a minimal context — avoid get_template_context() which
|
||||
# calls cross-service fragment fetches that may fail.
|
||||
@@ -150,44 +149,22 @@ async def _rich_error_page(errnum: str, message: str, image: str | None = None)
|
||||
pass
|
||||
|
||||
# Root header (site nav bar)
|
||||
from shared.sexp.helpers import root_header_html
|
||||
from shared.sexp.helpers import (
|
||||
root_header_html, post_header_html,
|
||||
header_child_html, error_content_html,
|
||||
)
|
||||
hdr = root_header_html(ctx)
|
||||
|
||||
# Post breadcrumb if we resolved a post
|
||||
post = (post_data or {}).get("post") or ctx.get("post") or {}
|
||||
if post.get("slug"):
|
||||
title = (post.get("title") or "")[:160]
|
||||
feature_image = post.get("feature_image")
|
||||
label_html = ""
|
||||
if feature_image:
|
||||
label_html += (
|
||||
f'<img src="{feature_image}" '
|
||||
f'class="h-8 w-8 rounded-full object-cover border border-stone-300 flex-shrink-0">'
|
||||
)
|
||||
label_html += f"<span>{escape(title)}</span>"
|
||||
post_row = render(
|
||||
"menu-row",
|
||||
id="post-row", level=1,
|
||||
link_href=call_url(ctx, "blog_url", f"/{post['slug']}/"),
|
||||
link_label_html=label_html,
|
||||
child_id="post-header-child",
|
||||
external=True,
|
||||
)
|
||||
hdr += (
|
||||
f'<div id="root-header-child" class="w-full">'
|
||||
f'{post_row}'
|
||||
f'</div>'
|
||||
)
|
||||
ctx["post"] = post
|
||||
post_row = post_header_html(ctx)
|
||||
if post_row:
|
||||
hdr += header_child_html(post_row)
|
||||
|
||||
# Error content
|
||||
error_html = (
|
||||
'<div class="text-center p-8 max-w-lg mx-auto">'
|
||||
f'<div class="font-bold text-2xl md:text-4xl text-red-500 mb-4">{errnum}</div>'
|
||||
f'<div class="text-stone-600 mb-4">{escape(message)}</div>'
|
||||
)
|
||||
if image:
|
||||
error_html += f'<div class="flex justify-center"><img src="{image}" width="300" height="300"></div>'
|
||||
error_html += "</div>"
|
||||
error_html = error_content_html(errnum, message, image)
|
||||
|
||||
return full_page(ctx, header_rows_html=hdr, content_html=error_html)
|
||||
except Exception:
|
||||
|
||||
@@ -67,6 +67,59 @@ def search_desktop_html(ctx: dict) -> str:
|
||||
)
|
||||
|
||||
|
||||
def post_header_html(ctx: dict, *, oob: bool = False) -> str:
|
||||
"""Build the post-level header row (level 1). Used by all apps + error pages."""
|
||||
post = ctx.get("post") or {}
|
||||
slug = post.get("slug", "")
|
||||
if not slug:
|
||||
return ""
|
||||
title = (post.get("title") or "")[:160]
|
||||
feature_image = post.get("feature_image")
|
||||
|
||||
label_html = render("post-label", feature_image=feature_image, title=title)
|
||||
|
||||
nav_parts: list[str] = []
|
||||
page_cart_count = ctx.get("page_cart_count", 0)
|
||||
if page_cart_count and page_cart_count > 0:
|
||||
cart_href = call_url(ctx, "cart_url", f"/{slug}/")
|
||||
nav_parts.append(render("page-cart-badge", href=cart_href, count=str(page_cart_count)))
|
||||
|
||||
container_nav = ctx.get("container_nav_html", "")
|
||||
if container_nav:
|
||||
nav_parts.append(container_nav)
|
||||
|
||||
admin_nav = ctx.get("post_admin_nav_html", "")
|
||||
if admin_nav:
|
||||
nav_parts.append(admin_nav)
|
||||
|
||||
nav_html = "".join(nav_parts)
|
||||
link_href = call_url(ctx, "blog_url", f"/{slug}/")
|
||||
|
||||
return render("menu-row",
|
||||
id="post-row", level=1,
|
||||
link_href=link_href, link_label_html=label_html,
|
||||
nav_html=nav_html, child_id="post-header-child",
|
||||
oob=oob, external=True,
|
||||
)
|
||||
|
||||
|
||||
def oob_header_html(parent_id: str, child_id: str, row_html: str) -> str:
|
||||
"""Wrap a header row in an OOB swap div with child placeholder."""
|
||||
return render("oob-header",
|
||||
parent_id=parent_id, child_id=child_id, row_html=row_html,
|
||||
)
|
||||
|
||||
|
||||
def header_child_html(inner_html: str, *, id: str = "root-header-child") -> str:
|
||||
"""Wrap inner HTML in a header-child div."""
|
||||
return render("header-child", id=id, inner_html=inner_html)
|
||||
|
||||
|
||||
def error_content_html(errnum: str, message: str, image: str | None = None) -> str:
|
||||
"""Render the error content block."""
|
||||
return render("error-content", errnum=errnum, message=message, image=image)
|
||||
|
||||
|
||||
def full_page(ctx: dict, *, header_rows_html: str,
|
||||
filter_html: str = "", aside_html: str = "",
|
||||
content_html: str = "", menu_html: str = "",
|
||||
|
||||
@@ -146,6 +146,32 @@
|
||||
(div :id child-id :class "flex flex-col w-full items-center"
|
||||
(when child-html (raw! child-html)))))))
|
||||
|
||||
(defcomp ~post-label (&key feature-image title)
|
||||
(<> (when feature-image
|
||||
(img :src feature-image :class "h-8 w-8 rounded-full object-cover border border-stone-300 flex-shrink-0"))
|
||||
(span title)))
|
||||
|
||||
(defcomp ~page-cart-badge (&key href count)
|
||||
(a :href href :class "relative inline-flex items-center gap-1.5 px-3 py-1.5 text-sm rounded-full border border-emerald-300 bg-emerald-50 text-emerald-800 hover:bg-emerald-100 transition"
|
||||
(i :class "fa fa-shopping-cart" :aria-hidden "true")
|
||||
(span count)))
|
||||
|
||||
(defcomp ~oob-header (&key parent-id child-id row-html)
|
||||
(div :id parent-id :hx-swap-oob "outerHTML" :class "w-full"
|
||||
(div :class "w-full" (raw! row-html)
|
||||
(div :id child-id))))
|
||||
|
||||
(defcomp ~header-child (&key id inner-html)
|
||||
(div :id (or id "root-header-child") :class "w-full" (raw! inner-html)))
|
||||
|
||||
(defcomp ~error-content (&key errnum message image)
|
||||
(div :class "text-center p-8 max-w-lg mx-auto"
|
||||
(div :class "font-bold text-2xl md:text-4xl text-red-500 mb-4" errnum)
|
||||
(div :class "text-stone-600 mb-4" message)
|
||||
(when image
|
||||
(div :class "flex justify-center"
|
||||
(img :src image :width "300" :height "300")))))
|
||||
|
||||
(defcomp ~nav-link (&key href hx-select label icon aclass select-colours is-selected)
|
||||
(div :class "relative nav-group"
|
||||
(a :href href
|
||||
|
||||
Reference in New Issue
Block a user