1use std::collections::HashSet;
2use wasm_bindgen::prelude::*;
3
4#[wasm_bindgen]
5extern "C" {
6 #[wasm_bindgen(js_namespace = sessionStorage)]
7 fn setItem(key: &str, value: &str);
8
9 #[wasm_bindgen(js_namespace = sessionStorage)]
10 fn getItem(key: &str) -> Option<String>;
11
12 #[wasm_bindgen(js_namespace = sessionStorage)]
13 fn removeItem(key: &str);
14}
15
16pub struct SessionManager;
18
19impl SessionManager {
20 const EXPANDED_MENUS_KEY: &'static str = "r2mo_expanded_menus";
21
22 pub fn save_expanded_menus(menus: &HashSet<String>) {
24 if menus.is_empty() {
25 removeItem(Self::EXPANDED_MENUS_KEY);
26 return;
27 }
28
29 match serde_json::to_string(menus) {
30 Ok(json) => {
31 setItem(Self::EXPANDED_MENUS_KEY, &json);
32 }
33 Err(_) => {
34 removeItem(Self::EXPANDED_MENUS_KEY);
36 }
37 }
38 }
39
40 pub fn load_expanded_menus() -> HashSet<String> {
42 match getItem(Self::EXPANDED_MENUS_KEY) {
43 Some(json) => serde_json::from_str(&json).unwrap_or_default(),
44 None => HashSet::new(),
45 }
46 }
47
48 pub fn clear_expanded_menus() {
50 removeItem(Self::EXPANDED_MENUS_KEY);
51 }
52}