claude-hindsight 1.1.0

20/20 hindsight for your Claude Code sessions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Navigation router for TUI views
//!
//! Manages navigation between projects, sessions, and session detail views.

use crate::config::Config;
use crate::error::Result;
use crate::parser::parse_session;
use crate::storage::SessionIndex;
use crate::tui::app::App;
use crate::tui::dashboard_view::{DashboardAction, DashboardView};
use crate::tui::projects_view::{ProjectAction, ProjectsView};
use crate::tui::prompts_view::{PromptAction, PromptsView};
use crate::tui::search_modal::{SearchAction, SearchContext, SearchModal};
use crate::tui::sessions_view::{SessionAction, SessionsView};
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::Frame;

/// Current view mode
#[derive(Debug, Clone)]
pub enum ViewMode {
    Projects,
    Dashboard,
    Prompts,
    Sessions(String), // Project name
    #[allow(dead_code)]
    SessionDetail(String), // Session ID
}

/// Main router application
pub struct Router {
    /// Current view mode
    pub view_mode: ViewMode,

    /// View stack for back navigation
    pub view_stack: Vec<ViewMode>,

    /// Projects view
    pub projects_view: Option<ProjectsView>,

    /// Dashboard view
    pub dashboard_view: Option<DashboardView>,

    /// Prompts view
    pub prompts_view: Option<PromptsView>,

    /// Sessions view
    pub sessions_view: Option<SessionsView>,

    /// Session detail view
    pub session_detail_view: Option<App>,

    /// Search modal (overlay)
    pub search_modal: SearchModal,

    /// Whether to quit
    pub should_quit: bool,

    /// Application configuration
    pub config: Config,
}

impl Router {
    /// Create a new router starting at projects view
    pub fn new() -> Result<Self> {
        let config = Config::load()?;

        // Validate config
        if let Err(e) = config.validate() {
            eprintln!("Warning: Invalid configuration: {}", e);
            eprintln!("Using default configuration.");
        }

        let projects_view = Some(ProjectsView::new(&config)?);
        let search_modal = SearchModal::new(SearchContext::Global);

        Ok(Router {
            view_mode: ViewMode::Projects,
            view_stack: vec![],
            projects_view,
            dashboard_view: None,
            prompts_view: None,
            sessions_view: None,
            session_detail_view: None,
            search_modal,
            should_quit: false,
            config,
        })
    }

    /// Create a router that goes directly to a session
    #[allow(dead_code)]
    pub fn new_with_session(session_id: String) -> Result<Self> {
        let index = SessionIndex::new()?;
        let session_file = index
            .find_by_id(&session_id)?
            .ok_or_else(|| crate::error::HindsightError::SessionNotFound(session_id.clone()))?;

        let session = parse_session(&session_file.path)?;
        let session_detail_view = Some(App::new(session));
        let config = Config::load()?;
        let search_modal = SearchModal::new(SearchContext::Session(session_id.clone()));

        Ok(Router {
            view_mode: ViewMode::SessionDetail(session_id),
            view_stack: vec![],
            projects_view: None,
            dashboard_view: None,
            prompts_view: None,
            sessions_view: None,
            session_detail_view,
            search_modal,
            should_quit: false,
            config,
        })
    }

    /// Handle keyboard input
    /// Process periodic updates (debounced search, etc.)
    pub fn tick(&mut self) {
        // Call tick on session detail view to handle debounced search
        if let ViewMode::SessionDetail(_) = &self.view_mode {
            if let Some(ref mut view) = self.session_detail_view {
                view.tick();
            }
        }
    }

