Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
ia0 committed Aug 1, 2024
1 parent fd1563f commit aa8caf7
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 50 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* @ia0
/crates/runner-host/crates/web-client/ @chris-dietz @ia0
15 changes: 6 additions & 9 deletions crates/runner-host/crates/web-client/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
# Wasefire Web Client

This is a fairly minimal web app for interacting with the runner that's built
with [Yew](https://yew.rs) and [Trunk](https://trunkrs.dev/).
This is a fairly minimal web app for interacting with the host runner. It is built with
[Yew](https://yew.rs) and [Trunk](https://trunkrs.dev/).

## Running

```bash
trunk serve
```

Rebuilds the app whenever a change is detected and runs a local server
to host it.
Rebuilds the app whenever a change is detected and runs a local server to host it.

There's also the `trunk watch` command which does the same thing but without
hosting it.
There's also the `trunk watch` command which does the same thing but without hosting it.

## Release

```bash
trunk build --release
```

This builds the app in release mode similar to `cargo build --release`.
You can also pass the `--release` flag to `trunk serve` if you need to get
every last drop of performance.
This builds the app in release mode similar to `cargo build --release`. You can also pass the
`--release` flag to `trunk serve` if you need to get every last drop of performance.

Unless overwritten, the output will be located in the `dist` directory.
15 changes: 4 additions & 11 deletions crates/runner-host/crates/web-client/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use gloo_utils::window;
use log::{info, warn};
use wasm_bindgen::UnwrapThrowExt;
use web_common::{Command, Event};
use web_common::Command;
use yew::prelude::*;

use crate::board::Board;
Expand All @@ -30,20 +30,17 @@ pub fn app() -> Html {
info!("Connecting to runner at {web_socket_url}");
use_runner_connection(web_socket_url)
};

let on_new_console_msg = Callback::from({
let runner_connection = runner_connection.clone();
move |msg: String| runner_connection.send_console_event(msg)
move |msg| runner_connection.send_console_event(msg)
});

let on_board_ready = Callback::from({
let runner_connection = runner_connection.clone();
move |()| runner_connection.send_board_ready()
});

let send_event_callback = Callback::from({
let runner_connection = runner_connection.clone();
move |event: Event| runner_connection.send_event(event)
move |event| runner_connection.send_event(event)
});

use_effect_with(runner_connection.command_state.clone(), move |command_state| {
Expand All @@ -60,11 +57,7 @@ pub fn app() -> Html {

html! {
<main>
<object
class="title"
type="image/svg+xml"
data="title.svg"
/>
<object class="title" type="image/svg+xml" data="title.svg" />
<Console
id={0}
command_state={runner_connection.command_state.clone()}
Expand Down
8 changes: 4 additions & 4 deletions crates/runner-host/crates/web-client/src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ pub fn Board(Props { command_state, on_board_ready, on_event }: &Props) -> Html
}
});

return html! {
html! {
<div class="board">{
if let Some(board_config) = &*board_config {
board_config.iter().map(|component| match component {
Component::Button{id} => html!(<Button id={id} on_event={on_event} />),
Component::MonochromeLed{id} => html! {
Component::Button { id } => html!(<Button id={id} on_event={on_event} />),
Component::MonochromeLed { id } => html! {
<LED id={id} command_state={command_state.clone()} />
}
}).collect::<Html>()
} else {
html!(<div></div>)
}
}</div>
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub fn Button(Props { id, on_event }: &Props) -> Html {
on_event.emit(Event::Button { component_id: id, state: ButtonState::Pressed });
}
});

let unpress = Callback::from({
let pressed = pressed.clone();
let on_event = on_event.clone();
Expand All @@ -51,7 +50,7 @@ pub fn Button(Props { id, on_event }: &Props) -> Html {
}
});

return html! {
html! {
<div
id={id.to_string()}
onmousedown={press}
Expand All @@ -73,5 +72,5 @@ pub fn Button(Props { id, on_event }: &Props) -> Html {
style={if !*pressed { "" } else { "display: none;" }}
/>
</div>
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub struct Props {
pub fn LED(Props { id, command_state }: &Props) -> Html {
let id = *id;
let lit = use_state(|| false);
let command_state = command_state.clone();
use_effect_with(command_state, {

use_effect_with(command_state.clone(), {
let lit = lit.clone();
move |command_state| {
if let Some(Command::Set { component_id, state }) = &**command_state {
Expand All @@ -40,7 +40,7 @@ pub fn LED(Props { id, command_state }: &Props) -> Html {
}
});

return html! {
html! {
<div class="monochrome_led" id={id.to_string()}>
<object
class="led" type="image/svg+xml"
Expand All @@ -54,5 +54,5 @@ pub fn LED(Props { id, command_state }: &Props) -> Html {
style={if !*lit { "" } else { "display: none;" }}
/>
</div>
};
}
}
12 changes: 5 additions & 7 deletions crates/runner-host/crates/web-client/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@ use yew::prelude::*;
use yew_hooks::prelude::*;

#[derive(Properties, PartialEq)]

pub struct Props {
#[prop_or_default]
pub id: usize,
pub command_state: UseStateHandle<Option<Command>>,
pub on_new_console_msg: Callback<String>,
}

#[function_component(Console)]
pub fn console(Props { id, command_state, on_new_console_msg }: &Props) -> Html {
let history = use_list(vec![]);
let console_ref = use_node_ref();
let button_enabled = use_state(|| false);

let onsubmit = Callback::from({
let history = history.clone();
let console_ref = console_ref.clone();
Expand All @@ -55,7 +54,6 @@ pub fn console(Props { id, command_state, on_new_console_msg }: &Props) -> Html
move |command_state| {
if let Some(command) = &**command_state {
info!("Command: {command:?}");

match command {
Command::Log { message } => {
history.push(format!("[recv]: {message}"));
Expand All @@ -77,12 +75,12 @@ pub fn console(Props { id, command_state, on_new_console_msg }: &Props) -> Html

html! {
<div id={id.to_string()} class={"console"}>
<div class="console-display">
{ for history.current().iter().rev().map(|message| html!(<div>{ message }</div>)) }
</div>
<div class="console-display">{
for history.current().iter().rev().map(|message| html!(<div>{ message }</div>))
}</div>
<form class="console-form" onsubmit={onsubmit}>
<input ref={console_ref} type="text" id="consolein" />
<input disabled={!*button_enabled} type="submit" value="Send" />
<input disabled={!*button_enabled} type="submit" value="Send" />
</form>
</div>
}
Expand Down
8 changes: 3 additions & 5 deletions crates/runner-host/crates/web-client/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ impl UseRunnerConnectionHandle {
warn!("Ignoring message: {console_msg}");
}
pub fn send_board_ready(&self) {
let command = Event::BoardReady;
self.ws.send(serde_json::to_string(&command).unwrap());
self.ws.send(serde_json::to_string(&Event::BoardReady).unwrap());
}
pub fn send_event(&self, event: Event) {
self.ws.send(serde_json::to_string(&event).unwrap());
Expand All @@ -55,7 +54,7 @@ pub fn use_runner_connection(backend_address: String) -> UseRunnerConnectionHand
let command_state = command_state.clone();
move |message| {
info!("Message: {message}");
match serde_json::from_str::<Command>(message.as_str()) {
match serde_json::from_str::<Command>(&message) {
Ok(command) => command_state.set(Some(command)),
Err(err) => warn!("Error parsing message: {err}"),
}
Expand All @@ -80,6 +79,5 @@ pub fn use_runner_connection(backend_address: String) -> UseRunnerConnectionHand
..Default::default()
},
);

return UseRunnerConnectionHandle { ws, command_state };
UseRunnerConnectionHandle { ws, command_state }
}
4 changes: 1 addition & 3 deletions crates/runner-host/crates/web-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use app::App;

mod app;
mod board;
mod board_components;
Expand All @@ -22,5 +20,5 @@ mod hooks;

fn main() {
wasm_logger::init(wasm_logger::Config::default());
yew::Renderer::<App>::new().render();
yew::Renderer::<app::App>::new().render();
}
7 changes: 3 additions & 4 deletions crates/runner-host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ async fn main() -> Result<()> {
}
}
});
let mut wrapper_command = std::process::Command::new("../../scripts/wrapper.sh");
let build_command =
wrapper_command.args(["trunk", "build", "--release", "crates/web-client/index.html"]);
wasefire_cli_tools::cmd::execute(build_command)?;
let mut trunk = std::process::Command::new("../../scripts/wrapper.sh");
trunk.args(["trunk", "build", "--release", "crates/web-client/index.html"]);
wasefire_cli_tools::cmd::execute(&mut trunk)?;
let url = format!("{}:{}", flags.web_options.web_host, flags.web_options.web_port);
web_server::Client::new(&url, sender).await?
};
Expand Down

0 comments on commit aa8caf7

Please sign in to comment.