1use anyhow::Result;
2
3use super::App;
4use crate::db::Space as SpaceRow;
5
6impl App {
7 pub fn switch_to_default_space(&mut self) -> Result<()> {
10 let default_id = self.db.default_space_id()?;
11 let row = self
12 .db
13 .list_spaces()?
14 .into_iter()
15 .find(|s| s.id == default_id)
16 .ok_or_else(|| anyhow::anyhow!("default space {default_id:?} no longer exists"))?;
17 self.set_active_space(row);
18 Ok(())
19 }
20
21 pub fn set_active_space(&mut self, row: SpaceRow) {
25 self.active_space = row;
26 self.session = None;
27 self.messages.clear();
28 self.context_total = None;
29 self.push_viewport_reset();
30 self.cleanup_incognito_images();
31 self.rescan_files();
32 self.refresh_toolbox();
33 self.push_status(format!("space: {}", self.active_space.name));
34 }
35
36 pub fn instructions_path_for_space(&self, name: &str) -> Option<std::path::PathBuf> {
41 let path = self.space.instructions_path(name);
42 if !path.exists() {
43 let _ = std::fs::write(
44 &path,
45 format!("<!-- instructions for the \"{name}\" space -->\n"),
46 );
47 }
48 Some(path)
49 }
50
51 pub fn memory_path_for_space(&self, name: &str) -> Option<std::path::PathBuf> {
55 let path = self.space.memory_path(name);
56 if !path.exists() {
57 let _ = std::fs::write(
58 &path,
59 format!(
60 "<!-- memory for the \"{name}\" space — numbered facts, one per line -->\n"
61 ),
62 );
63 }
64 Some(path)
65 }
66}