File size: 4,514 Bytes
2fc9965
 
 
 
 
1d7de65
 
 
 
2fc9965
 
1d7de65
 
 
 
 
 
 
2fc9965
 
1d7de65
2fc9965
 
1d7de65
 
2fc9965
 
 
 
 
 
 
 
 
 
 
 
 
1d7de65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fc9965
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
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 }

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BookmarkEntry { pub id: String, pub title: String, pub url: String, pub created_at: i64, pub updated_at: i64 }

const HISTORY_FILE: &str = "browser_history.json";
const BOOKMARKS_FILE: &str = "browser_bookmarks.json";
const MAX_TITLE_LEN: usize = 160;
const MAX_BOOKMARKS: usize = 1000;

fn clean_title(raw: &str, fallback: &str) -> String { let s: String = raw.chars().filter(|c| !c.is_control()).take(MAX_TITLE_LEN).collect(); let t = s.trim(); if t.is_empty() { fallback.to_string() } else { t.to_string() } }
fn valid_url(url: &str) -> bool { url.starts_with("http://") || url.starts_with("https://") }
fn host_title(url: &str) -> String { url.split("//").nth(1).and_then(|s| s.split('/').next()).unwrap_or(url).trim_start_matches("www.").to_string() }

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("lumaref-action://") || !valid_url(&url) { 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(()); } }
    let title = clean_title(&title, &host_title(&url));
    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_search(app: AppHandle, query: String, limit: Option<usize>) -> Result<Vec<HistoryEntry>, String> {
    let q = query.trim().to_lowercase();
    if q.is_empty() { return history_list(app, limit); }
    let max = limit.unwrap_or(8).min(50);
    let entries: Vec<HistoryEntry> = crate::persistence::load_json(&app, HISTORY_FILE).unwrap_or_default();
    let mut out = Vec::new();
    for e in entries {
        if e.url.to_lowercase().contains(&q) || e.title.to_lowercase().contains(&q) {
            out.push(e);
            if out.len() >= max { break; }
        }
    }
    Ok(out)
}

#[tauri::command]
pub fn history_clear(app: AppHandle) -> Result<(), String> { crate::persistence::save_json(&app, HISTORY_FILE, &Vec::<HistoryEntry>::new()) }

#[tauri::command]
pub fn bookmarks_list(app: AppHandle) -> Result<Vec<BookmarkEntry>, String> {
    let mut items: Vec<BookmarkEntry> = crate::persistence::load_json(&app, BOOKMARKS_FILE).unwrap_or_default();
    items.sort_by(|a, b| b.updated_at.cmp(&a.updated_at));
    Ok(items)
}

#[tauri::command]
pub fn bookmarks_add(app: AppHandle, url: String, title: Option<String>) -> Result<BookmarkEntry, String> {
    let url = url.trim().to_string();
    if !valid_url(&url) { return Err("Bookmark URL must be http(s)".into()); }
    let now = chrono::Utc::now().timestamp();
    let mut items: Vec<BookmarkEntry> = crate::persistence::load_json(&app, BOOKMARKS_FILE).unwrap_or_default();
    let title = clean_title(title.as_deref().unwrap_or(""), &host_title(&url));
    if let Some(existing) = items.iter_mut().find(|b| b.url == url) {
        existing.title = title;
        existing.updated_at = now;
        let result = existing.clone();
        crate::persistence::save_json(&app, BOOKMARKS_FILE, &items)?;
        return Ok(result);
    }
    let item = BookmarkEntry { id: Uuid::new_v4().to_string(), title, url, created_at: now, updated_at: now };
    items.insert(0, item.clone());
    items.truncate(MAX_BOOKMARKS);
    crate::persistence::save_json(&app, BOOKMARKS_FILE, &items)?;
    Ok(item)
}

#[tauri::command]
pub fn bookmarks_remove(app: AppHandle, id: String) -> Result<(), String> {
    let mut items: Vec<BookmarkEntry> = crate::persistence::load_json(&app, BOOKMARKS_FILE).unwrap_or_default();
    items.retain(|b| b.id != id);
    crate::persistence::save_json(&app, BOOKMARKS_FILE, &items)
}