Skip to main content

logicaffeine_web/ui/pages/registry/
browse.rs

1//! Package registry browse page.
2//!
3//! Main page for discovering and searching LOGOS packages. Features:
4//!
5//! - Package search with fuzzy matching
6//! - Category filtering
7//! - GitHub OAuth authentication for publishing
8//! - Package cards with download counts and versions
9//!
10//! # Authentication
11//!
12//! Uses GitHub OAuth for publisher authentication. Users can publish packages
13//! under their GitHub username namespace.
14//!
15//! # Route
16//!
17//! Accessed via [`Route::Registry`].
18
19use crate::ui::components::footer::Footer;
20use crate::ui::components::main_nav::{ActivePage, MainNav};
21use crate::ui::router::Route;
22use crate::ui::seo::{
23    breadcrumb_schema, organization_schema, pages as seo_pages, webpage_schema, BreadcrumbItem,
24    JsonLdMultiple, PageHead,
25};
26use crate::ui::state::{GitHubUser, RegistryAuthState, RegistryPackage};
27use dioxus::prelude::*;
28#[cfg(all(feature = "split", target_arch = "wasm32"))]
29use dioxus::wasm_split;
30
31const REGISTRY_API_URL: &str = "https://registry.logicaffeine.com";
32
33const REGISTRY_STYLE: &str = r#"
34.registry-container {
35    min-height: 100vh;
36    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
37    color: #e8e8e8;
38    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
39}
40
41.registry-header {
42    display: flex;
43    justify-content: space-between;
44    align-items: center;
45    padding: 24px 48px;
46    border-bottom: 1px solid rgba(255,255,255,0.1);
47}
48
49.header-left {
50    display: flex;
51    align-items: center;
52    gap: 24px;
53}
54
55.header-left h1 {
56    font-size: 24px;
57    font-weight: 700;
58    background: linear-gradient(135deg, #667eea, #764ba2);
59    -webkit-background-clip: text;
60    -webkit-text-fill-color: transparent;
61    margin: 0;
62}
63
64.back-link {
65    color: #888;
66    text-decoration: none;
67    font-size: 14px;
68}
69
70.back-link:hover {
71    color: #fff;
72}
73
74.login-btn {
75    display: inline-flex;
76    align-items: center;
77    gap: 8px;
78    padding: 10px 20px;
79    background: #24292e;
80    color: white;
81    text-decoration: none;
82    border-radius: 8px;
83    font-size: 14px;
84    font-weight: 500;
85    transition: background 0.2s;
86}
87
88.login-btn:hover {
89    background: #2f363d;
90}
91
92.user-menu {
93    display: flex;
94    align-items: center;
95    gap: 12px;
96}
97
98.user-avatar {
99    width: 36px;
100    height: 36px;
101    border-radius: 50%;
102    border: 2px solid rgba(255,255,255,0.2);
103}
104
105.user-name {
106    font-size: 14px;
107    color: #e8e8e8;
108}
109
110.logout-btn {
111    padding: 6px 12px;
112    background: transparent;
113    border: 1px solid rgba(255,255,255,0.2);
114    color: #888;
115    border-radius: 6px;
116    cursor: pointer;
117    font-size: 13px;
118}
119
120.logout-btn:hover {
121    border-color: rgba(255,255,255,0.4);
122    color: #fff;
123}
124
125.search-section {
126    padding: 48px;
127    text-align: center;
128}
129
130.search-title {
131    font-size: 32px;
132    font-weight: 700;
133    margin-bottom: 8px;
134}
135
136.search-subtitle {
137    color: #888;
138    margin-bottom: 32px;
139}
140
141.search-bar {
142    width: 100%;
143    max-width: 600px;
144    padding: 16px 24px;
145    font-size: 16px;
146    background: rgba(255,255,255,0.05);
147    border: 1px solid rgba(255,255,255,0.1);
148    border-radius: 12px;
149    color: #fff;
150    outline: none;
151    transition: border-color 0.2s, box-shadow 0.2s;
152}
153
154.search-bar:focus {
155    border-color: #667eea;
156    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
157}
158
159.search-bar::placeholder {
160    color: #666;
161}
162
163.package-grid {
164    display: grid;
165    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
166    gap: 24px;
167    padding: 0 48px 48px;
168}
169
170.package-card {
171    background: rgba(255,255,255,0.03);
172    border: 1px solid rgba(255,255,255,0.08);
173    border-radius: 12px;
174    padding: 24px;
175    text-decoration: none;
176    color: inherit;
177    transition: transform 0.2s, border-color 0.2s, background 0.2s;
178}
179
180.package-card:hover {
181    transform: translateY(-2px);
182    border-color: rgba(102, 126, 234, 0.4);
183    background: rgba(255,255,255,0.05);
184}
185
186.package-header {
187    display: flex;
188    justify-content: space-between;
189    align-items: flex-start;
190    margin-bottom: 12px;
191}
192
193.package-name {
194    font-size: 18px;
195    font-weight: 600;
196    color: #fff;
197    margin: 0;
198    display: flex;
199    align-items: center;
200    gap: 8px;
201}
202
203.verified-badge {
204    display: inline-flex;
205    align-items: center;
206    gap: 4px;
207    background: linear-gradient(135deg, #22c55e, #16a34a);
208    color: white;
209    font-size: 11px;
210    font-weight: 700;
211    padding: 3px 8px;
212    border-radius: 999px;
213}
214
215.package-version {
216    font-size: 13px;
217    color: #888;
218    background: rgba(255,255,255,0.05);
219    padding: 4px 8px;
220    border-radius: 4px;
221}
222
223.package-description {
224    color: #aaa;
225    font-size: 14px;
226    line-height: 1.5;
227    margin-bottom: 16px;
228    display: -webkit-box;
229    -webkit-line-clamp: 2;
230    -webkit-box-orient: vertical;
231    overflow: hidden;
232}
233
234.package-meta {
235    display: flex;
236    gap: 16px;
237    font-size: 13px;
238    color: #666;
239}
240
241.package-meta span {
242    display: flex;
243    align-items: center;
244    gap: 4px;
245}
246
247.package-keywords {
248    display: flex;
249    gap: 6px;
250    flex-wrap: wrap;
251    margin-top: 12px;
252}
253
254.keyword-tag {
255    font-size: 11px;
256    padding: 3px 8px;
257    background: rgba(102, 126, 234, 0.15);
258    color: #667eea;
259    border-radius: 4px;
260}
261
262.loading-spinner {
263    text-align: center;
264    padding: 48px;
265    color: #888;
266}
267
268.error-message {
269    text-align: center;
270    padding: 48px;
271    color: #f87171;
272}
273
274.empty-state {
275    text-align: center;
276    padding: 48px;
277    color: #888;
278}
279
280.stats-section {
281    display: flex;
282    justify-content: center;
283    gap: 48px;
284    padding: 24px 48px;
285    border-bottom: 1px solid rgba(255,255,255,0.1);
286}
287
288.stat-item {
289    text-align: center;
290}
291
292.stat-value {
293    font-size: 24px;
294    font-weight: 700;
295    color: #667eea;
296}
297
298.stat-label {
299    font-size: 13px;
300    color: #666;
301}
302"#;
303
304#[component(lazy)]
305pub fn Registry(
306    token: Option<String>,
307    login: Option<String>,
308    error: Option<String>,
309    q: Option<String>,
310) -> Element {
311    let mut auth_state = use_context::<RegistryAuthState>();
312    let auth_state_for_check = auth_state.clone();
313    let mut packages = use_signal(Vec::<RegistryPackage>::new);
314    #[cfg(target_arch = "wasm32")]
315    let q_for_bar = q.clone();
316    let mut search_query = use_signal(move || q.clone().unwrap_or_default());
317    let mut is_loading = use_signal(|| true);
318    #[cfg(target_arch = "wasm32")]
319    let oauth_error = error;
320    let mut error = use_signal(|| None::<String>);
321
322    // Consume OAuth callback params from the route
323    use_effect(move || {
324        #[cfg(target_arch = "wasm32")]
325        {
326            if let (Some(token), Some(login)) = (token.clone(), login.clone()) {
327                // Login successful
328                let user = GitHubUser {
329                    id: String::new(),
330                    login,
331                    name: None,
332                    avatar_url: None,
333                };
334                auth_state.login(token, user);
335            }
336
337            if let Some(err) = oauth_error.clone() {
338                error.set(Some(err));
339            }
340
341            // One-shot OAuth params consumed (or none present): keep the bar
342            // canonical, preserving a shareable ?q= search deep link.
343            let canonical = if q_for_bar.is_some() {
344                crate::ui::router::Route::Registry {
345                    token: None,
346                    login: None,
347                    error: None,
348                    q: q_for_bar.clone(),
349                }
350                .to_string()
351            } else {
352                "/registry".to_string()
353            };
354            crate::ui::router::replace_bar_url(&canonical);
355        }
356    });
357
358    // Fetch packages
359    use_effect(move || {
360        spawn(async move {
361            is_loading.set(true);
362            match fetch_packages(None).await {
363                Ok(pkgs) => packages.set(pkgs),
364                Err(e) => error.set(Some(e)),
365            }
366            is_loading.set(false);
367        });
368    });
369
370    let filtered_packages: Vec<RegistryPackage> = {
371        let query = search_query.read().to_lowercase();
372        if query.is_empty() {
373            packages.read().clone()
374        } else {
375            packages
376                .read()
377                .iter()
378                .filter(|p| {
379                    p.name.to_lowercase().contains(&query)
380                        || p.description
381                            .as_ref()
382                            .map(|d| d.to_lowercase().contains(&query))
383                            .unwrap_or(false)
384                        || p.keywords.iter().any(|k| k.to_lowercase().contains(&query))
385                })
386                .cloned()
387                .collect()
388        }
389    };
390
391    let breadcrumbs = vec![
392        BreadcrumbItem {
393            name: "Home",
394            path: "/",
395        },
396        BreadcrumbItem {
397            name: "Registry",
398            path: "/registry",
399        },
400    ];
401    let schemas = vec![
402        organization_schema(),
403        webpage_schema("Package Registry", "Browse and discover community-contributed logic modules and packages for LOGICAFFEINE.", "/registry"),
404        breadcrumb_schema(&breadcrumbs),
405    ];
406
407    rsx! {
408        PageHead {
409            title: seo_pages::REGISTRY.title,
410            description: seo_pages::REGISTRY.description,
411            canonical_path: seo_pages::REGISTRY.canonical_path,
412        }
413        style { "{REGISTRY_STYLE}" }
414        JsonLdMultiple { schemas }
415
416        div { class: "registry-container",
417            MainNav {
418                active: ActivePage::Registry,
419                subtitle: Some("Package Registry"),
420            }
421
422            // Registry auth section (kept separate from main nav)
423            header { class: "registry-header",
424                div { class: "header-left",
425                    h1 { "Package Registry" }
426                }
427                div { class: "header-right",
428                    if auth_state_for_check.is_authenticated() {
429                        UserMenu { auth_state: auth_state_for_check.clone() }
430                    } else {
431                        a {
432                            class: "login-btn",
433                            href: "{RegistryAuthState::get_auth_url()}",
434                            // GitHub icon
435                            svg {
436                                width: "20",
437                                height: "20",
438                                view_box: "0 0 24 24",
439                                fill: "currentColor",
440                                path {
441                                    d: "M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"
442                                }
443                            }
444                            "Login with GitHub"
445                        }
446                    }
447                }
448            }
449
450            div { class: "search-section",
451                h2 { class: "search-title", "Find LOGOS Packages" }
452                p { class: "search-subtitle", "Discover libraries to enhance your logic programs" }
453                input {
454                    class: "search-bar",
455                    r#type: "search",
456                    placeholder: "Search packages by name, description, or keyword...",
457                    value: "{search_query}",
458                    oninput: move |e| search_query.set(e.value()),
459                }
460            }
461
462            if *is_loading.read() {
463                div { class: "loading-spinner", "Loading packages..." }
464            } else if let Some(err) = error.read().as_ref() {
465                div { class: "error-message", "Error: {err}" }
466            } else if filtered_packages.is_empty() {
467                div { class: "empty-state",
468                    if search_query.read().is_empty() {
469                        "No packages published yet. Be the first!"
470                    } else {
471                        "No packages match your search."
472                    }
473                }
474            } else {
475                div { class: "package-grid",
476                    for package in filtered_packages {
477                        PackageCard { package: package }
478                    }
479                }
480            }
481
482            Footer {}
483        }
484    }
485}
486
487#[component]
488fn PackageCard(package: RegistryPackage) -> Element {
489    rsx! {
490        Link {
491            to: Route::PackageDetail { name: package.name.clone() },
492            class: "package-card",
493            div { class: "package-header",
494                h3 { class: "package-name",
495                    "{package.name}"
496                    if package.verified {
497                        span { class: "verified-badge", "Official" }
498                    }
499                }
500                if let Some(version) = &package.latest_version {
501                    span { class: "package-version", "v{version}" }
502                }
503            }
504            p { class: "package-description",
505                "{package.description.as_deref().unwrap_or(\"No description\")}"
506            }
507            div { class: "package-meta",
508                span { "{package.downloads} downloads" }
509                span { "by {package.owner}" }
510            }
511            if !package.keywords.is_empty() {
512                div { class: "package-keywords",
513                    for keyword in package.keywords.iter().take(3) {
514                        span { class: "keyword-tag", "{keyword}" }
515                    }
516                }
517            }
518        }
519    }
520}
521
522#[component]
523fn UserMenu(auth_state: RegistryAuthState) -> Element {
524    let user = auth_state.user.read().clone();
525    let auth_for_logout = auth_state.clone();
526
527    rsx! {
528        div { class: "user-menu",
529            if let Some(u) = user.as_ref() {
530                if let Some(avatar) = &u.avatar_url {
531                    img {
532                        class: "user-avatar",
533                        src: "{avatar}",
534                        alt: "{u.login}"
535                    }
536                }
537                span { class: "user-name", "{u.login}" }
538            }
539            button {
540                class: "logout-btn",
541                onclick: move |_| {
542                    let mut auth = auth_for_logout.clone();
543                    auth.logout();
544                },
545                "Logout"
546            }
547        }
548    }
549}
550
551async fn fetch_packages(search: Option<&str>) -> Result<Vec<RegistryPackage>, String> {
552    #[cfg(target_arch = "wasm32")]
553    {
554        use gloo_net::http::Request;
555
556        let url = match search {
557            Some(q) if !q.is_empty() => format!("{}/packages?search={}", REGISTRY_API_URL, q),
558            _ => format!("{}/packages", REGISTRY_API_URL),
559        };
560
561        let response = Request::get(&url).send().await.map_err(|e| e.to_string())?;
562
563        if !response.ok() {
564            return Err("Failed to fetch packages".to_string());
565        }
566
567        #[derive(serde::Deserialize)]
568        struct PackagesResponse {
569            packages: Vec<RegistryPackage>,
570        }
571
572        let data: PackagesResponse = response.json().await.map_err(|e| e.to_string())?;
573        Ok(data.packages)
574    }
575
576    #[cfg(not(target_arch = "wasm32"))]
577    {
578        Ok(vec![])
579    }
580}