use gpui_kit::component::WindowExt as _;
use gpui_kit::component::notification::{Notification, NotificationDelivery};
use gpui_kit::{
App, ElementId, Global, IntoElement, ParentElement as _, RenderOnce, SharedString, Styled as _,
Window,
};
use subspace_core::diagnostics::{ErrorDetails, LocalDiagnostics};
use subspace_lucide::IconName;
use crate::action::{Intent, action_button};
use crate::icon_button::IconButton;
use crate::tokens::ButtonRole;
pub struct CopyDiagnostics(LocalDiagnostics);
impl CopyDiagnostics {
pub fn new(diagnostics: LocalDiagnostics) -> Self {
Self(diagnostics)
}
}
impl Global for CopyDiagnostics {}
#[derive(IntoElement)]
pub struct CopyDetails {
id: ElementId,
error: Option<ErrorDetails>,
diagnostics: Option<LocalDiagnostics>,
role: ButtonRole,
}
impl CopyDetails {
pub fn new(id: impl Into<ElementId>, error: ErrorDetails) -> Self {
Self {
id: id.into(),
error: Some(error),
diagnostics: None,
role: ButtonRole::Action,
}
}
pub fn diagnostics(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
error: None,
diagnostics: None,
role: ButtonRole::Action,
}
}
pub fn role(mut self, role: ButtonRole) -> Self {
self.role = role;
self
}
pub fn with_diagnostics(mut self, diagnostics: LocalDiagnostics) -> Self {
self.diagnostics = Some(diagnostics);
self
}
}
impl RenderOnce for CopyDetails {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let show_log = self.error.is_some().then(|| {
action_button(
ElementId::NamedChild(std::sync::Arc::new(self.id.clone()), "show-log".into()),
Intent::Neutral,
Some(IconName::FileText),
"Show log",
)
.role(self.role)
.focus_on_click()
.on_click(|_, window, cx| {
window.dispatch_action(Box::new(crate::log_view::OpenLogView), cx)
})
});
let (label, confirmation) = if self.error.is_some() {
("Copy details", "Details copied")
} else {
("Copy diagnostics", "Diagnostics copied")
};
let copy = action_button(self.id, Intent::Neutral, Some(IconName::Copy), label)
.role(self.role)
.on_click(move |_, window, cx| {
let diagnostics = self.diagnostics.clone().unwrap_or_else(|| common(cx));
crate::links::copy(diagnostics.local_copy(self.error.as_ref()).markdown(), cx);
window.push_notification(
Notification::new()
.id::<CopyDetails>()
.message(confirmation)
.delivery(NotificationDelivery::InApp),
cx,
);
});
gpui_kit::component::h_flex()
.gap_2()
.flex_wrap()
.child(copy)
.children(show_log)
}
}
pub fn copy_text(
id: impl Into<ElementId>,
label: impl Into<SharedString>,
confirmation: impl Into<SharedString>,
text: impl Fn(&App) -> String + 'static,
) -> IconButton {
let confirmation = confirmation.into();
action_button(id, Intent::Neutral, Some(IconName::Copy), label).on_click(
move |_, window, cx| {
crate::links::copy(text(cx), cx);
window.push_notification(
Notification::new()
.id::<CopyDetails>()
.message(confirmation.clone())
.delivery(NotificationDelivery::InApp),
cx,
);
},
)
}
fn common(cx: &App) -> LocalDiagnostics {
cx.try_global::<CopyDiagnostics>()
.map(|diagnostics| diagnostics.0.clone())
.unwrap_or_default()
}
pub fn issue_paths(config_path: Option<&str>, cx: &App) -> subspace_core::diagnostics::IssuePaths {
common(cx).issue_paths(config_path)
}
pub fn log_error(target: &str, surface: &str, error: &ErrorDetails, cx: &App) {
common(cx).log_error(&subspace_core::log::Logger::new(target), surface, error);
}
#[cfg(test)]
mod tests;