Migrate from window.electron to tauri IPC functions across components
- Replaced `window.electron.invoke` calls with equivalent `tauri` function calls for all IPC interactions. - Removed `electron.d.ts` TypeScript definitions as they are no longer needed. - Updated related logic for offline/online state synchronization. - Added `types.rs` and `shared/mod.rs` modules to support Tauri IPC integration with Rust enums and shared logic. - Refactored IPC request queues to use updated handler names for consistency with Tauri.
This commit is contained in:
59
src-tauri/src/helpers.rs
Normal file
59
src-tauri/src/helpers.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use chrono::Utc;
|
||||
use regex::Regex;
|
||||
|
||||
|
||||
/// Returns the current UNIX timestamp in seconds.
|
||||
/// Equivalent to TS `System.timeStampInSeconds()`.
|
||||
pub fn timestamp_in_seconds() -> i64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|duration| duration.as_secs() as i64)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Creates a new UUID v4 string, or reuses an existing ID if provided.
|
||||
/// Equivalent to TS `System.createUniqueId()`.
|
||||
pub fn create_unique_id(existing_id: Option<&str>) -> String {
|
||||
match existing_id {
|
||||
Some(id) => id.to_string(),
|
||||
None => uuid::Uuid::new_v4().to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the current date as an ISO 8601 string.
|
||||
/// Equivalent to TS `System.getCurrentDate()`.
|
||||
pub fn get_current_date() -> String {
|
||||
Utc::now().to_rfc3339()
|
||||
}
|
||||
|
||||
/// Converts an ISO date string to a MySQL-compatible date (YYYY-MM-DD).
|
||||
/// Equivalent to TS `System.dateToMySqlDate()`.
|
||||
pub fn date_to_mysql_date(iso_date_string: &str) -> String {
|
||||
match chrono::DateTime::parse_from_rfc3339(iso_date_string) {
|
||||
Ok(date_object) => date_object.format("%Y-%m-%d").to_string(),
|
||||
Err(_) => iso_date_string.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts HTML content to plain text by stripping tags and decoding entities.
|
||||
/// Equivalent to TS `System.htmlToText()`.
|
||||
pub fn html_to_text(html_node: &str) -> String {
|
||||
let mut text: String = html_node.to_string();
|
||||
let p_regex: Regex = Regex::new(r"(?i)</?p[^>]*>").unwrap();
|
||||
let br_regex: Regex = Regex::new(r"(?i)<br\s*/?>").unwrap();
|
||||
let span_heading_regex: Regex = Regex::new(r"(?i)</?(span|h[1-6])[^>]*>").unwrap();
|
||||
text = p_regex.replace_all(&text, "\n").to_string();
|
||||
text = br_regex.replace_all(&text, "\n").to_string();
|
||||
text = span_heading_regex.replace_all(&text, "").to_string();
|
||||
text = text.replace("'", "'");
|
||||
text = text.replace(""", "\"");
|
||||
text = text.replace("&", "&");
|
||||
text = text.replace("<", "<");
|
||||
text = text.replace(">", ">");
|
||||
text = text.replace("'", "'");
|
||||
let double_newline_regex: Regex = Regex::new(r"\r?\n\s*\n").unwrap();
|
||||
text = double_newline_regex.replace_all(&text, "\n").to_string();
|
||||
let spaces_regex: Regex = Regex::new(r"[ \t]+").unwrap();
|
||||
text = spaces_regex.replace_all(&text, " ").to_string();
|
||||
text.trim().to_string()
|
||||
}
|
||||
Reference in New Issue
Block a user