Skip to main content

logicaffeine_web/ui/components/
socratic_guide.rs

1//! Socratic guide component for hints and feedback.
2//!
3//! Displays contextual hints and error messages using a Socratic teaching approach.
4//! Guides users toward understanding rather than providing direct answers.
5//!
6//! # Props
7//!
8//! - `mode` - Current guide state (Idle, Success, Error, Hint, Info)
9//! - `on_hint_request` - Optional callback for "Show me a hint" button
10//!
11//! # Modes
12//!
13//! - `Idle` - Placeholder when no input
14//! - `Success` - Green message for successful parsing
15//! - `Error` - Red message with Socratic guidance
16//! - `Hint` - Green hint text
17//! - `Info` - Blue informational message
18
19use dioxus::prelude::*;
20
21const GUIDE_STYLE: &str = r#"
22.socratic-guide {
23    display: flex;
24    align-items: center;
25    gap: 12px;
26    padding: 12px 20px;
27    background: transparent;
28    min-height: 44px;
29}
30
31.guide-content {
32    flex: 1;
33    display: flex;
34    align-items: center;
35    gap: 10px;
36}
37
38.guide-label {
39    font-size: 13px;
40    font-weight: 600;
41    text-transform: uppercase;
42    letter-spacing: 0.5px;
43    color: #667eea;
44    white-space: nowrap;
45}
46
47.guide-message {
48    font-size: 16px;
49    line-height: 1.5;
50    color: #e0e0e0;
51}
52
53.guide-message.error {
54    color: #e06c75;
55}
56
57.guide-message.hint {
58    color: #98c379;
59}
60
61.guide-message.info {
62    color: #61afef;
63}
64
65.guide-message code {
66    font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
67    background: rgba(255, 255, 255, 0.08);
68    padding: 2px 6px;
69    border-radius: 4px;
70    font-size: 15px;
71}
72
73.guide-actions {
74    display: flex;
75    gap: 8px;
76    margin-top: 8px;
77}
78
79.guide-btn {
80    padding: 6px 12px;
81    border-radius: 6px;
82    border: 1px solid rgba(255, 255, 255, 0.15);
83    background: rgba(255, 255, 255, 0.05);
84    color: #888;
85    font-size: 12px;
86    cursor: pointer;
87    transition: all 0.2s ease;
88}
89
90.guide-btn:hover {
91    background: rgba(255, 255, 255, 0.1);
92    color: #e8e8e8;
93}
94
95.guide-btn.primary {
96    background: linear-gradient(135deg, #667eea, #764ba2);
97    border-color: transparent;
98    color: white;
99}
100
101.guide-btn.primary:hover {
102    opacity: 0.9;
103}
104
105.guide-empty {
106    color: #666;
107    font-style: italic;
108}
109"#;
110
111#[derive(Clone, PartialEq)]
112pub enum GuideMode {
113    Idle,
114    Success(String),
115    Error(String),
116    Hint(String),
117    Info(String),
118}
119
120impl Default for GuideMode {
121    fn default() -> Self {
122        GuideMode::Idle
123    }
124}
125
126#[component]
127pub fn SocraticGuide(mode: GuideMode, on_hint_request: Option<EventHandler<()>>) -> Element {
128    let (message_class, label, message) = match &mode {
129        GuideMode::Idle => {
130            return rsx! {
131                style { "{GUIDE_STYLE}" }
132                div { class: "socratic-guide",
133                    div { class: "guide-content",
134                        div { class: "guide-message guide-empty",
135                            "Type an English sentence to translate it to First-Order Logic"
136                        }
137                    }
138                }
139            }
140        }
141        GuideMode::Success(msg) => ("guide-message", "Analysis", msg.clone()),
142        GuideMode::Error(msg) => ("guide-message error", "Something to consider", msg.clone()),
143        GuideMode::Hint(msg) => ("guide-message hint", "Hint", msg.clone()),
144        GuideMode::Info(msg) => ("guide-message info", "Observation", msg.clone()),
145    };
146
147    rsx! {
148        style { "{GUIDE_STYLE}" }
149
150        div { class: "socratic-guide",
151            div { class: "guide-content",
152                div { class: "guide-label", "{label}" }
153                div { class: "{message_class}",
154                    dangerous_inner_html: format_guide_message(&message)
155                }
156                if on_hint_request.is_some() && matches!(mode, GuideMode::Error(_)) {
157                    div { class: "guide-actions",
158                        button {
159                            class: "guide-btn",
160                            onclick: move |_| {
161                                if let Some(handler) = &on_hint_request {
162                                    handler.call(());
163                                }
164                            },
165                            "Show me a hint"
166                        }
167                    }
168                }
169            }
170        }
171    }
172}
173
174fn format_guide_message(message: &str) -> String {
175    let mut result = message.to_string();
176
177    let code_patterns = [
178        ("\u{2200}", "<code>\u{2200}</code>"),
179        ("\u{2203}", "<code>\u{2203}</code>"),
180        ("\u{2227}", "<code>\u{2227}</code>"),
181        ("\u{2228}", "<code>\u{2228}</code>"),
182        ("\u{2192}", "<code>\u{2192}</code>"),
183        ("\u{00AC}", "<code>\u{00AC}</code>"),
184    ];
185
186    for (pattern, replacement) in &code_patterns {
187        result = result.replace(pattern, replacement);
188    }
189
190    result
191}
192
193pub fn get_success_message(readings_count: usize) -> String {
194    match readings_count {
195        0 => "This sentence could not be resolved into a logical form.".to_string(),
196        1 => "This sentence has a single, unambiguous logical form.".to_string(),
197        2 => format!(
198            "This sentence is ambiguous: {} different readings found. \
199            Click the reading buttons above to explore each interpretation.",
200            readings_count
201        ),
202        n => format!(
203            "{} distinct logical readings found. \
204            Each represents a valid interpretation of your input.",
205            n
206        ),
207    }
208}
209
210pub fn get_context_hint(input: &str) -> Option<String> {
211    let lower = input.to_lowercase();
212
213    if lower.starts_with("every") || lower.starts_with("all") {
214        Some(
215            "Universal quantification (\u{2200}) asserts something about ALL members of a set."
216                .to_string(),
217        )
218    } else if lower.starts_with("some") || lower.starts_with("a ") {
219        Some(
220            "Existential quantification (\u{2203}) asserts the EXISTENCE of at least one entity."
221                .to_string(),
222        )
223    } else if lower.contains(" loves ") || lower.contains(" sees ") {
224        Some(
225            "Transitive verbs create two-place predicates relating a subject to an object."
226                .to_string(),
227        )
228    } else if lower.contains(" is ") && lower.contains(" not ") {
229        Some(
230            "Negation (\u{00AC}) inverts the truth value of the proposition it scopes over."
231                .to_string(),
232        )
233    } else {
234        None
235    }
236}
237
238/// What the Analysis row says about Logic input: for sentences, how many readings they have;
239/// for a theorem, whether its goal was proved. Never "could not be resolved" beside a
240/// formula the Logic Output is showing.
241pub fn logic_guide(
242    error: Option<&str>,
243    logic: Option<&str>,
244    readings: usize,
245    input: &str,
246    proof: &crate::ui::components::proof_panel::ProofStatus,
247) -> GuideMode {
248    use crate::ui::components::proof_panel::ProofStatus;
249    if let Some(err) = error {
250        return GuideMode::Error(err.to_string());
251    }
252    if logic.is_none() {
253        return GuideMode::Idle;
254    }
255    match proof {
256        ProofStatus::Success => {
257            return GuideMode::Success("Proved: the goal follows from the premises.".to_string())
258        }
259        ProofStatus::Failed(_) => {
260            return GuideMode::Info("The goal does not follow from the premises as they stand.".to_string())
261        }
262        ProofStatus::Uncertified(_) => {
263            return GuideMode::Hint(
264                "A derivation was found but is not certified. The kernel did not accept it as a proof.".to_string(),
265            )
266        }
267        ProofStatus::Proving => return GuideMode::Info("Searching for a proof\u{2026}".to_string()),
268        ProofStatus::Idle => {}
269    }
270    // A formula on screen is at least one reading.
271    let message = get_success_message(readings.max(1));
272    match get_context_hint(input) {
273        Some(hint) => GuideMode::Info(format!("{message} {hint}")),
274        None => GuideMode::Success(message),
275    }
276}
277
278#[cfg(test)]
279#[path = "../../../../../crates/logicaffeine_tests/unit/logicaffeine_web/ui/components/socratic_guide/logic_guide_tests.rs"]
280mod logic_guide_tests;