···1111use crate::{
1212 config::Config,
1313 tui::{Event, Tui, components::Zk},
1414- types::{KastenHandle, ZettelId},
1414+ types::{KastenHandle, Zettel},
1515};
16161717use super::{components::Component, signal::Signal};
···167167168168 Signal::Quit => self.should_quit = true,
169169170170+ Signal::NewZettel => {
171171+ // what the fuck am i going to do in here
172172+173173+ let ws = &self.kh.read().await.ws;
174174+ let z = Zettel::new("", ws).await?;
175175+ let path = z.absolute_path(ws);
176176+177177+ self.signal_tx.send(Signal::Helix { path })?;
178178+ }
179179+170180 Signal::Helix { path } => {
171181 tui.exit()?;
172182···187197 hx.join().unwrap().unwrap();
188198 // once we get out of the edit, we need to update the zettel for this
189199 // path and then update the db and the kasten for this stuff
190190- let zid = ZettelId::try_from(path)?;
191200192192- self.kh.write().await.process_zid(&zid).await?;
201201+ self.kh.write().await.process_path(&path).await?;
193202194203 self.signal_tx.send(Signal::ClosedZettel)?;
195204
+3-1
src/tui/signal.rs
···2222 MoveDown,
2323 MoveUp,
24242525+ /// New `Zettel`
2626+ NewZettel,
2527 /// User asks to open a `Zettel`
2628 OpenZettel,
2727-2829 /// The user is done editing a `Zettel`
2930 ClosedZettel,
3031···4546 "movedown" => Self::MoveDown,
4647 "moveup" => Self::MoveUp,
4748 "openzettel" => Self::OpenZettel,
4949+ "newzettel" => Self::NewZettel,
4850 _ => {
4951 return Err(eyre!(format!(
5052 "Attempt to construct a non-user Signal from str: {s}"
+63-7
src/types/kasten.rs
···11use crate::types::{Link, Zettel, ZettelId};
22use color_eyre::eyre::Result;
33+use dto::{TagEntity, ZettelEntity};
34use eframe::emath;
45use egui_graphs::{
56 Graph, Node,
66- petgraph::{Directed, graph::NodeIndex, prelude::StableGraph},
77+ petgraph::{Directed, Direction, graph::NodeIndex, prelude::StableGraph, visit::EdgeRef},
78};
89use rayon::iter::{ParallelBridge as _, ParallelIterator as _};
99-use std::{cmp::max, collections::HashMap, sync::Arc};
1010+use std::{cmp::max, collections::HashMap, path::Path, sync::Arc};
1011use tokio::sync::RwLock;
11121213use crate::types::Workspace;
···108109 /// processes the `Zettel` for the provided `ZettelId`,
109110 /// meaning it updates the internal state of the `Kasten`
110111 /// with the changes in `Zettel`.
111111- pub async fn process_zid(&mut self, zid: &ZettelId) -> Result<()> {
112112+ pub async fn process_path(&mut self, path: &Path) -> Result<()> {
112113 //NOTE: need to clone to get around borrowing rules but
113114 // ideally we dont have to do this, kind of cringe imo.
114115 let ws = self.ws.clone();
115116116116- let zettel = self
117117- .get_node_by_zettel_id_mut(zid)
118118- .expect("this should not happen ever")
119119- .payload_mut();
117117+ let zid = ZettelId::try_from(path)?;
118118+119119+ let mut gid = self.zid_to_gid.get(&zid).copied();
120120+ // sometimes this zid is new, so it wont be in the kasten
121121+ let zettel = if let Some(existing) = self.get_node_by_zettel_id_mut(&zid) {
122122+ existing.payload_mut()
123123+ } else {
124124+ // this should aleady be in the database though so lets get it from there first
125125+ let zettel: Zettel = ZettelEntity::load()
126126+ .filter_by_nano_id(zid)
127127+ .with(TagEntity)
128128+ .one(&ws.db)
129129+ .await?
130130+ .expect("This should be in the database already")
131131+ .into();
132132+133133+ let zid = zettel.id.clone();
134134+ let idx = self.graph.add_node(zettel);
135135+136136+ gid = Some(
137137+ self.zid_to_gid
138138+ .insert(zid.clone(), idx)
139139+ .expect("this cannot have existed already"),
140140+ );
141141+142142+ self.get_node_by_zettel_id_mut(&zid)
143143+ .expect("we just inserted it")
144144+ .payload_mut()
145145+ };
146146+147147+ // to get past borrowchecker rules
148148+ let mut zettel = zettel.clone();
149149+150150+ // gid must be set
151151+ let gid = gid.unwrap();
120152153153+ // and then we sync with the file
121154 zettel.sync_with_file(&ws).await?;
155155+156156+ // and now we manage the links going out of the file
157157+158158+ // remove all the old shit
159159+ self.graph
160160+ .edges_directed(gid, Direction::Outgoing)
161161+ .map(|e| e.id())
162162+ .collect::<Vec<_>>()
163163+ .into_iter()
164164+ .for_each(|e| {
165165+ let _ = self.graph.remove_edge(e);
166166+ });
167167+168168+ // add the links that actually exist
169169+ zettel.links(&ws).await?.into_iter().for_each(|link| {
170170+ // this is an option because a user c
171171+ let dest = self
172172+ .zid_to_gid
173173+ .get(&link.dest)
174174+ .expect("Links should be valid");
175175+176176+ self.graph.add_edge(gid, *dest, link);
177177+ });
122178123179 Ok(())
124180 }
+2
src/types/zettel.rs
···314314 continue;
315315 };
316316317317+ // TODO: check that the thing actually exists inside the ws.db
318318+ // instead of just seeing if we can turn it into a ZettelId
317319 let dst_id = ZettelId::try_from(canon_url)?;
318320319321 let link = Link::new(self.id.clone(), dst_id);