summaryrefslogtreecommitdiffstats
path: root/src/chat.rs
blob: 66124314355a47923f82613adac6fea262fa0bee (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
use std::ops::{Deref, DerefMut};

use filamento::{chat::Chat, user::User};
use jid::BareJID;
use leptos_fetch::QueryClient;
use reactive_stores::ArcStore;
use leptos::prelude::*;

use crate::{client::Client, user::MacawUser};

#[derive(Clone)]
pub struct GotChat(Chat);

async fn get_chat(jid: BareJID) -> ArcStore<Chat> {
    // let client: Client = use_context().expect("no client in context");
    // ArcStore::new(client.get_chat(jid).await.unwrap())
    let GotChat(chat) = use_context().expect("no chat in context");
    ArcStore::new(chat)
}

#[derive(Clone, Copy)]
pub struct MacawChat {
    pub chat: LocalResource<ArcStore<Chat>>,
    pub user: MacawUser,
    // user: StateListener<BareJID, ArcStore<User>>,
}

impl MacawChat {
    pub fn got_chat_and_user(chat: Chat, user: User) -> Self {
        let query_client: QueryClient = expect_context();

        let jid = chat.correspondent.clone();
        provide_context(GotChat(chat));
        let chat = query_client.local_resource(get_chat, move || jid.clone());
        let user = MacawUser::got_user(user);
        Self {
            chat,
            user,
        }
    }
}

impl Deref for MacawChat {
    type Target = LocalResource<ArcStore<Chat>>;

    fn deref(&self) -> &Self::Target {
        &self.chat
    }
}

impl DerefMut for MacawChat {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.chat
    }
}