//! Ponos-bord in het hoofdpaneel: de tegel «Ponos» in de navigatierail, direct //! onder Agents (Tim). Links alle groepen die je in Ponos ziet (eerst //! «Mijn Taken», je persoonlijke groep), rechts het bord met de kolommen van //! Ponos. Taken maken, bewerken, afronden en slepen tussen en binnen kolommen. //! //! Upstream-haken: `views/rail.rs` (tegel), `views/sidebar.rs` (`SidebarTab::Ponos`), //! `views/shell.rs` (hoofdpaneel). Zie KVT-FORK.md. use std::collections::HashMap; use gpui_kit::component::input::{Input, InputState, Textarea, TextareaState}; use gpui_kit::component::scroll::ScrollableElement as _; use gpui_kit::component::{h_flex, v_flex}; use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::*; use kvt_sleutels::format::{iso_date_nl, parse_date_input}; use kvt_sleutels::ponos::{ MY_TASKS_GROUP, PonosGroup, PonosNavigation, PonosTask, STATUS_DONE, STATUSES, TaskInput, apply_move, column, drop_position, group_url, name_from_email, }; use kvt_sleutels::{CardsClient, ClientError}; use super::modal::{primary_button, secondary_button}; use super::tasks::{chip, field, label, muted, not_linked, ponos_error}; use super::{Link, base_url, forget_token, is_demo, link, notice, open_external, token_and_client}; use crate::app_state::AppHandle; use crate::i18n::tr; use crate::runtime; use crate::theme::{self, ElevatedSurface as _}; use crate::views::widgets::symbol; pub const KVT_BOARD_TAG: &str = "kvt-ponos-board"; /// Laatst gekozen groep op het bord (alleen een id, in de lokale opslag). const BOARD_GROUP_META_KEY: &str = "kvt.ponos.board_group"; const TITLE_MAX: usize = 200; const HEADER_HEIGHT: f32 = 60.; const PAGE_PADDING: f32 = 20.; const GROUPS_WIDTH: f32 = 240.; const GROUP_ROW_HEIGHT: f32 = 36.; const COLUMN_MIN_WIDTH: f32 = 220.; const COLUMN_GAP: f32 = 12.; const CARD_GAP: f32 = 8.; const RADIUS: f32 = 10.; const EDITOR_WIDTH: f32 = 560.; const BACKDROP_OPACITY: f32 = 0.55; #[derive(Clone)] enum Nav { NotLinked, Loading, Loaded(PonosNavigation), Failed(ClientError), } #[derive(Clone)] enum Tasks { Loading, Loaded(Vec), Failed(ClientError), } #[derive(Clone)] struct DraggedTask { id: String, title: String, from_status: String, } struct TaskGhost { title: String, } impl Render for TaskGhost { fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { div() .w(px(COLUMN_MIN_WIDTH)) .px(px(12.)) .py(px(10.)) .rounded(px(8.)) .flyout_surface() .text_size(px(13.)) .text_color(theme::text()) .truncate() .child(self.title.clone()) } } struct Editor { /// `None`: nieuwe taak. task: Option, status: String, assignee: String, target_group: Option, title: Entity, description: Entity, due: Entity, saving: bool, error: Option, } pub struct PonosBoard { nav: Nav, group: Option, tasks: Tasks, generation: u64, editor: Option, /// Alleen `--demo`: verzonnen taken per groep. demo: HashMap>, } /// Het bord als element van het hoofdpaneel (eenmalig aangemaakt). pub fn board_view(cx: &mut App) -> AnyElement { let existing = super::read(cx, |kvt| kvt.board.clone()).flatten(); let board = match existing { Some(board) => board, None => { let board = cx.new(PonosBoard::new); if cx.has_global::() { cx.global_mut::().board = Some(board.clone()); } board } }; div() .flex_1() .min_w_0() .h_full() .child(board) .into_any_element() } /// De tegel is geopend: vers ophalen (bij de eerste keer laadt `new`). pub fn board_opened(cx: &mut App) { if let Some(board) = super::read(cx, |kvt| kvt.board.clone()).flatten() { board.update(cx, |board, cx| board.reload(cx)); } } /// Na «Taak maken in Ponos» vanuit een bericht. pub(super) fn refresh_board_if_open(cx: &mut App) { board_opened(cx); } fn remembered_group(cx: &App) -> Option { let handle = cx.try_global::()?; handle .0 .read(cx) .store .meta(BOARD_GROUP_META_KEY) .ok() .flatten() } fn remember_group(group: &str, cx: &App) { if let Some(handle) = cx.try_global::() { let _ = handle .0 .read(cx) .store .set_meta(BOARD_GROUP_META_KEY, group); } } /// Gekozen groep als die nog bestaat, anders de eerste (Mijn Taken). fn pick_group(wanted: Option<&str>, navigation: &PonosNavigation) -> Option { wanted .filter(|wanted| navigation.groups.iter().any(|group| group.id == *wanted)) .map(str::to_owned) .or_else(|| navigation.groups.first().map(|group| group.id.clone())) } fn status_fallback(status: &str) -> String { tr(match status { "in_progress" => "kvt.board_status.in_progress", STATUS_DONE => "kvt.board_status.done", _ => "kvt.board_status.todo", }) } impl PonosBoard { fn new(cx: &mut Context) -> Self { let demo = if is_demo(cx) { super::demo::board_tasks() } else { HashMap::new() }; let mut board = PonosBoard { nav: Nav::Loading, group: remembered_group(cx), tasks: Tasks::Loading, generation: 0, editor: None, demo, }; board.reload(cx); board } fn navigation(&self) -> Option<&PonosNavigation> { match &self.nav { Nav::Loaded(navigation) => Some(navigation), _ => None, } } fn current_group(&self) -> Option<&PonosGroup> { let group = self.group.as_deref()?; self.navigation()? .groups .iter() .find(|candidate| candidate.id == group) } fn user_email(&self) -> String { self.navigation() .map(|navigation| navigation.user_email.clone()) .unwrap_or_default() } fn status_label(&self, status: &str) -> String { match self.navigation() { Some(navigation) => navigation.status_label(status, || status_fallback(status)), None => status_fallback(status), } } // --- laden ------------------------------------------------------------ fn reload(&mut self, cx: &mut Context) { self.generation += 1; if is_demo(cx) { let navigation = super::demo::board_navigation(&self.demo); self.group = pick_group(self.group.as_deref(), &navigation); self.tasks = Tasks::Loaded(self.demo_tasks()); self.nav = Nav::Loaded(navigation); cx.notify(); return; } let Some((token, client)) = token_and_client(cx) else { self.nav = Nav::NotLinked; cx.notify(); return; }; if !matches!(self.nav, Nav::Loaded(_)) { self.nav = Nav::Loading; } let generation = self.generation; let wanted = self.group.clone(); let loading = runtime::spawn(async move { let navigation = client.ponos_navigation(&token).await?; let group = pick_group(wanted.as_deref(), &navigation); let tasks = match &group { Some(group) => client.ponos_tasks(&token, group).await, None => Ok(Vec::new()), }; Ok::<_, ClientError>((navigation, group, tasks)) }); cx.spawn(async move |this, cx| { let result = loading.await.unwrap_or(Err(ClientError::Unavailable)); cx.update(|cx| { let unauthorized = matches!(&result, Err(ClientError::Unauthorized)) || matches!(&result, Ok((_, _, Err(ClientError::Unauthorized)))); if unauthorized { forget_token(cx); } this.update(cx, |this, cx| { if this.generation != generation { return; } match result { Ok((navigation, group, tasks)) => { this.group = group; this.tasks = match tasks { Ok(tasks) => Tasks::Loaded(tasks), Err(error) => Tasks::Failed(error), }; this.nav = Nav::Loaded(navigation); } Err(error) => this.nav = Nav::Failed(error), } cx.notify(); }) .ok(); }); }) .detach(); } fn select_group(&mut self, group: String, cx: &mut Context) { if self.group.as_deref() == Some(group.as_str()) { return; } remember_group(&group, cx); self.group = Some(group); self.tasks = Tasks::Loading; self.editor = None; self.reload(cx); } // --- demo ------------------------------------------------------------- fn demo_tasks(&self) -> Vec { let Some(group) = self.group.as_deref() else { return Vec::new(); }; if group == MY_TASKS_GROUP { let mut mine: Vec = self .demo .values() .flatten() .filter(|task| task.assignee_email == super::demo::BOARD_USER) .cloned() .collect(); mine.sort_by(|a, b| a.id.cmp(&b.id)); return mine; } self.demo.get(group).cloned().unwrap_or_default() } fn demo_task_mut(&mut self, id: &str) -> Option<&mut PonosTask> { self.demo.values_mut().flatten().find(|task| task.id == id) } // --- acties ----------------------------------------------------------- /// Een schrijfactie bij Ponos; daarna altijd opnieuw laden. fn submit( &mut self, cx: &mut Context, done: Option, job: impl FnOnce(String, CardsClient) -> Fut, ) where Fut: std::future::Future> + Send + 'static, { let Some((token, client)) = token_and_client(cx) else { self.reload(cx); return; }; let running = runtime::spawn(job(token, client)); cx.spawn(async move |this, cx| { let result = running.await.unwrap_or(Err(ClientError::Unavailable)); cx.update(|cx| { match &result { Ok(()) => { if let Some(done) = done { notice(done, cx); } } Err(error) => { if *error == ClientError::Unauthorized { forget_token(cx); } notice(ponos_error(error), cx); } } this.update(cx, |this, cx| { if result.is_ok() { this.editor = None; } else if let Some(editor) = this.editor.as_mut() { editor.saving = false; } this.reload(cx); }) .ok(); }); }) .detach(); } fn drop_task( &mut self, dragged: &DraggedTask, status: &'static str, before: Option, cx: &mut Context, ) { let Some(group) = self.group.clone() else { return; }; let Tasks::Loaded(tasks) = &mut self.tasks else { return; }; let position = drop_position(tasks, &dragged.id, status, before); let unchanged = dragged.from_status == status && column(tasks, status).position(|task| task.id == dragged.id) == Some(position); if unchanged || !apply_move(tasks, &dragged.id, status, position) { return; } cx.notify(); let id = dragged.id.clone(); if is_demo(cx) { if let Some(task) = self.demo_task_mut(&id) { task.status = status.to_owned(); } if group != MY_TASKS_GROUP && let Some(list) = self.demo.get_mut(&group) { apply_move(list, &id, status, position); } return; } let virtual_group = group == MY_TASKS_GROUP; let status_changed = dragged.from_status != status; if virtual_group && !status_changed { // «Mijn Taken» heeft geen eigen volgorde in Ponos. return; } self.submit(cx, None, move |token, client| async move { if virtual_group { client.ponos_set_status(&token, &group, &id, status).await?; } else { client .ponos_reorder_task(&token, &group, &id, status, position) .await?; } Ok(()) }); } fn open_editor( &mut self, task: Option, status: &str, window: &mut Window, cx: &mut Context, ) { let (title_text, description_text, due_text) = match &task { Some(task) => ( task.title.clone(), task.description.clone(), iso_date_nl(&task.due_date).unwrap_or_default(), ), None => Default::default(), }; let title = cx.new(|cx| { InputState::new(window, cx) .placeholder(tr("kvt.ponos.title")) .default_value(title_text) }); let description = cx.new(|cx| { TextareaState::new(window, cx) .auto_grow(4, 10) .placeholder(tr("kvt.ponos.description")) .default_value(description_text) }); let due = cx.new(|cx| { InputState::new(window, cx) .placeholder(tr("kvt.board.due_placeholder")) .default_value(due_text) }); title.update(cx, |input, cx| input.focus(window, cx)); let assignee = match &task { Some(task) => task.assignee_email.clone(), // Nieuw in «Mijn Taken» kan niet; elders standaard niemand. None => String::new(), }; self.editor = Some(Editor { status: task .as_ref() .map_or_else(|| status.to_owned(), |task| task.status.clone()), assignee, target_group: None, task, title, description, due, saving: false, error: None, }); cx.notify(); } fn save_editor(&mut self, cx: &mut Context) { let Some(group) = self.group.clone() else { return; }; let Some(editor) = self.editor.as_mut() else { return; }; if editor.saving { return; } let title: String = editor .title .read(cx) .value() .trim() .chars() .take(TITLE_MAX) .collect(); let description = editor.description.read(cx).value().trim().to_owned(); if title.is_empty() || description.is_empty() { editor.error = Some(tr("kvt.ponos.fill_in")); cx.notify(); return; } let Some(due_date) = parse_date_input(&editor.due.read(cx).value()) else { editor.error = Some(tr("kvt.board.bad_date")); cx.notify(); return; }; let input = TaskInput { title, description, due_date, assignee_email: editor.assignee.clone(), target_group: editor.target_group.clone(), }; let status = editor.status.clone(); let existing = editor.task.clone(); editor.saving = true; editor.error = None; cx.notify(); if is_demo(cx) { self.save_demo(&group, existing, input, status); self.editor = None; notice(tr("kvt.board.saved"), cx); self.reload(cx); return; } let done = tr(if existing.is_some() { "kvt.board.saved" } else { "kvt.ponos.created" }); self.submit(cx, Some(done), move |token, client| async move { match existing { Some(task) => { let saved = client .ponos_update_task(&token, &group, &task.id, &input) .await?; if saved.status != status { let home = input.target_group.as_deref().unwrap_or(&group); client .ponos_set_status(&token, home, &saved.id, &status) .await?; } } None => { let created = client.ponos_create(&token, &group, &input).await?; if created.status != status { client .ponos_set_status(&token, &group, &created.id, &status) .await?; } } } Ok(()) }); } fn save_demo( &mut self, group: &str, existing: Option, input: TaskInput, status: String, ) { match existing { Some(task) => { let home = task.group().to_owned(); let Some(list) = self.demo.get_mut(&home) else { return; }; let Some(index) = list.iter().position(|other| other.id == task.id) else { return; }; let mut updated = list.remove(index); updated.title = input.title; updated.description = input.description; updated.due_date = input.due_date; updated.assignee_email = input.assignee_email; let target = input.target_group.unwrap_or(home.clone()); if target == home && updated.status == status { list.insert(index, updated); return; } updated.status = status; updated.group_id = target.clone(); updated.home_group_id = target.clone(); self.demo.entry(target).or_default().push(updated); } None => { let id = format!( "demo-board-{}", self.demo.values().map(Vec::len).sum::() + 1 ); let task = PonosTask { id, group_id: group.to_owned(), home_group_id: group.to_owned(), title: input.title, status, due_date: input.due_date, description: input.description, assignee_email: input.assignee_email, ..super::demo::empty_task() }; self.demo.entry(group.to_owned()).or_default().push(task); } } } fn complete_from_editor(&mut self, cx: &mut Context) { let Some(group) = self.group.clone() else { return; }; let Some(task) = self.editor.as_ref().and_then(|editor| editor.task.clone()) else { return; }; if is_demo(cx) { if let Some(demo) = self.demo_task_mut(&task.id) { demo.status = STATUS_DONE.to_owned(); } self.editor = None; notice(tr("kvt.tasks.completed"), cx); self.reload(cx); return; } if let Some(editor) = self.editor.as_mut() { editor.saving = true; } self.submit( cx, Some(tr("kvt.tasks.completed")), move |token, client| async move { client.ponos_complete_task(&token, &group, &task.id).await }, ); } // --- weergave --------------------------------------------------------- fn groups_list(&self, cx: &mut Context) -> AnyElement { let Some(navigation) = self.navigation() else { return div().into_any_element(); }; v_flex() .id("kvt-board-groups") .w(px(GROUPS_WIDTH)) .h_full() .flex_none() .p(px(8.)) .gap(px(2.)) .border_r_1() .border_color(theme::border()) .overflow_y_scrollbar() .children(navigation.groups.iter().enumerate().map(|(index, group)| { let selected = self.group.as_deref() == Some(group.id.as_str()); let id = group.id.clone(); h_flex() .id(ElementId::Name(format!("kvt-board-group-{index}").into())) .w_full() .h(px(GROUP_ROW_HEIGHT)) .flex_none() .px(px(10.)) .gap(px(8.)) .items_center() .rounded(px(6.)) .cursor_pointer() .text_size(px(13.)) .map(|row| { if selected { row.bg(theme::selection()) .text_color(theme::text_strong()) .font_weight(FontWeight::SEMIBOLD) } else { row.text_color(theme::text()) .hover(|row| row.bg(theme::row_hover())) } }) .on_click(cx.listener(move |this, _, _, cx| this.select_group(id.clone(), cx))) .child(symbol( if group.is_virtual { "person" } else { "group" }, 16., if selected { theme::accent_text() } else { theme::text_muted() }, )) .child( div() .flex_1() .min_w_0() .truncate() .child(group.name.clone()), ) .when(group.task_count > 0, |row| { row.child( div() .flex_none() .text_size(px(12.)) .text_color(theme::text_muted()) .child(group.task_count.to_string()), ) }) })) .into_any_element() } fn header(&self, cx: &mut Context) -> Div { let group = self.current_group(); let title = group.map_or_else(|| tr("kvt.board.title"), |group| group.name.clone()); let can_create = group.is_some_and(PonosGroup::accepts_new_tasks) && matches!(self.tasks, Tasks::Loaded(_)); let url = match self.group.as_deref() { Some(group) => group_url(&base_url(), group), None => format!("{}/ponos/", base_url()), }; h_flex() .h(px(HEADER_HEIGHT)) .flex_none() .px(px(PAGE_PADDING)) .gap(px(8.)) .items_center() .child( div() .flex_1() .min_w_0() .truncate() .text_size(px(16.)) .font_weight(FontWeight::BOLD) .text_color(theme::text()) .child(title), ) .when(can_create, |row| { row.child( primary_button("kvt-board-new", tr("kvt.board.new_task")).on_click( cx.listener(|this, _, window, cx| { this.open_editor(None, STATUSES[0], window, cx) }), ), ) }) .child( secondary_button("kvt-board-refresh", "refresh", tr("kvt.card.refresh")) .on_click(cx.listener(|this, _, _, cx| this.reload(cx))), ) .child( secondary_button("kvt-board-open", "open_in_new", tr("kvt.tasks.open_ponos")) .on_click(move |_, _, cx| open_external(&url, cx)), ) } fn columns(&self, cx: &mut Context) -> AnyElement { let tasks = match &self.tasks { Tasks::Loading => { return muted(tr("kvt.card.loading")) .px(px(PAGE_PADDING)) .into_any_element(); } Tasks::Failed(error) => { return muted(ponos_error(error)) .px(px(PAGE_PADDING)) .into_any_element(); } Tasks::Loaded(tasks) => tasks, }; let group = self.current_group(); let editable = group.is_some_and(PonosGroup::editable); let can_create = group.is_some_and(PonosGroup::accepts_new_tasks); let me = self.user_email(); h_flex() .id("kvt-board-columns") .flex_1() .min_h_0() .w_full() .px(px(PAGE_PADDING)) .pb(px(PAGE_PADDING)) .gap(px(COLUMN_GAP)) .items_start() .overflow_x_scrollbar() .children( STATUSES .iter() .map(|&status| self.column(tasks, status, editable, can_create, &me, cx)), ) .into_any_element() } fn column( &self, tasks: &[PonosTask], status: &'static str, editable: bool, can_create: bool, me: &str, cx: &mut Context, ) -> AnyElement { let cards: Vec<&PonosTask> = column(tasks, status).collect(); let today = chrono::Local::now().format("%Y-%m-%d").to_string(); let dot = match status { "in_progress" => theme::accent(), STATUS_DONE => theme::green(), _ => theme::text_faint(), }; v_flex() .id(ElementId::Name(format!("kvt-board-column-{status}").into())) .debug_selector(move || format!("kvt-board/column/{status}")) .flex_1() .min_w(px(COLUMN_MIN_WIDTH)) .h_full() .p(px(10.)) .gap(px(CARD_GAP)) .rounded(px(RADIUS)) .bg(theme::surface()) .drag_over::(|style, _, _, _| style.bg(theme::drop_background())) .on_drop(cx.listener(move |this, dragged: &DraggedTask, _, cx| { this.drop_task(dragged, status, None, cx) })) .child( h_flex() .w_full() .h(px(28.)) .flex_none() .gap(px(8.)) .items_center() .child(div().size(px(8.)).flex_none().rounded_full().bg(dot)) .child( div() .flex_1() .min_w_0() .truncate() .text_size(px(13.)) .font_weight(FontWeight::SEMIBOLD) .text_color(theme::text()) .child(self.status_label(status)), ) .child( div() .text_size(px(12.)) .text_color(theme::text_muted()) .child(cards.len().to_string()), ) .when(can_create, |row| { row.child( div() .id(ElementId::Name(format!("kvt-board-add-{status}").into())) .size(px(24.)) .flex_none() .flex() .items_center() .justify_center() .rounded_full() .cursor_pointer() .hover(|button| button.bg(theme::row_hover())) .on_click(cx.listener(move |this, _, window, cx| { this.open_editor(None, status, window, cx) })) .child(symbol("add", 16., theme::text_muted())), ) }), ) .child( v_flex() .id(ElementId::Name(format!("kvt-board-cards-{status}").into())) .w_full() .flex_1() .min_h(px(80.)) .gap(px(CARD_GAP)) .overflow_y_scrollbar() .when(cards.is_empty(), |list| { list.child( div() .w_full() .py(px(16.)) .text_center() .text_size(px(12.)) .text_color(theme::text_faint()) .child(tr("kvt.board.empty_column")), ) }) .children(cards.iter().enumerate().map(|(index, task)| { self.card(task, status, index, editable, me, &today, cx) })), ) .into_any_element() } #[allow(clippy::too_many_arguments)] fn card( &self, task: &PonosTask, status: &'static str, index: usize, editable: bool, me: &str, today: &str, cx: &mut Context, ) -> AnyElement { let movable = editable && task.can_edit; let overdue = status != STATUS_DONE && !task.due_date.is_empty() && task.due_date.as_str() < today; let due = iso_date_nl(&task.due_date); let assignee = if task.assignee_email.is_empty() { None } else if task.assignee_email.eq_ignore_ascii_case(me) { Some(tr("kvt.board.assignee_me")) } else { Some(name_from_email(&task.assignee_email)) }; let in_my_tasks = self.group.as_deref() == Some(MY_TASKS_GROUP); let open = task.clone(); let dragged = DraggedTask { id: task.id.clone(), title: task.title.clone(), from_status: task.status.clone(), }; v_flex() .id(ElementId::Name( format!("kvt-board-card-{}", task.id).into(), )) .w_full() .flex_none() .px(px(12.)) .py(px(10.)) .gap(px(6.)) .rounded(px(8.)) .bg(theme::surface_raised()) .cursor_pointer() .hover(|card| card.bg(theme::popup_hover())) .on_click(cx.listener(move |this, _, window, cx| { this.open_editor(Some(open.clone()), &open.status, window, cx) })) .when(movable, |card| { card.on_drag(dragged, |dragged, _, _, cx| { let title = dragged.title.clone(); cx.new(|_| TaskGhost { title }) }) .drag_over::(|style, _, _, _| style.bg(theme::drop_background())) .on_drop(cx.listener( move |this, dragged: &DraggedTask, _, cx| { cx.stop_propagation(); this.drop_task(dragged, status, Some(index), cx) }, )) }) .when(in_my_tasks && !task.home_group_name.is_empty(), |card| { card.child( div() .truncate() .text_size(px(11.)) .text_color(theme::text_muted()) .child(task.home_group_name.clone()), ) }) .child( div() .text_size(px(13.)) .line_height(px(18.)) .text_color(if status == STATUS_DONE { theme::text_muted() } else { theme::text() }) .when(status == STATUS_DONE, |text| text.line_through()) .child(task.title.clone()), ) .child( h_flex() .w_full() .flex_wrap() .gap(px(8.)) .items_center() .text_size(px(11.)) .text_color(theme::text_muted()) .when(!task.category_label.is_empty(), |row| { row.child( div() .px(px(6.)) .py(px(1.)) .rounded(px(4.)) .bg(theme::selection()) .text_color(theme::text_strong()) .child(task.category_label.clone()), ) }) .children(due.map(|due| { h_flex() .gap(px(3.)) .items_center() .text_color(if overdue { theme::red_soft() } else { theme::text_muted() }) .child(symbol( "event", 12., if overdue { theme::red_soft() } else { theme::text_muted() }, )) .child(due) })) .when(task.checklist_total > 0, |row| { row.child( h_flex() .gap(px(3.)) .items_center() .child(symbol("check", 12., theme::text_muted())) .child(format!("{}/{}", task.checklist_done, task.checklist_total)), ) }) .children(assignee.map(|assignee| { h_flex() .gap(px(3.)) .items_center() .child(symbol("person", 12., theme::text_muted())) .child(assignee) })), ) .into_any_element() } fn editor_layer(&self, cx: &mut Context) -> Option { let editor = self.editor.as_ref()?; let navigation = self.navigation()?; let me = navigation.user_email.clone(); let existing = editor.task.clone(); let editable = existing.as_ref().is_none_or(|task| task.can_edit); let home = existing .as_ref() .map(|task| task.group().to_owned()) .or_else(|| self.group.clone()) .unwrap_or_default(); let target = editor.target_group.clone().unwrap_or_else(|| home.clone()); let title = tr(if existing.is_some() { "kvt.board.edit_task" } else { "kvt.ponos.dialog_title" }); let detail = existing .as_ref() .filter(|task| !task.home_group_name.is_empty()) .map(|task| task.home_group_name.clone()) .unwrap_or_default(); let url = existing.as_ref().map(|task| task.url(&base_url())); let move_groups: Vec = navigation .groups .iter() .filter(|group| !group.is_virtual && group.has_task_access) .cloned() .collect(); let other_assignee = (!editor.assignee.is_empty() && !editor.assignee.eq_ignore_ascii_case(&me)) .then(|| name_from_email(&editor.assignee)); let assignee_is_me = !me.is_empty() && editor.assignee.eq_ignore_ascii_case(&me); let status_chips = h_flex() .w_full() .flex_wrap() .gap(px(6.)) .children(STATUSES.iter().enumerate().map(|(index, &status)| { chip( format!("kvt-board-status-{index}").into(), self.status_label(status), editor.status == status, ) .on_click(cx.listener(move |this, _, _, cx| { if let Some(editor) = this.editor.as_mut() { editor.status = status.to_owned(); cx.notify(); } })) })); let assignee_chips = h_flex() .w_full() .flex_wrap() .gap(px(6.)) .child( chip( "kvt-board-assignee-none".into(), tr("kvt.board.assignee_none"), editor.assignee.is_empty(), ) .on_click(cx.listener(|this, _, _, cx| { if let Some(editor) = this.editor.as_mut() { editor.assignee.clear(); cx.notify(); } })), ) .when(!me.is_empty(), |row| { let me = me.clone(); row.child( chip( "kvt-board-assignee-me".into(), tr("kvt.board.assignee_me"), assignee_is_me, ) .on_click(cx.listener(move |this, _, _, cx| { if let Some(editor) = this.editor.as_mut() { editor.assignee = me.clone(); cx.notify(); } })), ) }) .children( other_assignee.map(|name| chip("kvt-board-assignee-other".into(), name, true)), ); let group_chips = (existing.is_some() && move_groups.len() > 1).then(|| { h_flex().w_full().flex_wrap().gap(px(6.)).children( move_groups.iter().enumerate().map(|(index, group)| { let id = group.id.clone(); let home = home.clone(); chip( format!("kvt-board-move-{index}").into(), group.name.clone(), group.id == target, ) .on_click(cx.listener(move |this, _, _, cx| { if let Some(editor) = this.editor.as_mut() { editor.target_group = (id != home).then(|| id.clone()); cx.notify(); } })) }), ) }); let can_complete = existing .as_ref() .is_some_and(|task| task.status != STATUS_DONE && task.can_edit); let panel = v_flex() .debug_selector(|| "kvt-board-editor".into()) .w(px(EDITOR_WIDTH)) .max_w(relative(1.)) .max_h(relative(0.92)) .m(px(32.)) .p(px(16.)) .gap(px(10.)) .rounded(px(RADIUS)) .dialog_surface() .text_color(theme::text()) .occlude() .child( h_flex() .w_full() .items_center() .gap(px(12.)) .child( v_flex() .flex_1() .min_w_0() .child( div() .text_size(px(15.)) .font_weight(FontWeight::SEMIBOLD) .child(title), ) .when(!detail.is_empty(), |column| { column.child( div() .truncate() .text_size(px(12.)) .text_color(theme::text_muted()) .child(detail), ) }), ) .child( div() .id("kvt-board-editor-close") .size(px(28.)) .flex_none() .flex() .items_center() .justify_center() .rounded_full() .cursor_pointer() .hover(|button| button.bg(theme::popup_hover())) .on_click(cx.listener(|this, _, _, cx| { this.editor = None; cx.notify(); })) .child(symbol("close", 16., theme::text_muted())), ), ) .child(label(tr("kvt.ponos.title"))) .child( field().child( div() .flex_1() .child(Input::new(&editor.title).appearance(false).bordered(false)), ), ) .child(label(tr("kvt.ponos.description"))) .child(Textarea::new(&editor.description)) .child(label(tr("kvt.board.status"))) .child(status_chips) .child(label(tr("kvt.board.due"))) .child( field().child( div() .flex_1() .child(Input::new(&editor.due).appearance(false).bordered(false)), ), ) .child(label(tr("kvt.board.assignee"))) .child(assignee_chips) .when_some(group_chips, |panel, chips| { panel.child(label(tr("kvt.ponos.group"))).child(chips) }) .children(editor.error.clone().map(|error| { div() .text_size(px(13.)) .text_color(theme::red_soft()) .child(error) })) .child( h_flex() .w_full() .gap(px(8.)) .items_center() .children(url.map(|url| { secondary_button( "kvt-board-editor-open", "open_in_new", tr("kvt.tasks.open_ponos"), ) .on_click(move |_, _, cx| open_external(&url, cx)) })) .child(div().flex_1()) .when(can_complete, |row| { row.child( secondary_button( "kvt-board-complete", "check_circle", tr("kvt.board.complete"), ) .on_click(cx.listener(|this, _, _, cx| this.complete_from_editor(cx))), ) }) .child( secondary_button("kvt-board-cancel", "close", tr("common.cancel")) .on_click(cx.listener(|this, _, _, cx| { this.editor = None; cx.notify(); })), ) .when(editable, |row| { row.child( primary_button( "kvt-board-save", tr(if editor.saving { "kvt.ponos.saving" } else { "kvt.board.save" }), ) .on_click(cx.listener(|this, _, _, cx| this.save_editor(cx))), ) }), ); Some( div() .id("kvt-board-editor-layer") .on_key_down(cx.listener(|this, event: &KeyDownEvent, _, cx| { if event.keystroke.key == "escape" { cx.stop_propagation(); this.editor = None; cx.notify(); } })) .absolute() .top_0() .left_0() .size_full() .flex() .items_center() .justify_center() .child( div() .id("kvt-board-editor-backdrop") .absolute() .top_0() .left_0() .size_full() .bg(black().opacity(BACKDROP_OPACITY)) .occlude() .on_click(cx.listener(|this, _, _, cx| { this.editor = None; cx.notify(); })), ) .child(panel) .into_any_element(), ) } } impl Render for PonosBoard { fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { let linked = matches!(link(cx), Link::Linked(_)); if !linked && !is_demo(cx) { // Ontkoppeld: na opnieuw koppelen vers laden, niets van de oude token tonen. self.nav = Nav::NotLinked; self.editor = None; } else if matches!(self.nav, Nav::NotLinked) { self.reload(cx); } let body = match (&self.nav, linked) { (_, false) => v_flex() .p(px(PAGE_PADDING)) .max_w(px(EDITOR_WIDTH)) .child(not_linked()) .into_any_element(), (Nav::NotLinked | Nav::Loading, _) => muted(tr("kvt.card.loading")) .px(px(PAGE_PADDING)) .into_any_element(), (Nav::Failed(error), _) => muted(ponos_error(error)) .px(px(PAGE_PADDING)) .into_any_element(), (Nav::Loaded(_), _) => h_flex() .flex_1() .min_h_0() .w_full() .child(self.groups_list(cx)) .child( v_flex() .flex_1() .min_w_0() .h_full() .child(self.header(cx)) .child(self.columns(cx)), ) .into_any_element(), }; let page_title = (!matches!(self.nav, Nav::Loaded(_)) || !linked).then(|| { h_flex() .h(px(HEADER_HEIGHT)) .flex_none() .px(px(PAGE_PADDING)) .items_center() .child( div() .text_size(px(16.)) .font_weight(FontWeight::BOLD) .text_color(theme::text()) .child(tr("kvt.board.title")), ) }); v_flex() .debug_selector(|| KVT_BOARD_TAG.into()) .relative() .size_full() .rounded(px(RADIUS)) .bg(theme::surface()) .overflow_hidden() .children(page_title) .child(body) .children(self.editor_layer(cx)) } } #[cfg(test)] mod tests { use super::pick_group; use kvt_sleutels::ponos::{PonosGroup, PonosNavigation}; fn group(id: &str) -> PonosGroup { PonosGroup { id: id.to_owned(), name: id.to_owned(), is_virtual: false, can_create_tasks: true, has_task_access: true, task_count: 0, } } #[test] fn remembered_group_wins_when_it_still_exists() { let navigation = PonosNavigation { groups: vec![group("__my_tasks__"), group("g1")], ..Default::default() }; assert_eq!(pick_group(Some("g1"), &navigation).as_deref(), Some("g1")); assert_eq!( pick_group(Some("weg"), &navigation).as_deref(), Some("__my_tasks__") ); assert_eq!(pick_group(None, &PonosNavigation::default()), None); } }