pib-viewer 0.8.0

A viewer for public governmental data served over OParl
// SPDX-FileCopyrightText: Politik im Blick developers
// SPDX-FileCopyrightText: Wolfgang Silbermayr <wolfgang@silbermayr.at>
//
// SPDX-License-Identifier: AGPL-3.0-or-later OR EUPL-1.2

use dioxus::{
    document,
    hooks::use_signal,
    prelude::{component, dioxus_elements, rsx},
};
use dioxus_core::Element;
use dioxus_router::use_navigator;
use dioxus_signals::ReadableExt as _;
use manganis::{Asset, asset};
use tracing::info;

use crate::{
    CONFIG, ConfigState,
    client::{Config, Contents, SystemReference},
    components::ConnectDialog,
    route::{Route, UrlEncoded},
};

const WELCOME_PAGE_CSS: Asset = asset!("/assets/styling/components/welcome_page.css");

#[component]
pub fn WelcomePage() -> Element {
    let config = use_signal(|| CONFIG.read().clone());

    let navigator = use_navigator();

    let navigate_to_single_destination = move |_| {
        let ConfigState::ConfigLoaded {
            config: Config { contents, .. },
        } = config()
        else {
            info!(
                "Wrong config for navigating to single destination: {config:?}",
                config = config()
            );
            return;
        };

        match contents {
            Contents::SingleSystem {
                system: SystemReference { url, .. },
            } => {
                navigator.push(Route::OParlSystem {
                    system_url: UrlEncoded::from(url),
                });
            }
            Contents::SingleBody { body } => {
                navigator.push(Route::Body {
                    body_url: UrlEncoded::from(body.url.clone()),
                });
            }
            other => {
                info!("Cannot navigate to single page, found non-single page content: {other:?}");
            }
        }
    };

    rsx! {
        document::Link { rel: "stylesheet", href: WELCOME_PAGE_CSS }

        div {
            id: "welcome_page",

            h1 { "🗒️ PiB Viewer" }

            p {
                "👋 Hi! "
                b { "PiB Viewer" }
                " allows you to view public administration data from an "
                a {
                    href: "https://oparl.org/",
                    "OParl v1.1 service"
                }
                "."
            }

            match &*config.read_unchecked() {
                ConfigState::ConfigLoaded { config } => rsx! {
                    match &config.contents {
                        Contents::SingleSystem { system } => rsx! {
                            div {
                                onmounted: navigate_to_single_destination,
                                dioxus_router::Link {
                                    to: Route::OParlSystem {
                                        system_url: UrlEncoded::from(system.url.clone()),
                                    },
                                    "Go to {system.name}…"
                                }
                            }
                        },
                        Contents::SingleBody { body } => rsx! {
                            div {
                                onmounted: navigate_to_single_destination,
                                dioxus_router::Link {
                                    to: Route::Body {
                                        body_url: UrlEncoded::from(body.url.clone()),
                                    },
                                    "Go to 🏛️ {body.name}…"
                                }
                            }
                        },
                        Contents::ConnectDialog => rsx! {
                            ConnectDialog {},
                        }
                    }
                },
                ConfigState::NoConfigPresent => rsx! {
                    ConnectDialog {},
                },
                ConfigState::Failed{message} => rsx! {
                    div {
                        p { "Loading config failed, {message}" }
                        ConnectDialog {},
                    }
                },
                ConfigState::Loading | ConfigState::Initial=>  rsx! { p { "Loading config..." } }
            }
        }
    }
}