Add register_sx_layout infrastructure, convert account/federation/orders

Phase 0: Add _ctx_to_env() and render_to_sx_with_env() to shared/sx/helpers.py,
register_sx_layout() to shared/sx/layouts.py, and ~root-header/~root-mobile
wrapper defcomps to layout.sx. Convert built-in "root" layout to .sx.

Phases 1-3: Convert account (65→19 lines), federation (105→97 lines),
and orders (88→21 lines) to use register_sx_layout with .sx defcomps
that read ctx values as free variables from the evaluation environment.
No more Python building SX strings via SxExpr(await root_header_sx(ctx)).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-04 14:39:53 +00:00
parent 28388540d5
commit 45c5e4a0db
9 changed files with 244 additions and 229 deletions

49
orders/sx/layouts.sx Normal file
View File

@@ -0,0 +1,49 @@
;; Orders layout defcomps — read ctx values from env free variables.
;; Registered via register_sx_layout("orders", ...) in __init__.py.
;; --- orders layout: root + auth + orders rows ---
(defcomp ~orders-layout-full (&key list-url)
(<> (~root-header)
(~header-child-sx
:inner (<> (~auth-header-row :account-url account-url
:select-colours select-colours
:account-nav account-nav)
(~orders-header-row :list-url (or list-url "/"))))))
(defcomp ~orders-layout-oob (&key list-url)
(<> (~auth-header-row :account-url account-url
:select-colours select-colours
:account-nav account-nav
:oob true)
(~oob-header-sx
:parent-id "auth-header-child"
:row (~orders-header-row :list-url (or list-url "/")))
(~root-header :oob true)))
(defcomp ~orders-layout-mobile ()
(~root-mobile))
;; --- order-detail layout: root + auth + orders + order rows ---
(defcomp ~order-detail-layout-full (&key list-url detail-url)
(<> (~root-header)
(~order-detail-header-stack
:auth (~auth-header-row :account-url account-url
:select-colours select-colours
:account-nav account-nav)
:orders (~orders-header-row :list-url (or list-url "/"))
:order (~menu-row-sx :id "order-row" :level 3 :colour "sky"
:link-href (or detail-url "/") :link-label "Order"
:icon "fa fa-gbp"))))
(defcomp ~order-detail-layout-oob (&key detail-url)
(<> (~oob-header-sx
:parent-id "orders-header-child"
:row (~menu-row-sx :id "order-row" :level 3 :colour "sky"
:link-href (or detail-url "/") :link-label "Order"
:icon "fa fa-gbp" :oob true))
(~root-header :oob true)))
(defcomp ~order-detail-layout-mobile ()
(~root-mobile))

View File

@@ -1,8 +1,6 @@
"""Orders defpage setup — registers layouts and loads .sx pages."""
from __future__ import annotations
from typing import Any
def setup_orders_pages() -> None:
"""Register orders-specific layouts and load page definitions."""
@@ -17,110 +15,7 @@ def _load_orders_page_files() -> None:
load_page_dir(os.path.dirname(__file__), "orders")
# ---------------------------------------------------------------------------
# Layouts
# ---------------------------------------------------------------------------
def _register_orders_layouts() -> None:
from shared.sx.layouts import register_custom_layout
register_custom_layout("orders", _orders_full, _orders_oob, _orders_mobile)
register_custom_layout("order-detail", _order_detail_full, _order_detail_oob, _order_detail_mobile)
async def _orders_full(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, header_child_sx, call_url, render_to_sx
list_url = kw.get("list_url", "/")
account_url = call_url(ctx, "account_url", "")
root_hdr = await root_header_sx(ctx)
auth_hdr = await render_to_sx("auth-header-row",
account_url=account_url,
select_colours=ctx.get("select_colours", ""),
account_nav=_as_sx_nav(ctx),
)
orders_hdr = await render_to_sx("orders-header-row", list_url=list_url)
inner = "(<> " + auth_hdr + " " + orders_hdr + ")"
return "(<> " + root_hdr + " " + await header_child_sx(inner) + ")"
async def _orders_oob(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, render_to_sx
from shared.sx.helpers import call_url
from shared.sx.parser import SxExpr
list_url = kw.get("list_url", "/")
account_url = call_url(ctx, "account_url", "")
auth_hdr = await render_to_sx("auth-header-row",
account_url=account_url,
select_colours=ctx.get("select_colours", ""),
account_nav=_as_sx_nav(ctx),
oob=True,
)
orders_hdr = await render_to_sx("orders-header-row", list_url=list_url)
auth_child_oob = await render_to_sx("oob-header-sx",
parent_id="auth-header-child",
row=SxExpr(orders_hdr))
root_hdr = await root_header_sx(ctx, oob=True)
return "(<> " + auth_hdr + " " + auth_child_oob + " " + root_hdr + ")"
async def _orders_mobile(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import mobile_menu_sx, mobile_root_nav_sx
return mobile_menu_sx(await mobile_root_nav_sx(ctx))
async def _order_detail_full(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, render_to_sx
from shared.sx.helpers import call_url
from shared.sx.parser import SxExpr
list_url = kw.get("list_url", "/")
detail_url = kw.get("detail_url", "/")
account_url = call_url(ctx, "account_url", "")
root_hdr = await root_header_sx(ctx)
order_row = await render_to_sx(
"menu-row-sx",
id="order-row", level=3, colour="sky", link_href=detail_url,
link_label="Order", icon="fa fa-gbp",
)
auth_hdr = await render_to_sx("auth-header-row",
account_url=account_url,
select_colours=ctx.get("select_colours", ""),
account_nav=_as_sx_nav(ctx),
)
orders_hdr = await render_to_sx("orders-header-row", list_url=list_url)
detail_header = await render_to_sx(
"order-detail-header-stack",
auth=SxExpr(auth_hdr),
orders=SxExpr(orders_hdr),
order=SxExpr(order_row),
)
return "(<> " + root_hdr + " " + detail_header + ")"
async def _order_detail_oob(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, render_to_sx
from shared.sx.parser import SxExpr
detail_url = kw.get("detail_url", "/")
order_row_oob = await render_to_sx(
"menu-row-sx",
id="order-row", level=3, colour="sky", link_href=detail_url,
link_label="Order", icon="fa fa-gbp", oob=True,
)
header_child_oob = await render_to_sx("oob-header-sx",
parent_id="orders-header-child",
row=SxExpr(order_row_oob))
root_hdr = await root_header_sx(ctx, oob=True)
return "(<> " + header_child_oob + " " + root_hdr + ")"
async def _order_detail_mobile(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import mobile_menu_sx, mobile_root_nav_sx
return mobile_menu_sx(await mobile_root_nav_sx(ctx))
def _as_sx_nav(ctx: dict) -> Any:
"""Convert account_nav fragment to SxExpr for use in component calls."""
from shared.sx.helpers import _as_sx
return _as_sx(ctx.get("account_nav"))
from shared.sx.layouts import register_sx_layout
register_sx_layout("orders", "orders-layout-full", "orders-layout-oob", "orders-layout-mobile")
register_sx_layout("order-detail", "order-detail-layout-full", "order-detail-layout-oob", "order-detail-layout-mobile")