summaryrefslogtreecommitdiffstats
path: root/src/components/roster_list/roster_list_item.rs
blob: 842f66ca33cfd804eac735564f9e549b76f9ad6e (plain) (blame)
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
use std::ops::Deref;

use filamento::{chat::Chat, roster::{Contact, ContactStoreFields}, user::{User, UserStoreFields}};
use leptos::prelude::*;
use reactive_stores::{ArcStore, Store};
use tracing::debug;

use crate::{chat::{ArcMacawChat, MacawChat}, components::{avatar::AvatarWithPresence, sidebar::Open}, contact::MacawContact, open_chats::{OpenChatsPanel, OpenChatsPanelStoreFields}, user::get_name};

#[component]
pub fn RosterListItem(contact: MacawContact) -> impl IntoView {
    let name = move || get_name(contact.user.get().into(), false);

    let open_chats: Store<OpenChatsPanel> =
        use_context().expect("no open chats panel store in context");

    // TODO: why can this not be in the closure?????
    // TODO: not good, as overwrites preexisting chat state with possibly incorrect one...
    let chat = Chat {
        correspondent: contact.user.get().jid().get(),
        have_chatted: false,
    };
    let chat = ArcMacawChat::got_chat_and_user(chat, contact.user.get().get());

    let open_chat = move |_| {
        debug!("opening chat");
        open_chats.update(|open_chats| open_chats.open(Clone::clone(&chat)));
    };

    let open = move || {
        if let Some(open_chat) = &*open_chats.chat_view().read() {
            debug!("got open chat: {:?}", open_chat);
            if *open_chat == *contact.user.get().jid().read() {
                return Open::Focused;
            }
        }
        if let Some(_backgrounded_chat) = open_chats
            .chats()
            .read()
            .get(contact.user.get().jid().read().deref())
        {
            return Open::Open;
        }
        Open::Closed
    };
    let focused = move || open().is_focused();
    let open = move || open().is_open();

    view! {
        <div class="roster-list-item" class:open=move || open() class:focused=move || focused() on:click=open_chat>
            {move || {
                let user = contact.user.get().into();
                view! {
                    <AvatarWithPresence user />
                }
            }}
            <div class="item-info">
                <div class="main-info"><p class="name">{name}<span class="jid"> - {move || contact.user_jid().read().to_string()}</span></p></div>
                <div class="sub-info">{move || contact.subscription().read().to_string()}</div>
            </div>
        </div>
    }
}