use super::row;
use crate::{
components::{Button, ButtonSize, ButtonVariant},
icons::CarbonIcon,
theme::color,
tokens::ColorRole,
};
use gpui_kit::{
App, ElementId, IntoElement, ParentElement, RenderOnce, SharedString, Styled, Window, div,
prelude::FluentBuilder as _,
};
use std::rc::Rc;
#[derive(Clone)]
pub struct WorkflowRowTexts {
pub steps: Rc<dyn Fn(usize) -> SharedString>,
pub start: SharedString,
}
impl Default for WorkflowRowTexts {
fn default() -> Self {
Self {
steps: Rc::new(|n| format!("{n} steps").into()),
start: "Start".into(),
}
}
}
type Handler = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct WorkflowRow {
id: ElementId,
name: SharedString,
description: Option<SharedString>,
steps: Option<usize>,
trigger: Option<SharedString>,
last_run: Option<SharedString>,
texts: WorkflowRowTexts,
on_start: Option<Handler>,
}
impl WorkflowRow {
pub fn new(id: impl Into<ElementId>, name: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
name: name.into(),
description: None,
steps: None,
trigger: None,
last_run: None,
texts: WorkflowRowTexts::default(),
on_start: None,
}
}
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.description = Some(description.into());
self
}
pub fn steps(mut self, steps: usize) -> Self {
self.steps = Some(steps);
self
}
pub fn trigger(mut self, trigger: impl Into<SharedString>) -> Self {
self.trigger = Some(trigger.into());
self
}
pub fn last_run(mut self, last_run: impl Into<SharedString>) -> Self {
self.last_run = Some(last_run.into());
self
}
pub fn texts(mut self, texts: WorkflowRowTexts) -> Self {
self.texts = texts;
self
}
pub fn on_start(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_start = Some(Rc::new(f));
self
}
pub fn meta_runs(&self) -> Vec<SharedString> {
[
self.description.clone(),
self.steps.map(|n| (self.texts.steps)(n)),
self.trigger.clone(),
]
.into_iter()
.flatten()
.collect()
}
}
impl RenderOnce for WorkflowRow {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let meta = row::with_dots(self.meta_runs().into_iter().map(row::meta_text));
let on_start = self.on_start.clone();
row::frame()
.child(row::lead_icon(CarbonIcon::Flow))
.child(row::main(
self.name.clone(),
color(ColorRole::TextPrimary),
meta,
))
.when_some(self.last_run.clone(), |this, last| {
this.child(
div()
.flex_none()
.whitespace_nowrap()
.text_color(color(ColorRole::TextSecondary))
.child(last),
)
})
.child(
div().flex_none().child(
Button::new((self.id.clone(), "start"), self.texts.start.clone())
.variant(ButtonVariant::Tertiary)
.size(ButtonSize::Sm)
.icon(CarbonIcon::Play)
.when_some(on_start, |b, f| b.on_click(move |_, w, cx| f(w, cx))),
),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn meta_line_follows_the_reference_order() {
let row = WorkflowRow::new("w", "Release")
.description("Tests, notes, tag")
.steps(4)
.trigger("Manual");
assert_eq!(
row.meta_runs(),
vec![
SharedString::from("Tests, notes, tag"),
"4 steps".into(),
"Manual".into()
]
);
}
#[test]
fn unset_runs_are_skipped() {
let row = WorkflowRow::new("w", "Triage").trigger("On webhook");
assert_eq!(row.meta_runs(), vec![SharedString::from("On webhook")]);
let row = WorkflowRow::new("w", "Triage");
assert!(row.meta_runs().is_empty());
}
#[test]
fn step_count_comes_from_the_texts() {
let row = WorkflowRow::new("w", "Release")
.steps(2)
.texts(WorkflowRowTexts {
steps: Rc::new(|n| format!("{n} 步").into()),
start: "开始".into(),
});
assert_eq!(row.meta_runs(), vec![SharedString::from("2 步")]);
}
}