made db work on relative paths

This commit is contained in:
Iaphetes 2025-05-22 21:32:36 +02:00
parent b38d0cc149
commit 5ef9f17436
8 changed files with 74 additions and 23 deletions

10
package-lock.json generated
View file

@ -6,6 +6,7 @@
"": { "": {
"dependencies": { "dependencies": {
"@tauri-apps/plugin-dialog": "^2.2.0", "@tauri-apps/plugin-dialog": "^2.2.0",
"@tauri-apps/plugin-log": "^2.4.0",
"@tauri-apps/plugin-sql": "^2.2.0" "@tauri-apps/plugin-sql": "^2.2.0"
} }
}, },
@ -28,6 +29,15 @@
"@tauri-apps/api": "^2.0.0" "@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": { "node_modules/@tauri-apps/plugin-sql": {
"version": "2.2.0", "version": "2.2.0",
"resolved": "https://registry.npmjs.org/@tauri-apps/plugin-sql/-/plugin-sql-2.2.0.tgz", "resolved": "https://registry.npmjs.org/@tauri-apps/plugin-sql/-/plugin-sql-2.2.0.tgz",

View file

@ -1,6 +1,7 @@
{ {
"dependencies": { "dependencies": {
"@tauri-apps/plugin-dialog": "^2.2.0", "@tauri-apps/plugin-dialog": "^2.2.0",
"@tauri-apps/plugin-log": "^2.4.0",
"@tauri-apps/plugin-sql": "^2.2.0" "@tauri-apps/plugin-sql": "^2.2.0"
} }
} }

1
src-tauri/Cargo.lock generated
View file

@ -156,6 +156,7 @@ dependencies = [
"futures", "futures",
"fuzzy-matcher", "fuzzy-matcher",
"html_tag", "html_tag",
"log",
"rand 0.9.1", "rand 0.9.1",
"serde", "serde",
"serde_json", "serde_json",

View file

@ -36,5 +36,6 @@ confy = "0.6"
diff-match-patch-rs = "0.5" diff-match-patch-rs = "0.5"
rand = "0.9" rand = "0.9"
futures = "0.3" futures = "0.3"
log = "0.4.27"
# [profile.dev] # [profile.dev]
# codegen-backend = "cranelift" # codegen-backend = "cranelift"

View file

@ -20,6 +20,7 @@
"fs:allow-home-read-recursive", "fs:allow-home-read-recursive",
"fs:scope-home-recursive", "fs:scope-home-recursive",
"fs:read-all", "fs:read-all",
"dialog:default" "dialog:default",
"log:default"
] ]
} }

View file

