Use <a> instead of event handlers for functioning history.
This commit is contained in:
parent
4390d24492
commit
fd721c53d8
129
web/src/lib.rs
129
web/src/lib.rs
@ -22,46 +22,69 @@ const SEARCH_RESULTS_PER_PAGE: usize = 20;
|
|||||||
|
|
||||||
// `init` describes what should happen when your app started.
|
// `init` describes what should happen when your app started.
|
||||||
fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
|
fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
|
||||||
warn!("init called");
|
orders
|
||||||
log!(url);
|
.subscribe(on_url_changed)
|
||||||
let mut url = url.clone();
|
.notify(subs::UrlChanged(url.clone()));
|
||||||
let mut query = "".to_string();
|
|
||||||
let hpp = url.next_hash_path_part();
|
Model {
|
||||||
log!(hpp);
|
context: Context::None,
|
||||||
match hpp {
|
query: "".to_string(),
|
||||||
Some("t") => {
|
refreshing_state: RefreshingState::None,
|
||||||
let tid = url.next_hash_path_part().unwrap_or("").to_string();
|
}
|
||||||
orders.send_msg(Msg::ShowPrettyRequest(tid));
|
}
|
||||||
}
|
|
||||||
Some("s") => {
|
fn on_url_changed(uc: subs::UrlChanged) -> Msg {
|
||||||
query = url
|
let mut url = uc.0;
|
||||||
.next_hash_path_part()
|
info!(
|
||||||
.map(|q| Url::decode_uri_component(q).unwrap_or("".to_string()))
|
"url changed '{}', history {}",
|
||||||
.unwrap_or("".to_string());
|
url,
|
||||||
orders.send_msg(Msg::SearchRequest {
|
history().length().unwrap_or(0)
|
||||||
query: query.to_string(),
|
);
|
||||||
|
let hpp = url.remaining_hash_path_parts();
|
||||||
|
match hpp.as_slice() {
|
||||||
|
["t", tid] => Msg::ShowPrettyRequest(tid.to_string()),
|
||||||
|
["s", query] => {
|
||||||
|
let query = Url::decode_uri_component(query).unwrap_or("".to_string());
|
||||||
|
Msg::SearchRequest {
|
||||||
|
query,
|
||||||
page: 0,
|
page: 0,
|
||||||
results_per_page: SEARCH_RESULTS_PER_PAGE,
|
results_per_page: SEARCH_RESULTS_PER_PAGE,
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
["s", query, page] => {
|
||||||
|
let query = Url::decode_uri_component(query).unwrap_or("".to_string());
|
||||||
|
let page = page[1..].parse().unwrap_or(0);
|
||||||
|
Msg::SearchRequest {
|
||||||
|
query,
|
||||||
|
page,
|
||||||
|
results_per_page: SEARCH_RESULTS_PER_PAGE,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
p => {
|
p => {
|
||||||
log!(p);
|
if !p.is_empty() {
|
||||||
orders.send_msg(Msg::SearchRequest {
|
info!("Unhandled path '{p:?}'");
|
||||||
|
}
|
||||||
|
Msg::SearchRequest {
|
||||||
query: "".to_string(),
|
query: "".to_string(),
|
||||||
page: 0,
|
page: 0,
|
||||||
results_per_page: SEARCH_RESULTS_PER_PAGE,
|
results_per_page: SEARCH_RESULTS_PER_PAGE,
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
orders.subscribe(|uc: subs::UrlChanged| {
|
}
|
||||||
info!("uc {}", uc.0);
|
|
||||||
});
|
|
||||||
|
|
||||||
info!("init query '{}'", query);
|
mod urls {
|
||||||
Model {
|
use seed::Url;
|
||||||
context: Context::None,
|
pub fn search(query: &str, page: usize) -> Url {
|
||||||
query,
|
let query = Url::encode_uri_component(query);
|
||||||
refreshing_state: RefreshingState::None,
|
if page > 0 {
|
||||||
|
Url::new().set_hash_path(["s", &query, &format!("p{page}")])
|
||||||
|
} else {
|
||||||
|
Url::new().set_hash_path(["s", &query])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn thread(tid: &str) -> Url {
|
||||||
|
Url::new().set_hash_path(["t", tid])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,8 +158,6 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
|
|||||||
} => {
|
} => {
|
||||||
info!("searching for '{query}' pg {page} # / pg {results_per_page}");
|
info!("searching for '{query}' pg {page} # / pg {results_per_page}");
|
||||||
model.query = query.clone();
|
model.query = query.clone();
|
||||||
let url = Url::new().set_hash_path(["s", &query]);
|
|
||||||
orders.request_url(url);
|
|
||||||
orders.skip().perform_cmd(async move {
|
orders.skip().perform_cmd(async move {
|
||||||
Msg::SearchResult(search_request(&query, page, results_per_page).await)
|
Msg::SearchResult(search_request(&query, page, results_per_page).await)
|
||||||
});
|
});
|
||||||
@ -150,8 +171,6 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Msg::ShowRequest(tid) => {
|
Msg::ShowRequest(tid) => {
|
||||||
let url = Url::new().set_hash_path(["t", &tid]);
|
|
||||||
orders.request_url(url);
|
|
||||||
orders
|
orders
|
||||||
.skip()
|
.skip()
|
||||||
.perform_cmd(async move { Msg::ShowResult(show_request(&tid).await) });
|
.perform_cmd(async move { Msg::ShowResult(show_request(&tid).await) });
|
||||||
@ -165,8 +184,6 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Msg::ShowPrettyRequest(tid) => {
|
Msg::ShowPrettyRequest(tid) => {
|
||||||
let url = Url::new().set_hash_path(["t", &tid]);
|
|
||||||
orders.request_url(url);
|
|
||||||
orders
|
orders
|
||||||
.skip()
|
.skip()
|
||||||
.perform_cmd(async move { Msg::ShowPrettyResult(show_pretty_request(&tid).await) });
|
.perform_cmd(async move { Msg::ShowPrettyResult(show_pretty_request(&tid).await) });
|
||||||
@ -181,11 +198,7 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
|
|||||||
Msg::NextPage => {
|
Msg::NextPage => {
|
||||||
match &model.context {
|
match &model.context {
|
||||||
Context::Search(sr) => {
|
Context::Search(sr) => {
|
||||||
orders.send_msg(Msg::SearchRequest {
|
orders.request_url(urls::search(&sr.query, sr.page + 1));
|
||||||
query: sr.query.clone(),
|
|
||||||
page: sr.page + 1,
|
|
||||||
results_per_page: sr.results_per_page,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
Context::Thread(_) => (), // do nothing (yet?)
|
Context::Thread(_) => (), // do nothing (yet?)
|
||||||
Context::None => (), // do nothing (yet?)
|
Context::None => (), // do nothing (yet?)
|
||||||
@ -194,11 +207,7 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
|
|||||||
Msg::PreviousPage => {
|
Msg::PreviousPage => {
|
||||||
match &model.context {
|
match &model.context {
|
||||||
Context::Search(sr) => {
|
Context::Search(sr) => {
|
||||||
orders.send_msg(Msg::SearchRequest {
|
orders.request_url(urls::search(&sr.query, sr.page.saturating_sub(1)));
|
||||||
query: sr.query.clone(),
|
|
||||||
page: sr.page.saturating_sub(1),
|
|
||||||
results_per_page: sr.results_per_page,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
Context::Thread(_) => (), // do nothing (yet?)
|
Context::Thread(_) => (), // do nothing (yet?)
|
||||||
Context::None => (), // do nothing (yet?)
|
Context::None => (), // do nothing (yet?)
|
||||||
@ -421,6 +430,9 @@ fn tags_chiclet(tags: &[String], is_mobile: bool) -> impl Iterator<Item = Node<M
|
|||||||
let classes = C!["tag", IF!(is_mobile => "is-small")];
|
let classes = C!["tag", IF!(is_mobile => "is-small")];
|
||||||
let tag = tag.clone();
|
let tag = tag.clone();
|
||||||
a![
|
a![
|
||||||
|
attrs! {
|
||||||
|
At::Href => urls::search(&format!("tag:{tag}"), 0)
|
||||||
|
},
|
||||||
match tag.as_str() {
|
match tag.as_str() {
|
||||||
"attachment" => span![classes, style, "📎"],
|
"attachment" => span![classes, style, "📎"],
|
||||||
"replied" => span![classes, style, i![C!["fa-solid", "fa-reply"]]],
|
"replied" => span![classes, style, i![C!["fa-solid", "fa-reply"]]],
|
||||||
@ -523,9 +535,12 @@ fn view_search_results(query: &str, search_results: &shared::SearchResult) -> No
|
|||||||
C!["subject"],
|
C!["subject"],
|
||||||
tags_chiclet(&r.tags, false),
|
tags_chiclet(&r.tags, false),
|
||||||
" ",
|
" ",
|
||||||
span![
|
a![
|
||||||
|
C!["has-text-light"],
|
||||||
|
attrs! {
|
||||||
|
At::Href => urls::thread(&tid)
|
||||||
|
},
|
||||||
&r.subject,
|
&r.subject,
|
||||||
ev(Ev::Click, move |_| Msg::ShowPrettyRequest(tid))
|
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
td![C!["date"], &r.date_relative]
|
td![C!["date"], &r.date_relative]
|
||||||
@ -648,21 +663,17 @@ fn view_header(query: &str, refresh_request: &RefreshingState) -> Node<Msg> {
|
|||||||
],
|
],
|
||||||
a![
|
a![
|
||||||
C!["navbar-item", "button"],
|
C!["navbar-item", "button"],
|
||||||
|
attrs! {
|
||||||
|
At::Href => urls::search("is:unread", 0)
|
||||||
|
},
|
||||||
"Unread",
|
"Unread",
|
||||||
ev(Ev::Click, |_| Msg::SearchRequest {
|
|
||||||
query: "is:unread".to_string(),
|
|
||||||
page: 0,
|
|
||||||
results_per_page: SEARCH_RESULTS_PER_PAGE,
|
|
||||||
}),
|
|
||||||
],
|
],
|
||||||
a![
|
a![
|
||||||
C!["navbar-item", "button"],
|
C!["navbar-item", "button"],
|
||||||
|
attrs! {
|
||||||
|
At::Href => urls::search("", 0)
|
||||||
|
},
|
||||||
"All",
|
"All",
|
||||||
ev(Ev::Click, |_| Msg::SearchRequest {
|
|
||||||
query: "".to_string(),
|
|
||||||
page: 0,
|
|
||||||
results_per_page: SEARCH_RESULTS_PER_PAGE,
|
|
||||||
}),
|
|
||||||
],
|
],
|
||||||
input![
|
input![
|
||||||
C!["navbar-item", "input"],
|
C!["navbar-item", "input"],
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user