feat: moved components to their own module, use ollama for the chat.
This commit is contained in:
@@ -1,17 +1,19 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use feshared::daemon::DaemonState;
|
||||
use leptos::logging::log;
|
||||
use leptos::{prelude::*, reactive::spawn_local};
|
||||
use leptos_router::{
|
||||
components::{Route, Router, Routes},
|
||||
path,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use wasm_bindgen::{prelude::Closure, JsValue};
|
||||
use wasm_bindgen::JsValue;
|
||||
|
||||
use crate::bridge::{invoke, listen};
|
||||
use crate::popup::PopupView;
|
||||
use crate::{bridge::invoke, components::DarkModeToggle};
|
||||
use crate::{
|
||||
bridge::invoke_typed,
|
||||
components::{DaemonProvider, ThemeProvider},
|
||||
};
|
||||
|
||||
pub const BTN_PRIMARY: &str = "bg-slate-300 hover:bg-slate-400 dark:bg-slate-800 hover:dark-bg-slate-700 px-4 py-2 rounded-md";
|
||||
|
||||
@@ -35,18 +37,11 @@ fn Dashboard() -> impl IntoView {
|
||||
invoke("toggle_popup", empty_args).await;
|
||||
});
|
||||
};
|
||||
|
||||
let toggle_dark_mode = |_ev: leptos::ev::MouseEvent| {
|
||||
spawn_local(async {
|
||||
let _ = invoke("toggle_dark_mode", JsValue::UNDEFINED).await;
|
||||
});
|
||||
};
|
||||
view! {
|
||||
<ThemeProvider>
|
||||
<DaemonProvider>
|
||||
<div class="min-h-screen w-screen bg-white dark:bg-zinc-900 text-gray-950 dark:text-white">
|
||||
<button class=BTN_PRIMARY on:click=on_click>Open chat</button>
|
||||
<button class=BTN_PRIMARY on:click=toggle_dark_mode>asdf?</button>
|
||||
</div>
|
||||
</DaemonProvider>
|
||||
<div class="fixed bottom-0 right-0 p-2">
|
||||
@@ -56,22 +51,6 @@ fn Dashboard() -> impl IntoView {
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn DarkModeToggle() -> impl IntoView {
|
||||
let toggle_dark_mode = |_ev: leptos::ev::MouseEvent| {
|
||||
spawn_local(async {
|
||||
let _ = invoke("toggle_dark_mode", JsValue::UNDEFINED).await;
|
||||
});
|
||||
};
|
||||
view! {
|
||||
<div on:click=toggle_dark_mode class="text-gray-600 dark:text-gray-600">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M21.752 15.002A9.72 9.72 0 0 1 18 15.75c-5.385 0-9.75-4.365-9.75-9.75 0-1.33.266-2.597.748-3.752A9.753 9.753 0 0 0 3 11.25C3 16.635 7.365 21 12.75 21a9.753 9.753 0 0 0 9.002-5.998Z" />
|
||||
</svg>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn DaemonStatusIndicator() -> impl IntoView {
|
||||
let (poll_count, set_pool_count) = signal(0);
|
||||
@@ -81,8 +60,7 @@ pub fn DaemonStatusIndicator() -> impl IntoView {
|
||||
);
|
||||
let status = LocalResource::new(move || async move {
|
||||
poll_count.get();
|
||||
let val = invoke("daemon_state", JsValue::NULL).await;
|
||||
let s: DaemonState = serde_wasm_bindgen::from_value(val).unwrap();
|
||||
let s: DaemonState = invoke_typed("daemon_state", JsValue::NULL).await;
|
||||
s
|
||||
});
|
||||
|
||||
@@ -115,82 +93,3 @@ pub fn DaemonStatusIndicator() -> impl IntoView {
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn DaemonProvider(children: ChildrenFn) -> impl IntoView {
|
||||
let (poll_count, set_pool_count) = signal(0);
|
||||
set_interval(
|
||||
move || set_pool_count.update(|v| *v += 1),
|
||||
Duration::from_secs(1),
|
||||
);
|
||||
let status_res = LocalResource::new(move || async move {
|
||||
poll_count.get();
|
||||
let val = invoke("daemon_state", JsValue::NULL).await;
|
||||
let s: DaemonState = serde_wasm_bindgen::from_value(val).unwrap();
|
||||
s
|
||||
});
|
||||
|
||||
let is_daemon_ok = Memo::new(move |_| status_res.get().map(|s| (s.is_ok, s.error)));
|
||||
|
||||
provide_context(status_res);
|
||||
|
||||
move || match is_daemon_ok.get() {
|
||||
Some((true, _)) => children().into_any(),
|
||||
Some((false, err)) => view! { <DaemonErrorStatus error=err/> }.into_any(),
|
||||
None => view! { <p>Connecting...</p> }.into_any(),
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn DaemonErrorStatus(error: Option<String>) -> impl IntoView {
|
||||
view! {
|
||||
<ThemeProvider>
|
||||
<div class="w-screen h-screen bg-white dark:bg-zinc-900">
|
||||
<p>{ error.unwrap_or("Daemon error!".to_string()) } </p>
|
||||
</div>
|
||||
</ThemeProvider>
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone)]
|
||||
struct DarkMode {
|
||||
is_dark_mode: bool,
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn ThemeProvider(children: Children) -> impl IntoView {
|
||||
let (is_dark, set_dark) = signal(false);
|
||||
|
||||
Effect::new(move |_| {
|
||||
spawn_local(async move {
|
||||
let handler = Closure::wrap(Box::new(move |evt: JsValue| {
|
||||
log!("Received!!!");
|
||||
#[derive(Deserialize)]
|
||||
struct TauriEvent<T> {
|
||||
payload: T,
|
||||
}
|
||||
if let Ok(wrapper) = serde_wasm_bindgen::from_value::<TauriEvent<DarkMode>>(evt) {
|
||||
set_dark.set(wrapper.payload.is_dark_mode);
|
||||
}
|
||||
}) as Box<dyn FnMut(JsValue)>);
|
||||
let unlisten = listen("dark-mode-changed", &handler).await;
|
||||
// TODO use on_cleanup to call the unlisten JS function.
|
||||
handler.forget()
|
||||
});
|
||||
});
|
||||
|
||||
Effect::new(move |_| {
|
||||
let el = document()
|
||||
.document_element()
|
||||
.expect("HTML element not found!");
|
||||
if is_dark.get() {
|
||||
let _ = el.set_attribute("class", "dark");
|
||||
} else {
|
||||
let _ = el.set_attribute("class", "");
|
||||
}
|
||||
});
|
||||
|
||||
view! {
|
||||
{children()}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user