Skip to content

Commit

Permalink
Displays red favicon when websocket was closed
Browse files Browse the repository at this point in the history
  • Loading branch information
sanpii committed Jan 2, 2025
1 parent 0ba008b commit ec77441
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions front/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<link data-trunk rel="copy-file" href="/static/1px.png" />
<link data-trunk rel="copy-file" href="/static/favicon.ico" />
<link data-trunk rel="copy-file" href="/static/favicon-unread.ico" />
<link data-trunk rel="copy-file" href="/static/favicon-error.ico" />
<link data-trunk rel="copy-file" href="/static/logo.png" />
<link data-trunk rel="copy-file" href="/static/lib/bootstrap-icons/bootstrap-icons.svg"/>
<link data-trunk rel="copy-file" href="/static/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
Expand Down
19 changes: 16 additions & 3 deletions front/src/components/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ enum Message {
Event(crate::Event),
#[allow(dead_code)]
Websocket(wasm_sockets::Message),
WebsocketError,
}

#[derive(PartialEq, yew::Properties)]
Expand All @@ -92,10 +93,21 @@ impl ComponentLoc {

match wasm_sockets::EventClient::new(&ws_url) {
Ok(mut websocket) => {
let link = link.clone();

let l = link.clone();
websocket.set_on_message(Some(Box::new(move |_, msg| {
link.send_message(Message::Websocket(msg));
l.send_message(Message::Websocket(msg));
})));

let l = link.clone();
websocket.set_on_error(Some(Box::new(move |error| {
log::error!("{error:?}");
l.send_message(Message::WebsocketError);
})));

let l = link.clone();
websocket.set_on_close(Some(Box::new(move |event| {
log::error!("{event:?}");
l.send_message(Message::WebsocketError);
})));

Some(websocket)
Expand Down Expand Up @@ -149,6 +161,7 @@ impl yew::Component for ComponentLoc {
_ => (),
},
Message::Websocket(_) => self.event_bus.send(crate::Event::ItemUpdate),
Message::WebsocketError => self.event_bus.send(crate::Event::WebsocketError),
}

should_render
Expand Down
10 changes: 9 additions & 1 deletion front/src/components/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ pub struct Component {
current_route: super::app::Route,
event_bus: yew_agent::Dispatcher<crate::event::Bus>,
links: Links,
in_error: bool,
_producer: Box<dyn yew_agent::Bridge<crate::event::Bus>>,
}

Expand All @@ -138,6 +139,7 @@ impl yew::Component for Component {
let component = Self {
current_route: ctx.props().current_route.clone(),
event_bus: crate::event::Bus::dispatcher(),
in_error: false,
links: Links::new(),
_producer: crate::event::Bus::bridge(std::rc::Rc::new(callback)),
};
Expand All @@ -164,6 +166,10 @@ impl yew::Component for Component {

should_render = true;
}
crate::Event::WebsocketError => {
self.in_error = true;
should_render = true;
}
_ => (),
},
Message::Error(err) => crate::send_error(ctx, &err),
Expand Down Expand Up @@ -197,7 +203,9 @@ impl yew::Component for Component {
}

fn view(&self, ctx: &yew::Context<Self>) -> yew::Html {
let favicon = if self.links.has_unread() {
let favicon = if self.in_error {
"/favicon-error.ico"
} else if self.links.has_unread() {
"/favicon.ico"
} else {
"/favicon-unread.ico"
Expand Down
1 change: 1 addition & 0 deletions front/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum Event {
SourceUpdate,
Redirect(String),
Redirected(String),
WebsocketError,
}

#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
Expand Down
Binary file added front/static/favicon-error.ico
Binary file not shown.

0 comments on commit ec77441

Please sign in to comment.