2024-04-17 22:07:47 +02:00
|
|
|
use leptos::*;
|
|
|
|
use leptos_router::*;
|
2024-05-27 07:36:38 +02:00
|
|
|
use reqwest::Method;
|
2024-04-17 22:07:47 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2024-05-27 07:36:38 +02:00
|
|
|
use leptos_use::{storage::use_local_storage, use_cookie, utils::{FromToStringCodec, JsonCodec}};
|
2024-04-17 22:07:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
#[component]
|
|
|
|
pub fn App() -> impl IntoView {
|
2024-05-27 07:36:38 +02:00
|
|
|
let (token, set_token) = use_cookie::<String, FromToStringCodec>("token");
|
2024-05-02 02:07:11 +02:00
|
|
|
let (userid, set_userid) = use_cookie::<String, FromToStringCodec>("user_id");
|
2024-05-27 07:36:38 +02:00
|
|
|
let (config, set_config, _) = use_local_storage::<crate::Config, JsonCodec>("config");
|
|
|
|
|
2024-05-02 02:07:11 +02:00
|
|
|
let auth = Auth { token, userid };
|
2024-04-17 23:07:56 +02:00
|
|
|
provide_context(auth);
|
2024-05-12 00:11:28 +02:00
|
|
|
provide_context(config);
|
2024-04-17 22:07:47 +02:00
|
|
|
|
2024-05-03 03:55:26 +02:00
|
|
|
let username = auth.userid.get_untracked()
|
|
|
|
.map(|x| x.split('/').last().unwrap_or_default().to_string())
|
|
|
|
.unwrap_or_default();
|
|
|
|
let home_tl = Timeline::new(format!("{URL_BASE}/users/{username}/inbox/page"));
|
2024-04-17 22:07:47 +02:00
|
|
|
let server_tl = Timeline::new(format!("{URL_BASE}/inbox/page"));
|
2024-05-29 04:47:26 +02:00
|
|
|
let local_tl = Timeline::new(format!("{URL_BASE}/outbox/page"));
|
2024-05-03 03:55:26 +02:00
|
|
|
let user_tl = Timeline::new(format!("{URL_BASE}/users/{username}/outbox/page"));
|
2024-04-19 06:59:34 +02:00
|
|
|
let context_tl = Timeline::new(format!("{URL_BASE}/outbox/page"));
|
2024-04-17 22:07:47 +02:00
|
|
|
|
2024-05-01 16:46:19 +02:00
|
|
|
let reply_controls = ReplyControls::default();
|
|
|
|
provide_context(reply_controls);
|
|
|
|
|
2024-04-17 22:07:47 +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-23 02:48:30 +02:00
|
|
|
let (advanced, set_advanced) = create_signal(false);
|
2024-04-17 22:07:47 +02:00
|
|
|
|
2024-05-03 03:55:26 +02:00
|
|
|
let auth_present = auth.token.get_untracked().is_some(); // skip helper to use get_untracked
|
2024-05-27 16:50:42 +02:00
|
|
|
let title_target = move || if auth_present { "/web/home" } else { "/web/server" };
|
|
|
|
|
|
|
|
if let Some(tok) = token.get_untracked() {
|
2024-04-17 22:07:47 +02:00
|
|
|
spawn_local(async move {
|
2024-05-27 16:50:42 +02:00
|
|
|
// refresh token first, or verify that we're still authed
|
|
|
|
match reqwest::Client::new()
|
|
|
|
.request(Method::PATCH, format!("{URL_BASE}/auth"))
|
|
|
|
.json(&serde_json::json!({"token": tok}))
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Err(e) => tracing::error!("could not refresh token: {e}"),
|
|
|
|
Ok(res) => match res.error_for_status() {
|
|
|
|
Err(e) => tracing::error!("server rejected refresh: {e}"),
|
|
|
|
Ok(doc) => match doc.json::<AuthResponse>().await {
|
|
|
|
Err(e) => tracing::error!("failed parsing auth response: {e}"),
|
|
|
|
Ok(auth) => {
|
|
|
|
set_token.set(Some(auth.token));
|
|
|
|
set_userid.set(Some(auth.user));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2024-04-17 22:07:47 +02:00
|
|
|
}
|
|
|
|
|
2024-05-27 16:50:42 +02:00
|
|
|
server_tl.more(auth);
|
2024-05-29 18:04:27 +02:00
|
|
|
local_tl.more(auth);
|
2024-05-27 16:50:42 +02:00
|
|
|
if auth_present { home_tl.more(auth) };
|
|
|
|
})
|
2024-05-29 18:04:27 +02:00
|
|
|
} else {
|
|
|
|
server_tl.more(auth);
|
|
|
|
local_tl.more(auth);
|
|
|
|
}
|
2024-04-17 23:07:56 +02:00
|
|
|
|
2024-04-17 22:07:47 +02:00
|
|
|
view! {
|
|
|
|
<nav class="w-100 mt-1 mb-1 pb-s">
|
2024-05-27 16:50:42 +02:00
|
|
|
<code class="color ml-3" ><a class="upub-title" href=title_target >μpub</a></code>
|
2024-05-13 17:10:46 +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 22:07:47 +02:00
|
|
|
/* TODO kinda jank with the float but whatever, will do for now */
|
|
|
|
<input type="submit" class="mr-2 rev" on:click=move |_| set_menu.set(!menu.get()) value="menu" style="float: right" />
|
|
|
|
</nav>
|
2024-05-13 01:21:20 +02:00
|
|
|
<hr class="sep sticky" />
|
2024-04-17 22:07:47 +02:00
|
|
|
<div class="container mt-2 pt-2" >
|
|
|
|
<div class="two-col" >
|
2024-04-23 03:05:22 +02:00
|
|
|
<div class="col-side sticky pb-s" class:hidden=move || menu.get() >
|
2024-05-13 01:21:20 +02:00
|
|
|
<Navigator />
|
|
|
|
<hr class="mt-1 mb-1" />
|
2024-04-17 22:07:47 +02:00
|
|
|
<LoginBox
|
2024-05-01 18:22:25 +02:00
|
|
|
token_tx=set_token
|
2024-05-02 02:07:11 +02:00
|
|
|
userid_tx=set_userid
|
2024-04-17 22:07:47 +02:00
|
|
|
home_tl=home_tl
|
|
|
|
server_tl=server_tl
|
|
|
|
/>
|
|
|
|
<hr class="mt-1 mb-1" />
|
2024-05-13 01:21:20 +02:00
|
|
|
<div class:hidden=move || !auth.present() >
|
|
|
|
{move || if advanced.get() { view! {
|
|
|
|
<AdvancedPostBox advanced=set_advanced/>
|
|
|
|
}} else { view! {
|
|
|
|
<PostBox advanced=set_advanced/>
|
|
|
|
}}}
|
|
|
|
<hr class="only-on-mobile sep mb-0 pb-0" />
|
|
|
|
</div>
|
2024-04-17 22:07:47 +02:00
|
|
|
</div>
|
|
|
|
<div class="col-main" class:w-100=move || menu.get() >
|
|
|
|
<Router // TODO maybe set base="/web" ?
|
|
|
|
trailing_slash=TrailingSlash::Redirect
|
|
|
|
fallback=move || view! {
|
2024-05-13 04:03:29 +02:00
|
|
|
<Breadcrumb back=true >404</Breadcrumb>
|
2024-04-17 22:07:47 +02:00
|
|
|
<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>
|
|
|
|
<Route path="/web" view=move ||
|
2024-05-01 18:22:25 +02:00
|
|
|
if auth.present() {
|
2024-04-17 22:07:47 +02:00
|
|
|
view! { <Redirect path="/web/home" /> }
|
|
|
|
} else {
|
|
|
|
view! { <Redirect path="/web/server" /> }
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<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-05-29 04:47:26 +02:00
|
|
|
<Route path="/web/local" view=move || view! { <TimelinePage name="server" tl=local_tl /> } />
|
2024-04-17 22:07:47 +02:00
|
|
|
|
|
|
|
<Route path="/web/about" view=AboutPage />
|
2024-05-13 01:20:48 +02:00
|
|
|
<Route path="/web/config" view=move || view! { <ConfigPage setter=set_config /> } />
|
|
|
|
<Route path="/web/config/dev" view=DebugPage />
|
2024-04-17 22:07:47 +02:00
|
|
|
|
2024-04-19 06:58:54 +02:00
|
|
|
<Route path="/web/users/:id" view=move || view! { <UserPage tl=user_tl /> } />
|
2024-04-19 06:59:34 +02:00
|
|
|
<Route path="/web/objects/:id" view=move || view! { <ObjectPage tl=context_tl /> } />
|
2024-04-17 22:07:47 +02:00
|
|
|
|
2024-05-03 06:14:36 +02:00
|
|
|
<Route path="/web/search" view=SearchPage />
|
2024-05-13 04:02:43 +02:00
|
|
|
<Route path="/web/register" view=RegisterPage />
|
2024-04-21 19:37:20 +02:00
|
|
|
|
2024-04-17 22:07:47 +02:00
|
|
|
<Route path="/" view=move || view! { <Redirect path="/web" /> } />
|
|
|
|
</Routes>
|
|
|
|
</main>
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
</Router>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<footer>
|
2024-05-21 15:25:47 +02:00
|
|
|
<div class="sep-top">
|
2024-05-21 15:28:22 +02:00
|
|
|
<span class="footer" >"\u{26fc} woven under moonlight :: "<a class="clean" href="https://git.alemi.dev/upub.git" target="_blank" >src</a>" :: "<a class="clean" href="mailto:abuse@alemi.dev">contact</a>" :: "<a class="clean" href="javascript:window.scrollTo({top:0, behavior:'smooth'})">top</a></span>
|
2024-04-17 22:07:47 +02:00
|
|
|
</div>
|
|
|
|
</footer>
|
|
|
|
}
|
|
|
|
}
|