feat: make the web version fully working (#67)
Reviewed-on: #67
This commit was merged in pull request #67.
This commit is contained in:
@@ -9,7 +9,7 @@ use gpui_kit::component::input::{Input, InputEvent, InputState};
|
||||
use gpui_kit::component::{ActiveTheme, Disableable, WindowExt, v_flex};
|
||||
use instant::Duration;
|
||||
use nostr_connect::prelude::*;
|
||||
use state::NostrRegistry;
|
||||
use state::{NostrRegistry, StateEvent};
|
||||
|
||||
pub fn open(window: &mut Window, cx: &mut App) {
|
||||
let import = cx.new(|cx| ImportIdentity::new(window, cx));
|
||||
@@ -73,12 +73,13 @@ impl ImportIdentity {
|
||||
}
|
||||
|
||||
fn build(mode: Mode, window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||
let nostr = NostrRegistry::global(cx);
|
||||
let key_input = cx.new(|cx| InputState::new(window, cx).placeholder("secret or bunker://"));
|
||||
let pass_input = cx.new(|cx| InputState::new(window, cx).masked(true));
|
||||
let repass_input = cx.new(|cx| InputState::new(window, cx).masked(true));
|
||||
let error = cx.new(|_| None);
|
||||
|
||||
let subscriptions = vec![
|
||||
let mut subscriptions = vec![
|
||||
cx.subscribe_in(&key_input, window, |this, _input, event, _window, cx| {
|
||||
if let InputEvent::PressEnter { .. } = event {
|
||||
this.login(cx);
|
||||
@@ -96,6 +97,14 @@ impl ImportIdentity {
|
||||
}),
|
||||
];
|
||||
|
||||
subscriptions.push(
|
||||
cx.subscribe_in(&nostr, window, |this, _nostr, event, _window, cx| {
|
||||
if let StateEvent::Error(error) = event {
|
||||
this.set_error(error.clone(), cx);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
Self {
|
||||
mode,
|
||||
key_input,
|
||||
@@ -188,6 +197,14 @@ impl ImportIdentity {
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
fn extension(&mut self, cx: &mut Context<Self>) {
|
||||
let nostr = NostrRegistry::global(cx);
|
||||
nostr.update(cx, |this, cx| {
|
||||
this.connect_extension(cx);
|
||||
});
|
||||
}
|
||||
|
||||
fn set_loading(&mut self, status: bool, cx: &mut Context<Self>) {
|
||||
self.loading = status;
|
||||
cx.notify();
|
||||
@@ -226,7 +243,6 @@ impl Render for ImportIdentity {
|
||||
const BUNKER_WARN: &str = "Nostr Connect will usually take more time to get all your messages. Please keep your session open until you see all your messages.";
|
||||
const KEY_WARN: &str = "Coop encrypts your key with your passphrase and stores it on this device. You only need the passphrase to sign in next time.";
|
||||
|
||||
let is_wasm = cfg!(target_arch = "wasm32");
|
||||
let is_unlock = self.mode == Mode::Unlock;
|
||||
|
||||
let value = self.key_input.read(cx).value().trim().to_string();
|
||||
@@ -305,26 +321,45 @@ impl Render for ImportIdentity {
|
||||
)
|
||||
}),
|
||||
)
|
||||
.child(
|
||||
Button::new("login")
|
||||
.label(if is_unlock { "Unlock" } else { "Continue" })
|
||||
.primary()
|
||||
.loading(self.loading)
|
||||
.disabled(self.loading)
|
||||
.on_click(cx.listener(move |this, _ev, _window, cx| {
|
||||
this.login(cx);
|
||||
})),
|
||||
)
|
||||
.when(!is_wasm && !is_unlock, |this| {
|
||||
this.child(
|
||||
Button::new("proxy")
|
||||
.label("Connect via Web Extension (Experimental)")
|
||||
.ghost()
|
||||
.child({
|
||||
let content = v_flex().child(
|
||||
Button::new("login")
|
||||
.label(if is_unlock { "Unlock" } else { "Continue" })
|
||||
.primary()
|
||||
.loading(self.loading)
|
||||
.disabled(self.loading)
|
||||
.on_click(cx.listener(move |this, _ev, _window, cx| {
|
||||
this.proxy(cx);
|
||||
this.login(cx);
|
||||
})),
|
||||
)
|
||||
);
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let content = content.when(!is_unlock, |this| {
|
||||
this.child(
|
||||
Button::new("proxy")
|
||||
.label("Connect via Web Extension (Experimental)")
|
||||
.ghost()
|
||||
.disabled(self.loading)
|
||||
.on_click(cx.listener(move |this, _ev, _window, cx| {
|
||||
this.proxy(cx);
|
||||
})),
|
||||
)
|
||||
});
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let content = content.when(!is_unlock, |this| {
|
||||
this.child(
|
||||
Button::new("extension")
|
||||
.label("Continue with Browser Extension (NIP-07)")
|
||||
.ghost()
|
||||
.disabled(self.loading)
|
||||
.on_click(cx.listener(move |this, _ev, _window, cx| {
|
||||
this.extension(cx);
|
||||
})),
|
||||
)
|
||||
});
|
||||
|
||||
content
|
||||
})
|
||||
.when_some(self.error.read(cx).as_ref(), |this, error| {
|
||||
this.child(
|
||||
|
||||
@@ -2,8 +2,8 @@ use anyhow::Error;
|
||||
use gpui::prelude::FluentBuilder;
|
||||
use gpui::{
|
||||
App, AppContext, Context, Div, Entity, FocusHandle, InteractiveElement as _, IntoElement,
|
||||
ParentElement, PathPromptOptions, Render, SharedString, StatefulInteractiveElement as _,
|
||||
StyleRefinement, Styled, Subscription, Task, WeakEntity, Window, div, px, rems,
|
||||
ParentElement, Render, SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled,
|
||||
Subscription, Task, WeakEntity, Window, div, px, rems,
|
||||
};
|
||||
use gpui_base::{Popup, Select};
|
||||
use gpui_kit::assets::IconName;
|
||||
@@ -18,7 +18,7 @@ use gpui_kit::component::{ActiveTheme, Disableable, Icon, Sizable, WindowExt, h_
|
||||
use nip29::{GroupCreateOptions, GroupKey, GroupsEvent, GroupsRegistry};
|
||||
use nostr_sdk::prelude::*;
|
||||
use settings::AppSettings;
|
||||
use state::upload;
|
||||
use state::{pick_file, upload_file};
|
||||
use ui::{Avatar, settings_row};
|
||||
|
||||
const DEFAULT_RELAYS: [&str; 2] = ["wss://relay.ditto.net", "wss://chat.wisp.talk"];
|
||||
@@ -297,16 +297,11 @@ impl NewGroup {
|
||||
fn upload(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let server = AppSettings::get_file_server(cx);
|
||||
|
||||
let path = cx.prompt_for_paths(PathPromptOptions {
|
||||
files: true,
|
||||
directories: false,
|
||||
multiple: false,
|
||||
prompt: None,
|
||||
});
|
||||
let picker = pick_file(cx);
|
||||
|
||||
self.tasks.push(cx.spawn_in(window, async move |this, cx| {
|
||||
// Selecting no file means the prompt was cancelled
|
||||
let Some(path) = path.await??.and_then(|mut paths| paths.pop()) else {
|
||||
let Some(picked) = picker.await? else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
@@ -314,7 +309,7 @@ impl NewGroup {
|
||||
this.set_uploading(true, cx);
|
||||
})?;
|
||||
|
||||
match upload(server, path, cx).await {
|
||||
match upload_file(server, picked, cx).await {
|
||||
Ok(url) => {
|
||||
this.update(cx, |this, cx| {
|
||||
this.picture = Some(url.to_string().into());
|
||||
|
||||
@@ -4,8 +4,8 @@ use anyhow::Error;
|
||||
use chat::{ChatRegistry, Room, RoomKind};
|
||||
use gpui::prelude::FluentBuilder;
|
||||
use gpui::{
|
||||
App, AppContext, ClipboardItem, Context, Div, Entity, IntoElement, ParentElement,
|
||||
PathPromptOptions, Render, SharedString, Styled, Task, Window, div, retain_all,
|
||||
App, AppContext, ClipboardItem, Context, Div, Entity, IntoElement, ParentElement, Render,
|
||||
SharedString, Styled, Task, Window, div, retain_all,
|
||||
};
|
||||
use gpui_kit::assets::IconName;
|
||||
use gpui_kit::base::StyledExt as _;
|
||||
@@ -18,7 +18,7 @@ use instant::Duration;
|
||||
use nostr_sdk::prelude::*;
|
||||
use person::{Directory, Person, PersonRegistry, shorten_pubkey};
|
||||
use settings::AppSettings;
|
||||
use state::{NostrRegistry, upload};
|
||||
use state::{NostrRegistry, pick_file, upload_file};
|
||||
use ui::Avatar;
|
||||
|
||||
use crate::Workspace;
|
||||
@@ -179,17 +179,12 @@ impl ProfileDialog {
|
||||
// Get the user's configured blossom server
|
||||
let server = AppSettings::get_file_server(cx);
|
||||
|
||||
// Ask user for file upload
|
||||
let path = cx.prompt_for_paths(PathPromptOptions {
|
||||
files: true,
|
||||
directories: false,
|
||||
multiple: false,
|
||||
prompt: None,
|
||||
});
|
||||
// Ask the user for an image to upload
|
||||
let picker = pick_file(cx);
|
||||
|
||||
self.tasks.push(cx.spawn_in(window, async move |this, cx| {
|
||||
// Selecting no file means the prompt was cancelled
|
||||
let Some(path) = path.await??.and_then(|mut paths| paths.pop()) else {
|
||||
let Some(picked) = picker.await? else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
@@ -198,7 +193,7 @@ impl ProfileDialog {
|
||||
})?;
|
||||
|
||||
// Upload via blossom client
|
||||
match upload(server, path, cx).await {
|
||||
match upload_file(server, picked, cx).await {
|
||||
Ok(url) => {
|
||||
this.update_in(cx, |this, window, cx| {
|
||||
this.avatar_input.update(cx, |this, cx| {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use gpui::PathPromptOptions;
|
||||
use gpui::http_client::Url;
|
||||
use gpui::{
|
||||
App, AppContext, Context, Entity, IntoElement, ParentElement, PathPromptOptions, Render,
|
||||
Styled, Subscription, Window, div,
|
||||
App, AppContext, Context, Entity, IntoElement, ParentElement, Render, Styled, Subscription,
|
||||
Window, div,
|
||||
};
|
||||
use gpui_kit::assets::IconName;
|
||||
use gpui_kit::component::button::{Button, ButtonVariants};
|
||||
@@ -150,29 +152,41 @@ impl Preferences {
|
||||
}
|
||||
|
||||
/// Ask the user for an image to show behind chat and group panels
|
||||
fn choose_background(cx: &mut App) {
|
||||
let task = cx.prompt_for_paths(PathPromptOptions {
|
||||
files: true,
|
||||
directories: false,
|
||||
multiple: false,
|
||||
prompt: None,
|
||||
});
|
||||
fn choose_background(
|
||||
#[cfg_attr(not(target_arch = "wasm32"), allow(unused_variables))] window: &mut Window,
|
||||
cx: &mut App,
|
||||
) {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
let note = Notification::warning("Background images are not supported on web yet.");
|
||||
window.push_notification(note, cx);
|
||||
}
|
||||
|
||||
cx.spawn(async move |cx| {
|
||||
let Ok(Ok(Some(mut paths))) = task.await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let Some(path) = paths.pop() else {
|
||||
return;
|
||||
};
|
||||
|
||||
cx.update(|cx| {
|
||||
AppSettings::update_panel_background_path(Some(path), cx);
|
||||
cx.refresh_windows();
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let task = cx.prompt_for_paths(PathPromptOptions {
|
||||
files: true,
|
||||
directories: false,
|
||||
multiple: false,
|
||||
prompt: None,
|
||||
});
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.spawn(async move |cx| {
|
||||
let Ok(Ok(Some(mut paths))) = task.await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let Some(path) = paths.pop() else {
|
||||
return;
|
||||
};
|
||||
|
||||
cx.update(|cx| {
|
||||
AppSettings::update_panel_background_path(Some(path), cx);
|
||||
cx.refresh_windows();
|
||||
});
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,8 +344,8 @@ impl Render for Preferences {
|
||||
.ghost()
|
||||
.small()
|
||||
.disabled(!panel_background)
|
||||
.on_click(|_, _window, cx| {
|
||||
Self::choose_background(cx);
|
||||
.on_click(|_, window, cx| {
|
||||
Self::choose_background(window, cx);
|
||||
}),
|
||||
)
|
||||
.child(
|
||||
|
||||
@@ -54,6 +54,7 @@ impl Render for DragPanelPreview {
|
||||
.cursor_grab()
|
||||
.p_1()
|
||||
.whitespace_nowrap()
|
||||
.font_family(cx.theme().font_family.clone())
|
||||
.border_1()
|
||||
.border_color(cx.theme().border)
|
||||
.rounded(cx.theme().radius_lg)
|
||||
|
||||
+53
-26
@@ -2,8 +2,10 @@ use std::sync::Arc;
|
||||
|
||||
use ::settings::{AppSettings, AppearanceMode};
|
||||
use anyhow::Error;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use auto_update::AutoUpdater;
|
||||
use chat::{ChatEvent, ChatRegistry};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use common::download_dir;
|
||||
use gpui::{
|
||||
Action, App, AppContext, Context, Entity, InteractiveElement, IntoElement, ParentElement,
|
||||
@@ -337,34 +339,57 @@ impl Workspace {
|
||||
}
|
||||
Command::BackupEncryption => {
|
||||
let device = DeviceRegistry::global(cx).downgrade();
|
||||
let save_dialog = cx.prompt_for_new_path(download_dir(), Some("encryption.txt"));
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let save_dialog =
|
||||
cx.prompt_for_new_path(download_dir(), Some("encryption.txt"));
|
||||
|
||||
self.tasks.push(cx.spawn_in(window, async move |_this, cx| {
|
||||
// Get the output path from the save dialog
|
||||
let output_path = match save_dialog.await {
|
||||
Ok(Ok(Some(path))) => path,
|
||||
Ok(Ok(None)) | Err(_) => return Ok(()),
|
||||
Ok(Err(error)) => {
|
||||
cx.update(|window, cx| {
|
||||
let message =
|
||||
format!("Failed to pick save location: {error:#}");
|
||||
let note = Notification::error(message).autohide(false);
|
||||
window.push_notification(note, cx);
|
||||
})?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
// Get the backup task
|
||||
let backup = device
|
||||
.read_with(cx, |this, cx| this.backup(output_path.clone(), cx))?;
|
||||
|
||||
// Run the backup task
|
||||
backup.await?;
|
||||
|
||||
// Open the backup file with the system's default application
|
||||
cx.update(|_window, cx| {
|
||||
cx.open_with_system(output_path.as_path());
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}));
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
self.tasks.push(cx.spawn_in(window, async move |_this, cx| {
|
||||
// Get the output path from the save dialog
|
||||
let output_path = match save_dialog.await {
|
||||
Ok(Ok(Some(path))) => path,
|
||||
Ok(Ok(None)) | Err(_) => return Ok(()),
|
||||
Ok(Err(error)) => {
|
||||
cx.update(|window, cx| {
|
||||
let message = format!("Failed to pick save location: {error:#}");
|
||||
let note = Notification::error(message).autohide(false);
|
||||
window.push_notification(note, cx);
|
||||
})?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let backup = device.read_with(cx, |this, cx| this.backup_bytes(cx))?;
|
||||
let content = backup.await?;
|
||||
|
||||
// Get the backup task
|
||||
let backup =
|
||||
device.read_with(cx, |this, cx| this.backup(output_path.clone(), cx))?;
|
||||
|
||||
// Run the backup task
|
||||
backup.await?;
|
||||
|
||||
// Open the backup file with the system's default application
|
||||
cx.update(|_window, cx| {
|
||||
cx.open_with_system(output_path.as_path());
|
||||
})?;
|
||||
cx.update(|_window, _cx| {
|
||||
state::web_file::download(
|
||||
"encryption.txt",
|
||||
"text/plain",
|
||||
content.as_bytes(),
|
||||
)
|
||||
})
|
||||
.map_err(|e| anyhow::anyhow!(e))??;
|
||||
|
||||
Ok(())
|
||||
}));
|
||||
@@ -372,7 +397,9 @@ impl Workspace {
|
||||
Command::ImportEncryption => {
|
||||
self.import_encryption(window, cx);
|
||||
}
|
||||
Command::Update => {
|
||||
Command::Update =>
|
||||
{
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if let Some(auto_updater) = AutoUpdater::try_global(cx) {
|
||||
auto_updater.update(cx, |this, cx| this.check(cx));
|
||||
}
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use auto_update::AutoUpdater;
|
||||
use gpui::prelude::FluentBuilder;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use gpui::{AnyElement, Entity, IntoElement};
|
||||
use gpui::{
|
||||
AnyElement, App, Context, Div, Entity, InteractiveElement, IntoElement, ParentElement,
|
||||
Stateful, Styled, Window, div, px,
|
||||
App, Context, Div, InteractiveElement, ParentElement, Stateful, Styled, Window, div, px,
|
||||
};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use gpui_kit::component::Disableable;
|
||||
use gpui_kit::component::button::{Button, ButtonVariant, ButtonVariants};
|
||||
use gpui_kit::component::dialog::DialogButtonProps;
|
||||
use gpui_kit::component::menu::{DropdownMenu, PopupMenuItem};
|
||||
use gpui_kit::component::{
|
||||
ActiveTheme, Disableable, Icon, Selectable, Sizable, WindowExt, h_flex, v_flex,
|
||||
};
|
||||
use gpui_kit::component::{ActiveTheme, Icon, Selectable, Sizable, WindowExt, h_flex, v_flex};
|
||||
use nostr_sdk::prelude::*;
|
||||
use state::NostrRegistry;
|
||||
use ui::{Avatar, IconName};
|
||||
@@ -25,6 +27,7 @@ impl Sidebar {
|
||||
current_user: &PublicKey,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Stateful<Div> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let updater = AutoUpdater::try_global(cx);
|
||||
let profile = self.directory.read(cx).person(current_user, cx);
|
||||
let avatar = profile.avatar();
|
||||
@@ -62,12 +65,15 @@ impl Sidebar {
|
||||
)
|
||||
}),
|
||||
)
|
||||
.child(
|
||||
h_flex()
|
||||
.gap_1()
|
||||
.when_some(updater, |this, updater| {
|
||||
this.child(self.render_updater(updater, cx))
|
||||
})
|
||||
.child({
|
||||
let actions = h_flex().gap_1();
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let actions = actions.when_some(updater, |this, updater| {
|
||||
this.child(self.render_updater(updater, cx))
|
||||
});
|
||||
|
||||
actions
|
||||
.child(
|
||||
Button::new("sidebar-search")
|
||||
.icon(IconName::Search)
|
||||
@@ -104,8 +110,8 @@ impl Sidebar {
|
||||
this.hide_group(cx);
|
||||
})),
|
||||
)
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// The bottom navigation, pinned below the list like signed's sidebar.
|
||||
@@ -128,6 +134,7 @@ impl Sidebar {
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn render_updater(&self, updater: Entity<AutoUpdater>, cx: &mut App) -> AnyElement {
|
||||
let status = updater.read(cx).status();
|
||||
let up_to_date = updater.read(cx).up_to_date();
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use gpui::{AnyElement, App, IntoElement, ParentElement, SharedString, Styled, Window, img};
|
||||
use gpui_kit::component::button::{Button, ButtonVariants};
|
||||
use gpui_kit::component::v_flex;
|
||||
use instant::Instant;
|
||||
|
||||
use crate::dialogs::import;
|
||||
|
||||
@@ -11,12 +10,8 @@ const SIGNED_OUT_BANNERS: [&str; 2] = ["brand/bg1.jpg", "brand/bg2.jpg"];
|
||||
|
||||
/// Pick one of the signed-out backgrounds at random.
|
||||
pub(super) fn pick_banner() -> SharedString {
|
||||
let nanos = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|elapsed| elapsed.subsec_nanos())
|
||||
.unwrap_or_default();
|
||||
let nanos = Instant::now().elapsed().subsec_nanos();
|
||||
let index = nanos as usize % SIGNED_OUT_BANNERS.len();
|
||||
|
||||
SIGNED_OUT_BANNERS[index].into()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user