Skip to content

Commit 165ecf9

Browse files
committed
fix loading yaml in web
1 parent b2a098d commit 165ecf9

File tree

9 files changed

+262
-250
lines changed

9 files changed

+262
-250
lines changed

src/app.rs

Lines changed: 38 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,40 @@
11
use crate::models::{MyLifeApp, Yaml};
22
use crate::ui::{BottomPanel, CentralPanel, TopPanel};
3-
use crate::yaml_manager::get_yaml;
4-
#[cfg(not(target_arch = "wasm32"))]
5-
use crate::yaml_manager::{get_available_yamls, get_yaml_manager};
3+
use crate::yaml_manager::{get_yaml, get_yaml_manager};
64
use dioxus::prelude::*;
75

86
#[cfg(not(target_arch = "wasm32"))]
97
use dioxus_logger::tracing::error;
108

119
#[component]
1210
pub fn App() -> Element {
13-
let mut app_state = use_signal(|| MyLifeApp {
14-
yaml: get_yaml(),
15-
view: "Lifetime".to_string(),
16-
selected_yaml: "default.yaml".to_string(),
17-
selected_legend_item: None,
18-
original_legend_item: None,
19-
selected_life_period: None,
20-
value: 0.0,
21-
#[cfg(not(target_arch = "wasm32"))]
22-
yaml_files: get_available_yamls(),
23-
#[cfg(target_arch = "wasm32")]
24-
yaml_content: String::new(),
25-
loaded_yamls: Vec::new(),
26-
#[cfg(target_arch = "wasm32")]
27-
selected_yaml_index: 0,
28-
item_state: None,
29-
temp_start_date: String::new(),
30-
data_folder: "data".to_string(),
31-
});
11+
let mut yaml = use_signal(|| get_yaml());
3212

33-
use_effect(move || {
34-
#[cfg(not(target_arch = "wasm32"))]
35-
{
36-
let yamls = get_yaml_manager().get_available_yamls();
37-
app_state.write().yaml_files = yamls.unwrap_or_default();
38-
}
13+
let app_state = use_signal(|| {
14+
let loaded_yamls = load_yamls();
3915

40-
#[cfg(not(target_arch = "wasm32"))]
41-
{
42-
let loaded_yamls = load_yamls();
43-
app_state.write().loaded_yamls = loaded_yamls;
16+
MyLifeApp {
17+
view: "Lifetime".to_string(),
18+
selected_yaml: "default.yaml".to_string(),
19+
selected_legend_item: None,
20+
original_legend_item: None,
21+
selected_life_period: None,
22+
value: 0.0,
23+
loaded_yamls,
24+
item_state: None,
25+
temp_start_date: String::new(),
26+
data_folder: "data".to_string(),
4427
}
4528
});
29+
use_effect(move || {
30+
let mut app_state = use_context::<Signal<MyLifeApp>>();
31+
32+
let loaded_yamls = load_yamls();
33+
app_state.write().loaded_yamls = loaded_yamls;
34+
});
4635

4736
use_context_provider(|| app_state);
37+
use_context_provider(|| yaml);
4838

4939
rsx! {
5040
style { {include_str!("../assets/input.css")} }
@@ -62,23 +52,22 @@ pub fn App() -> Element {
6252
}
6353
}
6454

65-
#[cfg(not(target_arch = "wasm32"))]
6655
fn load_yamls() -> Vec<(String, Yaml)> {
67-
let yamls = get_yaml_manager().get_available_yamls();
68-
yamls
69-
.map(|yamls| {
70-
yamls
71-
.into_iter()
72-
.filter_map(|name| {
73-
get_yaml_manager()
74-
.load_yaml(&name)
75-
.map_err(|e| {
76-
error!("Failed to load yaml {}: {:?}", name, e);
77-
})
78-
.ok()
79-
.map(|yaml| (name, yaml))
56+
get_yaml_manager()
57+
.get_available_yamls()
58+
.unwrap_or_default()
59+
.into_iter()
60+
.filter_map(|name| {
61+
get_yaml_manager()
62+
.load_yaml(&name)
63+
.map_err(|e| {
64+
#[cfg(not(target_arch = "wasm32"))]
65+
error!("Failed to load yaml {}: {:?}", name, e);
66+
#[cfg(target_arch = "wasm32")]
67+
log::error!("Failed to load yaml {}: {:?}", name, e);
8068
})
81-
.collect()
69+
.ok()
70+
.map(|yaml| (name, yaml))
8271
})
83-
.unwrap_or_default()
84-
}
72+
.collect()
73+
}

src/models.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,14 @@ use uuid::Uuid;
55
#[serde(default)]
66
pub struct MyLifeApp {
77
pub selected_life_period: Option<Uuid>,
8-
pub yaml: Yaml,
98
pub view: String,
10-
#[cfg(not(target_arch = "wasm32"))]
11-
pub yaml_files: Vec<String>,
12-
#[cfg(target_arch = "wasm32")]
13-
pub yaml_content: String,
149
pub selected_yaml: String,
1510
#[serde(skip)]
1611
pub value: f32,
1712
pub selected_legend_item: Option<LegendItem>,
1813
#[serde(skip)]
1914
pub original_legend_item: Option<LegendItem>,
2015
pub loaded_yamls: Vec<(String, Yaml)>,
21-
#[cfg(target_arch = "wasm32")]
22-
pub selected_yaml_index: usize,
2316
pub item_state: Option<LegendItem>,
2417
pub temp_start_date: String,
2518
pub data_folder: String,

src/ui/bottom_panel.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::models::{LegendItem, MyLifeApp};
1+
use crate::models::{LegendItem, MyLifeApp, Yaml};
22
use crate::ui::{EditLegendItem, Legend};
33
use chrono::{Datelike, Local};
44
use dioxus::prelude::*;
@@ -7,6 +7,8 @@ use uuid::Uuid;
77
#[component]
88
pub fn BottomPanel() -> Element {
99
let mut app_state = use_context::<Signal<MyLifeApp>>();
10+
let yaml_state = use_context::<Signal<Yaml>>();
11+
1012
let add_new_item = move |_| {
1113
let current_view = &app_state().view;
1214
let default_start = if *current_view == "Lifetime" {
@@ -15,8 +17,7 @@ pub fn BottomPanel() -> Element {
1517
} else {
1618
// For EventView, use the start date of the selected life period
1719
if let Some(period_id) = app_state().selected_life_period {
18-
if let Some(period) = app_state()
19-
.yaml
20+
if let Some(period) = yaml_state()
2021
.life_periods
2122
.iter()
2223
.find(|p| p.id == period_id)

src/ui/edit_legend_item.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::models::{LifePeriod, LifePeriodEvent, MyLifeApp};
1+
use crate::models::{LifePeriod, LifePeriodEvent, MyLifeApp, Yaml};
22
use crate::utils::date_utils::is_valid_date;
33
use crate::yaml_manager::update_yaml;
44
use chrono::{Local, NaiveDate};
@@ -12,24 +12,23 @@ fn is_valid_hex_color(color: &str) -> bool {
1212
#[component]
1313
pub fn EditLegendItem() -> Element {
1414
let mut app_state = use_context::<Signal<MyLifeApp>>();
15+
let mut yaml_state = use_context::<Signal<Yaml>>();
1516
let mut color_input = use_signal(String::new);
1617
let mut date_error = use_signal(String::new);
1718
let mut current_date = use_signal(String::new);
1819

1920
let (min_date, max_date) = use_memo(move || {
2021
if let Some(item) = &app_state().item_state {
2122
if item.is_event {
22-
if let Some(period) = app_state()
23-
.yaml
23+
if let Some(period) = yaml_state()
2424
.life_periods
2525
.iter()
2626
.find(|p| p.id == app_state().selected_life_period.unwrap())
2727
{
2828
let period_start =
2929
NaiveDate::parse_from_str(&format!("{}-01", period.start), "%Y-%m-%d")
3030
.unwrap_or_default();
31-
let period_end = app_state()
32-
.yaml
31+
let period_end = yaml_state()
3332
.life_periods
3433
.iter()
3534
.find(|p| p.start > period.start)
@@ -56,17 +55,15 @@ pub fn EditLegendItem() -> Element {
5655
} else {
5756
let default_date = if item.is_event {
5857
debug!("Getting default date for new event");
59-
if let Some(period) = app_state()
60-
.yaml
58+
if let Some(period) = yaml_state()
6159
.life_periods
6260
.iter()
6361
.find(|p| p.id == app_state().selected_life_period.unwrap())
6462
{
6563
let period_start =
6664
NaiveDate::parse_from_str(&format!("{}-01", period.start), "%Y-%m-%d")
6765
.unwrap_or_default();
68-
let period_end = app_state()
69-
.yaml
66+
let period_end = yaml_state()
7067
.life_periods
7168
.iter()
7269
.find(|p| p.start > period.start)
@@ -134,8 +131,9 @@ pub fn EditLegendItem() -> Element {
134131
debug!("Attempting to update yaml item");
135132
if date_error().is_empty() {
136133
if let Some(item) = app_state().item_state {
134+
let mut new_yaml = yaml_state();
135+
137136
debug!("Updating item: {:?}", item);
138-
let mut new_yaml = app_state().yaml.clone();
139137
if item.is_event {
140138
if let Some(period) = new_yaml
141139
.life_periods
@@ -173,9 +171,9 @@ pub fn EditLegendItem() -> Element {
173171
}
174172
new_yaml.life_periods.sort_by(|a, b| a.start.cmp(&b.start));
175173
}
176-
app_state.write().yaml = new_yaml;
174+
yaml_state.set(new_yaml);
177175

178-
let _ = update_yaml(&app_state().yaml, &app_state().selected_yaml);
176+
let _ = update_yaml(&yaml_state(), &app_state().selected_yaml);
179177
debug!("Yaml updated successfully");
180178
} else {
181179
warn!("Attempted to update yaml with no item state");
@@ -258,7 +256,7 @@ pub fn EditLegendItem() -> Element {
258256

259257
let delete_item = move |_| {
260258
if let Some(item) = app_state().item_state {
261-
let mut new_yaml = app_state().yaml.clone();
259+
let mut new_yaml = yaml_state();
262260
if item.is_event {
263261
if let Some(period) = new_yaml
264262
.life_periods
@@ -270,8 +268,8 @@ pub fn EditLegendItem() -> Element {
270268
} else {
271269
new_yaml.life_periods.retain(|p| p.id != item.id);
272270
}
273-
app_state.write().yaml = new_yaml;
274-
let _ = update_yaml(&app_state().yaml, &app_state().selected_yaml);
271+
yaml_state.set(new_yaml);
272+
let _ = update_yaml(&yaml_state(), &app_state().selected_yaml);
275273
}
276274
app_state.write().item_state = None;
277275
app_state.write().temp_start_date = String::new();

src/ui/events_view.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use crate::models::{LifePeriodEvent, MyLifeApp};
1+
use crate::models::{LifePeriodEvent, MyLifeApp, Yaml};
22
use chrono::{Duration, Local, NaiveDate};
33
use dioxus::prelude::*;
44
use uuid::Uuid;
55

66
#[component]
77
pub fn EventView(selected_life_period_id: Uuid) -> Element {
88
let app_state = use_context::<Signal<MyLifeApp>>();
9+
let yaml_state = use_context::<Signal<Yaml>>();
910

1011
let life_period = use_memo(move || {
11-
app_state()
12-
.yaml
12+
yaml_state()
1313
.life_periods
1414
.iter()
1515
.find(|p| p.id == selected_life_period_id)
@@ -38,8 +38,7 @@ pub fn EventView(selected_life_period_id: Uuid) -> Element {
3838
.unwrap_or(Local::now().date_naive())
3939
});
4040

41-
let end_date = app_state()
42-
.yaml
41+
let end_date = yaml_state()
4342
.life_periods
4443
.iter()
4544
.find(|p| p.start > period.start)

src/ui/legend.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use crate::models::{LegendItem, MyLifeApp};
1+
use crate::models::{LegendItem, MyLifeApp, Yaml};
22
use dioxus::prelude::*;
33

44
#[component]
55
pub fn Legend() -> Element {
66
// Fetch signals from context
77
let mut app_state = use_context::<Signal<MyLifeApp>>();
8+
let mut yaml_state = use_context::<Signal<Yaml>>();
89

910
let mut open_edit_modal = move |item: LegendItem| {
1011
app_state.write().item_state = Some(item.clone());
@@ -16,7 +17,7 @@ pub fn Legend() -> Element {
1617
let mut legend_items = Vec::new();
1718
match app_state().view.as_str() {
1819
"Lifetime" => {
19-
let mut sorted_periods = app_state().yaml.life_periods.clone();
20+
let mut sorted_periods = yaml_state().life_periods.clone();
2021
sorted_periods.sort_by(|a, b| a.start.cmp(&b.start));
2122

2223
for period in sorted_periods {
@@ -44,8 +45,7 @@ pub fn Legend() -> Element {
4445
}
4546
"EventView" => {
4647
if let Some(period_id) = app_state().selected_life_period {
47-
if let Some(period) = app_state()
48-
.yaml
48+
if let Some(period) = yaml_state()
4949
.life_periods
5050
.iter()
5151
.find(|p| p.id == period_id)

0 commit comments

Comments
 (0)