1
0

main.rs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. use dioxus::prelude::*;
  2. use wifiscanner::Wifi;
  3. fn main() {
  4. dioxus::launch(app)
  5. }
  6. fn perform_scan() -> Status {
  7. if let Ok(devices) = wifiscanner::scan() {
  8. if devices.is_empty() {
  9. Status::NoneFound
  10. } else {
  11. Status::Found(devices)
  12. }
  13. } else {
  14. Status::NoneFound
  15. }
  16. }
  17. enum Status {
  18. NoneFound,
  19. Found(Vec<Wifi>),
  20. }
  21. fn app() -> Element {
  22. let mut status =
  23. use_resource(|| async { tokio::task::spawn_blocking(perform_scan).await.unwrap() });
  24. let scanning = !status.finished();
  25. rsx! {
  26. link { rel: "stylesheet", href: "https://unpkg.com/tailwindcss@^2.0/dist/tailwind.min.css" },
  27. div {
  28. div { class: "py-8 px-6",
  29. div { class: "container px-4 mx-auto",
  30. h2 { class: "text-2xl font-bold", "Scan for WiFi Networks" }
  31. button {
  32. class: "inline-block w-full md:w-auto px-6 py-3 font-medium text-white bg-indigo-500 hover:bg-indigo-600 rounded transition duration-200",
  33. disabled: scanning,
  34. onclick: move |_| {
  35. status.restart();
  36. },
  37. if scanning { "Scanning" } else { "Scan" }
  38. }
  39. }
  40. }
  41. section { class: "py-8",
  42. div { class: "container px-4 mx-auto",
  43. div { class: "p-4 mb-6 bg-white shadow rounded overflow-x-auto",
  44. table { class: "table-auto w-full",
  45. thead {
  46. tr { class: "text-xs text-gray-500 text-left",
  47. th { class: "pl-6 pb-3 font-medium", "Strength" }
  48. th { class: "pb-3 font-medium", "Network" }
  49. th { class: "pb-3 font-medium", "Channel" }
  50. th { class: "pb-3 px-2 font-medium", "Security" }
  51. }
  52. }
  53. match &*status.read() {
  54. None => rsx!(""),
  55. Some(Status::NoneFound) => rsx!("No networks found. Try scanning again"),
  56. Some(Status::Found(wifis)) => {
  57. // Create vector of tuples of (signal_level, wifi) for sorting by signal_level
  58. let mut sorted_wifis = wifis
  59. .iter()
  60. .map(|wif: &Wifi| (wif, wif.signal_level.parse::<f32>().unwrap()))
  61. .collect::<Vec<_>>();
  62. sorted_wifis.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
  63. rsx! {
  64. tbody {
  65. for (Wifi { mac: _, ssid, channel, signal_level, security }, _) in sorted_wifis.into_iter().rev() {
  66. tr { class: "text-xs bg-gray-50",
  67. td { class: "py-5 px-6 font-medium", "{signal_level}" }
  68. td { class: "flex py-3 font-medium", "{ssid}" }
  69. td { span { class: "inline-block py-1 px-2 text-white bg-green-500 rounded-full", "{channel}" } }
  70. td { span { class: "inline-block py-1 px-2 text-purple-500 bg-purple-50 rounded-full", "{security}" } }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }