| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use topcoat::Result; |
| 6 | use topcoat::view::{View, component, view}; |
| 7 | |
| 8 | use crate::i18n::Lang; |
| 9 | use crate::i18n::admin::fill; |
| 10 | use crate::i18n::structure::{K, t}; |
| 11 | use crate::views::admin::admin_tabs; |
| 12 | use crate::views::board::flag; |
| 13 | use crate::views::{alert, notice}; |
| 14 | |
| 15 | |
| 16 | #[derive(Debug, Clone)] |
| 17 | pub struct TagRow { |
| 18 | |
| 19 | pub id: String, |
| 20 | |
| 21 | pub name: String, |
| 22 | |
| 23 | pub colour: Option<String>, |
| 24 | |
| 25 | pub is_default: bool, |
| 26 | |
| 27 | pub uses: i64, |
| 28 | } |
| 29 | |
| 30 | |
| 31 | #[derive(Debug, Clone)] |
| 32 | pub struct TagsView { |
| 33 | |
| 34 | pub lang: Lang, |
| 35 | |
| 36 | pub tags: Vec<TagRow>, |
| 37 | |
| 38 | pub palette: Vec<String>, |
| 39 | |
| 40 | pub notice: Option<String>, |
| 41 | |
| 42 | pub alert: Option<String>, |
| 43 | } |
| 44 | |
| 45 | |
| 46 | #[derive(Debug, Clone)] |
| 47 | pub struct ColumnRow { |
| 48 | |
| 49 | pub id: String, |
| 50 | |
| 51 | pub name: String, |
| 52 | |
| 53 | pub is_done: bool, |
| 54 | |
| 55 | pub cards: i64, |
| 56 | } |
| 57 | |
| 58 | |
| 59 | #[derive(Debug, Clone)] |
| 60 | pub struct ColumnsView { |
| 61 | |
| 62 | pub lang: Lang, |
| 63 | |
| 64 | pub columns: Vec<ColumnRow>, |
| 65 | |
| 66 | pub notice: Option<String>, |
| 67 | |
| 68 | pub alert: Option<String>, |
| 69 | } |
| 70 | |
| 71 | |
| 72 | #[derive(Debug, Clone)] |
| 73 | pub struct MemberRow { |
| 74 | |
| 75 | pub sub: String, |
| 76 | |
| 77 | pub name: String, |
| 78 | |
| 79 | pub email: String, |
| 80 | |
| 81 | pub role: String, |
| 82 | |
| 83 | pub im_admin: bool, |
| 84 | |
| 85 | pub disabled: bool, |
| 86 | } |
| 87 | |
| 88 | |
| 89 | #[derive(Debug, Clone, Default)] |
| 90 | pub struct ServerInfo { |
| 91 | |
| 92 | pub base_url: String, |
| 93 | |
| 94 | pub listen: String, |
| 95 | |
| 96 | pub data_dir: String, |
| 97 | |
| 98 | pub database: String, |
| 99 | |
| 100 | pub schema: i64, |
| 101 | |
| 102 | pub build: String, |
| 103 | |
| 104 | pub sign_in: bool, |
| 105 | |
| 106 | pub storage_in: bool, |
| 107 | } |
| 108 | |
| 109 | |
| 110 | |
| 111 | #[derive(Debug, Clone, Default)] |
| 112 | pub struct StorageInfo { |
| 113 | |
| 114 | pub target: String, |
| 115 | |
| 116 | pub in_available: bool, |
| 117 | |
| 118 | pub local: i64, |
| 119 | |
| 120 | pub in_files: i64, |
| 121 | |
| 122 | pub moving: i64, |
| 123 | } |
| 124 | |
| 125 | |
| 126 | #[component] |
| 127 | pub async fn storage_section(lang: Lang, info: &StorageInfo, csrf: &str) -> Result<impl View> { |
| 128 | let in_chosen = info.target == "in"; |
| 129 | let counts = fill( |
| 130 | t(lang, K::StorageCounts), |
| 131 | &[ |
| 132 | &info.local.to_string(), |
| 133 | &info.in_files.to_string(), |
| 134 | &info.moving.to_string(), |
| 135 | ], |
| 136 | ); |
| 137 | |
| 138 | let waiting = if in_chosen { |
| 139 | info.local + info.moving |
| 140 | } else { |
| 141 | info.in_files + info.moving |
| 142 | }; |
| 143 | let target_word = if in_chosen { |
| 144 | t(lang, K::StorageIn) |
| 145 | } else { |
| 146 | t(lang, K::StorageLocal) |
| 147 | }; |
| 148 | let migrating = (waiting > 0).then(|| { |
| 149 | fill( |
| 150 | t(lang, K::StorageMigrating), |
| 151 | &[&waiting.to_string(), target_word], |
| 152 | ) |
| 153 | }); |
| 154 | Ok(view! { |
| 155 | <section class="card" id="storage"> |
| 156 | <h2>(t(lang, K::StorageHeading))</h2> |
| 157 | <p class="field-hint">(t(lang, K::StorageHelp))</p> |
| 158 | <p>(counts.as_str())</p> |
| 159 | if let Some(line) = &migrating { |
| 160 | <p class="field-hint">(line.as_str())</p> |
| 161 | } |
| 162 | <form method="post" action="/settings/attachments" class="stack"> |
| 163 | <input type="hidden" name="csrf" value=(csrf)> |
| 164 | <div class="field"> |
| 165 | <label><input type="radio" name="location" value="local" checked=(flag(!in_chosen))> (t(lang, K::StorageLocalOption))</label> |
| 166 | <label><input type="radio" name="location" value="in" checked=(flag(in_chosen)) disabled=(flag(!info.in_available))> (t(lang, K::StorageInOption))</label> |
| 167 | if !info.in_available { |
| 168 | <p class="field-hint">(t(lang, K::StorageInUnavailable))</p> |
| 169 | } |
| 170 | </div> |
| 171 | <button class="button" type="submit">(t(lang, K::StorageSave))</button> |
| 172 | </form> |
| 173 | </section> |
| 174 | }) |
| 175 | } |
| 176 | |
| 177 | |
| 178 | #[component] |
| 179 | pub async fn tags_content(page: &TagsView, csrf: &str) -> Result<impl View> { |
| 180 | let lang = page.lang; |
| 181 | Ok(view! { |
| 182 | <h1>(t(lang, K::TagsTitle))</h1> |
| 183 | admin_tabs(lang: lang, current: "tags") |
| 184 | if let Some(message) = &page.alert { |
| 185 | alert(message: message.as_str()) |
| 186 | } |
| 187 | if let Some(message) = &page.notice { |
| 188 | notice(message: message.as_str()) |
| 189 | } |
| 190 | <p class="field-hint">(t(lang, K::TagsHelp))</p> |
| 191 | <section class="card"> |
| 192 | <table class="admin-table"> |
| 193 | <tbody> |
| 194 | for tag in page.tags.iter() { |
| 195 | <tr> |
| 196 | <td> |
| 197 | <span class="tone-dot" data-colour=(tag.colour.as_deref().unwrap_or(""))></span> |
| 198 | " " (tag.name.as_str()) |
| 199 | if tag.is_default { |
| 200 | " " <span class="badge">(t(lang, K::TagDefault))</span> |
| 201 | } |
| 202 | <span class="field-hint">" " (fill(t(lang, K::TagUsedBy), &[&tag.uses.to_string()]))</span> |
| 203 | </td> |
| 204 | <td> |
| 205 | <form method="post" action=(format!("/tags/{}/rename", tag.id)) class="inline"> |
| 206 | <input type="hidden" name="csrf" value=(csrf)> |
| 207 | <input name="name" value=(tag.name.as_str()) maxlength="60" required=(flag(true))> |
| 208 | <button class="button" type="submit">(t(lang, K::TagRename))</button> |
| 209 | </form> |
| 210 | </td> |
| 211 | <td> |
| 212 | <form method="post" action=(format!("/tags/{}/colour", tag.id)) class="inline"> |
| 213 | <input type="hidden" name="csrf" value=(csrf)> |
| 214 | <select name="colour"> |
| 215 | <option value="" selected=(flag(tag.colour.is_none()))>(t(lang, K::TagNoColour))</option> |
| 216 | for colour in page.palette.iter() { |
| 217 | <option value=(colour.as_str()) selected=(flag(tag.colour.as_deref() == Some(colour.as_str())))>(colour.as_str())</option> |
| 218 | } |
| 219 | </select> |
| 220 | <button class="button" type="submit">(t(lang, K::TagSaveColour))</button> |
| 221 | </form> |
| 222 | </td> |
| 223 | <td> |
| 224 | <form method="post" action=(format!("/tags/{}/move", tag.id)) class="inline"> |
| 225 | <input type="hidden" name="csrf" value=(csrf)> |
| 226 | <button class="button ghost" type="submit" name="direction" value="up">(t(lang, K::TagMoveUp))</button> |
| 227 | <button class="button ghost" type="submit" name="direction" value="down">(t(lang, K::TagMoveDown))</button> |
| 228 | </form> |
| 229 | </td> |
| 230 | <td> |
| 231 | if !tag.is_default { |
| 232 | <form method="post" action=(format!("/tags/{}/delete", tag.id)) class="inline"> |
| 233 | <input type="hidden" name="csrf" value=(csrf)> |
| 234 | <button class="button danger" type="submit">(t(lang, K::TagDelete))</button> |
| 235 | </form> |
| 236 | } |
| 237 | </td> |
| 238 | </tr> |
| 239 | } |
| 240 | </tbody> |
| 241 | </table> |
| 242 | </section> |
| 243 | <section class="card"> |
| 244 | <h2>(t(lang, K::TagsNew))</h2> |
| 245 | <form method="post" action="/tags" class="stack"> |
| 246 | <input type="hidden" name="csrf" value=(csrf)> |
| 247 | <div class="field"> |
| 248 | <label for="tag-name">(t(lang, K::TagName))</label> |
| 249 | <input id="tag-name" name="name" maxlength="60" required=(flag(true))> |
| 250 | </div> |
| 251 | <div class="field"> |
| 252 | <label for="tag-colour">(t(lang, K::TagColour))</label> |
| 253 | <select id="tag-colour" name="colour"> |
| 254 | <option value="">(t(lang, K::TagNoColour))</option> |
| 255 | for colour in page.palette.iter() { |
| 256 | <option value=(colour.as_str())>(colour.as_str())</option> |
| 257 | } |
| 258 | </select> |
| 259 | </div> |
| 260 | <button class="button primary" type="submit">(t(lang, K::TagAdd))</button> |
| 261 | </form> |
| 262 | </section> |
| 263 | }) |
| 264 | } |
| 265 | |
| 266 | |
| 267 | #[component] |
| 268 | pub async fn columns_content(page: &ColumnsView, csrf: &str) -> Result<impl View> { |
| 269 | let lang = page.lang; |
| 270 | Ok(view! { |
| 271 | <h1>(t(lang, K::ColumnsTitle))</h1> |
| 272 | admin_tabs(lang: lang, current: "columns") |
| 273 | if let Some(message) = &page.alert { |
| 274 | alert(message: message.as_str()) |
| 275 | } |
| 276 | if let Some(message) = &page.notice { |
| 277 | notice(message: message.as_str()) |
| 278 | } |
| 279 | <p class="field-hint">(t(lang, K::ColumnsHelp))</p> |
| 280 | <section class="card"> |
| 281 | <table class="admin-table"> |
| 282 | <tbody> |
| 283 | for column in page.columns.iter() { |
| 284 | <tr> |
| 285 | <td> |
| 286 | (column.name.as_str()) |
| 287 | if column.is_done { |
| 288 | " " <span class="badge">(t(lang, K::ColumnDone))</span> |
| 289 | } |
| 290 | <span class="field-hint">" " (fill(t(lang, K::ColumnCards), &[&column.cards.to_string()]))</span> |
| 291 | </td> |
| 292 | <td> |
| 293 | <form method="post" action=(format!("/columns/{}/rename", column.id)) class="inline"> |
| 294 | <input type="hidden" name="csrf" value=(csrf)> |
| 295 | <input name="name" value=(column.name.as_str()) maxlength="60" required=(flag(true))> |
| 296 | <button class="button" type="submit">(t(lang, K::ColumnRename))</button> |
| 297 | </form> |
| 298 | </td> |
| 299 | <td> |
| 300 | <form method="post" action=(format!("/columns/{}/move", column.id)) class="inline"> |
| 301 | <input type="hidden" name="csrf" value=(csrf)> |
| 302 | <button class="button ghost" type="submit" name="direction" value="up">(t(lang, K::ColumnMoveUp))</button> |
| 303 | <button class="button ghost" type="submit" name="direction" value="down">(t(lang, K::ColumnMoveDown))</button> |
| 304 | </form> |
| 305 | </td> |
| 306 | <td> |
| 307 | if !column.is_done { |
| 308 | <form method="post" action=(format!("/columns/{}/retire", column.id)) class="inline"> |
| 309 | <input type="hidden" name="csrf" value=(csrf)> |
| 310 | <button class="button danger" type="submit">(t(lang, K::ColumnRetire))</button> |
| 311 | </form> |
| 312 | } |
| 313 | </td> |
| 314 | </tr> |
| 315 | } |
| 316 | </tbody> |
| 317 | </table> |
| 318 | </section> |
| 319 | <section class="card"> |
| 320 | <h2>(t(lang, K::ColumnsNew))</h2> |
| 321 | <form method="post" action="/columns" class="stack"> |
| 322 | <input type="hidden" name="csrf" value=(csrf)> |
| 323 | <div class="field"> |
| 324 | <label for="column-name">(t(lang, K::ColumnName))</label> |
| 325 | <input id="column-name" name="name" maxlength="60" required=(flag(true))> |
| 326 | </div> |
| 327 | <button class="button primary" type="submit">(t(lang, K::ColumnAdd))</button> |
| 328 | </form> |
| 329 | </section> |
| 330 | }) |
| 331 | } |
| 332 | |
| 333 | |
| 334 | #[component] |
| 335 | pub async fn members_section(lang: Lang, members: &[MemberRow], csrf: &str) -> Result<impl View> { |
| 336 | Ok(view! { |
| 337 | <section class="card" id="members"> |
| 338 | <h2>(t(lang, K::MembersHeading))</h2> |
| 339 | <p class="field-hint">(t(lang, K::MembersHelp))</p> |
| 340 | <table class="admin-table"> |
| 341 | <thead> |
| 342 | <tr> |
| 343 | <th>(t(lang, K::MemberName))</th> |
| 344 | <th>(t(lang, K::MemberEmail))</th> |
| 345 | <th>(t(lang, K::MemberFlags))</th> |
| 346 | <th>(t(lang, K::MemberRole))</th> |
| 347 | </tr> |
| 348 | </thead> |
| 349 | <tbody> |
| 350 | for member in members.iter() { |
| 351 | <tr> |
| 352 | <td>(member.name.as_str())</td> |
| 353 | <td>(member.email.as_str())</td> |
| 354 | <td> |
| 355 | if member.im_admin { |
| 356 | <span class="badge">(t(lang, K::MemberImAdmin))</span> |
| 357 | } |
| 358 | if member.disabled { |
| 359 | <span class="badge">(t(lang, K::MemberDisabled))</span> |
| 360 | } |
| 361 | </td> |
| 362 | <td> |
| 363 | if member.im_admin { |
| 364 | <span>(t(lang, K::RoleAdmin))</span> |
| 365 | } else { |
| 366 | <form method="post" action=(format!("/settings/members/{}/role", member.sub)) class="inline"> |
| 367 | <input type="hidden" name="csrf" value=(csrf)> |
| 368 | <select name="role" aria-label=(t(lang, K::MemberRole))> |
| 369 | <option value="admin" selected=(flag(member.role == "admin"))>(t(lang, K::RoleAdmin))</option> |
| 370 | <option value="member" selected=(flag(member.role == "member"))>(t(lang, K::RoleMember))</option> |
| 371 | <option value="viewer" selected=(flag(member.role == "viewer"))>(t(lang, K::RoleViewer))</option> |
| 372 | <option value="none" selected=(flag(member.role == "none"))>(t(lang, K::RoleNone))</option> |
| 373 | </select> |
| 374 | <button class="button" type="submit">(t(lang, K::MemberSave))</button> |
| 375 | </form> |
| 376 | } |
| 377 | </td> |
| 378 | </tr> |
| 379 | } |
| 380 | </tbody> |
| 381 | </table> |
| 382 | </section> |
| 383 | }) |
| 384 | } |
| 385 | |
| 386 | |
| 387 | #[component] |
| 388 | pub async fn limits_section( |
| 389 | lang: Lang, |
| 390 | limit_mib: &str, |
| 391 | extensions: &str, |
| 392 | csrf: &str, |
| 393 | ) -> Result<impl View> { |
| 394 | Ok(view! { |
| 395 | <section class="card" id="limits"> |
| 396 | <h2>(t(lang, K::LimitsHeading))</h2> |
| 397 | <p class="field-hint">(t(lang, K::LimitsHelp))</p> |
| 398 | <form method="post" action="/settings/limits" class="stack"> |
| 399 | <input type="hidden" name="csrf" value=(csrf)> |
| 400 | <div class="field"> |
| 401 | <label for="limit-mib">(t(lang, K::LimitMib))</label> |
| 402 | <input id="limit-mib" name="limit_mib" inputmode="numeric" value=(limit_mib) maxlength="4"> |
| 403 | </div> |
| 404 | <div class="field"> |
| 405 | <label for="limit-ext">(t(lang, K::LimitExtensions))</label> |
| 406 | <textarea id="limit-ext" name="extensions" maxlength="2000">(extensions)</textarea> |
| 407 | </div> |
| 408 | <button class="button" type="submit">(t(lang, K::LimitsSave))</button> |
| 409 | </form> |
| 410 | </section> |
| 411 | }) |
| 412 | } |
| 413 | |
| 414 | |
| 415 | #[component] |
| 416 | pub async fn server_section(lang: Lang, info: &ServerInfo) -> Result<impl View> { |
| 417 | let (on_text, off_text) = (t(lang, K::On), t(lang, K::Off)); |
| 418 | let storage = if info.storage_in { |
| 419 | t(lang, K::StorageIn) |
| 420 | } else { |
| 421 | t(lang, K::StorageLocal) |
| 422 | }; |
| 423 | let schema = info.schema.to_string(); |
| 424 | Ok(view! { |
| 425 | <section class="card" id="server"> |
| 426 | <h2>(t(lang, K::ServerHeading))</h2> |
| 427 | <p class="field-hint">(t(lang, K::ServerHelp))</p> |
| 428 | <dl class="facts"> |
| 429 | <dt>(t(lang, K::ServerBase))</dt><dd>(info.base_url.as_str())</dd> |
| 430 | <dt>(t(lang, K::ServerListen))</dt><dd>(info.listen.as_str())</dd> |
| 431 | <dt>(t(lang, K::ServerData))</dt><dd>(info.data_dir.as_str())</dd> |
| 432 | <dt>(t(lang, K::ServerDatabase))</dt><dd>(info.database.as_str())</dd> |
| 433 | <dt>(t(lang, K::ServerSchema))</dt><dd>(schema.as_str())</dd> |
| 434 | <dt>(t(lang, K::ServerBuild))</dt><dd>(info.build.as_str())</dd> |
| 435 | <dt>(t(lang, K::ServerSignIn))</dt><dd>(if info.sign_in { on_text } else { off_text })</dd> |
| 436 | <dt>(t(lang, K::ServerStorage))</dt><dd>(storage)</dd> |
| 437 | </dl> |
| 438 | </section> |
| 439 | }) |
| 440 | } |
| 441 | |
| 442 | |
| 443 | #[component] |
| 444 | pub async fn person_content( |
| 445 | lang: Lang, |
| 446 | name: &str, |
| 447 | role: &str, |
| 448 | zone: &str, |
| 449 | last_sign_in: &str, |
| 450 | counts: &[(&'static str, String)], |
| 451 | recent: &[(String, String, String)], |
| 452 | ) -> Result<impl View> { |
| 453 | let role_line = fill(t(lang, K::PersonRole), &[role]); |
| 454 | Ok(view! { |
| 455 | <h1>(name)</h1> |
| 456 | <p class="field-hint">(role_line.as_str())</p> |
| 457 | <section class="card"> |
| 458 | <dl class="facts"> |
| 459 | for (label, value) in counts.iter() { |
| 460 | <dt>(*label)</dt><dd>(value.as_str())</dd> |
| 461 | } |
| 462 | <dt>(t(lang, K::PersonZone))</dt><dd>(zone)</dd> |
| 463 | <dt>(t(lang, K::PersonLastSignIn))</dt><dd>(last_sign_in)</dd> |
| 464 | </dl> |
| 465 | </section> |
| 466 | <section class="card"> |
| 467 | <h2>(t(lang, K::PersonRecent))</h2> |
| 468 | if recent.is_empty() { |
| 469 | <p class="muted">(t(lang, K::PersonNoActivity))</p> |
| 470 | } |
| 471 | <ul class="activity-list"> |
| 472 | for (when, sentence, task) in recent.iter() { |
| 473 | <li> |
| 474 | <time>(when.as_str())</time> |
| 475 | " " |
| 476 | (sentence.as_str()) |
| 477 | " " |
| 478 | <span>(task.as_str())</span> |
| 479 | </li> |
| 480 | } |
| 481 | </ul> |
| 482 | </section> |
| 483 | <p><a class="button ghost" href="/">(t(lang, K::PersonBack))</a></p> |
| 484 | }) |
| 485 | } |