#![cfg_attr(test, allow(dead_code))]
use iced::{
Alignment, Element, Length,
widget::{Space, button, column, container, row, scrollable, text},
};
use knotra_ui::widget::{Tokens, record_row};
use crate::{
message::{DetailPanelMessage, Message, ProjectMessage, WorkspaceMessage},
state::{AppState, detail_panel::RecentCommitsPhase},
};
fn field_row<'a>(
tokens: &Tokens,
label: &'a str,
value: impl std::fmt::Display,
label_width: f32,
) -> Element<'a, Message> {
row![
text(label)
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens))
.width(Length::Fixed(label_width)),
text(value.to_string())
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens)),
]
.into()
}
const IDENTITY_LABEL_WIDTH: f32 = 67.0;
const STATUS_LABEL_WIDTH: f32 = 86.0;
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(
text("✕")
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens)),
)
.on_press(Message::DetailPanel(DetailPanelMessage::Closed));
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![
text(state.t("detail.section_identity"))
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens)),
field_row(
tokens,
state.t("detail.label_vcs"),
vcs,
IDENTITY_LABEL_WIDTH
),
field_row(
tokens,
state.t("detail.label_path"),
path,
IDENTITY_LABEL_WIDTH
),
field_row(
tokens,
state.t("detail.label_remote"),
remote,
IDENTITY_LABEL_WIDTH
),
]
.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![
text(state.t("detail.section_status"))
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens)),
field_row(
tokens,
state.t("detail.label_branch"),
branch,
STATUS_LABEL_WIDTH
),
field_row(
tokens,
state.t("detail.label_ahead"),
ahead,
STATUS_LABEL_WIDTH
),
field_row(
tokens,
state.t("detail.label_behind"),
behind,
STATUS_LABEL_WIDTH
),
field_row(
tokens,
state.t("detail.label_dirty"),
dirty,
STATUS_LABEL_WIDTH
),
field_row(
tokens,
state.t("detail.label_untracked"),
untracked,
STATUS_LABEL_WIDTH
),
field_row(
tokens,
state.t("detail.label_conflict"),
conflict,
STATUS_LABEL_WIDTH
),
]
.spacing(3)
} else {
column![
text(state.t("detail.section_status"))
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens)),
text(state.t("detail.loading"))
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_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 ok = log
.result
.per_project
.iter()
.find(|pp| &pp.project_id == id)
.map(|pp| pp.success)
.unwrap_or(false);
let icon = if ok { "✓" } else { "✗" };
text(format!(
"{} {} — {}",
icon,
super::operation_kind_label(state, &log.result.kind),
log.result.started_at.format("%m/%d %H:%M")
))
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens))
.into()
})
.collect();
let recent = column(
std::iter::once(
text(state.t("detail.section_recent_operations"))
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens))
.into(),
)
.chain(if recent_ops.is_empty() {
vec![
text(state.t("detail.none"))
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_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_small_size(tokens))
.line_height(snora::design::style::text::body_small_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_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens))
.into(),
]
} else if commits.entries.is_empty() {
vec![
text(state.t("detail.commits_empty"))
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_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_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(
tokens,
));
let detail = text(format!(
"{} — {}",
entry.author,
entry.date.format("%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_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens))
.into(),
],
};
let commits = column(
std::iter::once(
text(state.t("detail.section_recent_commits"))
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens))
.into(),
)
.chain(commits_body),
)
.spacing(3);
let refresh_btn = button(
text(state.t("detail.refresh"))
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens)),
)
.on_press(Message::Project(ProjectMessage::StatusRefreshRequested(
id.clone(),
)));
let fetch_btn = button(
text(state.t("detail.fetch"))
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens)),
)
.on_press_maybe(
(!state.operation_interlock.is_busy())
.then_some(Message::Project(ProjectMessage::FetchRequested(id.clone()))),
);
let remove_btn = button(
text(state.t("detail.remove_from_workspace"))
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens)),
)
.on_press(Message::Workspace(
WorkspaceMessage::RemoveProjectRequested(id.clone()),
));
let actions = column![
text(state.t("detail.section_actions"))
.size(snora::design::style::text::body_small_size(tokens))
.line_height(snora::design::style::text::body_small_line_height(tokens)),
row![refresh_btn, fetch_btn].spacing(6),
remove_btn,
]
.spacing(6);
let content = column![header, identity, status_col, recent, commits, actions,]
.spacing(16)
.padding(16);
Some(
container(scrollable(content))
.width(Length::Fixed(300.0))
.height(Length::Fill)
.into(),
)
}