euv-example 0.37.13

An example application demonstrating the euv UI framework with reactive signals, custom components, and WebAssembly.
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
use super::*;

/// Renders the desktop layout with a persistent left sidebar navigation.
///
/// # Arguments
///
/// - `VirtualNode<DesktopLayoutProps>` - The typed props containing route, theme, root class, and panel signals.
///
/// # Returns
///
/// - `VirtualNode` - The desktop application shell virtual DOM tree.
#[component]
pub(crate) fn desktop_layout(node: VirtualNode<DesktopLayoutProps>) -> VirtualNode {
    let DesktopLayoutProps {
        route_signal,
        theme_signal,
        root_class_signal,
        panel_open,
    }: DesktopLayoutProps = node.try_get_props().unwrap_or_default();
    let theme_feature_signal: Signal<String> =
        App::use_signal(|| current_theme_feature().to_string());
    html! {
        div {
            class: root_class_signal
            class: c_example_theme()
            nav {
                class: c_app_nav()
                a {
                    href: GITHUB_URL
                    target: LINK_TARGET_BLANK
                    onclick: Router::external_link_handler(GITHUB_URL)
                    class: c_nav_header()
                    euv_logo {
                        variant: LogoButtonVariant::Nav
                    }
                    span {
                        class: c_nav_brand_title()
                        "Euv"
                    }
                }
                p {
                    class: c_nav_section_label()
                    NAV_SECTION_LABEL_PAGES
                }
                build_desktop_nav_items {
                    route_signal: route_signal
                }
                theme_feature_selector {
                    value: theme_feature_signal
                }
                div {
                    class: c_nav_theme_toggle()
                    span {
                        class: c_nav_scheme_label()
                        scheme_name()
                    }
                    button {
                        class: c_nav_theme_button()
                        "aria-label": APP_THEME_TOGGLE_LABEL
                        onclick: ThemeState::toggle(theme_signal)
                        div {
                            "aria-hidden": "true"
                            class: if { theme_signal.get() == THEME_DARK } {
                                c_theme_icon_sun()
                            } else {
                                c_theme_icon_moon()
                            }
                        }
                    }
                }
            }
            main {
                class: c_app_main()
                page_router {
                    route_signal
                }
            }
            euv_vconsole_panel {
                panel_open
            }
        }
    }
}

