I'm trying to store a .json file that would be used for a user. I want to store a file path to this .json file that the user has selected from a function using FileDialogBuilder::new().pick_folder() which would also need to invoke another function using this path.
Currently the file is being written inside of the src-tauri directory.
async fn select_directory() -> Result<String, String> {
let (tx, rx) = std::sync::mpsc::channel();
FileDialogBuilder::new().pick_folder(move |folder_path| {
if let Some(path) = folder_path {
let path_str = path.to_str().unwrap_or("").to_string();
// Update the global BASE_FOLDER variable
let mut base_folder = BASE_FOLDER.lock().unwrap();
*base_folder = Some(path_str.clone());
// Write to a JSON file
let j = json!({
"base_folder": path_str.clone()
});
if let Err(e) = write("config.json", j.to_string()) {
eprintln!("Unable to write to config.json: {}", e);
}
// Debug statement
println!("Debug: BASE_FOLDER inside select_directory is now: {:?}", base_folder);
tx.send(Ok(path_str)).unwrap();
} else {
tx.send(Err("No folder was picked".to_string())).unwrap();
}
});
// Receive the result from the closure
let result = rx.recv().unwrap();
// Perform the scan_directory if folder was successfully picked
if let Ok(path_str) = &result {
if let Err(e) = scan_directory(path_str.clone()).await {
eprintln!("Failed to rescan directory after folder change: {}", e);
}
}
result
}```