Skip to main content

logicaffeine_web/ui/pages/registry/
package_detail.rs

1//! Package detail page.
2//!
3//! Displays comprehensive information about a single package including:
4//!
5//! - Package metadata (name, author, description)
6//! - README content (rendered markdown)
7//! - Version history with changelogs
8//! - Dependency graph
9//! - Installation instructions
10//!
11//! # Route
12//!
13//! Accessed via [`Route::PackageDetail`](crate::ui::router::Route::PackageDetail)
14//! with its `name` parameter.
15
16use crate::ui::components::main_nav::{ActivePage, MainNav};
17use crate::ui::seo::PageHead;
18use crate::ui::state::PackageDetails;
19use dioxus::prelude::*;
20#[cfg(all(feature = "split", target_arch = "wasm32"))]
21use dioxus::wasm_split;
22
23const REGISTRY_API_URL: &str = "https://registry.logicaffeine.com";
24
25const DETAIL_STYLE: &str = r#"
26.detail-container {
27    min-height: 100vh;
28    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
29    color: #e8e8e8;
30    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
31}
32
33.detail-header {
34    padding: 24px 48px;
35    border-bottom: 1px solid rgba(255,255,255,0.1);
36}
37
38.back-link {
39    color: #888;
40    text-decoration: none;
41    font-size: 14px;
42    margin-bottom: 16px;
43    display: inline-block;
44}
45
46.back-link:hover {
47    color: #fff;
48}
49
50.package-title {
51    display: flex;
52    align-items: center;
53    gap: 16px;
54    margin-bottom: 8px;
55}
56
57.package-title h1 {
58    font-size: 32px;
59    font-weight: 700;
60    margin: 0;
61}
62
63.verified-badge {
64    display: inline-flex;
65    align-items: center;
66    gap: 4px;
67    background: linear-gradient(135deg, #22c55e, #16a34a);
68    color: white;
69    font-size: 12px;
70    font-weight: 700;
71    padding: 4px 12px;
72    border-radius: 999px;
73}
74
75.package-meta {
76    display: flex;
77    gap: 24px;
78    color: #888;
79    font-size: 14px;
80}
81
82.package-meta a {
83    color: #667eea;
84    text-decoration: none;
85}
86
87.package-meta a:hover {
88    text-decoration: underline;
89}
90
91.detail-content {
92    display: grid;
93    grid-template-columns: 1fr 320px;
94    gap: 48px;
95    padding: 48px;
96}
97
98.main-content {
99    min-width: 0;
100}
101
102.tab-nav {
103    display: flex;
104    gap: 4px;
105    margin-bottom: 24px;
106    border-bottom: 1px solid rgba(255,255,255,0.1);
107    padding-bottom: 0;
108}
109
110.tab-btn {
111    padding: 12px 24px;
112    background: transparent;
113    border: none;
114    color: #888;
115    font-size: 14px;
116    cursor: pointer;
117    border-bottom: 2px solid transparent;
118    margin-bottom: -1px;
119    transition: color 0.2s, border-color 0.2s;
120}
121
122.tab-btn:hover {
123    color: #fff;
124}
125
126.tab-btn.active {
127    color: #667eea;
128    border-bottom-color: #667eea;
129}
130
131.readme-content {
132    background: rgba(255,255,255,0.03);
133    border: 1px solid rgba(255,255,255,0.08);
134    border-radius: 12px;
135    padding: 32px;
136    line-height: 1.7;
137}
138
139.readme-content h1, .readme-content h2, .readme-content h3 {
140    color: #fff;
141    margin-top: 24px;
142    margin-bottom: 12px;
143}
144
145.readme-content code {
146    background: rgba(255,255,255,0.1);
147    padding: 2px 6px;
148    border-radius: 4px;
149    font-size: 0.9em;
150}
151
152.readme-content pre {
153    background: rgba(0,0,0,0.3);
154    padding: 16px;
155    border-radius: 8px;
156    overflow-x: auto;
157}
158
159.versions-list {
160    background: rgba(255,255,255,0.03);
161    border: 1px solid rgba(255,255,255,0.08);
162    border-radius: 12px;
163    overflow: hidden;
164}
165
166.version-item {
167    display: flex;
168    justify-content: space-between;
169    align-items: center;
170    padding: 16px 24px;
171    border-bottom: 1px solid rgba(255,255,255,0.05);
172}
173
174.version-item:last-child {
175    border-bottom: none;
176}
177
178.version-name {
179    font-weight: 600;
180    color: #fff;
181}
182
183.version-meta {
184    display: flex;
185    gap: 16px;
186    font-size: 13px;
187    color: #666;
188}
189
190.yanked-badge {
191    background: rgba(239, 68, 68, 0.2);
192    color: #ef4444;
193    padding: 2px 8px;
194    border-radius: 4px;
195    font-size: 11px;
196}
197
198.sidebar {
199    position: sticky;
200    top: 24px;
201}
202
203.sidebar-section {
204    background: rgba(255,255,255,0.03);
205    border: 1px solid rgba(255,255,255,0.08);
206    border-radius: 12px;
207    padding: 24px;
208    margin-bottom: 24px;
209}
210
211.sidebar-section h3 {
212    font-size: 14px;
213    font-weight: 600;
214    color: #888;
215    text-transform: uppercase;
216    letter-spacing: 0.5px;
217    margin: 0 0 16px;
218}
219
220.install-cmd {
221    display: block;
222    background: rgba(0,0,0,0.3);
223    padding: 12px 16px;
224    border-radius: 8px;
225    font-family: monospace;
226    font-size: 14px;
227    color: #22c55e;
228    word-break: break-all;
229}
230
231.meta-item {
232    display: flex;
233    justify-content: space-between;
234    padding: 8px 0;
235    font-size: 14px;
236    border-bottom: 1px solid rgba(255,255,255,0.05);
237}
238
239.meta-item:last-child {
240    border-bottom: none;
241}
242
243.meta-label {
244    color: #888;
245}
246
247.meta-value {
248    color: #fff;
249}
250
251.meta-value a {
252    color: #667eea;
253    text-decoration: none;
254}
255
256.meta-value a:hover {
257    text-decoration: underline;
258}
259
260.loading-spinner {
261    text-align: center;
262    padding: 48px;
263    color: #888;
264}
265
266.error-message {
267    text-align: center;
268    padding: 48px;
269    color: #f87171;
270}
271
272.keywords-list {
273    display: flex;
274    flex-wrap: wrap;
275    gap: 8px;
276}
277
278.keyword-tag {
279    font-size: 12px;
280    padding: 4px 10px;
281    background: rgba(102, 126, 234, 0.15);
282    color: #667eea;
283    border-radius: 4px;
284}
285"#;
286
287#[derive(Clone, PartialEq)]
288enum DetailTab {
289    Readme,
290    Versions,
291}
292
293#[component(lazy)]
294pub fn PackageDetail(name: String) -> Element {
295    let mut details = use_signal(|| None::<PackageDetails>);
296    let mut error = use_signal(|| None::<String>);
297    let mut is_loading = use_signal(|| true);
298    let mut active_tab = use_signal(|| DetailTab::Readme);
299
300    // Fetch package details
301    use_effect({
302        let name = name.clone();
303        move || {
304            let name = name.clone();
305            spawn(async move {
306                is_loading.set(true);
307                match fetch_package_details(&name).await {
308                    Ok(d) => details.set(Some(d)),
309                    Err(e) => error.set(Some(e)),
310                }
311                is_loading.set(false);
312            });
313        }
314    });
315
316    let page_title = format!("{} - LOGICAFFEINE Registry", name);
317    let page_path = format!("/registry/package/{}", name);
318
319    rsx! {
320        PageHead {
321            title: page_title,
322            description: "Package details and documentation on the LOGICAFFEINE package registry.",
323            canonical_path: page_path,
324        }
325        style { "{DETAIL_STYLE}" }
326
327        MainNav { active: ActivePage::Registry, subtitle: Some("Package Details") }
328
329        div { class: "detail-container",
330            if *is_loading.read() {
331                div { class: "loading-spinner", "Loading package..." }
332            } else if let Some(err) = error.read().as_ref() {
333                div { class: "error-message",
334                    p { "Error: {err}" }
335                }
336            } else if let Some(pkg) = details.read().as_ref() {
337                // Header
338                header { class: "detail-header",
339                    div { class: "package-title",
340                        h1 { "{pkg.name}" }
341                        if pkg.verified {
342                            span { class: "verified-badge", "Official" }
343                        }
344                    }
345                    div { class: "package-meta",
346                        span { "by {pkg.owner}" }
347                        if let Some(repo) = &pkg.repository {
348                            a { href: "{repo}", target: "_blank", "Repository" }
349                        }
350                        if let Some(license) = &pkg.license {
351                            span { "{license}" }
352                        }
353                    }
354                }
355
356                // Content
357                div { class: "detail-content",
358                    main { class: "main-content",
359                        nav { class: "tab-nav",
360                            button {
361                                class: if *active_tab.read() == DetailTab::Readme { "tab-btn active" } else { "tab-btn" },
362                                onclick: move |_| active_tab.set(DetailTab::Readme),
363                                "README"
364                            }
365                            button {
366                                class: if *active_tab.read() == DetailTab::Versions { "tab-btn active" } else { "tab-btn" },
367                                onclick: move |_| active_tab.set(DetailTab::Versions),
368                                "Versions ({pkg.versions.len()})"
369                            }
370                        }
371
372                        match *active_tab.read() {
373                            DetailTab::Readme => rsx! {
374                                div { class: "readme-content",
375                                    if let Some(readme) = &pkg.readme {
376                                        // Note: In production, render markdown properly
377                                        pre { "{readme}" }
378                                    } else {
379                                        p { "No README available." }
380                                    }
381                                }
382                            },
383                            DetailTab::Versions => rsx! {
384                                div { class: "versions-list",
385                                    for version in pkg.versions.iter() {
386                                        div { class: "version-item",
387                                            span { class: "version-name",
388                                                "v{version.version}"
389                                                if version.yanked {
390                                                    span { class: "yanked-badge", "yanked" }
391                                                }
392                                            }
393                                            div { class: "version-meta",
394                                                span { "{format_size(version.size)}" }
395                                                span { "{version.published_at}" }
396                                            }
397                                        }
398                                    }
399                                }
400                            },
401                        }
402                    }
403
404                    aside { class: "sidebar",
405                        div { class: "sidebar-section",
406                            h3 { "Install" }
407                            code { class: "install-cmd", "largo add {pkg.name}" }
408                        }
409
410                        div { class: "sidebar-section",
411                            h3 { "Details" }
412                            div { class: "meta-item",
413                                span { class: "meta-label", "Downloads" }
414                                span { class: "meta-value", "{pkg.downloads}" }
415                            }
416                            if !pkg.versions.is_empty() {
417                                div { class: "meta-item",
418                                    span { class: "meta-label", "Latest" }
419                                    span { class: "meta-value", "v{pkg.versions[0].version}" }
420                                }
421                            }
422                            if let Some(homepage) = &pkg.homepage {
423                                div { class: "meta-item",
424                                    span { class: "meta-label", "Homepage" }
425                                    span { class: "meta-value",
426                                        a { href: "{homepage}", target: "_blank", "Link" }
427                                    }
428                                }
429                            }
430                        }
431
432                        if !pkg.keywords.is_empty() {
433                            div { class: "sidebar-section",
434                                h3 { "Keywords" }
435                                div { class: "keywords-list",
436                                    for keyword in pkg.keywords.iter() {
437                                        span { class: "keyword-tag", "{keyword}" }
438                                    }
439                                }
440                            }
441                        }
442                    }
443                }
444            }
445        }
446    }
447}
448
449fn format_size(bytes: u64) -> String {
450    if bytes < 1024 {
451        format!("{} B", bytes)
452    } else if bytes < 1024 * 1024 {
453        format!("{:.1} KB", bytes as f64 / 1024.0)
454    } else {
455        format!("{:.1} MB", bytes as f64 / (1024.0 * 1024.0))
456    }
457}
458
459async fn fetch_package_details(name: &str) -> Result<PackageDetails, String> {
460    #[cfg(target_arch = "wasm32")]
461    {
462        use gloo_net::http::Request;
463
464        let url = format!("{}/packages/{}", REGISTRY_API_URL, name);
465
466        let response = Request::get(&url).send().await.map_err(|e| e.to_string())?;
467
468        if !response.ok() {
469            return Err("Package not found".to_string());
470        }
471
472        response.json().await.map_err(|e| e.to_string())
473    }
474
475    #[cfg(not(target_arch = "wasm32"))]
476    {
477        Err("Not available in non-WASM builds".to_string())
478    }
479}