/// Renders the mobile layout with a top header bar and a slide-out navigation drawer.
///
/// # Arguments
///
/// - `VirtualNode<MobileLayoutProps>` - The typed props containing route, theme, root class, panel, and drawer signals.
///
/// # Returns
///
/// - `VirtualNode` - The mobile application shell virtual DOM tree.
#[component]
pub(crate) fn mobile_layout(node: VirtualNode<MobileLayoutProps>) -> VirtualNode {
    let MobileLayoutProps {
        route_signal,
        theme_signal,
        root_class_signal,
        panel_open,
        drawer_open,
    }: MobileLayoutProps = node.try_get_props().unwrap_or_default();
    let theme_feature_signal: Signal<String> =
        App::use_signal(|| current_theme_feature().to_string());
    let feature_signal_for_change: Signal<String> = theme_feature_signal;
    let on_theme_feature_change: Option<Rc<dyn Fn(Event)>> = Some(Rc::new(move |event: Event| {
        let selected_feature: Option<String> = event
            .target()
            .and_then(|target: EventTarget| target.dyn_into::<HtmlSelectElement>().ok())
            .map(|select: HtmlSelectElement| select.value());
        if let Some(feature) = selected_feature {
            feature_signal_for_change.set(feature.clone());
            navigate_to_theme_feature(&feature);
        }
    }));
    let on_overlay_click: Box<dyn FnMut(Event)> = Box::new(move |_: Event| {
        Router::overlay_stack_close();
        drawer_open.set(false);
    });
    let on_drawer_close_click: Box<dyn FnMut(Event)> = Box::new(move |_: Event| {
        Router::overlay_stack_close();
        drawer_open.set(false);
    });
    html! {
        div {
            class: root_class_signal
            class: c_example_theme()
            header {
                class: c_example_mobile_header()
                div {
                    class: c_example_mobile_header_left()
                    button {
                        class: if { drawer_open.get() } {
                            c_mobile_menu_button_active()
                        } else {
                            c_mobile_menu_button()
                        }
                        onclick: UseEuvLayout::use_drawer_toggle(drawer_open)
                        "☰"
                    }
                    a {
                        href: GITHUB_URL
                        target: LINK_TARGET_BLANK
                        onclick: Router::external_link_handler(GITHUB_URL)
                        class: c_mobile_header_logo()
                        euv_logo {
                            variant: LogoButtonVariant::Nav
                        }
                        span {
                            class: c_nav_brand_title()
                            "Euv"
                        }
                    }
                }
                div {
                    class: c_mobile_header_actions()
                    div {
                        class: c_mobile_theme_feature_selector()
                        euv_native_select {
                            size: EuvNativeSelectSize::Sm
                            id: THEME_FEATURE_SELECTOR_ID
                            aria_label: THEME_FEATURE_SELECTOR_LABEL.to_string()
                            value: theme_feature_signal
                            onchange: on_theme_feature_change
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[0].0
                                label: THEME_FEATURE_OPTIONS[0].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[1].0
                                label: THEME_FEATURE_OPTIONS[1].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[2].0
                                label: THEME_FEATURE_OPTIONS[2].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[3].0
                                label: THEME_FEATURE_OPTIONS[3].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[4].0
                                label: THEME_FEATURE_OPTIONS[4].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[5].0
                                label: THEME_FEATURE_OPTIONS[5].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[6].0
                                label: THEME_FEATURE_OPTIONS[6].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[7].0
                                label: THEME_FEATURE_OPTIONS[7].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[8].0
                                label: THEME_FEATURE_OPTIONS[8].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[9].0
                                label: THEME_FEATURE_OPTIONS[9].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[10].0
                                label: THEME_FEATURE_OPTIONS[10].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[11].0
                                label: THEME_FEATURE_OPTIONS[11].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[12].0
                                label: THEME_FEATURE_OPTIONS[12].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[13].0
                                label: THEME_FEATURE_OPTIONS[13].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[14].0
                                label: THEME_FEATURE_OPTIONS[14].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[15].0
                                label: THEME_FEATURE_OPTIONS[15].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[16].0
                                label: THEME_FEATURE_OPTIONS[16].0.trim_start_matches("theme-")
                            }
                            euv_native_select_option {
                                value: THEME_FEATURE_OPTIONS[17].0
                                label: THEME_FEATURE_OPTIONS[17].0.trim_start_matches("theme-")
                            }
                        }
                    }
                    button {
                        class: c_mobile_theme_button()
                        "aria-label": APP_THEME_TOGGLE_LABEL
                        onclick: ThemeState::toggle(theme_signal)
                        div {
                            "aria-hidden": "true"
                            class: if { theme_signal.get() == THEME_DARK } {
                                c_theme_icon_sun()
                            } else {
                                c_theme_icon_moon()
                            }
                        }
                    }
                }
            }
            main {
                class: c_mobile_main()
                page_router {
                    route_signal
                }
            }
            euv_vconsole_panel {
                panel_open
            }
            div {
                class: if { drawer_open.get() } {
                    c_mobile_overlay().to_string()
                } else {
                    format!("{} {}", c_mobile_overlay().get_name(), c_mobile_overlay_hidden().get_name())
                }
                onclick: on_overlay_click
            }
            nav {
                class: if { drawer_open.get() } {
                    c_mobile_nav_drawer().to_string()
                } else {
                    format!("{} {}", c_mobile_nav_drawer().get_name(), c_mobile_nav_drawer_closed().get_name())
                }
                div {
                    class: c_mobile_nav_drawer_header()
                    div {
                        class: c_mobile_header_logo()
                        a {
                            href: GITHUB_URL
                            target: LINK_TARGET_BLANK
                            onclick: Router::external_link_handler(GITHUB_URL)
                            class: c_mobile_header_logo()
                            euv_logo {
                                variant: LogoButtonVariant::Nav
                            }
                            span {
                                class: c_nav_brand_title()
                                "Euv"
                            }
                        }
                    }
                    button {
                        class: c_mobile_drawer_close_button()
                        onclick: on_drawer_close_click
                        "✕"
                    }
                }
                p {
                    class: c_nav_section_label()
                    NAV_SECTION_LABEL_PAGES
                }
                build_mobile_nav_items {
                    route_signal: route_signal
                    drawer_open: drawer_open
                }
            }
        }
    }
}

