feat: integrate event tracking for hotkey actions, history management, and settings updates

This commit is contained in:
PandaDEV 2024-12-23 14:34:21 +10:00
parent 3824f24be4
commit f44be512fe
No known key found for this signature in database
GPG key ID: 13EFF9BAF70EE75C
8 changed files with 149 additions and 36 deletions

View file

@ -4,6 +4,7 @@ use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use sqlx::{Row, SqlitePool};
use std::fs;
use tauri_plugin_aptabase::EventTracker;
pub async fn initialize_history(pool: &SqlitePool) -> Result<(), Box<dyn std::error::Error>> {
let id: String = thread_rng()
@ -53,6 +54,7 @@ pub async fn get_history(pool: tauri::State<'_, SqlitePool>) -> Result<Vec<Histo
#[tauri::command]
pub async fn add_history_item(
app_handle: tauri::AppHandle,
pool: tauri::State<'_, SqlitePool>,
item: HistoryItem,
) -> Result<(), String> {
@ -95,6 +97,10 @@ pub async fn add_history_item(
}
}
let _ = app_handle.track_event("history_item_added", Some(serde_json::json!({
"content_type": item.content_type.to_string()
})));
Ok(())
}
@ -163,6 +169,7 @@ pub async fn load_history_chunk(
#[tauri::command]
pub async fn delete_history_item(
app_handle: tauri::AppHandle,
pool: tauri::State<'_, SqlitePool>,
id: String,
) -> Result<(), String> {
@ -172,16 +179,23 @@ pub async fn delete_history_item(
.await
.map_err(|e| e.to_string())?;
let _ = app_handle.track_event("history_item_deleted", None);
Ok(())
}
#[tauri::command]
pub async fn clear_history(pool: tauri::State<'_, SqlitePool>) -> Result<(), String> {
pub async fn clear_history(
app_handle: tauri::AppHandle,
pool: tauri::State<'_, SqlitePool>
) -> Result<(), String> {
sqlx::query("DELETE FROM history")
.execute(&*pool)
.await
.map_err(|e| e.to_string())?;
let _ = app_handle.track_event("history_cleared", None);
Ok(())
}

View file

@ -3,6 +3,7 @@ use serde_json;
use sqlx::Row;
use sqlx::SqlitePool;
use tauri::{Emitter, Manager};
use tauri_plugin_aptabase::EventTracker;
#[derive(Deserialize, Serialize)]
struct KeybindSetting {
@ -26,9 +27,16 @@ pub async fn initialize_settings(pool: &SqlitePool) -> Result<(), Box<dyn std::e
#[tauri::command]
pub async fn save_keybind(
app_handle: tauri::AppHandle,
keybind: Vec<String>,
pool: tauri::State<'_, SqlitePool>,
keybind: Vec<String>,
) -> Result<(), String> {
let keybind_str = keybind.join("+");
let keybind_clone = keybind_str.clone();
app_handle
.emit("update-shortcut", &keybind_str)
.map_err(|e| e.to_string())?;
let json = serde_json::to_string(&keybind).map_err(|e| e.to_string())?;
sqlx::query("INSERT OR REPLACE INTO settings (key, value) VALUES ('keybind', ?)")
@ -37,10 +45,9 @@ pub async fn save_keybind(
.await
.map_err(|e| e.to_string())?;
let keybind_str = keybind.join("+");
app_handle
.emit("update-shortcut", keybind_str)
.map_err(|e| e.to_string())?;
let _ = app_handle.track_event("keybind_saved", Some(serde_json::json!({
"keybind": keybind_clone
})));
Ok(())
}
@ -61,17 +68,22 @@ pub async fn get_setting(
#[tauri::command]
pub async fn save_setting(
app_handle: tauri::AppHandle,
pool: tauri::State<'_, SqlitePool>,
key: String,
value: String,
) -> Result<(), String> {
sqlx::query("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)")
.bind(key)
.bind(key.clone())
.bind(value)
.execute(&*pool)
.await
.map_err(|e| e.to_string())?;
let _ = app_handle.track_event("setting_saved", Some(serde_json::json!({
"key": key
})));
Ok(())
}