add create new identity
This commit is contained in:
@@ -2,16 +2,26 @@ use std::sync::Arc;
|
||||
|
||||
use gpui::prelude::*;
|
||||
use gpui::{
|
||||
App, Context, EventEmitter, FocusHandle, Focusable, Render, Subscription, WeakEntity, Window,
|
||||
div,
|
||||
App, Context, EventEmitter, FocusHandle, Focusable, Render, SharedString, Subscription,
|
||||
WeakEntity, Window, div, px,
|
||||
};
|
||||
use gpui_component::button::{Button, ButtonVariants};
|
||||
use gpui_component::dialog::{DialogDescription, DialogFooter, DialogHeader, DialogTitle};
|
||||
use gpui_component::dock::{DockArea, DockPlacement, Panel, PanelEvent};
|
||||
use gpui_component::{ActiveTheme, v_flex};
|
||||
use gpui_component::form::{field, v_form};
|
||||
use gpui_component::input::{Input, InputState};
|
||||
use gpui_component::{ActiveTheme, Disableable, WindowExt, v_flex};
|
||||
use signed_state::{Backend, BackendEvent};
|
||||
|
||||
use crate::views::RepoListView;
|
||||
|
||||
/// Shared state for the Join Now dialog, so async results can be rendered.
|
||||
#[derive(Default)]
|
||||
struct JoinNowState {
|
||||
busy: bool,
|
||||
error: Option<SharedString>,
|
||||
}
|
||||
|
||||
/// Left-dock panel with navigation entries. Entries open content panels in
|
||||
/// the dock area.
|
||||
pub struct SidebarPanel {
|
||||
@@ -68,6 +78,145 @@ impl SidebarPanel {
|
||||
dock_area.add_panel(Arc::new(panel), DockPlacement::Center, None, window, cx);
|
||||
});
|
||||
}
|
||||
|
||||
/// Show the Join Now dialog.
|
||||
fn open_join_now(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let name_input = cx.new(|cx| InputState::new(window, cx).placeholder("Enter desired name"));
|
||||
let pass_input = cx.new(|cx| {
|
||||
InputState::new(window, cx)
|
||||
.placeholder("Passphrase to protect your keys")
|
||||
.masked(true)
|
||||
});
|
||||
let repass_input = cx.new(|cx| {
|
||||
InputState::new(window, cx)
|
||||
.placeholder("Repeat passphrase")
|
||||
.masked(true)
|
||||
});
|
||||
let state = cx.new(|_| JoinNowState::default());
|
||||
|
||||
window.open_dialog(cx, move |dialog, _window, _cx| {
|
||||
let name_input = name_input.clone();
|
||||
let pass_input = pass_input.clone();
|
||||
let repass_input = repass_input.clone();
|
||||
let state = state.clone();
|
||||
|
||||
dialog
|
||||
.width(px(520.))
|
||||
.margin_top(px(50.))
|
||||
.content(move |content, _window, cx| {
|
||||
let busy = state.read(cx).busy;
|
||||
let error = state.read(cx).error.clone();
|
||||
|
||||
content
|
||||
.child(
|
||||
DialogHeader::new()
|
||||
.child(DialogTitle::new().child("Create identity"))
|
||||
.child(
|
||||
DialogDescription::new()
|
||||
.child("Set up your Signed identity to get started."),
|
||||
),
|
||||
)
|
||||
.child(
|
||||
v_form()
|
||||
.child(
|
||||
field()
|
||||
.label("Name")
|
||||
.description("Max 255 characters")
|
||||
.required(true)
|
||||
.child(Input::new(&name_input)),
|
||||
)
|
||||
.child(
|
||||
field()
|
||||
.label("Passphrase")
|
||||
.required(true)
|
||||
.child(Input::new(&pass_input)),
|
||||
)
|
||||
.child(field().required(true).child(Input::new(&repass_input))),
|
||||
)
|
||||
.children(error.map(|message| {
|
||||
div().text_sm().text_color(cx.theme().danger).child(message)
|
||||
}))
|
||||
.child(
|
||||
DialogFooter::new().justify_end().child(
|
||||
Button::new("continue")
|
||||
.primary()
|
||||
.label("Create new identity")
|
||||
.tooltip("Create identity")
|
||||
.loading(busy)
|
||||
.disabled(busy)
|
||||
.on_click({
|
||||
let name_input = name_input.clone();
|
||||
let pass_input = pass_input.clone();
|
||||
let repass_input = repass_input.clone();
|
||||
let state = state.clone();
|
||||
|
||||
move |_ev, window, cx| {
|
||||
let name = name_input.read(cx).value().to_string();
|
||||
let pass = pass_input.read(cx).value().to_string();
|
||||
let repass = repass_input.read(cx).value().to_string();
|
||||
|
||||
if pass != repass {
|
||||
state.update(cx, |state, _| {
|
||||
state.busy = false;
|
||||
state.error =
|
||||
Some("Passphrases do not match".into());
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
state.update(cx, |state, _| {
|
||||
state.busy = true;
|
||||
state.error = None;
|
||||
});
|
||||
|
||||
let rx =
|
||||
Backend::global(cx).update(cx, |backend, cx| {
|
||||
backend.create_identity(&name, &pass, cx)
|
||||
});
|
||||
|
||||
let window_handle = window.window_handle();
|
||||
let state = state.clone();
|
||||
|
||||
cx.spawn(async move |cx| match rx.recv_async().await {
|
||||
Ok(Ok(_)) => {
|
||||
cx.update_window(
|
||||
window_handle,
|
||||
|_, window, cx| {
|
||||
window.close_dialog(cx);
|
||||
},
|
||||
)
|
||||
.ok();
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
cx.update_window(
|
||||
window_handle,
|
||||
|_, _window, cx| {
|
||||
state.update(cx, |state, _| {
|
||||
state.busy = false;
|
||||
state.error =
|
||||
Some(e.to_string().into());
|
||||
});
|
||||
},
|
||||
)
|
||||
.ok();
|
||||
}
|
||||
Err(_) => {}
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
}),
|
||||
),
|
||||
)
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
/// Show the Import Identity dialog.
|
||||
fn open_import(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
window.open_dialog(cx, move |dialog, _window, _cx| {
|
||||
dialog.title("Import identity").width(px(400.))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Panel for SidebarPanel {
|
||||
@@ -114,20 +263,27 @@ impl Render for SidebarPanel {
|
||||
.gap_2()
|
||||
.child(
|
||||
div()
|
||||
.text_xs()
|
||||
.text_sm()
|
||||
.text_color(cx.theme().muted_foreground)
|
||||
.child("Sign in to continue"),
|
||||
)
|
||||
.child(
|
||||
Button::new("get-started")
|
||||
.label("Get started")
|
||||
Button::new("join")
|
||||
.label("Join Now")
|
||||
.primary()
|
||||
.w_full(),
|
||||
.w_full()
|
||||
.on_click(
|
||||
cx.listener(|this, _ev, window, cx| this.open_join_now(window, cx)),
|
||||
),
|
||||
)
|
||||
.child(
|
||||
Button::new("import-identity")
|
||||
.label("Import identity")
|
||||
.w_full(),
|
||||
.secondary()
|
||||
.w_full()
|
||||
.on_click(
|
||||
cx.listener(|this, _ev, window, cx| this.open_import(window, cx)),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user