| use serde::{Deserialize, Serialize};
|
| use tauri::AppHandle;
|
| use uuid::Uuid;
|
|
|
| #[derive(Debug, Clone, Serialize, Deserialize)]
|
| pub struct HistoryEntry {
|
| pub id: String,
|
| pub tab_id: String,
|
| pub url: String,
|
| pub title: String,
|
| pub visited_at: i64,
|
| }
|
|
|
| const HISTORY_FILE: &str = "browser_history.json";
|
|
|
| pub fn record_visit(app: &AppHandle, tab_id: String, url: String, title: String) -> Result<(), String> {
|
| if url.is_empty() || url == "about:blank" || url.starts_with("muse-action://") { return Ok(()); }
|
| let mut entries: Vec<HistoryEntry> = crate::persistence::load_json(app, HISTORY_FILE).unwrap_or_default();
|
| let now = chrono::Utc::now().timestamp();
|
| if let Some(last) = entries.first() {
|
| if last.url == url && now - last.visited_at < 3 { return Ok(()); }
|
| }
|
| entries.insert(0, HistoryEntry { id: Uuid::new_v4().to_string(), tab_id, url, title, visited_at: now });
|
| entries.truncate(5000);
|
| crate::persistence::save_json(app, HISTORY_FILE, &entries)
|
| }
|
|
|
| #[tauri::command]
|
| pub fn history_list(app: AppHandle, limit: Option<usize>) -> Result<Vec<HistoryEntry>, String> {
|
| let mut entries: Vec<HistoryEntry> = crate::persistence::load_json(&app, HISTORY_FILE).unwrap_or_default();
|
| entries.truncate(limit.unwrap_or(250).min(5000));
|
| Ok(entries)
|
| }
|
|
|
| #[tauri::command]
|
| pub fn history_clear(app: AppHandle) -> Result<(), String> {
|
| crate::persistence::save_json(&app, HISTORY_FILE, &Vec::<HistoryEntry>::new())
|
| }
|
|
|