1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//! Hooks page - displays Claude Code hooks with split view (list + detail)
use leptos::prelude::*;
use serde::{Deserialize, Serialize};
/// API base URL constant (empty = relative URL, same origin)
const API_BASE_URL: &str = "";
/// Hook info structure matching backend API
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HookInfo {
pub name: String,
pub event: String,
pub command: String,
#[serde(default)]
pub description: Option<String>,
#[serde(default)]
pub r#async: bool,
#[serde(default)]
pub timeout: Option<u32>,
#[serde(default)]
pub cwd: Option<String>,
#[serde(default)]
pub matcher: Option<String>,
#[serde(default)]
pub script_path: Option<String>,
#[serde(default)]
pub script_content: Option<String>,
}
/// Hooks list response from API
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HooksResponse {
pub hooks: Vec<HookInfo>,
pub total: usize,
}
/// Fetch hooks from API
async fn fetch_hooks() -> Result<HooksResponse, String> {
let url = format!("{}/api/hooks", API_BASE_URL);
let response = gloo_net::http::Request::get(&url)
.send()
.await
.map_err(|e| format!("Failed to fetch hooks: {}", e))?;
if !response.ok() {
return Err(format!("HTTP error: {}", response.status()));
}
let hooks_response: HooksResponse = response
.json()
.await
.map_err(|e| format!("Failed to parse JSON: {}", e))?;
Ok(hooks_response)
}
/// Hook list item component
#[component]
fn HookListItem(hook: HookInfo, selected: bool, on_click: impl Fn() + 'static) -> impl IntoView {
let class = if selected {
"hook-list-item hook-list-item--selected"
} else {
"hook-list-item"
};
view! {
<div class=class on:click=move |_| on_click()>
<div class="hook-list-item__name">{hook.name.clone()}</div>
<div class="hook-list-item__event">{hook.event.clone()}</div>
</div>
}
}
/// Hook detail component
#[component]
fn HookDetail(hook: HookInfo) -> impl IntoView {
view! {
<div class="hook-detail">
<div class="hook-detail__header">
<h2 class="hook-detail__name">{hook.name.clone()}</h2>
<span class="hook-detail__badge">{hook.event.clone()}</span>
</div>
<div class="hook-detail__section">
<h3 class="hook-detail__section-title">"Command"</h3>
<code class="hook-detail__command">{hook.command.clone()}</code>
</div>
{hook.description.as_ref().map(|desc| view! {
<div class="hook-detail__section">
<h3 class="hook-detail__section-title">"Description"</h3>
<p class="hook-detail__description">{desc.clone()}</p>
</div>
})}
<div class="hook-detail__metadata">
<div class="hook-detail__meta-item">
<span class="hook-detail__meta-label">"Async:"</span>
<span class="hook-detail__meta-value">{if hook.r#async { "Yes" } else { "No" }}</span>
</div>
{hook.timeout.map(|t| view! {
<div class="hook-detail__meta-item">
<span class="hook-detail__meta-label">"Timeout:"</span>
<span class="hook-detail__meta-value">{format!("{}s", t)}</span>
</div>
})}
{hook.cwd.as_ref().map(|cwd| view! {
<div class="hook-detail__meta-item">
<span class="hook-detail__meta-label">"Working Dir:"</span>
<span class="hook-detail__meta-value">{cwd.clone()}</span>
</div>
})}
{hook.matcher.as_ref().map(|matcher| view! {
<div class="hook-detail__meta-item">
<span class="hook-detail__meta-label">"Matcher:"</span>
<span class="hook-detail__meta-value">{matcher.clone()}</span>
</div>
})}
</div>
{hook.script_content.as_ref().map(|script| view! {
<div class="hook-detail__section">
<h3 class="hook-detail__section-title">"Script Content"</h3>
{hook.script_path.as_ref().map(|path| view! {
<code class="hook-detail__script-path">{path.clone()}</code>
})}
<pre class="hook-detail__script">{script.clone()}</pre>
</div>
})}
</div>
}
}
/// Hooks page component
#[component]
pub fn Hooks() -> impl IntoView {
let hooks_resource = LocalResource::new(move || async move { fetch_hooks().await });
let selected_hook_index = RwSignal::new(0usize);
view! {
<div class="page hooks-page">
<div class="page-header">
<h1 class="page-title">"Hooks"</h1>
<Suspense fallback=|| view! { <span>"Loading..."</span> }>
{move || {
hooks_resource
.get()
.map(|result| {
match *result {
Ok(ref response) => {
view! {
<span class="page-subtitle">
{format!("{} hook(s) configured", response.total)}
</span>
}
.into_any()
}
Err(_) => view! { <span></span> }.into_any(),
}
})
}}
</Suspense>
</div>
<Suspense fallback=|| view! { <div class="loading">"Loading hooks..."</div> }>
{move || {
hooks_resource
.get()
.map(|result| {
match result.as_ref() {
Ok(response) => {
if response.hooks.is_empty() {
view! {
<div class="empty-state">
<p>"No hooks configured"</p>
</div>
}
.into_any()
} else {
let hooks = RwSignal::new(response.hooks.clone());
view! {
<div class="hooks-content">
<div class="hooks-list">
{move || {
let selected_idx = selected_hook_index.get();
hooks
.get()
.iter()
.enumerate()
.map(|(idx, hook): (usize, &HookInfo)| {
let hook_clone = hook.clone();
let is_selected = selected_idx == idx;
view! {
<HookListItem
hook=hook_clone
selected=is_selected
on_click=move || selected_hook_index.set(idx)
/>
}
})
.collect::<Vec<_>>()
}}
</div>
<div class="hooks-detail">
{move || {
let idx = selected_hook_index.get();
hooks
.with(|hooks_vec: &Vec<HookInfo>| {
if let Some(hook) = hooks_vec.get(idx) {
let h = hook.clone();
view! { <HookDetail hook=h /> }.into_any()
} else {
view! { <div>"No hook selected"</div> }.into_any()
}
})
}}
</div>
</div>
}
.into_any()
}
}
Err(e) => {
view! {
<div class="error-state">
<p>"Error loading hooks: " {e.to_string()}</p>
</div>
}
.into_any()
}
}
})
}}
</Suspense>
</div>
}
}