Skip to main content

things3_cloud/ui/components/
checklist.rs

1use iocraft::prelude::*;
2
3use crate::{
4    ids::matching::longest_shortest_unique_prefix_len,
5    store::ChecklistItem,
6    ui::components::checklist_item::CheckListRow,
7};
8
9#[derive(Default, Props)]
10pub struct CheckListProps<'a> {
11    pub items: Option<&'a [ChecklistItem]>,
12    pub show_ids: bool,
13    pub shift_left: bool,
14}
15
16#[component]
17pub fn CheckList<'a>(props: &CheckListProps<'a>) -> impl Into<AnyElement<'a>> {
18    let Some(items) = props.items else {
19        return element!(Fragment).into_any();
20    };
21
22    let prefix_len = prefix_len(items, props.show_ids);
23
24    let margin_left = if props.shift_left && prefix_len > 0 {
25        -(prefix_len as i32 + 3)
26    } else {
27        0
28    };
29
30    let items = items.iter().enumerate().map(move |(i, item)| {
31        let is_last = i == items.len() - 1;
32        element!(CheckListRow(item, id_prefix_len: prefix_len, is_last))
33    });
34
35    element! {
36        View(flex_direction: FlexDirection::Column, margin_left) {
37            #(items)
38        }
39    }
40    .into_any()
41}
42
43fn prefix_len(items: &[ChecklistItem], show_ids: bool) -> usize {
44    if !show_ids || items.is_empty() {
45        return 0;
46    }
47
48    let ids = items.iter().map(|i| i.uuid.clone()).collect::<Vec<_>>();
49    longest_shortest_unique_prefix_len(&ids)
50}