Skip to content

Commit

Permalink
use routes
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszKielar committed Apr 30, 2024
1 parent 689816f commit f0e569c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 50 deletions.
32 changes: 8 additions & 24 deletions src/frontend/components/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ where
}

#[component]
fn PromptTextArea<FIn, FKdn>(
on_input: FIn,
fn PromptTextArea<FKdn>(
on_keydown: FKdn,
#[prop(into)] user_prompt: Signal<String>,
user_prompt: RwSignal<String>,
#[prop(default = 24)] min_height: i32,
#[prop(default = 216)] max_height: i32,
) -> impl IntoView
where
FIn: Fn(Event) + 'static,
FKdn: Fn(KeyboardEvent) + 'static,
{
let textarea = create_node_ref::<Textarea>();
Expand All @@ -40,7 +38,7 @@ where
create_effect(move |_| {
set_dynamic_height(min_height);
// reset if user_prompt() if empty
let scroll_height = if user_prompt() == "" {
let scroll_height = if user_prompt.get() == "" {
min_height
} else {
// SAFETY: effect is triggered by user input into Textarea, so element has been already loaded,
Expand All @@ -53,10 +51,10 @@ where

view! {
<textarea
on:input=on_input
on:input=move |ev: Event| user_prompt.set(event_target_value(&ev))
on:keydown=on_keydown
type="text"
prop:value=user_prompt
prop:value=move || user_prompt.get()
node_ref=textarea
tab_index=0
placeholder="Message LokAI..."
Expand All @@ -72,29 +70,19 @@ where
}

#[component]
pub(crate) fn Prompt<FCl, FKdn, FIn, I>(
user_prompt: ReadSignal<String>,
set_messages: WriteSignal<Vec<models::Message>>,
on_input: FIn,
pub(crate) fn Prompt<FCl, FKdn, I>(
user_prompt: RwSignal<String>,
on_click: FCl,
on_keydown: FKdn,
send_user_prompt: Action<I, Result<models::Message, ServerFnError>>,
) -> impl IntoView
where
FCl: Fn(MouseEvent) + 'static,
FKdn: Fn(KeyboardEvent) + 'static,
FIn: Fn(Event) + 'static,
I: 'static,
{
let (button_disabled, set_button_disabled) = create_signal(true);

create_effect(move |_| {
if let Some(response) = send_user_prompt.value().get() {
let assistant_response = response.unwrap();
set_messages.update(|msgs| msgs.push(assistant_response));
}
});

create_effect(move |_| {
if user_prompt().len() == 0 || send_user_prompt.pending().get() {
set_button_disabled(true)
Expand All @@ -109,11 +97,7 @@ where

<div class="relative flex flex-col h-full flex-1 items-stretch md:flex-col">
<div class="flex flex-col w-full py-2 flex-grow md:py-3 md:pl-4 relative border border-black/10 bg-white dark:border-gray-900/50 dark:text-white dark:bg-gray-700 rounded-md shadow-[0_0_10px_rgba(0,0,0,0.10)] dark:shadow-[0_0_15px_rgba(0,0,0,0.10)]">
<PromptTextArea
on_input=on_input
on_keydown=on_keydown
user_prompt=user_prompt
/>
<PromptTextArea on_keydown=on_keydown user_prompt=user_prompt/>
<PromptSubmitButton on_click=on_click button_disabled=button_disabled/>
</div>
</div>
Expand Down
27 changes: 12 additions & 15 deletions src/frontend/views/chat.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use leptos::ev::{Event, KeyboardEvent, MouseEvent};
use leptos::ev::{KeyboardEvent, MouseEvent};
use leptos::{html::Div, *};
use leptos_router::use_params_map;
use uuid::Uuid;
Expand All @@ -19,7 +19,7 @@ pub(crate) fn Chat() -> impl IntoView {
.unwrap()
};

let (user_prompt, set_user_prompt) = create_signal(String::new());
let user_prompt = create_rw_signal(String::new());
let messages = create_rw_signal(Vec::<models::Message>::new());

// SAFETY: it's safe to unwrap because I provide context in App
Expand All @@ -46,38 +46,36 @@ pub(crate) fn Chat() -> impl IntoView {
let send_user_prompt = create_server_action::<AskAssistant>();

let dispatch = move || {
let user_message = models::Message::user(user_prompt(), conversation_id());
let user_message = models::Message::user(user_prompt.get(), conversation_id());
let user_message_clone = user_message.clone();
if user_message.content != "" {
messages.update(|msgs| msgs.push(user_message_clone));
send_user_prompt.dispatch(AskAssistant {
user_message,
new_conversation: false,
});
set_user_prompt("".to_string());
user_prompt.set("".to_string());
}
};

let on_click = move |ev: MouseEvent| {
ev.prevent_default();
dispatch();
};
let on_input = move |ev: Event| set_user_prompt(event_target_value(&ev));
let on_keydown = move |ev: KeyboardEvent| {
if ev.key() == "Enter" && !ev.shift_key() {
ev.prevent_default();
dispatch();
}
};

// create_effect(move |_| {
// if messages()
// .iter()
// .any(|m| m.conversation_id != conversation_id())
// {
// set_messages(Vec::new())
// }
// });
create_effect(move |_| {
if let Some(response) = send_user_prompt.value().get() {
// TODO: handle errors
let assistant_response = response.unwrap();
messages.update(|msgs| msgs.push(assistant_response));
};
});

view! {
<div class="flex max-w-full flex-1 flex-col">
Expand All @@ -93,6 +91,7 @@ pub(crate) fn Chat() -> impl IntoView {
</div>
}
}>
// TODO: use For and properly update only necessary elements

{if let Some(msgs) = db_messages.get() {
messages.set(msgs);
Expand All @@ -110,8 +109,6 @@ pub(crate) fn Chat() -> impl IntoView {
</div>
<Prompt
user_prompt=user_prompt
set_messages=messages.write_only()
on_input=on_input
on_click=on_click
on_keydown=on_keydown
send_user_prompt=send_user_prompt
Expand Down
20 changes: 10 additions & 10 deletions src/frontend/views/home.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use leptos::ev::{Event, KeyboardEvent, MouseEvent};
use leptos::ev::{KeyboardEvent, MouseEvent};
use leptos::*;
use leptos_router::{use_navigate, NavigateOptions};
use uuid::Uuid;
Expand All @@ -14,7 +14,7 @@ pub(crate) fn Home() -> impl IntoView {

let conversation_id = create_memo(|_| Uuid::new_v4());

let (user_prompt, set_user_prompt) = create_signal(String::new());
let user_prompt = create_rw_signal(String::new());
let messages = create_rw_signal(Vec::<models::Message>::new());

// SAFETY: it's safe to unwrap because I provide context in App
Expand All @@ -26,7 +26,7 @@ pub(crate) fn Home() -> impl IntoView {
let send_user_prompt = create_server_action::<AskAssistant>();

let dispatch = move || {
let user_message = models::Message::user(user_prompt(), conversation_id());
let user_message = models::Message::user(user_prompt.get(), conversation_id());
let user_message_clone = user_message.clone();
if user_message.content != "" {
messages.update(|msgs| msgs.push(user_message_clone));
Expand All @@ -36,19 +36,18 @@ pub(crate) fn Home() -> impl IntoView {
});
// TODO: I should prob get this object from server response
let conversation = models::Conversation {
id: conversation_id(),
name: user_prompt(),
id: conversation_id.get(),
name: user_prompt.get(),
};
conversations.update(|convs| convs.push(conversation));
set_user_prompt("".to_string());
user_prompt.set("".to_string());
}
};

let on_click = move |ev: MouseEvent| {
ev.prevent_default();
dispatch();
};
let on_input = move |ev: Event| set_user_prompt(event_target_value(&ev));
let on_keydown = move |ev: KeyboardEvent| {
if ev.key() == "Enter" && !ev.shift_key() {
ev.prevent_default();
Expand All @@ -57,7 +56,10 @@ pub(crate) fn Home() -> impl IntoView {
};

create_effect(move |_| {
if let Some(_) = send_user_prompt.value().get() {
if let Some(response) = send_user_prompt.value().get() {
// TODO: handle errors
let assistant_response = response.unwrap();
messages.update(|msgs| msgs.push(assistant_response));
navigate(
&format!("/c/{}", conversation_id.get()),
NavigateOptions::default(),
Expand All @@ -84,8 +86,6 @@ pub(crate) fn Home() -> impl IntoView {
</div>
<Prompt
user_prompt=user_prompt
set_messages=messages.write_only()
on_input=on_input
on_click=on_click
on_keydown=on_keydown
send_user_prompt=send_user_prompt
Expand Down
2 changes: 1 addition & 1 deletion src/server/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use uuid::Uuid;
#[cfg(feature = "ssr")]
async fn slow_down_db() {
use tokio;
let _ = tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
let _ = tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
}

#[server(AskAssistant, "/api")]
Expand Down

0 comments on commit f0e569c

Please sign in to comment.