Skip to main content

logicaffeine_web/ui/components/
guide_sidebar.rs

1//! Sidebar navigation component for the Programmer's Guide.
2//!
3//! Features:
4//! - Sticky positioning
5//! - Sections grouped by Part
6//! - Active section highlighting
7//! - Click navigation with anchor links
8
9use dioxus::prelude::*;
10
11const SIDEBAR_STYLE: &str = r#"
12.guide-sidebar {
13    position: sticky;
14    top: 90px;
15    width: 260px;
16    max-height: calc(100vh - 120px);
17    overflow-y: auto;
18    flex-shrink: 0;
19    padding: 4px 0;
20
21    /* Custom scrollbar */
22    scrollbar-width: thin;
23    scrollbar-color: rgba(255,255,255,0.1) transparent;
24}
25
26.guide-sidebar::-webkit-scrollbar {
27    width: 6px;
28}
29
30.guide-sidebar::-webkit-scrollbar-track {
31    background: transparent;
32}
33
34.guide-sidebar::-webkit-scrollbar-thumb {
35    background: rgba(255,255,255,0.1);
36    border-radius: 3px;
37}
38
39.guide-sidebar::-webkit-scrollbar-thumb:hover {
40    background: rgba(255,255,255,0.2);
41}
42
43.sidebar-part {
44    margin-bottom: 24px;
45}
46
47.sidebar-part:last-child {
48    margin-bottom: 0;
49}
50
51.sidebar-part-title {
52    font-size: 11px;
53    font-weight: 700;
54    text-transform: uppercase;
55    letter-spacing: 0.8px;
56    color: rgba(229,231,235,0.45);
57    padding: 0 16px;
58    margin-bottom: 10px;
59}
60
61.sidebar-section {
62    display: block;
63    padding: 9px 16px;
64    margin: 2px 8px;
65    border-radius: 0 4px 4px 0;
66    color: rgba(229,231,235,0.65);
67    font-size: 13px;
68    font-weight: 500;
69    text-decoration: none;
70    transition: all 0.18s ease;
71    cursor: pointer;
72    border-left: 2px solid transparent;
73}
74
75.sidebar-section:hover {
76    background: rgba(255,255,255,0.05);
77    color: rgba(229,231,235,0.9);
78}
79
80.sidebar-section.active {
81    background: rgba(167,139,250,0.12);
82    color: #a78bfa;
83    border-left-color: #a78bfa;
84    font-weight: 600;
85}
86
87.sidebar-section-number {
88    display: inline-block;
89    min-width: 22px;
90    color: rgba(229,231,235,0.35);
91    font-weight: 500;
92}
93
94.sidebar-section.active .sidebar-section-number {
95    color: rgba(167,139,250,0.7);
96}
97
98@media (max-width: 1024px) {
99    .guide-sidebar {
100        display: none;
101    }
102}
103
104/* Mobile sidebar toggle - shown on mobile */
105.sidebar-mobile-toggle {
106    display: none;
107    position: fixed;
108    bottom: 24px;
109    right: 24px;
110    width: 56px;
111    height: 56px;
112    border-radius: 50%;
113    background: linear-gradient(135deg, #60a5fa, #a78bfa);
114    border: none;
115    color: #060814;
116    font-size: 24px;
117    cursor: pointer;
118    box-shadow: 0 8px 32px rgba(0,0,0,0.4);
119    z-index: 100;
120    transition: transform 0.2s ease;
121}
122
123.sidebar-mobile-toggle:hover {
124    transform: scale(1.05);
125}
126
127@media (max-width: 1024px) {
128    .sidebar-mobile-toggle {
129        display: flex;
130        align-items: center;
131        justify-content: center;
132    }
133}
134"#;
135
136/// Information about a section for the sidebar
137#[derive(Clone, PartialEq, Debug)]
138pub struct SectionInfo {
139    pub id: String,
140    pub number: u8,
141    pub title: String,
142    pub part: String,
143    /// Graded exercises in the section (0 = no mastery ring).
144    pub exercise_total: usize,
145    /// How many the reader has passed at least once (SM-2 deck entries).
146    pub exercise_passed: usize,
147}
148
149#[derive(Props, Clone, PartialEq)]
150pub struct GuideSidebarProps {
151    pub sections: Vec<SectionInfo>,
152    pub active_section: String,
153    pub on_section_click: EventHandler<String>,
154    /// Section ids the reader has marked done (persisted per browser).
155    pub completed: Vec<String>,
156    pub on_toggle_complete: EventHandler<String>,
157}
158
159#[component]
160pub fn GuideSidebar(props: GuideSidebarProps) -> Element {
161    // Group sections by part
162    let grouped = group_sections_by_part(&props.sections);
163
164    rsx! {
165        style { "{SIDEBAR_STYLE}" }
166
167        nav { class: "guide-sidebar",
168            for (part, sections) in grouped {
169                div { class: "sidebar-part",
170                    h4 { class: "sidebar-part-title", "{part}" }
171
172                    for section in sections {
173                        {
174                            let section_id = section.id.clone();
175                            let is_active = props.active_section == section.id;
176                            let is_done = props.completed.iter().any(|c| *c == section.id);
177                            let check_id = section.id.clone();
178                            let on_toggle = props.on_toggle_complete.clone();
179                            let class_name = if is_active {
180                                "sidebar-section active"
181                            } else {
182                                "sidebar-section"
183                            };
184
185                            rsx! {
186                                div {
187                                    style: "display: flex; align-items: center; gap: 2px;",
188                                span {
189                                    class: if is_done { "guide-section-check done" } else { "guide-section-check" },
190                                    style: if is_done { "cursor: pointer; font-size: 11px; color: #22c55e; width: 14px; flex: none;" } else { "cursor: pointer; font-size: 11px; opacity: 0.35; width: 14px; flex: none;" },
191                                    title: "Mark section as read",
192                                    onclick: move |_| on_toggle.call(check_id.clone()),
193                                    if is_done { "✓" } else { "○" }
194                                }
195                                // Mastery is doing, not scrolling: the ring counts
196                                // exercises PASSED, separate from the read-mark.
197                                if section.exercise_total > 0 {
198                                    span {
199                                        class: if section.exercise_passed == section.exercise_total { "guide-mastery-ring done" } else { "guide-mastery-ring" },
200                                        style: if section.exercise_passed == section.exercise_total { "flex: none; font-size: 9px; font-weight: 700; color: #22c55e; border: 1px solid rgba(34,197,94,0.5); border-radius: 999px; padding: 1px 5px;" } else { "flex: none; font-size: 9px; font-weight: 700; color: rgba(229,231,235,0.5); border: 1px solid rgba(255,255,255,0.2); border-radius: 999px; padding: 1px 5px;" },
201                                        title: "{section.exercise_passed} of {section.exercise_total} exercises passed",
202                                        "{section.exercise_passed}/{section.exercise_total}"
203                                    }
204                                }
205                                a {
206                                    class: "{class_name}",
207                                    style: "flex: 1;",
208                                    href: "#{section_id}",
209                                    onclick: {
210                                        let id = section.id.clone();
211                                        let handler = props.on_section_click.clone();
212                                        move |evt: Event<MouseData>| {
213                                            evt.prevent_default();
214                                            handler.call(id.clone());
215
216                                            // Smooth scroll to section
217                                            #[cfg(target_arch = "wasm32")]
218                                            {
219                                                if let Some(window) = web_sys::window() {
220                                                    if let Some(document) = window.document() {
221                                                        if let Some(element) = document.get_element_by_id(&id) {
222                                                            let options = web_sys::ScrollIntoViewOptions::new();
223                                                            options.set_behavior(web_sys::ScrollBehavior::Smooth);
224                                                            options.set_block(web_sys::ScrollLogicalPosition::Start);
225                                                            let _ = element.scroll_into_view_with_scroll_into_view_options(&options);
226                                                        }
227                                                    }
228                                                }
229                                            }
230                                        }
231                                    },
232                                    span { class: "sidebar-section-number", "{section.number}." }
233                                    " {section.title}"
234                                }
235                                }
236                            }
237                        }
238                    }
239                }
240            }
241        }
242    }
243}
244
245/// Groups sections by their part name, preserving order
246fn group_sections_by_part(sections: &[SectionInfo]) -> Vec<(String, Vec<SectionInfo>)> {
247    let mut result: Vec<(String, Vec<SectionInfo>)> = Vec::new();
248
249    for section in sections {
250        if let Some((_, group)) = result.iter_mut().find(|(part, _)| part == &section.part) {
251            group.push(section.clone());
252        } else {
253            result.push((section.part.clone(), vec![section.clone()]));
254        }
255    }
256
257    result
258}
259