frontend done

This commit is contained in:
2025-02-01 15:10:24 -06:00
parent 592062225e
commit 9e8f7d4542
25 changed files with 5939 additions and 2 deletions

View File

@@ -0,0 +1,37 @@
use dioxus::prelude::*;
const ECHO_CSS: Asset = asset!("/assets/styling/echo.css");
/// Echo component that demonstrates fullstack server functions.
#[component]
pub fn Echo() -> Element {
let mut response = use_signal(|| String::new());
rsx! {
document::Link { rel: "stylesheet", href: ECHO_CSS }
div {
id: "echo",
h4 { "ServerFn Echo" }
input {
placeholder: "Type here to echo...",
oninput: move |event| async move {
let data = echo_server(event.value()).await.unwrap();
response.set(data);
},
}
if !response().is_empty() {
p {
"Server echoed: "
i { "{response}" }
}
}
}
}
}
/// Echo the user input on the server.
#[server(EchoServer)]
async fn echo_server(input: String) -> Result<String, ServerFnError> {
Ok(input)
}

View File

@@ -0,0 +1,21 @@
use dioxus::prelude::*;
const HEADER_SVG: Asset = asset!("/assets/header.svg");
#[component]
pub fn Hero() -> Element {
rsx! {
div {
id: "hero",
img { src: HEADER_SVG, id: "header" }
div { id: "links",
a { href: "https://dioxuslabs.com/learn/0.6/", "📚 Learn Dioxus" }
a { href: "https://dioxuslabs.com/awesome", "🚀 Awesome Dioxus" }
a { href: "https://github.com/dioxus-community/", "📡 Community Libraries" }
a { href: "https://github.com/DioxusLabs/sdk", "⚙️ Dioxus Development Kit" }
a { href: "https://marketplace.visualstudio.com/items?itemName=DioxusLabs.dioxus", "💫 VSCode Extension" }
a { href: "https://discord.gg/XgGxMSkvUM", "👋 Community Discord" }
}
}
}
}

View File

@@ -0,0 +1,11 @@
mod hero;
pub use hero::Hero;
mod navbar;
pub use navbar::Navbar;
mod echo;
pub use echo::Echo;
mod story;
pub use story::*;

View File

@@ -0,0 +1,21 @@
use crate::Route;
use dioxus::prelude::*;
const NAVBAR_CSS: Asset = asset!("/assets/styling/navbar.css");
#[component]
pub fn Navbar() -> Element {
rsx! {
document::Link { rel: "stylesheet", href: NAVBAR_CSS }
div {
id: "navbar",
Link {
to: Route::Home {},
"Home"
}
}
Outlet::<Route> {}
}
}

View File

@@ -0,0 +1,56 @@
use crate::{components::story, Notification};
use dioxus::prelude::*;
const CSS: Asset = asset!("/assets/styling/main.css");
#[component]
pub fn Story(static_notification: Notification) -> Element {
// Extract fields from the Notification object into local variables
let title = static_notification.title;
let description = static_notification.description;
let url = static_notification.url;
let image_url = static_notification.image_url;
// Join tickers into a single string
let tickers = static_notification.tickers.join(", ");
rsx! {
document::Link {rel: "stylesheet", href: CSS}
div {
class: "news-story",
// Image section, if an image URL is provided
img {
src: image_url,
alt: "News Image",
class: "news-image",
}
// Title section
h2 {
class: "news-title",
"{title}"
}
// Description section
p {
class: "news-description",
"{description}"
}
// Tickers section, now just displays the joined tickers string
div {
class: "news-tickers",
"Affected Stocks: {tickers}"
}
// Link to the full article
a {
href: url,
class: "news-link",
"Read more"
}
}
}
}

83
frontend/src/main.rs Normal file
View File

@@ -0,0 +1,83 @@
use std::{collections::VecDeque, hash::{Hash, Hasher}, sync::{Arc, Mutex}, thread};
use dioxus::prelude::*;
use components::Navbar;
use lazy_static::lazy_static;
use redis::Client;
use views::{Home};
use serde_json::Value;
use serde::{Deserialize, Serialize};
mod components;
mod views;
mod shared;
#[derive(Debug, Clone, Routable, PartialEq)]
#[rustfmt::skip]
enum Route {
#[layout(Navbar)]
#[route("/")]
Home {},
}
const FAVICON: Asset = asset!("/assets/favicon.ico");
const MAIN_CSS: Asset = asset!("/assets/styling/main.css");
const TAILWIND_CSS: Asset = asset!("/assets/tailwind.css");
fn main() {
let reference_arc = Arc::clone(&shared::NOTIFICATION_Q);
thread::spawn(move || {
let client = Client::open("redis://192.168.1.23/").unwrap();
let mut connection = client.get_connection().unwrap();
let mut pubsub = connection.as_pubsub();
pubsub.subscribe("notifications").unwrap();
loop{
let message = pubsub.get_message().unwrap();
let payload_string: String = message.get_payload().unwrap();
println!("{payload_string}");
let notif: Vec<Notification> = serde_json::from_str(payload_string.as_str()).unwrap();
println!("Just recieved: {:#?}", notif);
for notification in notif{
reference_arc.lock().unwrap().push_front(notification.clone());
}
while reference_arc.lock().unwrap().len() > 10{
reference_arc.lock().unwrap().pop_back();
}
let unique_vec: VecDeque<_> = reference_arc.lock().unwrap().clone().into_iter().collect::<std::collections::HashSet<_>>().into_iter().collect();
*reference_arc.lock().unwrap()=unique_vec;
}
}) ;
dioxus::launch(App);
}
#[component]
fn App() -> Element {
// Build cool things ✌️
rsx! {
// Global app resources
document::Link { rel: "icon", href: FAVICON }
document::Link { rel: "stylesheet", href: MAIN_CSS }
document::Link { rel: "stylesheet", href: TAILWIND_CSS }
Router::<Route> {}
}
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq, PartialOrd, Ord)]
pub struct Notification{
title: String,
description: String,
url: String,
image_url: String,
tickers: Vec<String>
}
impl Hash for Notification{
fn hash<H: Hasher>(&self, state: &mut H) {
self.title.hash(state);
self.url.hash(state);
}
}

6
frontend/src/shared.rs Normal file
View File

@@ -0,0 +1,6 @@
use lazy_static::lazy_static;
use std::{collections::VecDeque, sync::{Arc, Mutex}};
use crate::Notification;
lazy_static!{
pub static ref NOTIFICATION_Q: Arc<Mutex<VecDeque<Notification>>> = Arc::new(Mutex::new(VecDeque::new()));
}

View File

@@ -0,0 +1,13 @@
use crate::components::{Echo, Hero, Story};
use dioxus::prelude::*;
use crate::shared::NOTIFICATION_Q;
#[component]
pub fn Home() -> Element {
let vec_d_clone = NOTIFICATION_Q.lock().unwrap().clone();
rsx! {
for story in vec_d_clone{
Story{ static_notification: story.clone()}
}
}
}

View File

@@ -0,0 +1,5 @@
mod home;
pub use home::Home;
//mod blog;
//pub use blog::Blog;