|
1 | 1 | 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, |
3 | 6 | };
|
4 | 7 | use leptos::{html::*, *};
|
5 | 8 |
|
6 | 9 | 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 | + }; |
26 | 65 |
|
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 | + }); |
98 | 126 | })
|
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 | + })) |
100 | 136 | })
|
| 137 | + .collect_view() |
| 138 | + }) |
101 | 139 | }
|
102 |
| - |
|
0 commit comments