@ -187,17 +187,28 @@ pub async fn store_diff(
// 1 insert into file_diffs with parent // 1 insert into file_diffs with parent
// 2 make last diff not currennt diff // 2 make last diff not currennt diff
//
// id INTEGER NOT NULL, // id INTEGER NOT NULL,
// client_id TEXT NOT NULL, // client_id TEXT NOT NULL,
// diff_text TEXT NOT NULL, // diff_text TEXT NOT NULL,
// current_diff BOOLEAN, // current_diff BOOLEAN,
// FOREIGN KEY (id) // parent_diff_id INTEGER,
// REFERENCES file_diffs (parent_diff_id) // file_id INTEGER,
// FOREIGN KEY (id) // FOREIGN KEY (parent_diff_id) REFERENCES file_diffs(id),
// REFERENCES files (file_id) // FOREIGN KEY (file_id) REFERENCES files(id),
// PRIMARY KEY (id, client_id) // PRIMARY KEY (id, client_id)
let file_id = get_file_id(app_handle, relative_file_path, file_name).await?; 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!( let _ = sqlx::query(&format!(
" "
UPDATE file_diffs UPDATE file_diffs
@ -209,13 +220,13 @@ pub async fn store_diff(
)) ))
.execute(&db) .execute(&db)
.await .await
.map_err(|e| DBError::DatabaseQueryError(e.to_string()))?; .map_err(|e| DBError::DatabaseQueryError(e.to_string()));
let _ = sqlx::query(&format!( let _ = sqlx::query(&format!(
" "
INSERT INTO INSERT INTO
file_diffs (client_id, diff_text, current_diff, file_id) file_diffs (client_id, diff_text, current_diff, file_id)
VALUES VALUES
('{client_id}', '{diff}', 1, );" ('{client_id}', '{diff}', 1, {parent_diff_id});"
)) ))
.execute(&db) .execute(&db)
.await .await

View file

@ -4,14 +4,20 @@ use html_tag::HtmlTag;
use shellexpand; use shellexpand;
use std::{ use std::{
fs::{self, DirEntry}, fs::{self, DirEntry},
path::Path, ops::Deref,
path::{Path, PathBuf},
}; };
#[tauri::command] #[tauri::command]
pub async fn dir_tree_html(app_handle: tauri::AppHandle, filter: Vec<String>) -> String { pub async fn dir_tree_html(app_handle: tauri::AppHandle, filter: Vec<String>) -> String {
if let Some(basepath) = get_basepath(app_handle.clone()) { if let Some(basepath) = get_basepath(app_handle.clone()) {
add_dir_tree_node(&app_handle, &Path::new(&basepath), &filter) add_dir_tree_node(
.await &app_handle,
.to_html() &Path::new(&basepath),
&filter,
&Path::new(&basepath).parent().unwrap_or(Path::new("/")),
)
.await
.to_html()
} else { } else {
String::new() String::new()
} }
@ -20,6 +26,7 @@ async fn add_dir_tree_node(
app_handle: &tauri::AppHandle, app_handle: &tauri::AppHandle,
path: &Path, path: &Path,
filter: &Vec<String>, filter: &Vec<String>,
parent_path: &Path,
) -> HtmlTag { ) -> HtmlTag {
let mut html = HtmlTag::new("div") let mut html = HtmlTag::new("div")
.with_class("filetree-node") .with_class("filetree-node")
@ -48,24 +55,36 @@ async fn add_dir_tree_node(
), ),
), ),
); );
if let Ok(entries) = fs::read_dir(path) { if let Ok(entries) = fs::read_dir(path) {
for dir_entry_res in entries { for dir_entry_res in entries {
if let Ok(dir_entry) = dir_entry_res { if let Ok(dir_entry) = dir_entry_res {
if let Ok(metadata) = fs::metadata(dir_entry.path()) { 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() { if metadata.is_file() {
add_file( add_file(
app_handle, app_handle,
path.to_string_lossy().as_ref(), relative_path.to_string_lossy().as_ref(),
dir_entry.file_name().to_string_lossy().as_ref(), dir_entry.file_name().to_string_lossy().as_ref(),
) )
.await; .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() { } else if metadata.is_dir() {
html.add_child( html.add_child(
Box::pin(add_dir_tree_node(app_handle, &dir_entry.path(), &filter)) Box::pin(add_dir_tree_node(
.await app_handle,
.with_attribute("style", "visibility: hidden; height: 0px;"), // .with_style("visibility", "hidden") &dir_entry.path(),
// .with_style("height", "0px"), &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; 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") let mut file_div = HtmlTag::new("div")
.with_class("filetree-node") .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;"); .with_attribute("style", "visibility: hidden; height: 0px;");
let mut file_button = HtmlTag::new("button").with_class("filetree-file-button"); let mut file_button = HtmlTag::new("button").with_class("filetree-file-button");
match dir_entry match relative_path
.path()
.extension() .extension()
.unwrap_or_default() .unwrap_or_default()
.to_string_lossy() .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); file_div.add_child(file_button);
return file_div; return file_div;
} }

View file

@ -23,6 +23,7 @@ pub fn run() {
.plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_log::Builder::new().build()) .plugin(tauri_plugin_log::Builder::new().build())
.plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_log::Builder::new().build())
.setup(|app| { .setup(|app| {
// allowed the given directory // allowed the given directory
let scope = app.fs_scope(); let scope = app.fs_scope();