lk-inside 0.3.1

A terminal user interface (TUI) application for interactive data analysis.
Documentation
use ratatui::{
    prelude::*,
    widgets::{Block, Borders, Paragraph},
};
use crate::{AppState, AppScreen, InputMode, Workspace};
use crate::ui::components::input_widget::InputWidget;
use crossterm::event::KeyCode;
use std::path::PathBuf;
use anyhow::Result;

pub struct LoadStateScreen {
    pub load_path_input: InputWidget,
}

impl LoadStateScreen {
    pub fn new() -> Self {
        Self {
            load_path_input: InputWidget::new("Load File Path (e.g., 'workspace.json')".to_string()),
        }
    }
}

pub fn render_load_state_screen(f: &mut Frame, screen: &LoadStateScreen, app_state: &AppState) { // MODIFIED: Removed <B: Backend>
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Input box
            Constraint::Min(0),    // Message area
        ])
        .split(f.area());

    screen.load_path_input.render(f, chunks[0], app_state.input_mode == InputMode::EditingLoadPath); // MODIFIED

    let message_block = Block::default()
        .borders(Borders::ALL)
        .title("Load Status");
    let message_paragraph = Paragraph::new(app_state.status_message.clone())
        .block(message_block);
    f.render_widget(message_paragraph, chunks[1]);
}

pub fn handle_input(key: KeyCode, app_state: &mut AppState) -> Option<AppScreen> {
    let screen = &mut app_state.load_state_screen;
    match app_state.input_mode {
        InputMode::EditingLoadPath => match key {
            KeyCode::Enter => {
                let path_str = screen.load_path_input.get_input();
                let path = PathBuf::from(&path_str);

                match load_workspace_state(&path) {
                    Ok(loaded_workspace) => {
                        app_state.workspace = loaded_workspace;
                        app_state.status_message = format!("Workspace loaded successfully from {}", path_str);
                        app_state.input_mode = InputMode::Normal;
                        screen.load_path_input.reset();
                        Some(AppScreen::StatisticsView) // Go to StatisticsView after loading
                    }
                    Err(e) => {
                        app_state.status_message = format!("Error loading workspace: {}", e);
                        app_state.input_mode = InputMode::Normal;
                        screen.load_path_input.reset();
                        Some(AppScreen::Error)
                    }
                }
            }
            KeyCode::Esc => {
                app_state.input_mode = InputMode::Normal;
                app_state.status_message = "Load cancelled.".to_string();
                screen.load_path_input.reset();
                Some(AppScreen::StartMenu) // Or wherever you want to go after cancelling
            }
            _ => {
                screen.load_path_input.handle_key(key);
                None
            }
        },
        _ => None,
    }
}

pub fn load_workspace_state(path: &PathBuf) -> Result<Workspace> {
    let serialized = std::fs::read_to_string(path)?;
    let workspace: Workspace = serde_json::from_str(&serialized)?;
    Ok(workspace)
}