Skip to content

Commit cec35e6

Browse files
committed
[Feat] add click handler for post item
1 parent 53d1dbb commit cec35e6

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

app/src/commands/ls.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::vfs_data::{find_node, format_path, resolve_path, VfsKind, VfsNode};
44
use micro_cli::Parser;
55
use shell_parser::integration::{CommandInfo, ExecutableCommand};
66
use std::cmp::Ordering;
7+
use wasm_bindgen_futures::spawn_local;
78
use yew::prelude::*;
89

910
#[derive(Parser, Debug, Default)]
@@ -112,7 +113,20 @@ impl LsCommand {
112113
(None, None) => a.metadata.name.cmp(&b.metadata.name),
113114
});
114115

115-
ctx.terminal.push_component(render_posts(&posts));
116+
let on_post_click = {
117+
let terminal = ctx.terminal.clone();
118+
Callback::from(move |metadata: VfsNode| {
119+
let terminal = terminal.clone();
120+
let path = format!("/posts/{}", metadata.path.clone());
121+
122+
spawn_local(async move {
123+
terminal.execute_command(&format!("navigate {path}")).await;
124+
});
125+
})
126+
};
127+
128+
ctx.terminal
129+
.push_component(render_posts(&posts, on_post_click));
116130
Ok(())
117131
}
118132
}
@@ -147,12 +161,12 @@ impl PostEntry {
147161
}
148162
}
149163

150-
fn render_posts(posts: &[PostEntry]) -> Html {
164+
fn render_posts(posts: &[PostEntry], on_click: Callback<VfsNode>) -> Html {
151165
html! {
152166
<div class="py-6 space-y-3">
153167
{ for posts.iter().map(|post| {
154168
html! {
155-
<PostItem metadata={post.metadata.clone()} />
169+
<PostItem metadata={post.metadata.clone()} on_click={on_click.clone()} />
156170
}
157171
}) }
158172
</div>

app/src/components/post_item.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use yew::prelude::*;
66
pub struct PostItemProps {
77
/// Metadata for the post. For post directories, pass the `index.md` metadata.
88
pub metadata: VfsNode,
9+
pub on_click: Callback<VfsNode>,
910
}
1011

1112
#[function_component(PostItem)]
@@ -38,8 +39,19 @@ pub fn post_item(props: &PostItemProps) -> Html {
3839
_ => String::new(),
3940
};
4041

42+
let on_click = {
43+
let on_click = props.on_click.clone();
44+
let metadata = props.metadata.clone();
45+
Callback::from(move |_| {
46+
on_click.emit(metadata.clone());
47+
})
48+
};
49+
4150
html! {
42-
<div class="flex flex-col gap-1 text-post hover:text-post-hover hover:cursor-pointer transition-colors duration-150 text-base">
51+
<div
52+
class="flex flex-col gap-1 text-post hover:text-post-hover hover:cursor-pointer transition-colors duration-150 text-base"
53+
onclick={on_click}
54+
>
4355
<div class="flex items-center gap-3">
4456
<span>{ title }</span>
4557
if !meta_text.is_empty() {

0 commit comments

Comments
 (0)