    pub fn handle_key(&mut self, key: KeyEvent) -> Result<()> {
        // If search modal is active, let it handle the key first
        if self.search_modal.is_active {
            return self.handle_search_key(key);
        }

        // Check for global '/' key to open search (like vim/less)
        if matches!(key.code, KeyCode::Char('/')) {
            self.activate_search();
            return Ok(());
        }

        match &self.view_mode {
            ViewMode::Projects => {
                // D key opens dashboard from projects view
                if key.code == KeyCode::Char('d') || key.code == KeyCode::Char('D') {
                    self.navigate_to_dashboard()?;
                    return Ok(());
                }
                // P key opens prompts view from projects view
                if key.code == KeyCode::Char('p') || key.code == KeyCode::Char('P') {
                    self.navigate_to_prompts()?;
                    return Ok(());
                }
                if let Some(ref mut view) = self.projects_view {
                    match view.handle_key(key)? {
                        ProjectAction::None => {}
                        ProjectAction::SelectProject(project_name) => {
                            self.navigate_to_sessions(project_name)?;
                        }
                        ProjectAction::Quit => {
                            self.should_quit = true;
                        }
                    }
                }
            }

            ViewMode::Dashboard => {
                if let Some(ref mut view) = self.dashboard_view {
                    match view.handle_key(key)? {
                        DashboardAction::None => {}
                        DashboardAction::Back => {
                            self.navigate_back()?;
                        }
                        DashboardAction::Quit => {
                            self.should_quit = true;
                        }
                    }
                }
            }

            ViewMode::Prompts => {
                if let Some(ref mut view) = self.prompts_view {
                    match view.handle_key(key)? {
                        PromptAction::None => {}
                        PromptAction::SelectSession(session_id) => {
                            self.navigate_to_session_detail(session_id)?;
                        }
                        PromptAction::Back => {
                            self.navigate_back()?;
                        }
                        PromptAction::Quit => {
                            self.should_quit = true;
                        }
                    }
                }
            }

            ViewMode::Sessions(_) => {
                if let Some(ref mut view) = self.sessions_view {
                    match view.handle_key(key)? {
                        SessionAction::None => {}
                        SessionAction::SelectSession(session_id) => {
                            self.navigate_to_session_detail(session_id)?;
                        }
                        SessionAction::Back => {
                            self.navigate_back()?;
                        }
                        SessionAction::Quit => {
                            self.should_quit = true;
                        }
                    }
                }
            }

            ViewMode::SessionDetail(_) => {
                let (should_go_back, should_quit) =
                    if let Some(ref mut view) = self.session_detail_view {
                        view.handle_key(key)?;
                        (
                            view.should_quit && !self.view_stack.is_empty(),
                            view.should_quit && self.view_stack.is_empty(),
                        )
                    } else {
                        (false, false)
                    };

                if should_go_back {
                    self.navigate_back()?;
                    // Reset quit flag
                    if let Some(ref mut view) = self.session_detail_view {
                        view.should_quit = false;
                    }
                } else if should_quit {
                    self.should_quit = true;
                }
            }
        }

        Ok(())
    }

    /// Navigate to dashboard
    fn navigate_to_dashboard(&mut self) -> Result<()> {
        self.view_stack.push(self.view_mode.clone());
        self.dashboard_view = Some(DashboardView::new()?);
        self.view_mode = ViewMode::Dashboard;
        Ok(())
    }

    /// Navigate to prompts view
    fn navigate_to_prompts(&mut self) -> Result<()> {
        self.view_stack.push(self.view_mode.clone());
        self.prompts_view = Some(PromptsView::new()?);
        self.view_mode = ViewMode::Prompts;
        Ok(())
    }

    /// Navigate to sessions view for a project
    fn navigate_to_sessions(&mut self, project_name: String) -> Result<()> {
        // Push current view to stack
        self.view_stack.push(self.view_mode.clone());

        // Create sessions view
        self.sessions_view = Some(SessionsView::new(project_name.clone(), &self.config)?);

        // Update view mode
        self.view_mode = ViewMode::Sessions(project_name);

        Ok(())
    }

