diff --git a/client/src-tauri/src/main.rs b/client/src-tauri/src/main.rs index 91198a3..79cc383 100644 --- a/client/src-tauri/src/main.rs +++ b/client/src-tauri/src/main.rs @@ -1,15 +1,16 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -use std::sync::Arc; +use std::{sync::Arc, thread}; use anyhow::Result; use mraow_common::chat_proto::{ - chat_client::ChatClient, ChatMessage, ReceiveMsgsRequest, + chat_client::ChatClient, ChatMessage, ReceiveMsgsRequest, RoomAction, }; +use serde_json::json; use tauri::{ async_runtime::{Mutex, TokioHandle}, - State, + Manager, State, }; use tonic::{transport::channel::Channel, IntoRequest}; use uuid::Uuid; @@ -29,11 +30,18 @@ async fn send_message( let mut client = state.lock().await; let user_id = user_id.inner(); - client.send_msg(ChatMessage { - from_user_id: user_id.0.to_string(), - content: message, - ..Default::default() - }).await.unwrap(); + let resp = client + .send_msg(ChatMessage { + from_user_id: user_id.0.to_string(), + to_room_id: "general".to_string(), + content: message, + ..Default::default() + }) + .await + .unwrap(); + + println!("Sent message to server. {resp:?}"); + /* client .say_hello(HelloRequest { message, @@ -57,20 +65,35 @@ async fn main() -> Result<()> { tauri::Builder::default() .setup(move |app| { + let main_window = app.get_window("main").unwrap(); + tokio::spawn(async move { - let stream = { - let mut client = chat_client2.lock().await; - client - .receive_msgs(ReceiveMsgsRequest { - user_id: uuid.to_string(), - }) - .await - .unwrap() - }; + let mut client = chat_client2.lock().await; + client + .room_action(RoomAction { + room_id: "general".to_string(), + user_id: uuid.to_string(), + action: "join".to_string(), + }) + .await + .unwrap(); + + let stream = client + .receive_msgs(ReceiveMsgsRequest { + user_id: uuid.to_string(), + }) + .await + .unwrap(); + + std::mem::drop(client); let mut stream = stream.into_inner(); while let Ok(Some(message)) = stream.message().await { println!("SHIET message {message:?}"); + + main_window + .emit_all("new-message", json!({ "content" : message.content })) + .unwrap(); } }); diff --git a/client/src/App.tsx b/client/src/App.tsx index 2ca2e7f..d862b71 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -1,6 +1,5 @@ import { Provider } from "react-redux"; -import { store } from "./store"; -import { useState } from "react"; +import { store, useAppDispatch } from "./store"; import styles from "./App.module.scss"; import LeftSidebar from "./components/LeftSidebar"; diff --git a/client/src/components/CenterPanel.tsx b/client/src/components/CenterPanel.tsx index 02e8a51..a788f3e 100644 --- a/client/src/components/CenterPanel.tsx +++ b/client/src/components/CenterPanel.tsx @@ -1,9 +1,11 @@ import { invoke } from "@tauri-apps/api/tauri"; import styles from "./CenterPanel.module.scss"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import { useAppDispatch, useAppSelector } from "../store"; import { messageSelectors, messageSlice } from "../store/messages"; import { v4 as uuidv4 } from "uuid"; +import { emit, listen } from "@tauri-apps/api/event"; +import { appWindow, WebviewWindow } from "@tauri-apps/api/window"; export default function CenterPanel() { const [currentMessage, setCurrentMessage] = useState(""); @@ -12,12 +14,32 @@ export default function CenterPanel() { messageSelectors.selectAll(state) ); + useEffect(() => { + let unlisten; + + (async () => { + unlisten = await appWindow.listen("new-message", (event) => { + console.log("NEW EVENT", event); + const id = "lol"; + const time = Date.now(); + const content = event.payload.content; + dispatch(messageSlice.actions.addMessage({ id, time, content })); + }); + + console.log("Listen handler active."); + })(); + + return () => { + if (unlisten) unlisten(); + }; + }); + const onSubmit = (e) => { e.preventDefault(); invoke("send_message", { message: currentMessage }); const id = uuidv4(); - const time = new Date(); + const time = Date.now(); dispatch( messageSlice.actions.addMessage({ id, time, content: currentMessage }) ); @@ -31,7 +53,8 @@ export default function CenterPanel() {