|
| 1 | +use std::collections::BTreeMap; |
| 2 | + |
| 3 | +use leptos::*; |
| 4 | +use tauri_sys::tauri::invoke; |
| 5 | + |
| 6 | +use crate::invoke::{Invoke, InvokeQueryDbDeleteArgs, InvokeQueryDbInsertArgs}; |
| 7 | + |
| 8 | +use super::tabs::TabsStore; |
| 9 | + |
| 10 | +#[derive(Clone, Copy, Debug)] |
| 11 | +pub struct QueriesStore(pub RwSignal<BTreeMap<String, String>>); |
| 12 | + |
| 13 | +impl Default for QueriesStore { |
| 14 | + fn default() -> Self { |
| 15 | + Self::new() |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +impl QueriesStore { |
| 20 | + #[must_use] |
| 21 | + pub fn new() -> Self { |
| 22 | + Self(create_rw_signal(BTreeMap::new())) |
| 23 | + } |
| 24 | + |
| 25 | + pub async fn load_queries(&self) { |
| 26 | + let saved_queries = invoke::<_, BTreeMap<String, String>>(Invoke::QueryDbSelect.as_ref(), &()) |
| 27 | + .await |
| 28 | + .unwrap(); |
| 29 | + self.0.update(|prev| { |
| 30 | + *prev = saved_queries.into_iter().collect(); |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + pub async fn insert_query(&self, project_id: &str, title: &str) { |
| 35 | + let tabs_store = expect_context::<TabsStore>(); |
| 36 | + let sql = tabs_store.select_active_editor_value(); |
| 37 | + let _ = invoke::<_, ()>( |
| 38 | + Invoke::QueryDbInsert.as_ref(), |
| 39 | + &InvokeQueryDbInsertArgs { |
| 40 | + query_id: &format!("{}:{}", project_id, title), |
| 41 | + sql: &sql, |
| 42 | + }, |
| 43 | + ) |
| 44 | + .await; |
| 45 | + self.load_queries().await; |
| 46 | + } |
| 47 | + |
| 48 | + pub async fn delete_query(&self, query_id: &str) { |
| 49 | + let _ = invoke::<_, ()>( |
| 50 | + Invoke::QueryDbDelete.as_ref(), |
| 51 | + &InvokeQueryDbDeleteArgs { query_id }, |
| 52 | + ) |
| 53 | + .await; |
| 54 | + self.load_queries().await; |
| 55 | + } |
| 56 | +} |
| 57 | + |
0 commit comments