/// Renders the sidebar selector for every independently compiled theme variant.
///
/// # Arguments
///
/// - `VirtualNode<ThemeFeatureSelectorProps>` - The current build's feature value.
///
/// # Returns
///
/// - `VirtualNode` - The labelled selector that reloads into the selected build.
#[component]
pub(crate) fn theme_feature_selector(node: VirtualNode<ThemeFeatureSelectorProps>) -> VirtualNode {
    let ThemeFeatureSelectorProps { value }: ThemeFeatureSelectorProps =
        node.try_get_props().unwrap_or_default();
    let options: Vec<EuvSelectOption> = THEME_FEATURE_OPTIONS
        .iter()
        .copied()
        .map(
            |(value, label): (&'static str, &'static str)| EuvSelectOption {
                value,
                label,
                group: "",
                separator_before: false,
                disabled: false,
            },
        )
        .collect();
    let on_change: Option<Rc<dyn Fn(&'static str)>> = Some(Rc::new(|feature: &'static str| {
        navigate_to_theme_feature(feature)
    }));
    html! {
        div {
            class: c_nav_theme_feature_selector()
            euv_select {
                value: value
                options: options
                size: EuvSelectSize::Sm
                label: THEME_FEATURE_SELECTOR_LABEL
                aria_label: THEME_FEATURE_SELECTOR_LABEL.to_string()
                id: THEME_FEATURE_SELECTOR_ID
                on_change: on_change
            }
        }
    }
}

/// Navigates to the separately built page for a selected theme feature.
///
/// # Arguments
///
/// - `&str` - The `default` selector value or a declared `theme-*` feature.
///
/// # Returns
///
/// - `()` - No value; attempts navigation and reports browser errors.
fn navigate_to_theme_feature(feature: &str) {
    let Some(window_value): Option<Window> = window() else {
        Console::error("Cannot switch theme feature without a browser window.");
        return;
    };
    let location: Location = window_value.location();
    let pathname: String = match location.pathname() {
        Ok(value) => value,
        Err(error) => {
            Console::error(format!(
                "Cannot read the current path while switching theme feature: {}",
                error.as_string().unwrap_or(ERROR_NULL_TEXT.to_string())
            ));
            return;
        }
    };
    let base_path: String = theme_feature_base_path(&pathname);
    let hash: String = location.hash().unwrap_or_default();
    let target_url: String = format!(
        "{base_path}/{THEME_FEATURES_DIRECTORY}/{feature}/{THEME_FEATURE_INDEX_FILE}{hash}"
    );
    if let Err(error) = location.set_href(&target_url) {
        Console::error(format!(
            "Failed to switch to theme feature {feature}: {}",
            error.as_string().unwrap_or(ERROR_NULL_TEXT.to_string())
        ));
    }
}

/// Returns the feature that was enabled for the current example build.
///
/// # Returns
///
/// - `&'static str` - The current `theme-*` feature, or `default` when none is enabled.
fn current_theme_feature() -> &'static str {
    let selected_scheme: &str = scheme_name();
    let feature: &str = THEME_FEATURE_OPTIONS
        .iter()
        .find_map(|(feature, _): &(&'static str, &'static str)| {
            (feature.strip_prefix(THEME_FEATURE_PREFIX) == Some(selected_scheme))
                .then_some(*feature)
        })
        .unwrap_or(THEME_FEATURE_DEFAULT);
    if feature == THEME_FEATURE_MONOCHROME && !cfg!(feature = "theme-monochrome") {
        return THEME_FEATURE_DEFAULT;
    }
    feature
}

