2024-04-16 06:34:50 +02:00
|
|
|
use leptos::{leptos_dom::logging::console_error, *};
|
2024-04-15 01:56:33 +02:00
|
|
|
use leptos_router::*;
|
2024-04-14 06:45:51 +02:00
|
|
|
|
2024-04-17 03:12:17 +02:00
|
|
|
use leptos_use::{use_cookie, utils::FromToStringCodec};
|
2024-04-14 06:45:51 +02:00
|
|
|
use upub_web::{
|
2024-04-17 03:12:17 +02:00
|
|
|
URL_BASE, context::Timeline, About, LoginBox, MaybeToken, ObjectPage, PostBox,
|
2024-04-17 21:10:20 +02:00
|
|
|
TimelinePage, Navigator, UserPage, Breadcrumb
|
2024-04-14 06:45:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
2024-04-16 08:26:56 +02:00
|
|
|
_ = console_log::init_with_level(log::Level::Info);
|
2024-04-14 06:45:51 +02:00
|
|
|
console_error_panic_hook::set_once();
|
2024-04-17 15:51:50 +02:00
|
|
|
let (token, set_token) = use_cookie::<String, FromToStringCodec>("token");
|
2024-04-17 03:12:17 +02:00
|
|
|
let (username, set_username) = use_cookie::<String, FromToStringCodec>("username");
|
2024-04-17 15:51:50 +02:00
|
|
|
provide_context(token);
|
2024-04-16 06:34:50 +02:00
|
|
|
|
2024-04-17 03:12:17 +02:00
|
|
|
let home_tl = Timeline::new(format!("{URL_BASE}/users/{}/inbox/page", username.get().unwrap_or_default()));
|
2024-04-16 06:34:50 +02:00
|
|
|
let server_tl = Timeline::new(format!("{URL_BASE}/inbox/page"));
|
|
|
|
|
2024-04-17 15:52:08 +02:00
|
|
|
let screen_width = window().screen().map(|x| x.avail_width().unwrap_or_default()).unwrap_or_default();
|
|
|
|
|
|
|
|
let (menu, set_menu) = create_signal(screen_width <= 786);
|
2024-04-17 05:39:45 +02:00
|
|
|
|
2024-04-16 06:34:50 +02:00
|
|
|
spawn_local(async move {
|
2024-04-17 15:51:50 +02:00
|
|
|
if let Err(e) = server_tl.more(token).await {
|
2024-04-16 06:34:50 +02:00
|
|
|
console_error(&format!("error populating timeline: {e}"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-04-17 15:51:50 +02:00
|
|
|
if token.get().is_some() {
|
2024-04-16 06:34:50 +02:00
|
|
|
spawn_local(async move {
|
2024-04-17 15:51:50 +02:00
|
|
|
if let Err(e) = home_tl.more(token).await {
|
2024-04-16 06:34:50 +02:00
|
|
|
console_error(&format!("error populating timeline: {e}"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-14 06:45:51 +02:00
|
|
|
mount_to_body(
|
|
|
|
move || view! {
|
2024-04-17 04:33:34 +02:00
|
|
|
<nav class="w-100 mt-1 mb-1 pb-s">
|
2024-04-17 15:51:50 +02:00
|
|
|
<code class="color ml-3" ><a class="upub-title" href=move || if token.get().present() { "/web/home" } else { "/web/server" } >μpub</a></code>
|
2024-04-17 15:29:25 +02:00
|
|
|
<small class="ml-1 mr-1 hidden-on-tiny" ><a class="clean" href="/web/server" >micro social network, federated</a></small>
|
2024-04-17 04:33:34 +02:00
|
|
|
/* TODO kinda jank with the float but whatever, will do for now */
|
2024-04-17 05:39:45 +02:00
|
|
|
<input type="submit" class="mr-2 rev" on:click=move |_| set_menu.set(!menu.get()) value="menu" style="float: right" />
|
2024-04-14 06:45:51 +02:00
|
|
|
</nav>
|
2024-04-17 05:39:45 +02:00
|
|
|
<hr class="sep" />
|
2024-04-17 04:33:34 +02:00
|
|
|
<div class="container mt-2 pt-2" >
|
2024-04-17 06:51:21 +02:00
|
|
|
<div class="two-col" >
|
|
|
|
<div class="col-side sticky" class:hidden=move || menu.get() >
|
|
|
|
<LoginBox
|
2024-04-17 15:51:50 +02:00
|
|
|
token_tx=set_token
|
|
|
|
token=token
|
2024-04-17 06:51:21 +02:00
|
|
|
username_tx=set_username
|
|
|
|
username=username
|
|
|
|
home_tl=home_tl
|
2024-04-17 21:10:20 +02:00
|
|
|
server_tl=server_tl
|
2024-04-17 06:51:21 +02:00
|
|
|
/>
|
|
|
|
<hr class="mt-1 mb-1" />
|
2024-04-17 15:51:50 +02:00
|
|
|
<Navigator />
|
2024-04-17 06:51:21 +02:00
|
|
|
<hr class="mt-1 mb-1" />
|
|
|
|
<PostBox username=username />
|
|
|
|
</div>
|
|
|
|
<div class="col-main" class:w-100=move || menu.get() >
|
|
|
|
<Router // TODO maybe set base="/web" ?
|
|
|
|
trailing_slash=TrailingSlash::Redirect
|
|
|
|
fallback=move || view! {
|
|
|
|
<div class="center">
|
|
|
|
<h3>nothing to see here!</h3>
|
|
|
|
<p><a href="/web"><button type="button">back to root</button></a></p>
|
|
|
|
</div>
|
|
|
|
}.into_view()
|
|
|
|
>
|
|
|
|
// TODO this is kind of ugly: the whole router gets rebuilt every time we log in/out
|
|
|
|
// in a sense it's what we want: refreshing the home tl is main purpose, but also
|
|
|
|
// server tl may contain stuff we can no longer see, or otherwise we may now be
|
|
|
|
// entitled to see new posts. so while being ugly it's techically correct ig?
|
|
|
|
{move || {
|
|
|
|
view! {
|
|
|
|
<main>
|
|
|
|
<Routes>
|
2024-04-17 15:51:50 +02:00
|
|
|
<Route path="/web" view=move ||
|
|
|
|
if token.get().is_some() {
|
|
|
|
view! { <Redirect path="/web/home" /> }
|
|
|
|
} else {
|
|
|
|
view! { <Redirect path="/web/server" /> }
|
|
|
|
}
|
|
|
|
/>
|
2024-04-16 06:34:50 +02:00
|
|
|
|
2024-04-17 06:51:21 +02:00
|
|
|
<Route path="/web/home" view=move || view! { <TimelinePage name="home" tl=home_tl /> } />
|
|
|
|
<Route path="/web/server" view=move || view! { <TimelinePage name="server" tl=server_tl /> } />
|
2024-04-16 06:34:50 +02:00
|
|
|
|
2024-04-17 15:51:50 +02:00
|
|
|
<Route path="/web/about" view=About />
|
|
|
|
|
2024-04-17 06:51:21 +02:00
|
|
|
<Route path="/web/users/:id" view=UserPage />
|
|
|
|
<Route path="/web/objects/:id" view=ObjectPage />
|
2024-04-16 06:34:50 +02:00
|
|
|
|
2024-04-17 06:51:21 +02:00
|
|
|
<Route path="/" view=move || view! { <Redirect path="/web" /> } />
|
|
|
|
</Routes>
|
|
|
|
</main>
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
</Router>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-04-14 06:45:51 +02:00
|
|
|
</div>
|
2024-04-17 05:39:45 +02:00
|
|
|
<footer>
|
|
|
|
<div>
|
|
|
|
<hr class="sep" />
|
|
|
|
<span class="footer" >"\u{26fc} woven under moonlight :: "<a href="https://git.alemi.dev/upub.git" target="_blank" >src</a>" :: wip by alemi "</span>
|
|
|
|
</div>
|
|
|
|
</footer>
|
2024-04-14 06:45:51 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|