2024-04-17 22:07:47 +02:00
|
|
|
use leptos::*;
|
|
|
|
use leptos_router::*;
|
|
|
|
use crate::prelude::*;
|
2024-06-21 05:19:50 +02:00
|
|
|
use crate::CONTACT;
|
2024-04-17 22:07:47 +02:00
|
|
|
|
2024-06-12 15:27:10 +02:00
|
|
|
use leptos_use::{signal_debounced, storage::use_local_storage, use_cookie, use_element_size, use_window_scroll, utils::{FromToStringCodec, JsonCodec}, UseElementSizeReturn};
|
2024-04-17 22:07:47 +02:00
|
|
|
|
2024-06-08 03:39:38 +02:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct Feeds {
|
|
|
|
pub home: Timeline,
|
|
|
|
pub global: Timeline,
|
2024-06-28 04:11:25 +02:00
|
|
|
pub notifications: Timeline,
|
2024-06-08 03:39:38 +02:00
|
|
|
// exploration feeds
|
|
|
|
pub user: Timeline,
|
|
|
|
pub server: Timeline,
|
|
|
|
pub context: Timeline,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Feeds {
|
|
|
|
pub fn new(username: &str) -> Self {
|
|
|
|
Feeds {
|
2024-06-28 04:11:25 +02:00
|
|
|
home: Timeline::new(format!("{URL_BASE}/actors/{username}/inbox/page")),
|
|
|
|
notifications: Timeline::new(format!("{URL_BASE}/actors/{username}/notifications/page")),
|
|
|
|
global: Timeline::new(format!("{URL_BASE}/inbox/page")),
|
2024-06-08 03:39:38 +02:00
|
|
|
user: Timeline::new(format!("{URL_BASE}/actors/{username}/outbox/page")),
|
|
|
|
server: Timeline::new(format!("{URL_BASE}/outbox/page")),
|
|
|
|
context: Timeline::new(format!("{URL_BASE}/outbox/page")), // TODO ehhh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset(&self) {
|
|
|
|
self.home.reset(None);
|
2024-06-28 04:11:25 +02:00
|
|
|
self.notifications.reset(None);
|
2024-06-08 03:39:38 +02:00
|
|
|
self.global.reset(None);
|
|
|
|
self.user.reset(None);
|
|
|
|
self.server.reset(None);
|
|
|
|
self.context.reset(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 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();
|
2024-05-29 20:51:30 +02:00
|
|
|
|
2024-06-08 03:39:38 +02:00
|
|
|
let feeds = Feeds::new(&username);
|
|
|
|
|
|
|
|
provide_context(auth);
|
|
|
|
provide_context(config);
|
|
|
|
provide_context(feeds);
|
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-06-12 06:02:36 +02:00
|
|
|
let screen_width = document().body().map(|x| x.client_width()).unwrap_or_default();
|
|
|
|
tracing::info!("detected width of {screen_width}");
|
2024-04-17 22:07:47 +02:00
|
|
|
|
2024-06-12 06:02:36 +02:00
|
|
|
let (menu, set_menu) = create_signal(screen_width < 768);
|
2024-04-23 02:48:30 +02:00
|
|
|
let (advanced, set_advanced) = create_signal(false);
|
2024-04-17 22:07:47 +02:00
|
|
|
|
2024-06-29 04:30:07 +02:00
|
|
|
let title_target = move || if auth.present() { "/web/home" } else { "/web/global" };
|
2024-05-27 16:50:42 +02:00
|
|
|
|
2024-06-12 06:02:36 +02:00
|
|
|
// refresh token immediately and every hour
|
|
|
|
let refresh = move || spawn_local(async move { Auth::refresh(auth.token, set_token, set_userid).await; });
|
|
|
|
refresh();
|
|
|
|
set_interval(refresh, std::time::Duration::from_secs(3600));
|
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-06-29 04:30:07 +02:00
|
|
|
<small class="ml-1 mr-1 hidden-on-tiny" ><a class="clean" href="/web/global" >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-06-12 06:02:36 +02:00
|
|
|
<div class="col-side sticky pb-s" class:hidden=menu >
|
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
|
|
|
/>
|
|
|
|
<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>
|
2024-06-12 06:02:36 +02:00
|
|
|
<div class="col-main" class:w-100=menu >
|
2024-04-17 22:07:47 +02:00
|
|
|
<Router // TODO maybe set base="/web" ?
|
|
|
|
trailing_slash=TrailingSlash::Redirect
|
2024-06-10 21:44:28 +02:00
|
|
|
fallback=|| view! { <NotFound /> }
|
2024-04-17 22:07:47 +02:00
|
|
|
>
|
2024-05-29 18:04:47 +02:00
|
|
|
<main>
|
|
|
|
<Routes>
|
|
|
|
<Route path="/" view=move || view! { <Redirect path="/web" /> } />
|
2024-06-12 06:02:36 +02:00
|
|
|
<Route path="/web" view=Scrollable >
|
2024-06-10 21:44:28 +02:00
|
|
|
<Route path="" view=move ||
|
|
|
|
if auth.present() {
|
|
|
|
view! { <Redirect path="home" /> }
|
|
|
|
} else {
|
2024-07-03 03:22:00 +02:00
|
|
|
view! { <Redirect path="global" /> }
|
2024-06-10 21:44:28 +02:00
|
|
|
}
|
|
|
|
/>
|
2024-06-12 06:02:36 +02:00
|
|
|
<Route path="home" view=move || view! { <Feed tl=feeds.home /> } />
|
2024-06-28 04:11:25 +02:00
|
|
|
<Route path="global" view=move || view! { <Feed tl=feeds.global /> } />
|
2024-06-12 06:02:36 +02:00
|
|
|
<Route path="local" view=move || view! { <Feed tl=feeds.server /> } />
|
2024-06-28 04:11:25 +02:00
|
|
|
<Route path="notifications" view=move || view! { <Feed tl=feeds.notifications /> } />
|
2024-06-10 21:44:28 +02:00
|
|
|
|
|
|
|
<Route path="about" view=AboutPage />
|
|
|
|
<Route path="config" view=move || view! { <ConfigPage setter=set_config /> } />
|
|
|
|
<Route path="dev" view=DebugPage />
|
|
|
|
|
2024-06-12 06:02:36 +02:00
|
|
|
<Route path="actors/:id" view=ActorHeader > // TODO can we avoid this?
|
|
|
|
<Route path="" view=ActorPosts />
|
|
|
|
<Route path="following" view=move || view! { <FollowList outgoing=true /> } />
|
|
|
|
<Route path="followers" view=move || view! { <FollowList outgoing=false /> } />
|
2024-06-10 21:44:28 +02:00
|
|
|
</Route>
|
|
|
|
|
2024-06-12 06:02:36 +02:00
|
|
|
<Route path="objects/:id" view=ObjectView />
|
2024-06-10 21:44:28 +02:00
|
|
|
// <Route path="/web/activities/:id" view=move || view! { <ActivityPage tl=context_tl /> } />
|
|
|
|
|
|
|
|
<Route path="search" view=SearchPage />
|
|
|
|
<Route path="register" view=RegisterPage />
|
|
|
|
</Route>
|
2024-05-29 18:04:47 +02:00
|
|
|
</Routes>
|
|
|
|
</main>
|
2024-04-17 22:07:47 +02:00
|
|
|
</Router>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<footer>
|
2024-05-21 15:25:47 +02:00
|
|
|
<div class="sep-top">
|
2024-06-21 05:19:50 +02:00
|
|
|
<span class="footer" >"\u{26fc} woven under moonlight :: "<a class="clean" href="https://join.upub.social/" target="_blank" >about</a>" :: "<a class="clean" href=format!("mailto:{CONTACT}")>contact</a>" :: "<a class="clean" href="/web/dev">dev</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>
|
|
|
|
}
|
|
|
|
}
|
2024-06-10 21:44:28 +02:00
|
|
|
|
|
|
|
#[component]
|
2024-06-12 06:02:36 +02:00
|
|
|
fn Scrollable() -> impl IntoView {
|
2024-06-10 21:44:28 +02:00
|
|
|
let location = use_location();
|
2024-06-22 06:00:01 +02:00
|
|
|
let feeds = use_context::<Feeds>().expect("missing feeds context");
|
|
|
|
let relevant_timeline = Signal::derive(move || {
|
|
|
|
let path = location.pathname.get();
|
|
|
|
if path.contains("/web/home") {
|
|
|
|
Some(feeds.home)
|
2024-06-28 04:11:25 +02:00
|
|
|
} else if path.contains("/web/global") {
|
2024-06-22 06:00:01 +02:00
|
|
|
Some(feeds.global)
|
2024-06-28 04:11:25 +02:00
|
|
|
} else if path.contains("/web/local") {
|
2024-06-22 06:00:01 +02:00
|
|
|
Some(feeds.server)
|
2024-06-28 04:11:25 +02:00
|
|
|
} else if path.starts_with("/web/notifications") {
|
|
|
|
Some(feeds.notifications)
|
2024-06-22 06:00:01 +02:00
|
|
|
} else if path.starts_with("/web/actors") {
|
|
|
|
Some(feeds.user)
|
|
|
|
} else if path.starts_with("/web/objects") {
|
|
|
|
Some(feeds.context)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
2024-06-10 21:44:28 +02:00
|
|
|
let breadcrumb = Signal::derive(move || {
|
|
|
|
let path = location.pathname.get();
|
2024-06-12 06:02:36 +02:00
|
|
|
let mut path_iter = path.split('/').skip(2);
|
2024-06-10 21:44:28 +02:00
|
|
|
// TODO wow this breadcrumb logic really isnt nice can we make it better??
|
|
|
|
match path_iter.next() {
|
|
|
|
Some("actors") => match path_iter.next() {
|
|
|
|
None => "actors :: all".to_string(),
|
|
|
|
Some(id) => {
|
|
|
|
let mut out = "actors :: ".to_string();
|
|
|
|
if id.starts_with('+') {
|
|
|
|
out.push_str("proxy");
|
|
|
|
} else {
|
|
|
|
out.push_str(id);
|
|
|
|
}
|
|
|
|
if let Some(x) = path_iter.next() {
|
|
|
|
out.push_str(" :: ");
|
|
|
|
out.push_str(x);
|
|
|
|
}
|
|
|
|
out
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Some(p) => p.to_string(),
|
|
|
|
None => "?".to_string(),
|
|
|
|
}
|
|
|
|
});
|
2024-06-12 06:02:36 +02:00
|
|
|
let element = create_node_ref();
|
2024-06-12 15:21:37 +02:00
|
|
|
let should_load = use_scroll_limit(element, 500.0);
|
2024-06-12 06:02:36 +02:00
|
|
|
provide_context(should_load);
|
2024-06-10 21:44:28 +02:00
|
|
|
view! {
|
2024-06-12 06:02:36 +02:00
|
|
|
<div class="mb-1" node_ref=element>
|
|
|
|
<div class="tl-header w-100 center mb-1">
|
|
|
|
<a class="breadcrumb mr-1" href="javascript:history.back()" ><b>"<<"</b></a>
|
|
|
|
<b>{crate::NAME}</b>" :: "{breadcrumb}
|
2024-06-22 06:00:01 +02:00
|
|
|
{move || relevant_timeline.get().map(|tl| view! {
|
2024-06-22 06:24:45 +02:00
|
|
|
<a class="breadcrumb ml-1" href="#" on:click=move|_| tl.refresh() ><b>"↺"</b></a>
|
2024-06-22 06:00:01 +02:00
|
|
|
})}
|
2024-06-12 06:02:36 +02:00
|
|
|
</div>
|
|
|
|
<Outlet />
|
2024-06-10 21:44:28 +02:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[component]
|
|
|
|
pub fn NotFound() -> impl IntoView {
|
|
|
|
view! {
|
|
|
|
<div class="center">
|
|
|
|
<h3>nothing to see here!</h3>
|
|
|
|
<p><a href="/web"><button type="button">back to root</button></a></p>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[component]
|
2024-06-12 06:02:36 +02:00
|
|
|
pub fn Loader() -> impl IntoView {
|
2024-06-10 21:44:28 +02:00
|
|
|
view! {
|
2024-06-12 06:02:36 +02:00
|
|
|
<div class="center mt-1 mb-1" >
|
2024-06-10 21:44:28 +02:00
|
|
|
<button type="button" disabled>"loading "<span class="dots"></span></button>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
2024-06-12 06:02:36 +02:00
|
|
|
|
|
|
|
pub fn use_scroll_limit<El, T>(el: El, offset: f64) -> Signal<bool>
|
|
|
|
where
|
|
|
|
El: Into<leptos_use::core::ElementMaybeSignal<T, web_sys::Element>> + Clone + 'static,
|
|
|
|
T: Into<web_sys::Element> + Clone + 'static,
|
|
|
|
{
|
|
|
|
let (load, set_load) = create_signal(false);
|
|
|
|
let (_x, y) = use_window_scroll();
|
2024-06-12 15:21:37 +02:00
|
|
|
let UseElementSizeReturn { height: screen_height, .. } = use_element_size("html");
|
2024-06-12 06:02:36 +02:00
|
|
|
let UseElementSizeReturn { height, .. } = use_element_size(el);
|
2024-06-12 15:21:37 +02:00
|
|
|
let scroll_state = Signal::derive(move || (y.get(), height.get(), screen_height.get()));
|
|
|
|
let scroll_state_throttled = signal_debounced(
|
|
|
|
scroll_state,
|
|
|
|
50.
|
|
|
|
);
|
2024-06-12 06:02:36 +02:00
|
|
|
let _ = watch(
|
|
|
|
move || scroll_state_throttled.get(),
|
2024-06-12 15:21:37 +02:00
|
|
|
move |(y, height, screen), _, _| {
|
2024-06-12 06:02:36 +02:00
|
|
|
let before = load.get();
|
2024-06-12 15:21:37 +02:00
|
|
|
let after = *height <= *screen || y + screen + offset >= *height;
|
|
|
|
let force = *y + screen >= *height;
|
|
|
|
if force || after != before || *height < *screen {
|
|
|
|
set_load.set(after)
|
|
|
|
}
|
2024-06-12 06:02:36 +02:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
load.into()
|
|
|
|
}
|