Skip to content

Commit

Permalink
fix(post_view): gracefully handle broken initial post
Browse files Browse the repository at this point in the history
  • Loading branch information
rupansh committed Jan 15, 2024
1 parent efbbfad commit cbb3e84
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
37 changes: 21 additions & 16 deletions src/page/post_view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn ScrollingView() -> impl IntoView {

view! {
<div
class="snap-mandatory snap-y overflow-y-scroll h-screen"
class="snap-mandatory snap-y overflow-y-scroll h-screen bg-black"
style:scroll-snap-points-y="repeat(100vh)"
>
<For
Expand All @@ -102,17 +102,25 @@ pub fn ScrollingView() -> impl IntoView {
}

#[component]
pub fn PostViewWithUpdates(initial_post: PostDetails) -> impl IntoView {
pub fn PostViewWithUpdates(initial_post: Option<PostDetails>) -> impl IntoView {
let (fetch_cursor, set_fetch_cursor) = create_signal({
let mut fetch_cursor = FetchCursor {
start: 1,
limit: POST_CNT as u64,
};
if initial_post.is_some() {
fetch_cursor.limit -= 1;
}
fetch_cursor
});

// TODO: this is a dead simple with no GC
// We're using virtual lists for DOM, so this doesn't consume much memory
// as uids only occupy 32 bytes each
// but ideally this should be cleaned up
let (video_queue, set_video_queue) = create_signal(vec![initial_post]);
let (video_queue, set_video_queue) =
create_signal(initial_post.map(|p| vec![p]).unwrap_or_default());
let current_idx = create_rw_signal(0);
let (fetch_cursor, set_fetch_cursor) = create_signal(FetchCursor {
start: 1,
limit: POST_CNT as u64 - 1,
});

let fetch_video_uids = Resource::once(move || async move {
let canisters = expect_context::<Canisters>();
Expand Down Expand Up @@ -182,17 +190,15 @@ pub fn PostView() -> impl IntoView {
go_to_root();
return None;
};
let uid = match get_post_uid(&canisters, canister, post).await {
Ok(Some(uid)) => uid,

match get_post_uid(&canisters, canister, post).await {
Ok(Some(uid)) => Some(uid),
Err(e) => {
failure_redirect(e);
return None;
None
}
Ok(None) => {
panic!("initial post not found");
}
};
Some(uid)
Ok(None) => None,
}
},
);

Expand All @@ -202,7 +208,6 @@ pub fn PostView() -> impl IntoView {
{move || {
fetch_first_video_uid
.get()
.flatten()
.map(|post| view! { <PostViewWithUpdates initial_post=post/> })
}}

Expand Down
16 changes: 9 additions & 7 deletions src/page/post_view/video_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ pub fn HlsVideo(video_ref: NodeRef<Video>, allow_show: RwSignal<bool>) -> impl I
..
} = expect_context();

let current_uid =
create_memo(move |_| with!(|video_queue| video_queue[current_idx()].uid.clone()));
let current_uid = create_memo(move |_| {
with!(|video_queue| video_queue.get(current_idx()).map(|q| q.uid.clone()))
});
let wasp = create_rw_signal(None::<WaspHlsPlayerW>);
let bg_url = move || bg_url(current_uid());
let bg_url = move || current_uid().map(bg_url);

create_effect(move |_| {
let video = video_ref.get()?;
Expand Down Expand Up @@ -65,8 +66,8 @@ pub fn HlsVideo(video_ref: NodeRef<Video>, allow_show: RwSignal<bool>) -> impl I
with!(|wasp| {
let wasp = wasp.as_ref()?;
let video = video_ref.get()?;
wasp.load(&stream_url(current_uid()));
video.set_poster(&bg_url());
wasp.load(&stream_url(current_uid()?));
video.set_poster(&bg_url()?);
Some(())
})
});
Expand All @@ -84,8 +85,9 @@ pub fn ThumbView(idx: usize) -> impl IntoView {
..
} = expect_context();

let uid = create_memo(move |_| with!(|video_queue| video_queue[idx].uid.clone()));
let view_bg_url = move || bg_url(uid());
let uid =
create_memo(move |_| with!(|video_queue| video_queue.get(idx).map(|q| q.uid.clone())));
let view_bg_url = move || uid().map(bg_url);

use_intersection_observer_with_options(
container_ref,
Expand Down

0 comments on commit cbb3e84

Please sign in to comment.