flams_router_content/
components.rs

1#![allow(clippy::must_use_candidate)]
2use flams_ontology::uris::{NarrativeURI, URI};
3use flams_router_base::uris::{DocURIComponents, URIComponents, URIComponentsTrait};
4use flams_web_utils::{components::wait_and_then_fn, do_css};
5use ftml_viewer_components::components::{
6    TOCSource,
7    documents::{DocumentString, FragmentString, FragmentStringProps},
8    omdoc::OMDocSource,
9};
10use leptos::prelude::*;
11use leptos_router::hooks::use_query_map;
12
13#[component(transparent)]
14pub fn URITop() -> impl IntoView {
15    use flams_web_utils::components::Themer;
16    use ftml_viewer_components::FTMLGlobalSetup;
17    use leptos::either::EitherOf3 as Either;
18    use leptos_meta::Stylesheet;
19    use thaw::Scrollbar;
20    #[cfg(not(feature = "ssr"))]
21    let qm = leptos_router::hooks::use_location();
22    #[cfg(not(feature = "ssr"))]
23    let _ = Effect::new(move |_| {
24        let Ok(origin) = window().location().origin() else {
25            tracing::error!("Getting URL origin failed");
26            panic!("Getting URL origin failed");
27        };
28        let url = format!(
29            "{origin}{}{}{}",
30            qm.pathname.get(),
31            qm.query.get().to_query_string(),
32            qm.hash.get()
33        );
34        let Ok(js_url) = window().location().href() else {
35            tracing::error!("Getting URL failed");
36            panic!("Getting URL failed");
37        };
38        if url != js_url {
39            if !window().location().set_href(&url).is_ok() {
40                tracing::error!("Updating url failed");
41                panic!("Updating url failed");
42            }
43        }
44    });
45    view! {
46      <Stylesheet id="leptos" href="/pkg/flams.css"/>
47      <Themer><FTMLGlobalSetup>//<Login>
48      <Scrollbar style="width:100vw;max-height:100vh;">
49        <div style="min-height:100vh;color:black;width:min-content">{
50          use_query_map().with_untracked(|m| m.as_doc().map_or_else(
51            || {
52              let Some(uri) = m.as_comps() else {
53                return Either::C(flams_web_utils::components::display_error("Invalid URI".into()));
54              };
55              Either::B(view!(<Fragment uri/>))
56            },
57            |doc| Either::A(view!(<Document doc/>))
58          ))
59        }</div>
60      </Scrollbar>//</Login>
61      </FTMLGlobalSetup></Themer>
62    }
63}
64
65#[component]
66pub fn DocumentOfTop(uri: URI) -> impl IntoView {
67    use leptos_router::components::Redirect;
68    wait_and_then_fn(
69        move || super::server_fns::document_of(uri.clone()),
70        |u| view!(<Redirect path=format!("/?uri={}",urlencoding::encode(&u.to_string()))/>),
71    )
72}
73
74#[component]
75pub fn Fragment(uri: URIComponents) -> impl IntoView {
76    wait_and_then_fn(
77        move || uri.clone().into_args(super::server_fns::fragment),
78        move |(uri, css, html)| {
79            for css in css {
80                do_css(css);
81            }
82            //leptos::logging::log!("Here 2: {html}");
83            if let URI::Narrative(NarrativeURI::Element(uri)) = uri {
84                leptos::either::Either::Left(view! {
85                    //<pre>"Here: "{html.clone()}</pre>
86                    <div><FragmentString html uri/></div>
87                })
88            } else {
89                leptos::either::Either::Right(view! {
90                    //<pre>"Here: "{html.clone()}</pre>
91                    <div style="padding: 0 60px;--rustex-this-width:590px;">
92                    <FragmentString html/>
93                    </div>
94                })
95            }
96        },
97    )
98}
99
100#[component]
101pub fn Document(doc: DocURIComponents) -> impl IntoView {
102    wait_and_then_fn(
103        move || doc.clone().into_args(super::server_fns::document),
104        |(uri, css, html)| {
105            for css in css {
106                do_css(css);
107            }
108            view! {<div>
109                <DocumentString html uri toc=TOCSource::Get omdoc=OMDocSource::Get/>
110            </div>}
111        },
112    )
113}
114
115#[component]
116pub fn DocumentInner(doc: DocURIComponents) -> impl IntoView {
117    let doc: URIComponents = doc.into();
118    wait_and_then_fn(
119        move || doc.clone().into_args(super::server_fns::fragment),
120        |(_, css, html)| {
121            view! {<div>{
122                for css in css { do_css(css); }
123                FragmentString(FragmentStringProps{html,uri:None})
124            }</div>}
125        },
126    )
127}