/// Finds the site root shared by the root page and all variant build pages.
///
/// # Arguments
///
/// - `&str` - The current page path from `window.location.pathname`.
///
/// # Returns
///
/// - `String` - The path prefix preceding the variant directory or entry file.
fn theme_feature_base_path(pathname: &str) -> String {
    let variant_root: Option<(&str, &str)> = pathname.split_once(THEME_FEATURES_PATH_SEPARATOR);
    if let Some((root_path, _variant_path)) = variant_root {
        return root_path.to_string();
    }
    match pathname.rsplit_once('/') {
        Some((root_path, _entry_file)) => root_path.to_string(),
        None => String::new(),
    }
}

/// Asynchronously fetches the docs status with retry logic.
///
/// Attempts to fetch the crate status JSON up to `VERSION_FETCH_MAX_RETRY_COUNT` times,
/// waiting `VERSION_FETCH_RETRY_DELAY_MS` milliseconds between each retry on failure.
///
/// # Returns
///
/// - `Result<DocsStatus, ()>` - The parsed docs status on success, or an error indicator on failure.
async fn fetch_docs_status_with_retry() -> Result<DocsStatus, ()> {
    let Some(window_value): Option<Window> = window() else {
        return Err(());
    };
    let mut attempt: u32 = 0;
    loop {
        let promise: Promise = window_value.fetch_with_str(DOCS_STATUS_URL);
        match JsFuture::from(promise).await {
            Ok(response) => {
                let response_value: Response = match response.dyn_into() {
                    Ok(value) => value,
                    Err(error) => {
                        Console::error(format!(
                            "Failed to convert fetch response: {}",
                            error.as_string().unwrap_or(ERROR_NULL_TEXT.to_string())
                        ));
                        return Err(());
                    }
                };
                let text_string: String = match response_value.text() {
                    Ok(promise) => match JsFuture::from(promise).await {
                        Ok(value) => value.as_string().unwrap_or_default(),
                        Err(error) => {
                            Console::error(format!(
                                "Failed to read response text: {}",
                                error.as_string().unwrap_or(ERROR_NULL_TEXT.to_string())
                            ));
                            return Err(());
                        }
                    },
                    Err(error) => {
                        Console::error(format!(
                            "Failed to get response text promise: {}",
                            error.as_string().unwrap_or(ERROR_NULL_TEXT.to_string())
                        ));
                        return Err(());
                    }
                };
                Console::log(&text_string);
                return Ok(serde_json::from_str::<DocsStatus>(&text_string).unwrap_or_default());
            }
            Err(error) => {
                attempt += 1;
                if attempt >= VERSION_FETCH_MAX_RETRY_COUNT {
                    Console::error(format!(
                        "Failed to fetch version status after {VERSION_FETCH_MAX_RETRY_COUNT} attempts: {}",
                        error.as_string().unwrap_or(ERROR_NULL_TEXT.to_string())
                    ));
                    return Err(());
                }
                Console::warn(format!(
                    "Failed to fetch version status (attempt {attempt}/{VERSION_FETCH_MAX_RETRY_COUNT}): {}. Retrying in {VERSION_FETCH_RETRY_DELAY_MS}ms...",
                    error.as_string().unwrap_or(ERROR_NULL_TEXT.to_string())
                ));
                sleep_ms(VERSION_FETCH_RETRY_DELAY_MS).await;
            }
        }
    }
}

