Skip to content

Commit c8c1069

Browse files
committed
feat: upgrade sidebar
1 parent 844f7fa commit c8c1069

File tree

10 files changed

+371
-276
lines changed

10 files changed

+371
-276
lines changed

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rust-sql-gui-ui"
3-
version = "0.0.0"
3+
version = "1.0.0-alpha.1"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -18,5 +18,3 @@ monaco = "0.4.0"
1818

1919
[workspace]
2020
members = ["src-tauri"]
21-
22-

leptosfmt.toml

Lines changed: 0 additions & 5 deletions
This file was deleted.

rustfmt.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
edition = "2021"
1+
edition = "2021"
2+
max_width = 100 # Maximum width of each line
3+
tab_spaces = 2 # Number of spaces per tab

src-tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rust-sql-gui"
3-
version = "0.0.0"
3+
version = "1.0.0-alpha.1"
44
description = "A Tauri App"
55
authors = ["you"]
66
license = ""

src/db_connector.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@ use leptos::{html::*, *};
33
use crate::store::{db::DBStore, query::QueryState};
44

55
pub fn db_connector() -> impl IntoView {
6-
let db = use_context::<DBStore>().unwrap();
7-
let refetch_projects = use_context::<Resource<(), Vec<String>>>().unwrap();
8-
let connect = create_action(move |db: &DBStore| {
9-
let mut db_clone = *db;
10-
async move {
11-
db_clone.connect().await;
12-
refetch_projects.refetch();
13-
}
14-
});
15-
let query_state = use_context::<QueryState>().unwrap();
16-
let run_query = create_action(move |query_state: &QueryState| {
17-
let query_state = *query_state;
18-
async move { query_state.run_query().await }
19-
});
6+
let db = use_context::<DBStore>().unwrap();
7+
let connect = create_action(move |db: &DBStore| {
8+
let mut db_clone = *db;
9+
async move {
10+
db_clone.connect().await;
11+
}
12+
});
13+
let query_state = use_context::<QueryState>().unwrap();
14+
let run_query = create_action(move |query_state: &QueryState| {
15+
let query_state = *query_state;
16+
async move { query_state.run_query().await }
17+
});
2018

21-
header()
19+
header()
2220
.attr(
2321
"class",
2422
"flex flex-row justify-between p-4 gap-2 border-b-1 border-neutral-200",
@@ -108,4 +106,3 @@ pub fn db_connector() -> impl IntoView {
108106
),
109107
)
110108
}
111-

src/invoke.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,55 @@
1+
use std::fmt::Display;
2+
13
use serde::{Deserialize, Serialize};
24

5+
#[allow(non_camel_case_types)]
6+
pub enum Invoke {
7+
get_projects,
8+
get_project_details,
9+
remove_project,
10+
get_sql_result,
11+
get_schema_tables,
12+
pg_connector,
13+
}
14+
15+
impl Display for Invoke {
16+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17+
match self {
18+
Invoke::get_projects => write!(f, "get_projects"),
19+
Invoke::get_project_details => write!(f, "get_project_details"),
20+
Invoke::remove_project => write!(f, "remove_project"),
21+
Invoke::get_sql_result => write!(f, "get_sql_result"),
22+
Invoke::get_schema_tables => write!(f, "get_schema_tables"),
23+
Invoke::pg_connector => write!(f, "pg_connector"),
24+
}
25+
}
26+
}
27+
328
#[derive(Serialize, Deserialize)]
429
pub struct InvokePostgresConnectionArgs {
5-
pub project: String,
6-
pub key: String,
30+
pub project: String,
31+
pub key: String,
732
}
833

934
#[derive(Serialize, Deserialize)]
1035
pub struct InvokeTablesArgs {
11-
pub schema: String,
36+
pub schema: String,
1237
}
1338

1439
#[derive(Serialize, Deserialize)]
1540
pub struct InvokeQueryArgs {
16-
pub sql: String,
41+
pub sql: String,
1742
}
1843

1944
#[derive(Serialize, Deserialize)]
2045
pub struct InvokeProjectsArgs;
2146

