1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Renders the shell framing every admin page.
use topcoat::{
Result,
asset::Asset,
context::Cx,
font::Font,
icon::icon,
router::Slot,
runtime::{Event, Signal, signal},
view::{Attributes, BoxView, Child, ViewExt, attributes, internal::ThenView, view},
};
use super::{
Panel,
state::{ResourceTrail, current},
};
use crate::{navigation::NavigationItem, topcoat_compat::async_page};
/// `extra` plus the attribute that sends a sidebar link through runtime
/// navigation. The menu button writes the `href` itself, so this carries none.
fn sidebar_link(cx: &Cx, mut extra: Attributes) -> Attributes {
let mut attrs = crate::navigation::runtime_link(cx, "");
attrs.remove("href");
extra.extend(attrs);
extra
}
/// The application-owned assets used by [`Panel::layout_shell`].
///
/// Tailwind's generated stylesheet is necessarily a call-site asset because
/// every application scans a different source tree. The Panel therefore takes
/// the generated stylesheet and the application's chosen font as values while
/// still owning the document markup that links them.
#[derive(Debug, Clone, Copy)]
pub(crate) struct ShellAssets {
pub(crate) stylesheet: Asset,
pub(crate) font: Font,
}
impl Panel {
async fn sidebar_navigation<'a>(
cx: &'a Cx,
nav_items: &[NavigationItem],
current_path: &str,
current_query: &str,
mobile_open: Signal<bool>,
) -> Result<BoxView<'a>> {
use tablo_ui::{
sidebar_group, sidebar_group_content, sidebar_group_label, sidebar_menu,
sidebar_menu_badge, sidebar_menu_button, sidebar_menu_item,
};
// A Panel resolves every item it owns (`Panel::resource`, `page`, `home`); one that
// reaches the sidebar unresolved has no URL to render: a framework bug, not user error.
debug_assert!(
nav_items.iter().all(|item| item.url().is_some()),
"navigation items are resolved by the Panel that owns them"
);
let nav_items = sidebar::in_sidebar_order(nav_items);
// One active entry, chosen the way the breadcrumbs choose theirs.
let active = crate::navigation::current_index(&nav_items, current_path, current_query);
let sections = sidebar::sidebar_sections(nav_items);
Ok(view! {
for (label, entries) in sections {
sidebar_group(
sidebar_group_label((label))
sidebar_group_content(
sidebar_menu(
for (index, item) in entries {
let is_active = active == Some(index);
let label = item.label.localize(cx);
let attrs = sidebar_link(
cx,
attributes! {
// Tapping a link in the mobile sheet closes
// it; on desktop the navigation is the effect.
@click=$(|_e: Event| mobile_open.set(false))
},
);
sidebar_menu_item(
sidebar_menu_button(
active: is_active,
href: item.url(),
tooltip: Some(label.as_str()),
attrs: attrs,
if let Some(data) = item.icon.clone() {
icon(data: data)
}
<span>(label.clone())</span>
)
if let Some(badge) = item.badge.clone() {
sidebar_menu_badge((badge))
}
)
}
)
)
)
}
}
.boxed())
}
/// Whether the persisted [`SIDEBAR_COOKIE`] asks for an expanded
/// desktop sidebar (default: expanded).
///
/// The value only seeds the runtime
/// signal's initial `data-state`; after hydration the browser owns the
/// state, and `assets/sidebar.js` mirrors changes back to the cookie.
fn sidebar_starts_open(cx: &Cx) -> bool {
request_cookie(cx, SIDEBAR_COOKIE).as_deref() != Some("collapsed")
}
/// Renders the shell around `slot` with an optional outer class.
pub async fn render_shell<'a>(
cx: &'a Cx,
nav_items: &[NavigationItem],
current_path: &str,
slot: Child<'a>,
extra_class: Option<String>,
) -> Result<BoxView<'a>> {
// A hoisting body: the sidebar signals are declared while this view
// resolves, and a page re-run resumes them from the client. The owned
// copies pin the caller's navigation to the lazy body's lifetime.
let nav_items = nav_items.to_vec();
let current_path = current_path.to_string();
Ok(async_page(async move {
Self::render_shell_body(cx, &nav_items, ¤t_path, slot, extra_class).await
}))
}
async fn render_shell_body<'a>(
cx: &'a Cx,
nav_items: &[NavigationItem],
current_path: &str,
slot: Child<'a>,
extra_class: Option<String>,
) -> Result<BoxView<'a>> {
use tablo_ui::{
SeparatorOrientation, SidebarCollapsible, SidebarVariant, separator, sidebar,
sidebar_content, sidebar_footer, sidebar_header, sidebar_inset, sidebar_provider,
sidebar_trigger,
};
let sidebar_open = signal(cx, || Self::sidebar_starts_open(cx));
let mobile_open = signal(cx, || false);
let outer_class = extra_class.clone().unwrap_or_default();
let header_title = Self::brand_name(cx);
// One navigation tree: the upstream sidebar renders its children once
// and shares them between the desktop panel and the mobile sheet.
// The query the browser sent: an entry that names parameters the request carries wins
// over one that names none, at the same path.
let current_query = topcoat::router::request::original_uri(cx)
.query()
.unwrap_or_default();
let navigation = Self::sidebar_navigation(
cx,
nav_items,
current_path,
current_query,
mobile_open.clone(),
)
.await?;
let sidebar_brand = Self::render_brand(cx).await?;
let resources: Vec<&ResourceTrail> = current(cx)
.map(|panel| {
panel
.nav_items
.iter()
.filter_map(|entry| entry.resource_list.as_ref())
.collect()
})
.unwrap_or_default();
let trail = match super::error_page::current_error(cx) {
Some(error) => {
breadcrumbs::failed(nav_items, current_path, current_query, error.title())
}
None => breadcrumbs::trail(nav_items, &resources, current_path, current_query),
};
let area = current(cx).and_then(|panel| {
let area = panel.area_at(current_path)?;
Some((panel.areas[area].clone(), panel.realm.prefix.clone()))
});
let trail = match &area {
Some((area, _)) => breadcrumbs::within(area.label.clone(), &area.url, trail),
None => trail,
};
let back = area.map_or_else(|| ().boxed(), |(_, home)| area::back_link(cx, &home));
let crumbs = breadcrumbs::render(cx, trail);
// The user and tenant menus, present only with a session (ADR-0013). `ensure_token` runs
// before any streaming starts so their forms, and the language menu's, always carry a
// matching CSRF pair.
let (user_menu, tenant_menu): (BoxView<'_>, BoxView<'_>) = match crate::auth::signed(cx) {
Some(signed) => {
let csrf = crate::csrf::ensure_token(cx);
let items: Vec<NavigationItem> = user_menu::account_items(cx)
.into_iter()
.chain(
current(cx)
.map(|panel| panel.user_menu.clone())
.unwrap_or_default(),
)
.collect();
(
Self::user_menu(
cx,
signed.user.as_ref(),
&items,
crate::auth::logout_url(cx),
&csrf,
),
Self::tenant_menu(cx, signed.user.tenants(), &csrf),
)
}
// Without a session the footer still offers the theme, and the language where the
// panel has one to pick: only that menu's forms need a token here.
None => {
let csrf = if crate::i18n::offered(cx).len() > 1 {
crate::csrf::ensure_token(cx)
} else {
String::new()
};
(Self::theme_menu(cx, &csrf), ().boxed())
}
};
let toaster = Self::toaster(cx).await?;
Ok(view! {
sidebar_provider(
attrs: attributes! { class=(outer_class) },
// No `sidebar_rail`: the header's `sidebar_trigger` is the one toggle.
sidebar(
open: $(sidebar_open.get()),
mobile_open: $(mobile_open.get()),
collapsible: SidebarCollapsible::Icon,
variant: SidebarVariant::Inset,
sheet_attrs: attributes! {
id="mobile-sidebar-sheet"
aria-label=(crate::t!(cx, "tablo-shell-navigation"))
@keydown=$(|e: Event| {
if e.key == "Escape" {
mobile_open.set(false);
}
})
@click=$(|e: Event| {
if e.target.id == "mobile-sidebar-sheet" {
mobile_open.set(false);
}
})
},
sidebar_header(
// Collapsed to icons, the rail leaves the brand only its mark: centred,
// it lines up with the menu icons below.
<div
class="flex items-center gap-2 px-2 md:group-data-[collapsible=icon]/sidebar:justify-center md:group-data-[collapsible=icon]/sidebar:px-0"
>
(sidebar_brand)
// Below `md` the sheet covers the inset header,
// so the sheet's own header carries the close
// control (upstream `examples/ui`); the mobile
// trigger in the inset header is behind the veil.
tablo_ui::button(
variant: tablo_ui::ButtonVariant::Ghost,
size: tablo_ui::ButtonSize::Icon,
attrs: attributes! {
type="button"
class="md:hidden ml-auto"
aria-label=(crate::t!(cx, "tablo-shell-close-sidebar"))
@click=$(|_e: Event| mobile_open.set(false))
},
icon(data: tablo_ui::icons::X)
)
</div>
)
sidebar_content(
// The icon rail is too narrow for a scrollbar: it still scrolls, without
// one.
attrs: attributes! {
class="md:group-data-[collapsible=icon]/sidebar:[scrollbar-width:none]"
},
(tenant_menu)
(back)
(navigation)
)
sidebar_footer((user_menu))
)
sidebar_inset(
sidebar_header(
// The desktop trigger collapses the rail; below md the
// mobile trigger opens the sheet instead (shadcn
// SidebarTrigger pair, upstream `examples/ui`).
sidebar_trigger(
open: $(sidebar_open.get()),
attrs: attributes! {
class="max-md:hidden"
aria-controls="mobile-sidebar-sheet"
@click=$(|_e: Event| sidebar_open.toggle())
}
)
sidebar_trigger(
open: $(mobile_open.get()),
attrs: attributes! {
class="md:hidden"
aria-controls="mobile-sidebar-sheet"
@click=$(|_e: Event| mobile_open.toggle())
}
)
// The sidebar carries the brand on desktop; below md it hides in
// the sheet, so the topbar names the panel there.
<div
class="min-w-0 truncate font-semibold text-foreground md:hidden"
>
(header_title)
</div>
// The separator stays a direct child of the topbar,
// where `sidebar_inset` sizes a vertical rule.
separator(
orientation: SeparatorOrientation::Vertical,
attrs: attributes! { class="max-sm:hidden" }
)
(crumbs)
(super::search::topbar_end(cx))
)
// `sidebar_inset` is the document's one `<main>`; a second
// nested landmark is invalid and confuses landmark
// navigation (upstream `examples/ui` uses a plain div).
// The page container (`tablo_ui::page`) owns the width
// and the padding.
(super::impersonation::banner(cx))
<div class="flex flex-1 flex-col">(slot)</div>
)
(toaster)
) // Scripts are owned by the document (layout_shell).
}
.boxed())
}
/// The panel shell around a page, as a complete HTML document: the layout
/// every panel registers at its prefix unless [`Panel::layout`] replaces
/// it, and what a replacement calls to keep the shell around its own
/// markup.
///
/// The stylesheet and font are supplied to the Panel builder with
/// [`Self::shell_assets`]. A Panel without those values remains renderable
/// for tests and custom document owners, but does not pretend that a CSS
/// bundle exists. Errors from the page slot propagate unchanged when the
/// document view is resolved.
pub fn layout_shell<'a>(cx: &'a Cx, slot: Slot<'a>) -> BoxView<'a> {
Box::pin(ThenView::new(Self::render_layout_shell(cx, slot)))
}
async fn render_layout_shell<'a>(cx: &'a Cx, slot: Slot<'a>) -> Result<BoxView<'a>> {
// A page a creatable choice's dialog frames shows only itself and its toasts.
if super::url::picking(cx) {
let body = async_page(async move {
Ok(view! {
// `main` paints the frame; the block inside it is what the frame fits.
<main class="min-h-dvh bg-card text-card-foreground">
<div class="px-6 pt-6 pb-6" data-pick-page="">(slot)</div>
</main>
(Self::toaster(cx).await?)
}
.boxed())
});
let panel = current(cx).map(|panel| &**panel);
return Self::render_document(cx, panel, Self::brand_name(cx), body).await;
}
// The browser's URL, not a rewrite's: the error page names where the visitor is.
let path = topcoat::router::request::original_uri(cx)
.path()
.to_string();
// The sidebar of the area the path is under, else the panel's, or a single Home entry
// outside any panel.
let panel = current(cx);
let entries: Vec<_> = panel
.map(|panel| area::listed(cx, panel, panel.area_at(&path)))
.unwrap_or_default();
// One count per badge, run together rather than one after another.
let badges = futures_util::future::join_all(entries.iter().map(|entry| async {
match &entry.badge {
Some(badge) => Some(badge(cx).await),
None => None,
}
}))
.await;
crate::notification::prefetch_bell(cx).await;
let mut nav_items: Vec<NavigationItem> = entries
.iter()
.zip(badges)
.map(|(entry, badge)| {
let mut item = area::item(cx, entry);
if let Some(badge) = badge {
item.badge = badge;
}
item
})
.collect();
if nav_items.is_empty() {
nav_items.push(NavigationItem::at(
crate::t!(cx, "tablo-shell-home"),
super::gate::panel_prefix(cx),
));
}
let shell = Self::render_shell(cx, &nav_items, &path, slot, None).await?;
Self::render_document(
cx,
current(cx).map(|panel| &**panel),
Self::brand_name(cx),
shell,
)
.await
}
}
mod area;
mod brand;
mod breadcrumbs;
mod document;
mod environment;
mod language;
pub(super) mod sidebar;
mod theme;
mod toaster;
mod user_menu;
pub(in crate::panel) use area::area_entries;
pub use brand::{Brand, Radius};
pub use environment::Environment;
pub(crate) use language::{LANGUAGE_SEGMENT, language_post};
use theme::{SIDEBAR_COOKIE, Theme};
use crate::cookie::request_cookie;