From 5ef9f174367bae7b541db80641b5ab680bc413e7 Mon Sep 17 00:00:00 2001 From: Iaphetes Date: Thu, 22 May 2025 21:32:36 +0200 Subject: [PATCH] made db work on relative paths --- package-lock.json | 10 ++++++ package.json | 1 + src-tauri/Cargo.lock | 1 + src-tauri/Cargo.toml | 1 + src-tauri/capabilities/default.json | 3 +- src-tauri/src/database.rs | 25 +++++++++---- src-tauri/src/file_tree.rs | 55 +++++++++++++++++++++-------- src-tauri/src/lib.rs | 1 + 8 files changed, 74 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8ad6a37..4c8693c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,6 +6,7 @@ "": { "dependencies": { "@tauri-apps/plugin-dialog": "^2.2.0", + "@tauri-apps/plugin-log": "^2.4.0", "@tauri-apps/plugin-sql": "^2.2.0" } }, @@ -28,6 +29,15 @@ "@tauri-apps/api": "^2.0.0" } }, + "node_modules/@tauri-apps/plugin-log": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/@tauri-apps/plugin-log/-/plugin-log-2.4.0.tgz", + "integrity": "sha512-j7yrDtLNmayCBOO2esl3aZv9jSXy2an8MDLry3Ys9ZXerwUg35n1Y2uD8HoCR+8Ng/EUgx215+qOUfJasjYrHw==", + "license": "MIT OR Apache-2.0", + "dependencies": { + "@tauri-apps/api": "^2.0.0" + } + }, "node_modules/@tauri-apps/plugin-sql": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/@tauri-apps/plugin-sql/-/plugin-sql-2.2.0.tgz", diff --git a/package.json b/package.json index 92d444a..d0628f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "dependencies": { "@tauri-apps/plugin-dialog": "^2.2.0", + "@tauri-apps/plugin-log": "^2.4.0", "@tauri-apps/plugin-sql": "^2.2.0" } } diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 69bab6c..d12136c 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -156,6 +156,7 @@ dependencies = [ "futures", "fuzzy-matcher", "html_tag", + "log", "rand 0.9.1", "serde", "serde_json", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 6b1ad7b..3d53e1c 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -36,5 +36,6 @@ confy = "0.6" diff-match-patch-rs = "0.5" rand = "0.9" futures = "0.3" +log = "0.4.27" # [profile.dev] # codegen-backend = "cranelift" diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index fe31510..3120c5c 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -20,6 +20,7 @@ "fs:allow-home-read-recursive", "fs:scope-home-recursive", "fs:read-all", - "dialog:default" + "dialog:default", + "log:default" ] } \ No newline at end of file diff --git a/src-tauri/src/database.rs b/src-tauri/src/database.rs index 30559dd..07a67e3 100644 --- a/src-tauri/src/database.rs +++ b/src-tauri/src/database.rs @@ -187,17 +187,28 @@ pub async fn store_diff( // 1 insert into file_diffs with parent // 2 make last diff not currennt diff - // + // id INTEGER NOT NULL, // client_id TEXT NOT NULL, // diff_text TEXT NOT NULL, // current_diff BOOLEAN, - // FOREIGN KEY (id) - // REFERENCES file_diffs (parent_diff_id) - // FOREIGN KEY (id) - // REFERENCES files (file_id) + // parent_diff_id INTEGER, + // file_id INTEGER, + // FOREIGN KEY (parent_diff_id) REFERENCES file_diffs(id), + // FOREIGN KEY (file_id) REFERENCES files(id), // PRIMARY KEY (id, client_id) let file_id = get_file_id(app_handle, relative_file_path, file_name).await?; + let parent_diff_id: i32 = sqlx::query_scalar(&format!( + " + SELECT id from file_diffs + WHERE relative_path IS '{relative_file_path}' + AND file_name IS '{file_name}' + AND working_diff is 1 + " + )) + .fetch_one(&db) + .await + .unwrap_or(0); let _ = sqlx::query(&format!( " UPDATE file_diffs @@ -209,13 +220,13 @@ pub async fn store_diff( )) .execute(&db) .await - .map_err(|e| DBError::DatabaseQueryError(e.to_string()))?; + .map_err(|e| DBError::DatabaseQueryError(e.to_string())); let _ = sqlx::query(&format!( " INSERT INTO file_diffs (client_id, diff_text, current_diff, file_id) VALUES - ('{client_id}', '{diff}', 1, );" + ('{client_id}', '{diff}', 1, {parent_diff_id});" )) .execute(&db) .await diff --git a/src-tauri/src/file_tree.rs b/src-tauri/src/file_tree.rs index 4ea9c09..07c4dc6 100644 --- a/src-tauri/src/file_tree.rs +++ b/src-tauri/src/file_tree.rs @@ -4,14 +4,20 @@ use html_tag::HtmlTag; use shellexpand; use std::{ fs::{self, DirEntry}, - path::Path, + ops::Deref, + path::{Path, PathBuf}, }; #[tauri::command] pub async fn dir_tree_html(app_handle: tauri::AppHandle, filter: Vec) -> String { if let Some(basepath) = get_basepath(app_handle.clone()) { - add_dir_tree_node(&app_handle, &Path::new(&basepath), &filter) - .await - .to_html() + add_dir_tree_node( + &app_handle, + &Path::new(&basepath), + &filter, + &Path::new(&basepath).parent().unwrap_or(Path::new("/")), + ) + .await + .to_html() } else { String::new() } @@ -20,6 +26,7 @@ async fn add_dir_tree_node( app_handle: &tauri::AppHandle, path: &Path, filter: &Vec, + parent_path: &Path, ) -> HtmlTag { let mut html = HtmlTag::new("div") .with_class("filetree-node") @@ -48,24 +55,36 @@ async fn add_dir_tree_node( ), ), ); + if let Ok(entries) = fs::read_dir(path) { for dir_entry_res in entries { if let Ok(dir_entry) = dir_entry_res { if let Ok(metadata) = fs::metadata(dir_entry.path()) { + let absolute_path = dir_entry.path(); + let relative_path = + absolute_path.strip_prefix(parent_path).unwrap_or_else(|_| { + log::error!("{:?}:{:?}", path, parent_path); + Path::new("/") + }); if metadata.is_file() { add_file( app_handle, - path.to_string_lossy().as_ref(), + relative_path.to_string_lossy().as_ref(), dir_entry.file_name().to_string_lossy().as_ref(), ) .await; - html.add_child(div_from_dir_entry(&dir_entry)) + html.add_child(div_from_dir_entry(&absolute_path, &relative_path)) } else if metadata.is_dir() { html.add_child( - Box::pin(add_dir_tree_node(app_handle, &dir_entry.path(), &filter)) - .await - .with_attribute("style", "visibility: hidden; height: 0px;"), // .with_style("visibility", "hidden") - // .with_style("height", "0px"), + Box::pin(add_dir_tree_node( + app_handle, + &dir_entry.path(), + &filter, + parent_path, + )) + .await + .with_attribute("style", "visibility: hidden; height: 0px;"), // .with_style("visibility", "hidden") + // .with_style("height", "0px"), ); } } @@ -75,14 +94,13 @@ async fn add_dir_tree_node( return html; } -fn div_from_dir_entry(dir_entry: &DirEntry) -> HtmlTag { +fn div_from_dir_entry(absolute_path: &Path, relative_path: &Path) -> HtmlTag { let mut file_div = HtmlTag::new("div") .with_class("filetree-node") - .with_id(&format!("{}", dir_entry.path().to_string_lossy())) + .with_id(&format!("{}", absolute_path.to_string_lossy())) .with_attribute("style", "visibility: hidden; height: 0px;"); let mut file_button = HtmlTag::new("button").with_class("filetree-file-button"); - match dir_entry - .path() + match relative_path .extension() .unwrap_or_default() .to_string_lossy() @@ -102,7 +120,14 @@ fn div_from_dir_entry(dir_entry: &DirEntry) -> HtmlTag { ), }; - file_button.add_child(HtmlTag::new("a").with_body(&dir_entry.file_name().to_string_lossy())); + file_button.add_child( + HtmlTag::new("a").with_body( + &relative_path + .file_name() + .unwrap_or_default() + .to_string_lossy(), + ), + ); file_div.add_child(file_button); return file_div; } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 044a575..b4ddcf7 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -23,6 +23,7 @@ pub fn run() { .plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_log::Builder::new().build()) .plugin(tauri_plugin_shell::init()) + .plugin(tauri_plugin_log::Builder::new().build()) .setup(|app| { // allowed the given directory let scope = app.fs_scope();