From b6baaa6ca32dbaf5302ba96d2cbc8beb5282ca48 Mon Sep 17 00:00:00 2001 From: suri-codes Date: Mon, 6 Apr 2026 09:31:10 -0700 Subject: [PATCH] fix: creation of zettel's indexes properly --- src/tui/app.rs | 17 +++++++++++++++-- src/tui/components/zk/mod.rs | 21 ++++++++++++--------- src/types/index.rs | 11 ++++++++--- src/types/kasten.rs | 20 ++++++++++++++++---- src/types/zettel/mod.rs | 19 ++++++++++++++++--- 5 files changed, 67 insertions(+), 21 deletions(-) diff --git a/src/tui/app.rs b/src/tui/app.rs index fce883a..a6ab3e7 100644 --- a/src/tui/app.rs +++ b/src/tui/app.rs @@ -1,6 +1,6 @@ use std::{process::Command, thread::spawn}; -use color_eyre::eyre::Result; +use color_eyre::eyre::{Context, Result}; use crossterm::event::KeyEvent; use ratatui::layout::Rect; use serde::{Deserialize, Serialize}; @@ -192,7 +192,20 @@ impl App { // once we get out of the edit, we need to update the zettel for this // path and then update the db and the kasten for this stuff - self.kh.write().await.process_path(&path).await?; + self.kh + .write() + .await + .process_path(&path) + .await + .with_context(|| { + format!( + "Failed to process the path + for this zettel: {}", + path.display() + ) + })?; + + debug!("successfully processed path: {}", path.display()); self.signal_tx.send(Signal::ClosedZettel)?; diff --git a/src/tui/components/zk/mod.rs b/src/tui/components/zk/mod.rs index a53e25b..6d055d7 100644 --- a/src/tui/components/zk/mod.rs +++ b/src/tui/components/zk/mod.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use color_eyre::eyre::{ContextCompat, Result}; +use color_eyre::eyre::{Context as _, ContextCompat, Result}; use crossterm::event::KeyEvent; use dto::{QueryOrder, TagEntity, ZettelColumns, ZettelEntity}; use ratatui::{prelude::*, widgets::ListState}; @@ -243,7 +243,10 @@ impl Component for Zk<'_> { let mut kt = self.kh.write().await; // we create the zettel with the query as the - let z = Zettel::new(self.search.query(), &mut kt).await?; + let z = Zettel::new(self.search.query(), &mut kt) + .await + .with_context(|| "Failed to create a new Zettel!")?; + let path = z.absolute_path(&kt.index).to_path_buf(); drop(kt); @@ -258,6 +261,13 @@ impl Component for Zk<'_> { .selected() .expect("This must be the zettel we just edited"); + // regenerate a fresh zettel list + self.zettel_list = ZettelList::new( + self.get_zettels_by_current_query().await?, + self.zettel_list.state, + self.zettel_list.width, + ); + let Some(zid) = self.zettel_list.id_list.get(selected) else { return Ok(None); }; @@ -272,13 +282,6 @@ impl Component for Zk<'_> { self.search.clear_query(); self.zettel_list.state.select_first(); - // regenerate a fresh zettel list - self.zettel_list = ZettelList::new( - self.get_zettels_by_current_query().await?, - self.zettel_list.state, - self.zettel_list.width, - ); - self.zettel_view = ZettelView::from(&zettel); self.preview = Preview::from(zettel.content(&kt.index).clone()); drop(kt); diff --git a/src/types/index.rs b/src/types/index.rs index e4b8d5d..8e7b896 100644 --- a/src/types/index.rs +++ b/src/types/index.rs @@ -49,7 +49,14 @@ impl Index { let id: ZettelId = path.as_path().try_into()?; let (fm, body) = FrontMatter::extract_from_file(&path)?; - Ok((id, ZettelOnDisk { fm, body, path })) + Ok(( + id, + ZettelOnDisk { + fm, + body, + path: path.canonicalize()?, + }, + )) }) .collect::>>()? .into_iter() @@ -166,8 +173,6 @@ impl Index { Ok(()) } - //TODO:need to process - pub fn get_zod(&self, zid: &ZettelId) -> &ZettelOnDisk { self.zods.get(zid).expect("Invariant broken. Any zid we lookup must exist in the index, otherwise the db is corrupt or not sync'd.") } diff --git a/src/types/kasten.rs b/src/types/kasten.rs index 70e3840..5190f10 100644 --- a/src/types/kasten.rs +++ b/src/types/kasten.rs @@ -11,7 +11,7 @@ use tokio::{ }; use tracing::debug; -use crate::types::{Index, ZettelId}; +use crate::types::{FrontMatter, Index, ZettelId, index::ZettelOnDisk}; #[derive(Debug, Clone)] pub struct Kasten { @@ -91,11 +91,23 @@ impl Kasten { //NOTE: need to clone to get around borrowing rules but // ideally we dont have to do this, kind of cringe imo. - let path = path.as_ref(); - let zid = ZettelId::try_from(path)?; + let path = path.as_ref().canonicalize()?; + let zid = ZettelId::try_from(path.as_path())?; + + if !self.index.zods.contains_key(&zid) { + let (fm, body) = FrontMatter::extract_from_file(&path)?; + self.index.zods.insert( + zid.clone(), + ZettelOnDisk { + fm, + body, + path: path.clone(), + }, + ); + } // incase the path of the zettel changed - self.index.update_path_for_zid(&zid, path.to_path_buf()); + self.index.update_path_for_zid(&zid, path.clone()); // let the index process the zettel, basically update the internal state of the zod self.index.process_zid(&zid)?; // and then we sync tags diff --git a/src/types/zettel/mod.rs b/src/types/zettel/mod.rs index 93de993..c86d250 100644 --- a/src/types/zettel/mod.rs +++ b/src/types/zettel/mod.rs @@ -4,7 +4,7 @@ use dto::{ DatabaseConnection, DateTime, TagEntity, ZettelActiveModel, ZettelEntity, ZettelModelEx, }; -use color_eyre::eyre::Result; +use color_eyre::eyre::{Context, Result}; use dto::NanoId; use tokio::{fs::File, io::AsyncWriteExt}; @@ -53,8 +53,14 @@ impl Zettel { let local_file_path = format!("{nano_id}.md"); + let absolute_file_path = kt.root.clone().join(&local_file_path); + // now we have to create the file - let mut file = File::create_new(kt.root.clone().join(&local_file_path)).await?; + let mut file = File::create_new(&absolute_file_path) + .await + .with_context(|| { + format!("Failed to create file at local file path: {local_file_path}") + })?; let inserted = ZettelActiveModel::builder() .set_title(title.clone()) @@ -79,7 +85,14 @@ impl Zettel { file.write_all(front_matter.to_string().as_bytes()).await?; - kt.process_path(zettel.file_path.clone()).await?; + kt.process_path(&absolute_file_path) + .await + .with_context(|| { + format!( + "Kasten fails to process new Zettel at path: {}", + absolute_file_path.display(), + ) + })?; Ok(zettel.into()) } -- 2.51.2