    /// Navigate to session detail view
    fn navigate_to_session_detail(&mut self, session_id: String) -> Result<()> {
        // Push current view to stack
        self.view_stack.push(self.view_mode.clone());

        // Load session
        let index = SessionIndex::new()?;
        let session_file = index
            .find_by_id(&session_id)?
            .ok_or_else(|| crate::error::HindsightError::SessionNotFound(session_id.clone()))?;

        let session = parse_session(&session_file.path)?;

        // Create session detail view
        self.session_detail_view = Some(App::new(session));

        // Update view mode
        self.view_mode = ViewMode::SessionDetail(session_id);

        Ok(())
    }

    /// Navigate back to previous view
    fn navigate_back(&mut self) -> Result<()> {
        if let Some(prev_view) = self.view_stack.pop() {
            self.view_mode = prev_view;

            // Re-create the view if needed
            match &self.view_mode {
                ViewMode::Projects => {
                    if self.projects_view.is_none() {
                        self.projects_view = Some(ProjectsView::new(&self.config)?);
                    } else if let Some(ref mut view) = self.projects_view {
                        view.refresh()?;
                    }
                }
                ViewMode::Dashboard => {
                    if self.dashboard_view.is_none() {
                        self.dashboard_view = Some(DashboardView::new()?);
                    }
                }
                ViewMode::Prompts => {
                    if self.prompts_view.is_none() {
                        self.prompts_view = Some(PromptsView::new()?);
                    }
                }
                ViewMode::Sessions(project_name) => {
                    if self.sessions_view.is_none() {
                        self.sessions_view =
                            Some(SessionsView::new(project_name.clone(), &self.config)?);
                    } else if let Some(ref mut view) = self.sessions_view {
                        view.refresh()?;
                    }
                }
                ViewMode::SessionDetail(_) => {
                    // Session detail view should already exist
                }
            }
        }

        Ok(())
    }

    /// Activate search modal with appropriate context
    fn activate_search(&mut self) {
        let context = match &self.view_mode {
            ViewMode::Projects | ViewMode::Dashboard | ViewMode::Prompts => SearchContext::Global,
            ViewMode::Sessions(project) => SearchContext::Project(project.clone()),
            ViewMode::SessionDetail(session_id) => SearchContext::Session(session_id.clone()),
        };

        self.search_modal.context = context;
        self.search_modal.activate();
    }

    /// Handle key when search modal is active
    fn handle_search_key(&mut self, key: KeyEvent) -> Result<()> {
        match self.search_modal.handle_key(key)? {
            SearchAction::None => Ok(()),
            SearchAction::Cancel => Ok(()),
            SearchAction::SelectSession(session_id) => {
                // Navigate to the selected session
                self.navigate_to_session_detail(session_id)?;
                Ok(())
            }
            SearchAction::SelectNode(node_uuid) => {
                // Jump to the selected node in session detail view
                if let Some(ref mut view) = self.session_detail_view {
                    view.select_node_by_uuid(&node_uuid);
                }
                Ok(())
            }
        }
    }

    /// Render the current view
    pub fn render(&mut self, f: &mut Frame) {
        // Render the main view
        match &self.view_mode {
            ViewMode::Projects => {
                if let Some(ref mut view) = self.projects_view {
                    view.render(f, f.area());
                }
            }

            ViewMode::Dashboard => {
                if let Some(ref view) = self.dashboard_view {
                    view.render(f, f.area());
                }
            }

            ViewMode::Prompts => {
                if let Some(ref mut view) = self.prompts_view {
                    view.render(f, f.area());
                }
            }

            ViewMode::Sessions(_) => {
                if let Some(ref mut view) = self.sessions_view {
                    view.render(f, f.area());
                }
            }

            ViewMode::SessionDetail(_) => {
                if let Some(ref mut view) = self.session_detail_view {
                    crate::tui::ui::draw(f, view);
                }
            }
        }

        // Render search modal as overlay (if active)
        self.search_modal.render(f, f.area());
    }
}