/// Asynchronously checks for euv documentation updates from docs.rs.
///
/// Fetches the crate status JSON with retry logic, parses the `DocsStatus` payload,
/// and if a newer version is available, invokes the bridge `update_cache` command
/// to synchronize the local cache. The bridge invocation is itself retried up to
/// `VIEW_UPDATE_RETRY_COUNT` times (with `VIEW_UPDATE_RETRY_DELAY_MS` between
/// attempts) when the native side reports a failure or the JS-side invoke rejects.
///
/// # Returns
///
/// - `UpdateResult` - The documentation status, version, and whether an update was triggered.
async fn check_docs_update() -> UpdateResult {
    let parsed: DocsStatus = match fetch_docs_status_with_retry().await {
        Ok(status) => status,
        Err(()) => {
            return UpdateResult {
                doc_status: false,
                version: String::new(),
                updating: false,
                data: String::new(),
                message: MESSAGE_FETCH_DOCS_STATUS_FAILED.to_string(),
            };
        }
    };
    if !matches!(
        CompareVersion::compare_version(parsed.get_version(), EUV_VERSION),
        Ok(VersionLevel::Greater)
    ) {
        Console::log(format!(
            "Current version v{EUV_VERSION} is already the latest version"
        ));
        return UpdateResult {
            doc_status: parsed.get_doc_status(),
            version: parsed.get_version().clone(),
            updating: false,
            data: String::new(),
            message: MESSAGE_ALREADY_LATEST_VERSION.to_string(),
        };
    }
    if !BridgeConfig::is_available(None) {
        return UpdateResult {
            doc_status: parsed.get_doc_status(),
            version: parsed.get_version().clone(),
            updating: false,
            data: String::new(),
            message: MESSAGE_BRIDGE_UNAVAILABLE.to_string(),
        };
    }
    notify_native_with_retry(parsed.get_doc_status(), parsed.get_version().clone()).await
}

/// Invokes the bridge `update_cache` command and retries on failure.
///
/// A failure is anything other than a native-side `UpdateStatus::Success`:
/// JS-side promise rejection, native-side `UpdateStatus::Failed`, or a
/// payload that cannot be deserialized into `UpdateStatus`. Retries up to
/// `VIEW_UPDATE_RETRY_COUNT` total attempts with `VIEW_UPDATE_RETRY_DELAY_MS`
/// between attempts, then collapses to a final `UpdateResult` with
/// `updating: false` and the last observed native message.
///
/// The webview-derived `doc_status` / `version` travel in as parameters
/// so the eventual `UpdateResult` keeps the docs.rs fetch context even
/// after the bridge call fails.
///
/// # Arguments
///
/// - `bool` - The docs.rs documentation status carried through the bridge call.
/// - `String` - The version string carried through the bridge call.
///
/// # Returns
///
/// - `UpdateResult` - UI-facing snapshot carrying docs.rs context plus
///   the most recent `data` / `message` reported by the native side.
async fn notify_native_with_retry(doc_status: bool, version: String) -> UpdateResult {
    let mut attempt: u32 = 0;
    loop {
        match try_notify_native_once().await {
            Ok((UpdateStatus::Success, payload)) => {
                if attempt > 0 {
                    Console::log(format!(
                        "update_cache bridge returned after {attempt}/{VIEW_UPDATE_RETRY_COUNT} prior failure(s): {payload}"
                    ));
                } else {
                    Console::log(format!("update_cache bridge returned: {payload}"));
                }
                return UpdateResult {
                    doc_status,
                    version,
                    updating: true,
                    data: payload.get_data().clone(),
                    message: payload.get_message().clone(),
                };
            }
            Ok((UpdateStatus::Failed, payload)) => {
                attempt += 1;
                if attempt >= VIEW_UPDATE_RETRY_COUNT {
                    Console::error(format!(
                        "update_cache bridge returned after {attempt}/{VIEW_UPDATE_RETRY_COUNT} attempts ({attempt} failure(s)): {payload}",
                    ));
                    return UpdateResult {
                        doc_status,
                        version,
                        updating: false,
                        data: String::new(),
                        message: format!(
                            "{} (attempt {attempt}/{VIEW_UPDATE_RETRY_COUNT}, {attempt} failure(s))",
                            payload.get_message()
                        ),
                    };
                }
                Console::warn(format!(
                    "update_cache bridge returned (attempt {attempt}/{VIEW_UPDATE_RETRY_COUNT}): {payload}. Retrying in {VIEW_UPDATE_RETRY_DELAY_MS}ms...",
                ));
                sleep_ms(VIEW_UPDATE_RETRY_DELAY_MS).await;
            }
            Err(error) => {
                attempt += 1;
                if attempt >= VIEW_UPDATE_RETRY_COUNT {
                    Console::error(format!(
                        "update_cache bridge failed after {attempt}/{VIEW_UPDATE_RETRY_COUNT} attempts ({attempt} failure(s)): {error} (no native payload)"
                    ));
                    return UpdateResult {
                        doc_status,
                        version,
                        updating: false,
                        data: String::new(),
                        message: format!(
                            "{error} (attempt {attempt}/{VIEW_UPDATE_RETRY_COUNT}, {attempt} failure(s))"
                        ),
                    };
                }
                Console::warn(format!(
                    "update_cache bridge failed (attempt {attempt}/{VIEW_UPDATE_RETRY_COUNT}): {error} (no native payload). Retrying in {VIEW_UPDATE_RETRY_DELAY_MS}ms..."
                ));
                sleep_ms(VIEW_UPDATE_RETRY_DELAY_MS).await;
            }
        }
    }
}

