| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use forge_core::store::commit_status::{CommitStatus, StatusState, combine}; |
| 6 | use topcoat::Result; |
| 7 | use topcoat::context::Cx; |
| 8 | use topcoat::view::{View, component, view}; |
| 9 | |
| 10 | use crate::i18n::Lang; |
| 11 | use crate::i18n::code::{self as g, K}; |
| 12 | |
| 13 | |
| 14 | |
| 15 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 16 | pub struct StatusLang(pub Lang); |
| 17 | |
| 18 | impl Default for StatusLang { |
| 19 | fn default() -> Self { |
| 20 | StatusLang(Lang::En) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | |
| 25 | #[derive(Clone)] |
| 26 | pub struct StatusItem { |
| 27 | pub context: String, |
| 28 | |
| 29 | pub icon: &'static str, |
| 30 | pub icon_class: String, |
| 31 | pub description: String, |
| 32 | pub has_description: bool, |
| 33 | pub link: String, |
| 34 | pub has_link: bool, |
| 35 | } |
| 36 | |
| 37 | impl From<&CommitStatus> for StatusItem { |
| 38 | fn from(status: &CommitStatus) -> Self { |
| 39 | let description = status.description.clone().unwrap_or_default(); |
| 40 | let link = status.target_url.clone().unwrap_or_default(); |
| 41 | Self { |
| 42 | context: status.context.clone(), |
| 43 | icon: icon(status.state), |
| 44 | icon_class: format!("status-icon status-{}", css(status.state)), |
| 45 | has_description: !description.is_empty(), |
| 46 | description, |
| 47 | has_link: !link.is_empty(), |
| 48 | link, |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
| 54 | #[derive(Clone, Default)] |
| 55 | pub struct StatusSummary { |
| 56 | |
| 57 | pub state: Option<StatusState>, |
| 58 | pub items: Vec<StatusItem>, |
| 59 | } |
| 60 | |
| 61 | impl StatusSummary { |
| 62 | pub fn from_rows(rows: &[CommitStatus]) -> Self { |
| 63 | Self { |
| 64 | state: combine(rows.iter().map(|row| row.state)), |
| 65 | items: rows.iter().map(StatusItem::from).collect(), |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | pub fn icon(state: StatusState) -> &'static str { |
| 72 | match state { |
| 73 | StatusState::Success => "✓", |
| 74 | StatusState::Failure | StatusState::Error => "✗", |
| 75 | StatusState::Pending => "●", |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
| 80 | pub fn css(state: StatusState) -> &'static str { |
| 81 | match state { |
| 82 | StatusState::Success => "success", |
| 83 | StatusState::Failure | StatusState::Error => "failure", |
| 84 | StatusState::Pending => "pending", |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | #[component] |
| 91 | pub async fn commit_status( |
| 92 | cx: &Cx, |
| 93 | summary: StatusSummary, |
| 94 | #[default] lang: StatusLang, |
| 95 | ) -> Result<impl View> { |
| 96 | let lang = lang.0; |
| 97 | let has_state = summary.state.is_some(); |
| 98 | let state = summary.state.unwrap_or(StatusState::Pending); |
| 99 | let icon_text = icon(state); |
| 100 | let css_class = format!("status-icon status-{}", css(state)); |
| 101 | let title = format!( |
| 102 | "{}{}", |
| 103 | g::t(lang, K::CommitStatusPrefix), |
| 104 | g::state_word(lang, state) |
| 105 | ); |
| 106 | let items = summary.items; |
| 107 | Ok(view! { |
| 108 | if has_state { |
| 109 | <details class="commit-status"> |
| 110 | <summary class=(css_class.as_str()) title=(title.as_str()) aria-label=(title.as_str())> |
| 111 | <span aria-hidden="true">(icon_text)</span> |
| 112 | </summary> |
| 113 | status_list(items: items, lang: lang) |
| 114 | </details> |
| 115 | } |
| 116 | }) |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | #[component] |
| 122 | pub async fn checks_summary( |
| 123 | cx: &Cx, |
| 124 | summary: StatusSummary, |
| 125 | #[default] lang: StatusLang, |
| 126 | ) -> Result<impl View> { |
| 127 | let lang = lang.0; |
| 128 | let (text, state) = match summary.state { |
| 129 | None => (g::t(lang, K::NoChecksReported), None), |
| 130 | Some(StatusState::Success) => (g::t(lang, K::AllChecksPassed), Some(StatusState::Success)), |
| 131 | Some(StatusState::Pending) => (g::t(lang, K::ChecksPending), Some(StatusState::Pending)), |
| 132 | Some(StatusState::Failure) | Some(StatusState::Error) => { |
| 133 | (g::t(lang, K::SomeChecksFailed), Some(StatusState::Failure)) |
| 134 | } |
| 135 | }; |
| 136 | let icon_text = state.map_or("", icon); |
| 137 | let css_class = format!("checks-summary status-{}", state.map_or("none", css)); |
| 138 | let items = summary.items; |
| 139 | let has_items = !items.is_empty(); |
| 140 | Ok(view! { |
| 141 | <div class=(css_class.as_str())> |
| 142 | <span class="status-icon" aria-hidden="true">(icon_text)</span> |
| 143 | <strong>(text)</strong> |
| 144 | if has_items { |
| 145 | <details class="checks-details"> |
| 146 | <summary class="act">(g::t(lang, K::ShowChecks))</summary> |
| 147 | status_list(items: items, lang: lang) |
| 148 | </details> |
| 149 | } |
| 150 | </div> |
| 151 | }) |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | fn description_text(lang: Lang, text: &str) -> &str { |
| 157 | use crate::i18n::s; |
| 158 | match text { |
| 159 | "Waiting for approval" => s(lang, "Waiting for approval", "Onay bekliyor"), |
| 160 | "Waiting for other jobs" => s(lang, "Waiting for other jobs", "Diğer işleri bekliyor"), |
| 161 | "Queued" => s(lang, "Queued", "Sırada"), |
| 162 | "Running" => s(lang, "Running", "Çalışıyor"), |
| 163 | "Succeeded" => s(lang, "Succeeded", "Başarılı"), |
| 164 | "Failed" => s(lang, "Failed", "Başarısız"), |
| 165 | "Cancelled" => s(lang, "Cancelled", "İptal edildi"), |
| 166 | "Errored" => s(lang, "Errored", "Hata verdi"), |
| 167 | other => other, |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | |
| 172 | #[component] |
| 173 | pub async fn status_list(cx: &Cx, items: Vec<StatusItem>, lang: Lang) -> Result<impl View> { |
| 174 | Ok(view! { |
| 175 | <ul class="status-list"> |
| 176 | for item in items.iter() { |
| 177 | <li class="status-item"> |
| 178 | <span class=(item.icon_class.as_str())>(item.icon)</span> |
| 179 | <code>(item.context.as_str())</code> |
| 180 | if item.has_description { |
| 181 | <span class="hint">(description_text(lang, item.description.as_str()))</span> |
| 182 | } |
| 183 | if item.has_link { |
| 184 | <a class="act" href=(item.link.as_str()) rel="nofollow noopener noreferrer">(g::t(lang, K::DetailsLink))</a> |
| 185 | } |
| 186 | </li> |
| 187 | } |
| 188 | </ul> |
| 189 | }) |
| 190 | } |