update sidebar

This commit is contained in:
2026-09-14 08:56:32 +07:00
parent 1a13d4e0d7
commit c6ada10afa
5 changed files with 267 additions and 74 deletions
+95 -46
View File
@@ -1,6 +1,6 @@
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use std::ops::Range;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
@@ -10,7 +10,7 @@ use dock::{
};
use gpui::prelude::*;
use gpui::{
AnyElement, App, Context, Div, EventEmitter, FocusHandle, Focusable, ObjectFit, Render,
AnyElement, App, Context, Div, EventEmitter, FocusHandle, Focusable, Hsla, ObjectFit, Render,
SharedString, Subscription, WeakEntity, Window, div, img, px, relative, uniform_list, white,
};
use gpui_base::Button as BaseButton;
@@ -18,9 +18,10 @@ use gpui_component::button::{Button, ButtonVariants};
use gpui_component::input::InputState;
use gpui_component::{ActiveTheme, Icon, IconName, Sizable, StyledExt, h_flex, v_flex};
use nostr::prelude::RelayUrl;
use signed_core::{Announcement, RepoAddr, identifier_from_name};
use signed_core::{Announcement, RepoAddr};
use signed_state::{
Backend, BackendEvent, CheckoutsStore, LocalReposStore, Profile, ProfileStore, RepoListStore,
Backend, BackendEvent, CheckoutsStore, LocalReposStore, Nip34Binding, Nip34Kind, Profile,
ProfileStore, RepoListStore, ResolvedLocalRepo, resolve_local_repos,
};
use signed_ui::{NavItem, PixelAvatar, UserAvatar, title_bar_drag_handlers};
@@ -35,18 +36,31 @@ mod settings_dialog;
use self::onboarding_dialog::OnboardingState;
/// The tool that bound a repository, `"nak"` or `"ngit"`.
fn local_tool(binding: &Nip34Binding) -> Option<&'static str> {
let signals = &binding.signals;
if signals.nip34_json || signals.nip34_grasp_remote || signals.nip34_state_refs {
Some("nak")
} else if signals.nostr_repo_config {
Some("ngit")
} else {
None
}
}
pub struct SidebarPanel {
focus_handle: FocusHandle,
dock_area: WeakEntity<DockArea>,
inbox: Option<WeakEntity<InboxView>>,
explore: Option<WeakEntity<RepoListView>>,
banner: SharedString,
/// The signed-in user's announced repositories, newest first.
/// User's announced repositories.
announcements: Arc<Vec<Announcement>>,
/// Local repositories found by the scan that are not announced yet.
local_repos: Arc<Vec<PathBuf>>,
local_repos: Arc<Vec<ResolvedLocalRepo>>,
scanning: bool,
/// Unpushed commit counts per announced repository, shown as row badges.
/// Unpushed commit counts per announced repository.
unpushed: HashMap<RepoAddr, usize>,
_subscriptions: Vec<Subscription>,
}
@@ -116,30 +130,21 @@ impl SidebarPanel {
let backend = Backend::global(cx);
let user = backend.read(cx).current_user();
let repo_list = RepoListStore::global(cx);
let announcements = user
.as_ref()
.map(|user| repo_list.read(cx).announcements_of(user))
.unwrap_or_default();
let (announcements, local_repos, scanning) = {
let repo_list = RepoListStore::global(cx);
let repo_list = repo_list.read(cx);
// Drop a scanned repository once the user announces it, so it is not listed twice.
let local = LocalReposStore::global(cx);
let scanning = local.read(cx).scanning;
let announcements = user
.as_ref()
.map(|user| repo_list.announcements_of(user))
.unwrap_or_default();
let local_repos = {
let ids: HashSet<String> = announcements.iter().map(|a| a.id.clone()).collect();
local
.read(cx)
.repos
.iter()
.filter(|repo| {
let Some(name) = repo.path.file_name() else {
return true;
};
!ids.contains(&identifier_from_name(&name.to_string_lossy()))
})
.map(|repo| repo.path.clone())
.collect()
let local = LocalReposStore::global(cx);
let local = local.read(cx);
let local_repos =
resolve_local_repos(&local.repos, &repo_list.announcements, &announcements);
(announcements, local_repos, local.scanning)
};
let announcements_changed = *self.announcements != announcements;
@@ -270,6 +275,22 @@ impl SidebarPanel {
.ok();
}
/// A local repository opens as the announced repository when its binding matches one,
/// and as a local-only repository otherwise.
fn open_local_entry(
&mut self,
entry: ResolvedLocalRepo,
window: &mut Window,
cx: &mut Context<Self>,
) {
if let Some(announcement) = entry.announcement {
self.open_repo(&announcement, window, cx);
return;
}
self.open_local_repo(entry.path, window, cx);
}
fn render_repos(&self, cx: &mut Context<Self>) -> impl IntoElement {
let announcements = self.announcements.clone();
let local_repos = self.local_repos.clone();
@@ -361,11 +382,10 @@ impl SidebarPanel {
})
}
/// Renders row `ix` of the merged list: an announced repository or a local one.
fn render_repo_at(
&self,
announcements: &[Announcement],
local_repos: &[PathBuf],
local_repos: &[ResolvedLocalRepo],
ix: usize,
cx: &mut Context<Self>,
) -> AnyElement {
@@ -376,9 +396,9 @@ impl SidebarPanel {
}
let local_ix = ix - announcements.len();
let path = &local_repos[local_ix];
let entry = &local_repos[local_ix];
self.render_local_row(path, cx).into_any_element()
self.render_local_row(entry, cx).into_any_element()
}
fn render_repo_row(
@@ -419,23 +439,43 @@ impl SidebarPanel {
)
}
/// A local repository that is not yet set up for NIP-34, marked with a warning.
fn render_local_row(&self, path: &Path, cx: &mut Context<Self>) -> impl IntoElement {
let name = path
fn render_local_row(
&self,
entry: &ResolvedLocalRepo,
cx: &mut Context<Self>,
) -> impl IntoElement {
let name = entry
.path
.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or("Untitled".into());
let path = path.to_path_buf();
let avatar = PixelAvatar::new(path.to_string_lossy());
let avatar = PixelAvatar::new(entry.path.to_string_lossy());
NavItem::new(format!("local-repo:{}", path.display()), name, avatar)
.suffix(
Icon::new(IconName::TriangleAlert)
.small()
.text_color(cx.theme().warning),
)
let suffix: AnyElement = match entry.nip34.as_ref().map(|binding| binding.kind) {
Some(Nip34Kind::Initialized) => {
let label = match entry.nip34.as_ref().and_then(local_tool) {
Some(tool) => format!("NIP-34 · {tool}"),
None => "NIP-34".to_owned(),
};
local_badge(&label, cx.theme().muted_foreground)
}
Some(Nip34Kind::Cloned) => local_badge("NIP-34 clone", cx.theme().muted_foreground),
Some(Nip34Kind::ToolingOnly) => {
local_badge("Nostr tooling", cx.theme().muted_foreground)
}
None => Icon::new(IconName::TriangleAlert)
.small()
.text_color(cx.theme().warning)
.into_any_element(),
};
let id = format!("local-repo:{}", entry.path.display());
let entry = entry.clone();
NavItem::new(id, name, avatar)
.suffix(suffix)
.on_click(cx.listener(move |this, _ev, window, cx| {
this.open_local_repo(path.clone(), window, cx);
this.open_local_entry(entry.clone(), window, cx);
}))
}
@@ -563,6 +603,15 @@ pub(super) fn server_host(relay: &RelayUrl) -> SharedString {
.unwrap_or_else(|| SharedString::from(relay.to_string()))
}
fn local_badge(label: &str, color: Hsla) -> AnyElement {
div()
.flex_shrink_0()
.text_xs()
.text_color(color)
.child(SharedString::from(label.to_owned()))
.into_any_element()
}
fn pick_banner() -> SharedString {
let num = SystemTime::now()
.duration_since(UNIX_EPOCH)