/// Performs one bridge invocation and classifies the outcome.
///
/// Deserializes the native `CacheUpdateResult` payload into
/// `UpdateResultPayload` and surfaces every field to the retry loop so it
/// can build a complete `UpdateResult` (success log message, snapshot
/// name, or last error description).
///
/// # Returns
///
/// - `Result<(UpdateStatus, UpdateResultPayload), String>` - `Ok` carries the
///   native-side outcome plus the full payload, so the caller can read `data` /
///   `message` without re-deserializing; `Err` is a transport-level or
///   deserialization failure that justifies a retry.
async fn try_notify_native_once() -> Result<(UpdateStatus, UpdateResultPayload), String> {
    let promise: Promise = BridgeConfig::invoke(INVOKE_UPDATE_CACHE, None, None)?;
    let value: JsValue = JsFuture::from(promise)
        .await
        .map_err(|error: JsValue| error.as_string().unwrap_or(ERROR_NULL_TEXT.to_string()))?;
    let payload: UpdateResultPayload =
        serde_wasm_bindgen::from_value(value).map_err(|error: serde_wasm_bindgen::Error| {
            format!("failed to deserialize update_cache result: {error}")
        })?;
    Ok((payload.result, payload))
}

/// Suspends the current task for `millis` milliseconds via `setTimeout`.
///
/// Centralizes the timer wiring used by retry loops so call sites stay
/// declarative and the timeout closure is a single point to swap for tests.
///
/// # Arguments
///
/// - `u32` - Number of milliseconds to sleep.
async fn sleep_ms(millis: u32) {
    let Some(window): Option<Window> = window() else {
        return;
    };
    let _: Result<JsValue, JsValue> =
        JsFuture::from(Promise::new(&mut |resolve: Function, _reject: Function| {
            let _: Result<i32, JsValue> = window
                .set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, millis as i32);
        }))
        .await;
}

/// Renders the application shell with navigation and route-based page content.
///
/// Detects viewport size and switches between desktop sidebar layout and
/// mobile header + drawer layout accordingly.
///
/// # Returns
///
/// - `VirtualNode` - The root application virtual DOM tree.
pub(crate) fn app() -> VirtualNode {
    Console::init();
    let route_signal: Signal<String> = App::use_signal(Router::current_route);
    let panel_open: Signal<bool> = App::use_signal(|| false);
    let drawer_open: Signal<bool> = App::use_signal(|| false);
    let mobile_signal: Signal<bool> = UseEuvLayout::use_resize();
    let theme_state: ThemeState = ThemeState::use_theme_state(mobile_signal);
    let theme_signal: Signal<String> = theme_state.get_theme();
    let root_class_signal: Signal<String> = theme_state.get_root_class();
    UseCacheUpdate::use_cache_state().load(check_docs_update);
    Router::use_hash_change(route_signal);
    Router::use_scroll_to_top(route_signal);
    Router::use_overlay_history(drawer_open, mobile_signal);
    Router::use_scroll_drawer_to_active(drawer_open);
    UseEuvLayout::use_safe_area_fix();
    html! {
        if { mobile_signal } {
            mobile_layout {
                route_signal
                theme_signal
                root_class_signal
                panel_open
                drawer_open
            }
        } else {
            desktop_layout {
                route_signal
                theme_signal
                root_class_signal
                panel_open
            }
        }
    }
}