Skip to main content

logicaffeine_web/ui/
router.rs

1//! Application routing and navigation.
2//!
3//! Defines all routes for the Logicaffeine web application using the Dioxus
4//! router. Each route maps to a page component in [`crate::ui::pages`].
5//!
6//! # Routes
7//!
8//! | Path | Component | Description |
9//! |------|-----------|-------------|
10//! | `/` | [`Landing`] | Marketing homepage |
11//! | `/learn#:module` | [`Learn`] | The Learn roadmap; `/profile` redirects here |
12//! | `/studio?:file` | [`Studio`] | Playground for experimentation |
13//! | `/contact#:section` | [`Contact`] | The contact page; `/pricing` redirects here |
14//! | `/guide` | [`Guide`] | Documentation and tutorials |
15//! | `/registry?:token&:login&:error&:q` | [`Registry`] | Package browser |
16//! | `/workspace/:subject` | [`Workspace`] | Subject-specific workspace |
17//!
18//! # Navigation
19//!
20//! Use the Dioxus `Link` component with `Route` variants:
21//!
22//! ```no_run
23//! # use dioxus::prelude::*;
24//! use logicaffeine_web::ui::router::Route;
25//!
26//! # fn Example() -> Element {
27//! rsx! {
28//!     Link { to: Route::Learn { module: String::new() }, "Start Learning" }
29//!     Link { to: Route::Studio { file: None }, "Open Studio" }
30//! }
31//! # }
32//! ```
33//!
34//! # Query parameters are part of the route type
35//!
36//! On startup the router parses the browser URL into [`Route`], serializes it
37//! back, and replaces the browser URL with the serialization whenever the two
38//! differ. Any URL state the route type does not model is silently destroyed
39//! before page components can read it. Pages therefore MUST receive query
40//! parameters as route props (declared with `?:name` in the `#[route]`
41//! attribute) — never by scraping the browser location's search string; the
42//! `query_scraping_is_forbidden` lock enforces this.
43
44use crate::ui::pages::registry::{PackageDetail, Registry};
45use crate::ui::pages::{
46    Benchmarks, Contact, Crates, Guide, Landing, Learn, News, NewsArticle, Privacy, Roadmap,
47    Studio, Terms, Workspace,
48};
49use dioxus::prelude::*;
50
51/// Application routes.
52///
53/// This enum is derived with `Routable` to generate route matching logic.
54/// Each variant corresponds to a URL pattern and its associated page component.
55#[derive(Clone, Routable, Debug, PartialEq)]
56pub enum Route {
57    /// Marketing homepage at `/`.
58    #[route("/")]
59    Landing {},
60
61    /// The contact page at `/contact`.
62    ///
63    /// `/contact#<section>` opens at that section (`#contact` is the form); the fragment is
64    /// modelled here so boot normalization keeps it. `/contact`, where people look for the team,
65    /// the old `/pricing` address redirects here.
66    #[redirect("/pricing", || Route::Contact { section: String::new() })]
67    #[route("/contact#:section")]
68    Contact {
69        /// The section to open at (a section's id), or empty for the top of the page.
70        section: String,
71    },
72
73    /// Privacy policy at `/privacy`.
74    ///
75    /// `/privacy#<section>` opens at that section of the policy; the contact form's privacy note
76    /// links `#pricing-requests`.
77    #[route("/privacy#:section")]
78    Privacy {
79        /// The section to open at (an id in the policy), or empty for the top of the page.
80        section: String,
81    },
82
83    /// Terms of service at `/terms`.
84    #[route("/terms")]
85    Terms {},
86
87    /// Product roadmap at `/roadmap`.
88    #[route("/roadmap")]
89    Roadmap {},
90
91    /// Documentation and tutorials at `/guide`.
92    #[route("/guide")]
93    Guide {},
94
95    /// Crate documentation at `/crates`.
96    #[route("/crates")]
97    Crates {},
98
99
100    /// Playground for experimentation at `/studio`.
101    ///
102    /// Deep links carry the VFS path of the file to open as a `file` query
103    /// parameter (without the leading slash), e.g.
104    /// `/studio?file=examples/logic/prover-demo.logic`.
105    #[route("/studio?:file")]
106    Studio {
107        /// VFS path of the file to open, if any.
108        file: Option<String>,
109    },
110
111    /// Main learning interface at `/learn`.
112    ///
113    /// The Learn roadmap: one lesson at a time, with practice behind each.
114    /// `/learn#<module-id>` opens that module's first unfinished lesson; the fragment is modelled
115    /// here so boot normalization keeps it. `/profile`, the page that once kept a learner's score,
116    /// redirects here.
117    #[redirect("/profile", || Route::Learn { module: String::new() })]
118    #[route("/learn#:module")]
119    Learn {
120        /// The module to open at (a curriculum module id), or empty for the top of the page.
121        module: String,
122    },
123
124    /// Subject-specific workspace at `/workspace/:subject`.
125    #[route("/workspace/:subject")]
126    Workspace {
127        /// The workspace subject identifier (e.g., "logic", "proofs").
128        subject: String,
129    },
130
131    /// Package registry browser at `/registry`.
132    ///
133    /// The GitHub OAuth callback redirects here with `token`/`login` on
134    /// success or `error` on failure; `q` pre-fills the package search (the
135    /// SearchAction URL advertised in the JSON-LD).
136    #[route("/registry?:token&:login&:error&:q")]
137    Registry {
138        /// OAuth access token from the callback, if any.
139        token: Option<String>,
140        /// GitHub login name from the callback, if any.
141        login: Option<String>,
142        /// OAuth error message from the callback, if any.
143        error: Option<String>,
144        /// Package search query to pre-fill, if any.
145        q: Option<String>,
146    },
147
148    /// Package detail page at `/registry/package/:name`.
149    #[route("/registry/package/:name")]
150    PackageDetail {
151        /// The package name to display.
152        name: String,
153    },
154
155    /// Performance benchmarks at `/benchmarks`.
156    #[route("/benchmarks")]
157    Benchmarks {},
158
159    /// News index page at `/news`.
160    ///
161    /// An optional `tag` query parameter pre-applies a tag filter.
162    #[route("/news?:tag")]
163    News {
164        /// Tag to filter articles by, if any.
165        tag: Option<String>,
166    },
167
168    /// News article page at `/news/:slug`.
169    #[route("/news/:slug")]
170    NewsArticle {
171        /// The article slug.
172        slug: String,
173    },
174
175    /// Catch-all for unknown routes, renders 404 page.
176    #[route("/:..route")]
177    NotFound {
178        /// Path segments that didn't match any route.
179        route: Vec<String>,
180    },
181}
182
183/// The canonical shareable URL for a studio file.
184///
185/// Built from [`Route::Studio`] itself so link writers can never drift from
186/// what the router parses and re-serializes at boot. Accepts the VFS path
187/// with or without its leading slash.
188pub fn studio_file_url(vfs_path: &str) -> String {
189    Route::Studio {
190        file: Some(vfs_path.trim_start_matches('/').to_string()),
191    }
192    .to_string()
193}
194
195/// Replace the address-bar URL in place — no history entry, no router
196/// navigation. Pages use this to keep the bar on the canonical form of the
197/// page they are showing (e.g. after consuming one-shot query params, or
198/// because route serialization leaves a bare `?` when all params are absent).
199#[cfg(target_arch = "wasm32")]
200pub fn replace_bar_url(url: &str) {
201    if let Some(window) = web_sys::window() {
202        if let Ok(history) = window.history() {
203            let _ = history.replace_state_with_url(&wasm_bindgen::JsValue::NULL, "", Some(url));
204        }
205    }
206}
207
208#[cfg(test)]
209#[path = "../../../../crates/logicaffeine_tests/unit/logicaffeine_web/ui/router/tests.rs"]
210mod tests;
211
212#[component]
213fn NotFound(route: Vec<String>) -> Element {
214    rsx! {
215        div {
216            style: "min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%); color: #e8e8e8;",
217            h1 { style: "font-size: 48px; margin-bottom: 16px;", "404" }
218            p { style: "color: #888; margin-bottom: 24px;", "Page not found: /{route.join(\"/\")}" }
219            Link {
220                to: Route::Landing {},
221                style: "padding: 12px 24px; background: linear-gradient(135deg, #667eea, #764ba2); border-radius: 8px; color: white; text-decoration: none;",
222                "Go Home"
223            }
224        }
225    }
226}