2247
#[derive(Serialize, Deserialize)]
2348
pub struct InvokeProjectDetailsArgs {
24-
pub project: String,
49+
pub project: String,
2550
}
2651

52+
#[derive(Serialize, Deserialize)]
53+
pub struct InvokeRemoveProjectArgs {
54+
pub project: String,
55+
}

src/sidebar.rs

Lines changed: 130 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,139 @@
11
use crate::{
2-
invoke::InvokeProjectsArgs, store::db::DBStore, tables::tables, wasm_functions::invoke,
2+
invoke::{Invoke, InvokeProjectsArgs},
3+
store::db::DBStore,
4+
tables::tables,
5+
wasm_functions::invoke,
36
};
47
use leptos::{html::*, *};
58

69
pub fn sidebar() -> impl IntoView {
7-
let db = use_context::<DBStore>().unwrap();
8-
let get_project_details = create_action(move |(db, project): &(DBStore, String)| {
9-
let mut db_clone = *db;
10-
let project = project.clone();
11-
async move { db_clone.get_project_details(project).await }
12-
});
13-
let projects = create_resource(
14-
|| {},
15-
move |_| async move {
16-
let projects = invoke(
17-
"get_projects",
18-
serde_wasm_bindgen::to_value(&InvokeProjectsArgs).unwrap(),
19-
)
20-
.await;
21-
let projects = serde_wasm_bindgen::from_value::<Vec<String>>(projects).unwrap();
22-
projects
23-
},
24-
);
25-
provide_context(projects);
10+
let mut db = use_context::<DBStore>().unwrap();
11+
let get_project_details = create_action(move |(db, project): &(DBStore, String)| {
12+
let mut db_clone = *db;
13+
let project = project.clone();
14+
async move { db_clone.get_project_details(project).await }
15+
});
16+
let projects = create_resource(
17+
move || db.is_connecting.get(),
18+
move |_| async move {
19+
let projects = invoke(
20+
&Invoke::get_projects.to_string(),
21+
serde_wasm_bindgen::to_value(&InvokeProjectsArgs).unwrap_or_default(),
22+
)
23+
.await;
24+
serde_wasm_bindgen::from_value::<Vec<String>>(projects).unwrap()
25+
},
26+
);
27+
let remove_project = create_action(move |db: &DBStore| {
28+
let mut db_clone = *db;
29+
async move {
30+
db_clone.remove_project().await.unwrap();
31+
projects.refetch();
32+
}
33+
});
34+
let projects_result = move || {
35+
projects
36+
.get()
37+
.unwrap_or_default()
38+
.into_iter()
39+
.enumerate()
40+
.map(|(idx, project)| {
41+
div()
42+
.attr("key", idx)
43+
.attr("class", "flex flex-row justify-between items-center")
44+
.child(
45+
button()
46+
.attr("class", "hover:font-semibold")
47+
.child(&project)
48+
.on(ev::click, {
49+
let project = project.clone();
50+
move |_| get_project_details.dispatch((db, project.clone()))
51+
}),
52+
)
53+
.child(
54+
button()
55+
.attr("class", "px-2 rounded-full hover:bg-gray-200")
56+
.child("-")
57+
.on(ev::click, move |_| {
58+
remove_project.dispatch(db);
59+
projects.update(|prev| prev.as_mut().unwrap().retain(|p| p != &project.clone()));
60+
}),
61+
)
62+
})
63+
.collect_view()
64+
};
2665

27-
div()
28-
.attr(
29-
"class",
30-
"flex border-r-1 border-neutral-200 flex-col gap-2 px-4 pt-4 overflow-auto",
31-
)
32-
.child(Suspense(SuspenseProps {
33-
children: ChildrenFn::to_children(move || {
34-
Fragment::new(vec![div()
35-
.child(p().attr("class", "font-semibold").child("Projects"))
36-
.child(move || {
37-
projects
38-
.get()
39-
.unwrap_or(Vec::new())
40-
.into_iter()
41-
.enumerate()
42-
.map(|(idx, project)| {
43-
button()
44-
.attr("key", idx)
45-
.attr("class", "hover:font-semibold")
46-
.child(&project)
47-
.on(ev::click, move |_| {
48-
get_project_details.dispatch((db.clone(), project.clone()))
49-
})
50-
})
51-
.collect_view()
52-
})
53-
.into_view()])
54-
}),
55-
fallback: ViewFn::from(|| p().child("Loading...")),
56-
}))
57-
.child(p().attr("class", "font-semibold").child("Schemas"))
58-
.child(Show(ShowProps {
59-
when: move || db.is_connecting.get(),
60-
children: ChildrenFn::to_children(move || {
61-
Fragment::new(vec![p().child("Loading...").into_view()])
62-
}),
63-
fallback: ViewFn::from(div),
64-
}))
65-
.child(move || {
66-
db.schemas
67-
.get()
68-
.into_iter()
69-
.map(|(schema, toggle)| {
70-
let s = schema.clone();
71-
div()
72-
.attr("key", &schema)
73-
.child(
74-
button()
75-
.attr(
76-
"class",
77-
if toggle {
78-
"font-semibold"
79-
} else {
80-
"hover:font-semibold"
81-
},
82-
)
83-
.on(ev::click, move |_| {
84-
let s_clone = s.clone();
85-
db.schemas.update(move |prev| {
86-
prev.insert(s_clone, !toggle);
87-
});
88-
})
89-
.child(&schema),
90-
)
91-
.child(Show(ShowProps {
92-
when: move || toggle,
93-
children: ChildrenFn::to_children(move || {
94-
Fragment::new(vec![tables(schema.clone()).into_view()])
95-
}),
96-
fallback: ViewFn::from(div),
97-
}))
66+
div()
67+
.attr(
68+
"class",
69+
"flex border-r-1 min-w-[200px] border-neutral-200 flex-col gap-2 px-4 pt-4 overflow-auto",
70+
)
71+
.child(
72+
div()
73+
.attr("class", "flex w-full flex-row justify-between items-center")
74+
.child(p().attr("class", "font-semibold").child("Projects"))
75+
.child(
76+
button()
77+
.attr("class", "px-2 rounded-full hover:bg-gray-200")
78+
.child("+")
79+
.on(ev::click, move |_| db.reset()),
80+
),
81+
)
82+
.child(Suspense(SuspenseProps {
83+
children: ChildrenFn::to_children(move || {
84+
Fragment::new(vec![Show(ShowProps {
85+
when: move || !projects.get().unwrap_or_default().is_empty(),
86+
fallback: ViewFn::from(|| p().attr("class", "text-xs").child("No projects")),
87+
children: ChildrenFn::to_children(move || {
88+
Fragment::new(vec![projects_result.into_view()])
89+
}),
90+
})
91+
.into_view()])
92+
}),
93+
fallback: ViewFn::from(|| p().child("Loading...")),
94+
}))
95+
.child(p().attr("class", "font-semibold").child("Schemas"))
96+
.child(Show(ShowProps {
97+
when: move || db.is_connecting.get(),
98+
children: ChildrenFn::to_children(move || {
99+
Fragment::new(vec![p().child("Loading...").into_view()])
100+
}),
101+
fallback: ViewFn::from(div),
102+
}))
103+
.child(move || {
104+
db.schemas
105+
.get()
106+
.into_iter()
107+
.map(|(schema, toggle)| {
108+
let s = schema.clone();
109+
div()
110+
.attr("key", &schema)
111+
.child(
112+
button()
113+
.attr(
114+
"class",
115+
if toggle {
116+
"font-semibold"
117+
} else {
118+
"hover:font-semibold"
119+
},
120+
)
121+
.on(ev::click, move |_| {
122+
let s_clone = s.clone();
123+
db.schemas.update(move |prev| {
124+
prev.insert(s_clone, !toggle);
125+
});
98126
})
99-
.collect_view()
127+
.child(&schema),
128+
)
129+
.child(Show(ShowProps {
130+
when: move || toggle,
131+
children: ChildrenFn::to_children(move || {
132+
Fragment::new(vec![tables(schema.clone()).into_view()])
133+
}),
134+
fallback: ViewFn::from(div),
135+
}))
100136
})
137+
.collect_view()
138+
})
101139
}
102-

0 commit comments

Comments
 (0)