main.rs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #![allow(non_snake_case, unused)]
  2. use dioxus::prelude::*;
  3. // Define the Hackernews API and types
  4. use chrono::{DateTime, Utc};
  5. use serde::{Deserialize, Serialize};
  6. use std::{
  7. fmt::{Display, Formatter},
  8. num::ParseIntError,
  9. str::FromStr,
  10. };
  11. use svg_attributes::to;
  12. fn main() {
  13. #[cfg(feature = "web")]
  14. tracing_wasm::set_as_global_default();
  15. #[cfg(feature = "server")]
  16. tracing_subscriber::fmt::init();
  17. launch(|| rsx! { Router::<Route> {} });
  18. }
  19. #[derive(Clone, Routable)]
  20. enum Route {
  21. #[redirect("/", || Route::Homepage { story: PreviewState { active_story: None } })]
  22. #[route("/:story")]
  23. Homepage { story: PreviewState },
  24. }
  25. pub fn App() -> Element {
  26. rsx! {
  27. Router::<Route> {}
  28. }
  29. }
  30. const STYLE: &str = r#"@keyframes spin {
  31. 0% {
  32. transform: rotate(0deg);
  33. }
  34. 100% {
  35. transform: rotate(360deg);
  36. }
  37. }
  38. .spinner {
  39. width: 10px;
  40. height: 10px;
  41. border: 4px solid #f3f3f3;
  42. border-top: 4px solid #3498db;
  43. border-radius: 50%;
  44. animation: spin 2s linear infinite;
  45. }"#;
  46. #[component]
  47. fn Homepage(story: ReadOnlySignal<PreviewState>) -> Element {
  48. rsx! {
  49. style {
  50. {STYLE}
  51. }
  52. div { display: "flex", flex_direction: "row", width: "100%",
  53. div {
  54. width: "50%",
  55. SuspenseBoundary {
  56. fallback: |context: SuspenseContext| rsx! {
  57. "Loading..."
  58. },
  59. Stories {}
  60. }
  61. }
  62. div { width: "50%",
  63. SuspenseBoundary {
  64. fallback: |context: SuspenseContext| rsx! {
  65. "Loading preview..."
  66. },
  67. Preview {
  68. story
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. #[component]
  76. fn Stories() -> Element {
  77. let stories: Resource<dioxus::Result<Vec<i64>>> = use_server_future(move || async move {
  78. let url = format!("{}topstories.json", BASE_API_URL);
  79. let mut stories_ids = reqwest::get(&url).await?.json::<Vec<i64>>().await?;
  80. stories_ids.truncate(30);
  81. Ok(stories_ids)
  82. })?;
  83. match stories().unwrap() {
  84. Ok(list) => rsx! {
  85. div {
  86. for story in list {
  87. ChildrenOrLoading {
  88. key: "{story}",
  89. StoryListing { story }
  90. }
  91. }
  92. }
  93. },
  94. Err(err) => rsx! {"An error occurred while fetching stories {err}"},
  95. }
  96. }
  97. #[component]
  98. fn StoryListing(story: ReadOnlySignal<i64>) -> Element {
  99. let story = use_server_future(move || get_story(story()))?;
  100. let StoryItem {
  101. title,
  102. url,
  103. by,
  104. score,
  105. time,
  106. kids,
  107. id,
  108. ..
  109. } = story().unwrap()?.item;
  110. let url = url.as_deref().unwrap_or_default();
  111. let hostname = url
  112. .trim_start_matches("https://")
  113. .trim_start_matches("http://")
  114. .trim_start_matches("www.");
  115. let score = format!("{score} {}", if score == 1 { " point" } else { " points" });
  116. let comments = format!(
  117. "{} {}",
  118. kids.len(),
  119. if kids.len() == 1 {
  120. " comment"
  121. } else {
  122. " comments"
  123. }
  124. );
  125. let time = time.format("%D %l:%M %p");
  126. rsx! {
  127. div {
  128. padding: "0.5rem",
  129. position: "relative",
  130. div { font_size: "1.5rem",
  131. Link {
  132. to: Route::Homepage { story: PreviewState { active_story: Some(id) } },
  133. "{title}"
  134. }
  135. a {
  136. color: "gray",
  137. href: "https://news.ycombinator.com/from?site={hostname}",
  138. text_decoration: "none",
  139. " ({hostname})"
  140. }
  141. }
  142. div { display: "flex", flex_direction: "row", color: "gray",
  143. div { "{score}" }
  144. div { padding_left: "0.5rem", "by {by}" }
  145. div { padding_left: "0.5rem", "{time}" }
  146. div { padding_left: "0.5rem", "{comments}" }
  147. }
  148. }
  149. }
  150. }
  151. #[derive(Clone, Debug, Default)]
  152. struct PreviewState {
  153. active_story: Option<i64>,
  154. }
  155. impl FromStr for PreviewState {
  156. type Err = ParseIntError;
  157. fn from_str(s: &str) -> Result<Self, Self::Err> {
  158. let state = i64::from_str(s)?;
  159. Ok(PreviewState {
  160. active_story: Some(state),
  161. })
  162. }
  163. }
  164. impl Display for PreviewState {
  165. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  166. if let Some(id) = &self.active_story {
  167. write!(f, "{id}")?;
  168. }
  169. Ok(())
  170. }
  171. }
  172. #[component]
  173. fn Preview(story: ReadOnlySignal<PreviewState>) -> Element {
  174. let PreviewState {
  175. active_story: Some(id),
  176. } = story()
  177. else {
  178. return rsx! {"Hover over a story to preview it here"};
  179. };
  180. let story = use_server_future(use_reactive!(|id| get_story(id)))?;
  181. let story = story().unwrap()?;
  182. rsx! {
  183. div { padding: "0.5rem",
  184. div { font_size: "1.5rem", a { href: story.item.url, "{story.item.title}" } }
  185. if let Some(text) = &story.item.text { div { dangerous_inner_html: "{text}" } }
  186. for comment in story.item.kids.iter().copied() {
  187. ChildrenOrLoading {
  188. key: "{comment}",
  189. Comment { comment }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. #[component]
  196. fn Comment(comment: i64) -> Element {
  197. let comment: Resource<dioxus::Result<CommentData>> =
  198. use_server_future(use_reactive!(|comment| async move {
  199. let url = format!("{}{}{}.json", BASE_API_URL, ITEM_API, comment);
  200. let mut comment = reqwest::get(&url).await?.json::<CommentData>().await?;
  201. Ok(comment)
  202. }))?;
  203. let CommentData {
  204. by,
  205. time,
  206. text,
  207. id,
  208. kids,
  209. ..
  210. } = comment().unwrap()?;
  211. rsx! {
  212. div { padding: "0.5rem",
  213. div { color: "gray", "by {by}" }
  214. div { dangerous_inner_html: "{text}" }
  215. for comment in kids.iter().copied() {
  216. ChildrenOrLoading {
  217. key: "{comment}",
  218. Comment { comment }
  219. }
  220. }
  221. }
  222. }
  223. }
  224. pub static BASE_API_URL: &str = "https://hacker-news.firebaseio.com/v0/";
  225. pub static ITEM_API: &str = "item/";
  226. pub static USER_API: &str = "user/";
  227. const COMMENT_DEPTH: i64 = 1;
  228. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
  229. pub struct StoryPageData {
  230. #[serde(flatten)]
  231. pub item: StoryItem,
  232. #[serde(default)]
  233. pub comments: Vec<CommentData>,
  234. }
  235. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
  236. pub struct CommentData {
  237. pub id: i64,
  238. /// there will be no by field if the comment was deleted
  239. #[serde(default)]
  240. pub by: String,
  241. #[serde(default)]
  242. pub text: String,
  243. #[serde(with = "chrono::serde::ts_seconds")]
  244. pub time: DateTime<Utc>,
  245. #[serde(default)]
  246. pub kids: Vec<i64>,
  247. pub r#type: String,
  248. }
  249. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
  250. pub struct StoryItem {
  251. pub id: i64,
  252. pub title: String,
  253. pub url: Option<String>,
  254. pub text: Option<String>,
  255. #[serde(default)]
  256. pub by: String,
  257. #[serde(default)]
  258. pub score: i64,
  259. #[serde(default)]
  260. pub descendants: i64,
  261. #[serde(with = "chrono::serde::ts_seconds")]
  262. pub time: DateTime<Utc>,
  263. #[serde(default)]
  264. pub kids: Vec<i64>,
  265. pub r#type: String,
  266. }
  267. pub async fn get_story(id: i64) -> dioxus::Result<StoryPageData> {
  268. let url = format!("{}{}{}.json", BASE_API_URL, ITEM_API, id);
  269. Ok(reqwest::get(&url).await?.json::<StoryPageData>().await?)
  270. }
  271. #[component]
  272. fn ChildrenOrLoading(children: Element) -> Element {
  273. rsx! {
  274. SuspenseBoundary {
  275. fallback: |context: SuspenseContext| {
  276. rsx! {
  277. if let Some(placeholder) = context.suspense_placeholder() {
  278. {placeholder}
  279. } else {
  280. LoadingIndicator {}
  281. }
  282. }
  283. },
  284. children
  285. }
  286. }
  287. }
  288. fn LoadingIndicator() -> Element {
  289. rsx! {
  290. div {
  291. class: "spinner",
  292. }
  293. }
  294. }