1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use cod_cli::notification::view::ViewNotificationArgs;
use cod_client::CodebergClient;
use cod_render::datetime::render_datetime_and_info;
use cod_render::ui::fuzzy_select_with_key;
use cod_types::api::notification::notification_thread::NotificationThread;

pub async fn view_notifications(
    args: ViewNotificationArgs,
    client: &CodebergClient,
) -> anyhow::Result<()> {
    let selected_notification_thread = if let Some(thread_id) = args.id {
        let notification_thread = client.get_notification_thread(thread_id).await?;
        tracing::debug!("{notification_thread:?}");
        notification_thread
    } else {
        let notification_threads_list = client.get_all_notifications_unfiltered(args.all).await?;

        let notification_thread =
            fuzzy_select_with_key(notification_threads_list, "notification thread")?
                .ok_or_else(|| anyhow::anyhow!("Nothing selected. Aborting."))?;
        tracing::debug!("{notification_thread:?}");
        notification_thread
    };

    present_notification_thread_details(selected_notification_thread);

    Ok(())
}

fn present_notification_thread_details(notification_thread: NotificationThread) {
    use cod_render::prelude::*;
    use std::iter::once;

    let rows = once(Row::new([
        TableCell::new_with_alignment("Title", 1, Alignment::Center),
        TableCell::new_with_alignment(
            notification_thread.subject.title.as_str(),
            1,
            Alignment::Left,
        ),
    ]))
    .chain(once(Row::new([
        TableCell::new_with_alignment("URL", 1, Alignment::Center),
        TableCell::new_with_alignment(notification_thread.subject.html_url, 1, Alignment::Left),
    ])))
    .chain(once(Row::new([
        TableCell::new_with_alignment("Type", 1, Alignment::Center),
        TableCell::new_with_alignment(notification_thread.subject.notify_type, 1, Alignment::Left),
    ])))
    .chain(once(Row::new([
        TableCell::new_with_alignment("State", 1, Alignment::Center),
        TableCell::new_with_alignment(notification_thread.subject.state, 1, Alignment::Left),
    ])))
    .chain(once(Row::new([
        TableCell::new_with_alignment("Unread", 1, Alignment::Center),
        TableCell::new_with_alignment(
            if notification_thread.unread {
                "Yes"
            } else {
                "No"
            },
            1,
            Alignment::Left,
        ),
    ])))
    .chain(once(Row::new([
        TableCell::new_with_alignment("Pinned", 1, Alignment::Center),
        TableCell::new_with_alignment(
            if notification_thread.pinned {
                "Yes"
            } else {
                "No"
            },
            1,
            Alignment::Left,
        ),
    ])))
    .chain(once(Row::new([
        TableCell::new_with_alignment("Last updated", 1, Alignment::Center),
        TableCell::new_with_alignment(
            render_datetime_and_info(notification_thread.updated_at),
            1,
            Alignment::Left,
        ),
    ])));

    let table = CodTableBuilder::new()
        .with_max_column_width(100)
        .add_rows(rows)
        .build();

    println!("{}", table.render());
}