From d8cc8d11866a3eaa77b2e0d12b870bc9f23c8e6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 20 Jul 2025 22:03:48 +0200 Subject: Conversation list and selection --- src/Main.hs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/Main.hs') diff --git a/src/Main.hs b/src/Main.hs index 75abd05..8fa2931 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -48,6 +48,7 @@ data GlobalState = GlobalState , globalHead :: Head LocalState , peerListVar :: MVar [ ( Peer, String ) ] , currentConversationVar :: MVar (Maybe Conversation) + , conversationsVar :: MVar [ ( Int, Conversation ) ] } initGlobalState :: IO GlobalState @@ -58,6 +59,7 @@ initGlobalState = do { lsPrev = Nothing, lsIdentity = idExtData identity, lsShared = [], lsOther = [] } peerListVar <- newMVar [] currentConversationVar <- newMVar Nothing + conversationsVar <- newMVar [] return GlobalState {..} foreign export javascript setup :: IO () @@ -85,6 +87,11 @@ setup = do H.input ! A.id "msg_text" ! A.type_ "text" H.input ! A.type_ "submit" ! A.value "send" H.hr + H.div $ do + H.h2 $ do + "Conversations" + H.div ! A.id "conversation_list" $ return () + H.hr H.div $ do H.h2 $ do "Peers" @@ -96,6 +103,7 @@ setup = do gs@GlobalState {..} <- initGlobalState watchIdentityUpdates gs + watchConversations gs let devName = T.pack "WebApp" let st = globalStorage @@ -203,6 +211,29 @@ interactiveIdentityUpdate name fidentity = do } +watchConversations :: GlobalState -> IO () +watchConversations gs@GlobalState {..} = do + void $ watchHead globalHead $ \ls -> do + modifyMVar_ conversationsVar $ \_ -> do + conversations <- zip [1 ..] . fst <$> + runLocalHeadT lookupConversations globalStorage (headStoredObject ls) + + convList <- JS.getElementById "conversation_list" + ul <- js_document_createElement (toJSString "ul") + forM_ conversations $ \( _, conv ) -> do + a <- js_document_createElement (toJSString "a") + js_setAttribute a (toJSString "href") (toJSString "javascript:void(0)") + JS.addEventListener a "click" $ \_ -> do + selectConversation gs conv + + li <- js_document_createElement (toJSString "li") + js_set_textContent a $ toJSString $ T.unpack $ conversationName conv + js_appendChild li a + js_appendChild ul li + js_replaceChildren convList ul + + return conversations + selectConversation :: GlobalState -> Conversation -> IO () selectConversation GlobalState {..} conv = do void $ swapMVar currentConversationVar (Just conv) -- cgit v1.2.3