feat: add basic profile panel
This commit is contained in:
@@ -0,0 +1,274 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
AnyElement, App, AppContext as _, Context, Entity, IntoElement, ParentElement as _, Pixels,
|
||||
Render, ScrollHandle, SharedString, Styled as _, Subscription, Task, WeakEntity, Window, div,
|
||||
px,
|
||||
};
|
||||
use helpers::{
|
||||
ActivityItem, ActivityRow, RepositoryItem, RepositoryRow, activity_items, repository_items,
|
||||
};
|
||||
use metadata::ProfileDirectory;
|
||||
use nostr::prelude::PublicKey;
|
||||
use signed_core::{Deletions, Filters};
|
||||
use signed_state::{Backend, BackendEvent, RepoListStore};
|
||||
use ui::avatar::Avatar;
|
||||
use ui::common::{Sizable as _, StyledExt as _, v_flex};
|
||||
use ui::empty_state::EmptyState;
|
||||
use ui::layout::panel::{Panel, PanelSlot, PanelStack};
|
||||
use ui::scroll_area::ScrollArea;
|
||||
use ui::theme::ActiveTheme as _;
|
||||
|
||||
use crate::app::AppRoot;
|
||||
use crate::shared::log_dropped;
|
||||
|
||||
mod helpers;
|
||||
|
||||
const AVATAR_SIDE: Pixels = px(96.);
|
||||
/// The centered content width, so the profile does not stretch across a wide window.
|
||||
const CONTENT_WIDTH: Pixels = px(720.);
|
||||
|
||||
/// A user's identity, announced repositories, and recent activity.
|
||||
pub struct ProfileView {
|
||||
scroll_handle: ScrollHandle,
|
||||
public_key: PublicKey,
|
||||
workspace: WeakEntity<AppRoot>,
|
||||
directory: Entity<ProfileDirectory>,
|
||||
store: Entity<RepoListStore>,
|
||||
repositories: Arc<Vec<RepositoryItem>>,
|
||||
activities: Arc<Vec<ActivityItem>>,
|
||||
activity_request: u64,
|
||||
activity_task: Option<Task<()>>,
|
||||
_subscriptions: Vec<Subscription>,
|
||||
}
|
||||
|
||||
impl ProfileView {
|
||||
/// Opens the profile of `public_key` in the main stack, keyed by that key.
|
||||
pub(crate) fn open(
|
||||
panels: &Entity<PanelStack>,
|
||||
workspace: WeakEntity<AppRoot>,
|
||||
public_key: PublicKey,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) {
|
||||
let id = SharedString::from(format!("profile:{}", public_key.to_hex()));
|
||||
panels.update(cx, |panels, cx| {
|
||||
panels.open(
|
||||
PanelSlot::Main,
|
||||
id,
|
||||
move |_, cx| cx.new(|cx| Self::new(workspace.clone(), public_key, cx)),
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
fn new(workspace: WeakEntity<AppRoot>, public_key: PublicKey, cx: &mut Context<Self>) -> Self {
|
||||
let backend = Backend::global(cx);
|
||||
let store = RepoListStore::global(cx);
|
||||
let directory = ProfileDirectory::new(cx);
|
||||
|
||||
directory.update(cx, |directory, cx| {
|
||||
directory.track([public_key], cx);
|
||||
});
|
||||
|
||||
let repositories = Arc::new(repository_items(store.read(cx), &public_key));
|
||||
let weak = cx.weak_entity();
|
||||
let mut subscriptions = vec![];
|
||||
|
||||
subscriptions.push(cx.observe(&store, |this, _store, cx| {
|
||||
this.sync_repositories(cx);
|
||||
}));
|
||||
|
||||
subscriptions.push(
|
||||
cx.subscribe(&backend, |this, _backend, event, cx| match event {
|
||||
BackendEvent::Synced => {
|
||||
this.reload_activity(cx);
|
||||
}
|
||||
BackendEvent::RepoUpdates(updates)
|
||||
if updates.iter().any(|n| n.author == this.public_key) =>
|
||||
{
|
||||
this.reload_activity(cx);
|
||||
}
|
||||
_ => {}
|
||||
}),
|
||||
);
|
||||
|
||||
backend.update(cx, |backend, cx| {
|
||||
backend.sync_bootstraps(vec![Filters::authored_activity(public_key)], cx);
|
||||
});
|
||||
|
||||
cx.defer(move |cx| {
|
||||
log_dropped(weak.update(cx, |this, cx| {
|
||||
this.reload_activity(cx);
|
||||
}));
|
||||
});
|
||||
|
||||
Self {
|
||||
public_key,
|
||||
workspace,
|
||||
directory,
|
||||
store,
|
||||
repositories,
|
||||
activities: Arc::new(Vec::new()),
|
||||
activity_request: 0,
|
||||
activity_task: None,
|
||||
scroll_handle: ScrollHandle::new(),
|
||||
_subscriptions: subscriptions,
|
||||
}
|
||||
}
|
||||
|
||||
/// Queries the author's local activity, ignoring results a newer query replaced.
|
||||
fn reload_activity(&mut self, cx: &mut Context<Self>) {
|
||||
self.activity_request = self.activity_request.wrapping_add(1);
|
||||
let request = self.activity_request;
|
||||
let author = self.public_key;
|
||||
let backend = Backend::global(cx);
|
||||
let client = backend.read(cx).client();
|
||||
|
||||
let work = cx.background_spawn(async move {
|
||||
let events = client
|
||||
.database()
|
||||
.query(Filters::authored_activity(author))
|
||||
.await?;
|
||||
|
||||
let deletion_events = client.database().query(Filters::deletions()).await?;
|
||||
let deletions = Deletions::from_events(deletion_events);
|
||||
|
||||
Ok::<_, anyhow::Error>(activity_items(author, events, &deletions))
|
||||
});
|
||||
|
||||
let task = cx.spawn(async move |this, cx| {
|
||||
let items = match work.await {
|
||||
Ok(items) => items,
|
||||
Err(error) => {
|
||||
log::warn!("failed to load profile activity: {error}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
log_dropped(this.update(cx, |this, cx| {
|
||||
if this.activity_request != request {
|
||||
return;
|
||||
}
|
||||
this.activities = Arc::new(items);
|
||||
cx.notify();
|
||||
}));
|
||||
});
|
||||
|
||||
self.activity_task = Some(task);
|
||||
}
|
||||
|
||||
fn sync_repositories(&mut self, cx: &mut Context<Self>) {
|
||||
let repositories = repository_items(self.store.read(cx), &self.public_key);
|
||||
self.repositories = Arc::new(repositories);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn render_identity(&self, cx: &App) -> impl IntoElement {
|
||||
let profile = self.directory.read(cx).profile(&self.public_key, cx);
|
||||
let name = profile.name();
|
||||
let about = profile.about();
|
||||
|
||||
v_flex()
|
||||
.w_full()
|
||||
.items_center()
|
||||
.text_center()
|
||||
.gap_3()
|
||||
.child(
|
||||
Avatar::new(self.public_key)
|
||||
.name(name.clone())
|
||||
.picture(profile.picture())
|
||||
.fully_rounded(true)
|
||||
.size(AVATAR_SIDE),
|
||||
)
|
||||
.child(div().text_xl().font_semibold().child(name))
|
||||
.when_some(about, |this, about| {
|
||||
this.child(
|
||||
div()
|
||||
.max_w(px(480.))
|
||||
.text_sm()
|
||||
.text_color(cx.theme().colors.muted_foreground)
|
||||
.child(about),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn render_repositories(&self, _cx: &App) -> impl IntoElement {
|
||||
v_flex()
|
||||
.w_full()
|
||||
.gap_3()
|
||||
.child(
|
||||
div()
|
||||
.w_full()
|
||||
.text_center()
|
||||
.text_sm()
|
||||
.font_semibold()
|
||||
.child("Repositories"),
|
||||
)
|
||||
.when(self.repositories.is_empty(), |this| {
|
||||
this.child(EmptyState::new("No repositories yet"))
|
||||
})
|
||||
.children(
|
||||
self.repositories
|
||||
.iter()
|
||||
.map(|item| RepositoryRow::new(item, &self.workspace)),
|
||||
)
|
||||
}
|
||||
|
||||
fn render_activity(&self, _cx: &App) -> impl IntoElement {
|
||||
v_flex()
|
||||
.w_full()
|
||||
.gap_3()
|
||||
.child(
|
||||
div()
|
||||
.w_full()
|
||||
.text_center()
|
||||
.text_sm()
|
||||
.font_semibold()
|
||||
.child("Recent activity"),
|
||||
)
|
||||
.when(self.activities.is_empty(), |this| {
|
||||
this.child(EmptyState::new("No activity yet"))
|
||||
})
|
||||
.children(self.activities.iter().cloned().map(ActivityRow::new))
|
||||
}
|
||||
}
|
||||
|
||||
impl Panel for ProfileView {
|
||||
fn title(&self, _cx: &App) -> SharedString {
|
||||
SharedString::from("Profile")
|
||||
}
|
||||
|
||||
fn prefix(&self, cx: &App) -> Option<AnyElement> {
|
||||
let profile = self.directory.read(cx).profile(&self.public_key, cx);
|
||||
|
||||
Some(
|
||||
Avatar::new(self.public_key)
|
||||
.name(profile.name())
|
||||
.picture(profile.picture())
|
||||
.small()
|
||||
.into_any_element(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for ProfileView {
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
ScrollArea::new(format!("profile-scroll:{}", self.public_key.to_hex()))
|
||||
.handle(self.scroll_handle.clone())
|
||||
.size_full()
|
||||
.child(
|
||||
v_flex().w_full().py_12().items_center().child(
|
||||
v_flex()
|
||||
.w(CONTENT_WIDTH)
|
||||
.px_6()
|
||||
.gap_10()
|
||||
.child(self.render_identity(cx))
|
||||
.child(self.render_repositories(cx))
|
||||
.child(self.render_activity(cx)),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user