#![cfg_attr(test, allow(dead_code))]
use iced::{
Alignment, Element, Length,
widget::{Space, button, column, container, row, scrollable, text},
};
use knotra_ui::widget::{
BUTTON_HEIGHT, Tokens, body_badge_diameter, centered_button_label, centered_button_row, icon,
list, record_row, status_badge, strong, style,
};
use crate::{
message::{DetailPanelMessage, Message, ProjectMessage, WorkspaceMessage},
state::{AppState, dashboard::badge_for_operation_outcome, detail_panel::RecentCommitsPhase},
};
fn field_row<'a>(
tokens: &Tokens,
label: &'a str,
value: impl std::fmt::Display,
) -> Element<'a, Message> {
column![
text(label)
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens))
.color(snora::design::style::color::to_iced_color(
tokens.palette.text_secondary
)),
text(value.to_string())
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
]
.spacing(1)
.into()
}
pub fn view<'a>(state: &'a AppState) -> Option<Element<'a, Message>> {
let tokens = &state.theme.tokens;
let id = state.detail_panel.open_project_id.as_ref()?;
let project = state
.workspace
.as_ref()
.and_then(|ws| ws.projects.iter().find(|p| &p.id == id))?;
let status = state
.workspace_status
.as_ref()
.and_then(|ws| ws.projects.iter().find(|ps| &ps.project_id == id));
let close_btn = button(centered_button_row(row![
icon::icon_element(&icon::close())
]))
.height(Length::Fixed(BUTTON_HEIGHT))
.width(Length::Fixed(BUTTON_HEIGHT))
.on_press(Message::DetailPanel(DetailPanelMessage::Closed))
.style({
let t = tokens.clone();
move |_theme, status| style::with_focus_ring(&t, false, style::ghost(&t, status))
});
let header = row![
text(project.name.clone())
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
Space::new().width(Length::Fill),
close_btn,
]
.align_y(Alignment::Center);
let vcs = status
.map(|s| s.identity.vcs_kind.to_string())
.unwrap_or_else(|| "—".into());
let path = project.path.clone();
let remote = status
.and_then(|s| s.remote.upstream.clone())
.unwrap_or_else(|| "—".into());
let identity = column![
strong(
text(state.t("detail.section_identity"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
),
field_row(tokens, state.t("detail.label_vcs"), vcs),
field_row(tokens, state.t("detail.label_path"), path),
field_row(tokens, state.t("detail.label_remote"), remote),
]
.spacing(3);
let status_col = if let Some(s) = status {
let branch = s.context.as_ref().map(|c| c.label.as_str()).unwrap_or("—");
let ahead = s.remote.ahead;
let behind = s.remote.behind;
let dirty = s.working_tree.uncommitted_count;
let untracked = s.working_tree.untracked_count;
let conflict = if s.conflict.has_conflict {
state.t("detail.conflict_yes")
} else if s.conflict.detection_unavailable {
state.t("detail.conflict_unknown")
} else {
state.t("detail.conflict_no")
};
column![
strong(
text(state.t("detail.section_status"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
),
field_row(tokens, state.t("detail.label_branch"), branch),
field_row(tokens, state.t("detail.label_ahead"), ahead),
field_row(tokens, state.t("detail.label_behind"), behind),
field_row(tokens, state.t("detail.label_dirty"), dirty),
field_row(tokens, state.t("detail.label_untracked"), untracked),
field_row(tokens, state.t("detail.label_conflict"), conflict),
]
.spacing(3)
} else {
column![
strong(
text(state.t("detail.section_status"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
),
text(state.t("detail.loading"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
]
};
let recent_ops: Vec<Element<'_, Message>> = state
.operation_logs
.iter()
.rev()
.filter(|log| log.result.per_project.iter().any(|pp| &pp.project_id == id))
.take(5)
.map(|log| {
let outcome = log
.result
.per_project
.iter()
.find(|pp| &pp.project_id == id)
.map(|pp| pp.effective_outcome())
.unwrap_or(knotra_vcs::model::operation::ProjectOperationOutcome::Failed);
let (tone, glyph) = badge_for_operation_outcome(outcome);
container(
row![
status_badge(tokens, tone, &glyph, body_badge_diameter(tokens)),
text(format!(
"{} — {}",
super::operation_kind_label(state, &log.result.kind),
log.result.started_at.strftime("%m/%d %H:%M")
))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
]
.spacing(6)
.align_y(Alignment::Start),
)
.padding([7, 8])
.into()
})
.collect();
let recent = column![
strong(
text(state.t("detail.section_recent_operations"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
),
list(
tokens,
if recent_ops.is_empty() {
vec![
text(state.t("detail.none"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.into(),
]
} else {
recent_ops
},
),
]
.spacing(3);
let commits_body: Vec<Element<'_, Message>> = match &state.detail_panel.commits_phase {
RecentCommitsPhase::Loading(loading_id) if loading_id == id => {
vec![
text(state.t("detail.commits_loading"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.into(),
]
}
RecentCommitsPhase::Loaded {
project_id,
commits,
} if project_id == id => {
if let Some(err) = &commits.error {
vec![
text(format!("{} {}", state.t("detail.commits_error"), err))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.into(),
]
} else if commits.entries.is_empty() {
vec![
text(state.t("detail.commits_empty"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.into(),
]
} else {
commits
.entries
.iter()
.map(|entry| {
let short_hash = &entry.hash[..entry.hash.len().min(7)];
let summary = text(format!("{short_hash} {}", entry.subject))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens));
let detail = text(format!(
"{} — {}",
entry.author,
entry.date.strftime("%Y-%m-%d %H:%M")
))
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens));
record_row(summary.into(), Some(detail.into()))
})
.collect()
}
}
_ => vec![
text(state.t("detail.commits_loading"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.into(),
],
};
let commits = column![
strong(
text(state.t("detail.section_recent_commits"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
),
list(tokens, commits_body),
]
.spacing(3);
let refresh_btn = button(centered_button_label(
text(state.t("detail.refresh"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
))
.height(Length::Fixed(BUTTON_HEIGHT))
.padding([0, 18])
.on_press(Message::Project(ProjectMessage::StatusRefreshRequested(
id.clone(),
)))
.style({
let t = tokens.clone();
move |_theme, status| style::with_focus_ring(&t, false, style::secondary(&t, status))
});
let fetch_btn = button(centered_button_label(
text(state.t("detail.fetch"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
))
.height(Length::Fixed(BUTTON_HEIGHT))
.padding([0, 18])
.on_press_maybe(
(!state.operation_interlock.is_busy())
.then_some(Message::Project(ProjectMessage::FetchRequested(id.clone()))),
)
.style({
let t = tokens.clone();
move |_theme, status| style::with_focus_ring(&t, false, style::secondary(&t, status))
});
let remove_btn = button(centered_button_label(
text(state.t("detail.remove_from_workspace"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
))
.height(Length::Fixed(BUTTON_HEIGHT))
.padding([0, 18])
.on_press(Message::Workspace(
WorkspaceMessage::RemoveProjectRequested(id.clone()),
))
.style({
let t = tokens.clone();
move |_theme, status| style::with_focus_ring(&t, false, style::danger(&t, status))
});
let actions = column![
strong(
text(state.t("detail.section_actions"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens)),
),
row![refresh_btn, fetch_btn].spacing(6).wrap(),
remove_btn,
]
.spacing(6);
let content = column![header, identity, status_col, recent, commits, actions,]
.spacing(16)
.padding(16);
let surface = snora::design::style::color::to_iced_color(tokens.palette.surface);
Some(
container(scrollable(content))
.width(Length::Fixed(300.0))
.height(Length::Fill)
.style(move |_theme| iced::widget::container::Style {
background: Some(surface.into()),
..iced::widget::container::Style::default()
})
.into(),
)
}
#[cfg(test)]
mod tests {
#[test]
fn recent_operation_timestamp_format_is_stable() {
let ts = jiff::Timestamp::from_second(1_709_528_767).unwrap();
assert_eq!(ts.strftime("%m/%d %H:%M").to_string(), "03/04 05:06");
}
#[test]
fn recent_commit_date_format_is_stable() {
let ts = jiff::Timestamp::from_second(1_709_528_767).unwrap();
assert_eq!(
ts.strftime("%Y-%m-%d %H:%M").to_string(),
"2024-03-04 05:06"
);
}
}