mcai_workflow/
workflow_line.rs

1use crate::components::StepJobsProgress;
2use css_in_rust_next::Style;
3use mcai_models::WorkflowInstance;
4use yew::{html, Component, Context, Html, Properties};
5use yew_feather::{chevron_down::ChevronDown, chevron_up::ChevronUp};
6
7#[derive(PartialEq, Properties)]
8pub struct WorkflowLineProperties {
9  pub workflow: WorkflowInstance,
10  pub duration: Option<usize>,
11}
12
13#[derive(PartialEq)]
14pub enum InternalMessage {
15  ToggleExpand,
16}
17
18pub struct WorkflowLine {
19  style: Style,
20  expanded: bool,
21}
22
23impl Component for WorkflowLine {
24  type Message = InternalMessage;
25  type Properties = WorkflowLineProperties;
26
27  fn create(_ctx: &Context<Self>) -> Self {
28    let style = Style::create("Component", include_str!("workflow_line.css")).unwrap();
29
30    WorkflowLine {
31      style,
32      expanded: false,
33    }
34  }
35
36  fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
37    if msg == InternalMessage::ToggleExpand {
38      self.expanded = !self.expanded
39    }
40    true
41  }
42
43  fn view(&self, ctx: &Context<Self>) -> Html {
44    let workflow = &ctx.props().workflow;
45
46    let created_at = workflow
47      .created_at
48      .clone()
49      .format("%e %B %Y %H:%M:%S")
50      .to_string();
51
52    let duration = ctx
53      .props()
54      .duration
55      .map(|duration| {
56        let hours = duration / 60 / 60;
57        let minutes = (duration / 60) % 60;
58        let seconds = duration % 60;
59
60        format!("{:02}:{:02}:{:02}", hours, minutes, seconds)
61      })
62      .unwrap_or_else(|| "00:00:00".to_string());
63
64    let icon = if self.expanded {
65      html!(<ChevronUp></ChevronUp>)
66    } else {
67      html!(<ChevronDown></ChevronDown>)
68    };
69
70    let steps: Html = workflow
71      .steps
72      .iter()
73      .map(|step| {
74        step
75          .jobs
76          .clone()
77          .map(|jobs_status|
78            html!(
79              <StepJobsProgress jobs_status={jobs_status} expanded={self.expanded} title={step.label.clone()}></StepJobsProgress>
80            )
81          ).unwrap_or_default()
82      })
83      .collect();
84
85    html!(
86      <div class={self.style.clone()}>
87        <div>
88          <span class="icon" onclick={ctx.link().callback(|_| {InternalMessage::ToggleExpand})}>
89            {icon}
90          </span>
91          <span class="id">
92            {"#"}{workflow.id}
93          </span>
94          <span>
95            {&workflow.identifier}
96          </span>
97          <span>
98            {"Created at: "}{created_at}
99          </span>
100          <span>
101            {"Duration: "}{duration}
102          </span>
103          <span>
104            {"Steps: "}{workflow.get_passed_steps_count()}{"/"}{workflow.steps.len()}
105          </span>
106        </div>
107        <div>
108          {steps}
109        </div>
110      </div>
111    )
112  }
113}