1module Matterhorn.State.ChannelListOverlay
2  ( enterChannelListOverlayMode
3
4  , channelListSelectDown
5  , channelListSelectUp
6  , channelListPageDown
7  , channelListPageUp
8  )
9where
10
11import           Prelude ()
12import           Matterhorn.Prelude
13
14import qualified Brick.Widgets.List as L
15import qualified Data.Vector as Vec
16import qualified Data.Sequence as Seq
17import           Data.Function ( on )
18import           Lens.Micro.Platform ( to )
19
20import           Network.Mattermost.Types
21import qualified Network.Mattermost.Endpoints as MM
22
23import           Matterhorn.State.ListOverlay
24import           Matterhorn.State.Channels
25import           Matterhorn.Types
26
27
28enterChannelListOverlayMode :: MH ()
29enterChannelListOverlayMode = do
30    myTId <- use csCurrentTeamId
31    myChannels <- use (csChannels.to (filteredChannelIds (const True)))
32    enterListOverlayMode (csTeam(myTId).tsChannelListOverlay) ChannelListOverlay
33        AllChannels enterHandler (fetchResults myTId myChannels)
34
35enterHandler :: Channel -> MH Bool
36enterHandler chan = do
37    joinChannel (getId chan)
38    return True
39
40fetchResults :: TeamId
41             -> [ChannelId]
42             -- ^ The channels to exclude from the results
43             -> ChannelSearchScope
44             -- ^ The scope to search
45             -> Session
46             -- ^ The connection session
47             -> Text
48             -- ^ The search string
49             -> IO (Vec.Vector Channel)
50fetchResults myTId exclude AllChannels session searchString = do
51    resultChans <- MM.mmSearchChannels myTId searchString session
52    let filteredChans = Seq.filter (\ c -> not (channelId c `elem` exclude)) resultChans
53        sortedChans = Vec.fromList $ toList $ Seq.sortBy (compare `on` channelName) filteredChans
54    return sortedChans
55
56-- | Move the selection up in the channel list overlay by one channel.
57channelListSelectUp :: MH ()
58channelListSelectUp = channelListMove L.listMoveUp
59
60-- | Move the selection down in the channel list overlay by one channel.
61channelListSelectDown :: MH ()
62channelListSelectDown = channelListMove L.listMoveDown
63
64-- | Move the selection up in the channel list overlay by a page of channels
65-- (channelListPageSize).
66channelListPageUp :: MH ()
67channelListPageUp = channelListMove (L.listMoveBy (-1 * channelListPageSize))
68
69-- | Move the selection down in the channel list overlay by a page of channels
70-- (channelListPageSize).
71channelListPageDown :: MH ()
72channelListPageDown = channelListMove (L.listMoveBy channelListPageSize)
73
74-- | Transform the channel list results in some way, e.g. by moving the
75-- cursor, and then check to see whether the modification warrants a
76-- prefetch of more search results.
77channelListMove :: (L.List Name Channel -> L.List Name Channel) -> MH ()
78channelListMove = listOverlayMove (csCurrentTeam.tsChannelListOverlay)
79
80-- | The number of channels in a "page" for cursor movement purposes.
81channelListPageSize :: Int
82channelListPageSize = 10
83