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
use crate::cases::state::{GlobalScene, SceneState, SCENE};
use crate::{alerts, cases, explorer, welcome};
use anyhow::Error;
use rate_ui::agents::live::LiveAgent;
use rate_ui::shared_object::{DataChanged, SharedObject};
use rate_ui::widget::{Context, NotificationHandler, OnWireEvent, Widget, WidgetRuntime};
use yew::{html, Html};

pub type App = WidgetRuntime<AppWidget>;

pub struct AppWidget {
    scene: SharedObject<SceneState>,
}

impl Default for AppWidget {
    fn default() -> Self {
        Self {
            scene: SCENE.with(SharedObject::clone),
        }
    }
}

impl Widget for AppWidget {
    type Event = ();
    type Tag = ();
    type Properties = ();
    type Meta = ();

    fn init(&mut self, ctx: &mut Context<Self>) {
        // Just to keep the connection alive all the time.
        ctx.live();
        self.scene.subscribe(ctx);
    }

    fn view(&self, _ctx: &Context<Self>) -> Html {
        let state = self.scene.read();
        let menu;
        let body;
        match &state.global_scene {
            GlobalScene::Home => {
                menu = html! {};
                body = html! {
                    <welcome::Shield />
                };
            }
            GlobalScene::Cases => {
                menu = html! {
                    <cases::DashboardMenu />
                };
                body = html! {
                    <cases::Dashboard />
                };
            }
            GlobalScene::Explorer => {
                menu = html! {
                    <explorer::DashboardMenu />
                };
                body = html! {
                    <explorer::Dashboard />
                };
            }
        }
        html! {
            <div class="d-flex flex-column bg-white" style="min-height: 100vh;">
                <nav class="navbar z-1000 border shadow-sm bg-light">
                    <div class="container-fluid">
                        <div class="navbar-brand me-3">
                            <div class="fw-bold">{ "RillRate" }</div>
                        </div>
                        <div class="mx-2">
                            <welcome::SceneSelector />
                        </div>
                        <div class="flex-grow-1 d-flex flex-row justify-content-between">
                            { menu }
                        </div>
                    </div>
                </nav>
                <div class="flex-grow-1 p-3" style="width: 100%; height: 100%;">
                    { body }
                </div>
                <nav class="navbar bg-light" style="height: 50px;">
                    <div class="container-fluid">
                        <div class="text-secondary">{ "© 2021 RillRate OÜ" }</div>
                        <div>{ "v" }{ crate::meta::VERSION }</div>
                        <img style="height: 1.8rem;" src="./images/logo.svg" />
                    </div>
                </nav>
                <alerts::AlertToast />
                <cases::Loader />
                <explorer::Loader />
            </div>
        }
    }
}

impl OnWireEvent<LiveAgent> for AppWidget {}

impl NotificationHandler<DataChanged<SceneState>> for AppWidget {
    fn handle(
        &mut self,
        _event: DataChanged<SceneState>,
        ctx: &mut Context<Self>,
    ) -> Result<(), Error> {
        ctx.redraw();
        Ok(())
    }
}