Skip to main content

flams_router_content/
lib.rs

1#![recursion_limit = "256"]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3
4#[cfg(any(
5    all(feature = "ssr", feature = "hydrate", not(feature = "docs-only")),
6    not(any(feature = "ssr", feature = "hydrate"))
7))]
8compile_error!("exactly one of the features \"ssr\" or \"hydrate\" must be enabled");
9
10pub mod archive_views;
11pub mod backend;
12pub mod checks;
13pub mod components;
14//pub mod errors;
15pub mod server_fns;
16#[cfg(feature = "ssr")]
17mod toc;
18
19use ftml_solver_trace::results::DocumentCheckResult;
20use leptos::prelude::*;
21
22#[server(prefix = "/api", endpoint = "checklog")]
23pub async fn get_check_log(uri: ftml_uris::DocumentUri) -> Result<String, ServerFnError<String>> {
24    use flams_math_archives::BuildableArchive;
25    use flams_math_archives::backend::LocalBackend;
26    use ftml_uris::IsNarrativeUri;
27    use ftml_uris::UriWithArchive;
28    use ftml_uris::UriWithPath;
29    tokio::task::spawn_blocking(move || {
30        let s = flams_system::backend::backend().with_local_archive(uri.archive_id(), |a| {
31            a.and_then(|a| {
32                let rp = a.rel_path_of(uri.path(), uri.document_name(), uri.language)?;
33                let f = a.get_log(rp.as_os_str().to_str()?, ftml_solver::CHECK.id());
34                std::fs::read_to_string(f).ok()
35            })
36        });
37        s.map_or_else(|| Err("No checking log found".to_string()), Ok)
38    })
39    .await
40    .map_err(|e| e.to_string())?
41    .map_err(Into::into)
42}
43
44pub struct Continuations;
45impl ftml_components::ViewContinuations for Continuations {
46    fn document_drawer(
47        &self,
48        doc: &ftml_ontology::narrative::documents::Document,
49    ) -> leptos::prelude::AnyView {
50        use crate::checks::ResultExt;
51        use flams_web_utils::components::wait_and_then_fn;
52        use ftml_component_utils::{BoldCaption, Header, LazyCollapsible};
53        let uri = doc.uri.clone();
54        view! {
55            <LazyCollapsible>
56                <Header slot><BoldCaption>"Checking results"</BoldCaption></Header>
57                {
58                    let uri = uri.clone();
59                    wait_and_then_fn(
60                        move || get_check_log(uri.clone()),
61                        |s| DocumentCheckResult::from_json(&s)
62                            .map_or_else(|v| view!{<pre>{v}</pre>}.into_any(),DocumentCheckResult::render)//ResultExt::render
63                    )
64                }
65            </LazyCollapsible>
66        }
67        .into_any()
68    }
69}
70
71pub type Views = ftml_components::Views;
72
73#[cfg(feature = "ssr")]
74mod ssr {
75    use ftml_ontology::utils::Css;
76
77    pub fn insert_base_url(mut v: Box<[Css]>) -> Box<[Css]> {
78        //v.sort();
79        for c in &mut v {
80            if let Css::Link(lnk) = c
81                && let Some(r) = lnk.strip_prefix("srv:")
82            {
83                *lnk = format!(
84                    "{}{r}",
85                    flams_system::settings::Settings::get().external_url()
86                )
87                .into_boxed_str();
88            }
89        }
90        v
91    }
92}