Skip to content

Commit

Permalink
Merge pull request #808 from The-Commit-Company/show-unread-channels-…
Browse files Browse the repository at this point in the history
…dms-separately

feat: show unread channels and DMs in a separate section
  • Loading branch information
nikkothari22 authored Apr 4, 2024
2 parents 59734af + 7508844 commit 3053986
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 15 deletions.
20 changes: 14 additions & 6 deletions raven-app/src/components/feature/channels/ChannelList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CreateChannelButton } from "./CreateChannelModal"
import { useContext, useMemo, useState } from "react"
import { ChannelListContext, ChannelListContextType, ChannelListItem, UnreadCountData } from "../../../utils/channel/ChannelListProvider"
import { ChannelIcon } from "@/utils/layout/channelIcon"
import { Flex, Text } from "@radix-ui/themes"
import { Box, Flex, Text } from "@radix-ui/themes"
import { useLocation, useParams } from "react-router-dom"

export const ChannelList = ({ unread_count }: { unread_count?: UnreadCountData }) => {
Expand All @@ -22,11 +22,14 @@ export const ChannelList = ({ unread_count }: { unread_count?: UnreadCountData }
<SidebarGroupItem gap='2' className={'pl-1.5'}>
<SidebarViewMoreButton onClick={toggle} />
<Flex width='100%' justify='between' align='center' gap='2'>
<Flex gap='3' align='center'>
<Flex gap='2' align='center'>
<SidebarGroupLabel className='cal-sans'>Channels</SidebarGroupLabel>
<CreateChannelButton updateChannelList={mutate} />
</Flex>
{!showData && unread_count && unread_count.total_unread_count_in_channels > 0 && <SidebarBadge>{unread_count.total_unread_count_in_channels}</SidebarBadge>}
{!showData && unread_count && unread_count.total_unread_count_in_channels > 0 &&
<Box pr='2'>
<SidebarBadge>{unread_count.total_unread_count_in_channels}</SidebarBadge>
</Box>}
</Flex>
</SidebarGroupItem>
<SidebarGroup>
Expand All @@ -45,6 +48,12 @@ const ChannelItem = ({ channel, unreadCount }: { channel: ChannelListItem, unrea

const unreadCountForChannel = useMemo(() => unreadCount.find((unread) => unread.name == channel.name)?.unread_count, [channel.name, unreadCount])

return <ChannelItemElement channel={channel} unreadCount={unreadCountForChannel} />

}

export const ChannelItemElement = ({ channel, unreadCount }: { channel: ChannelListItem, unreadCount?: number }) => {

const { channelID } = useParams()

const { state } = useLocation()
Expand All @@ -53,16 +62,15 @@ const ChannelItem = ({ channel, unreadCount }: { channel: ChannelListItem, unrea
* Show the unread count if it exists and either the channel is not the current channel,
* or if it is the current channel, the user is viewing a base message
*/
const showUnread = unreadCountForChannel && (channelID !== channel.name || state?.baseMessage)
const showUnread = unreadCount && (channelID !== channel.name || state?.baseMessage)

return (
<SidebarItem to={channel.name} className={'py-1.5'}>
<ChannelIcon type={channel.type} size='18' />
<Flex justify='between' align={'center'} width='100%'>
<Text size='2' className="text-ellipsis line-clamp-1" as='span' weight={showUnread ? 'bold' : 'regular'}>{channel.channel_name}</Text>
{showUnread ? <SidebarBadge>{unreadCountForChannel}</SidebarBadge> : null}
{showUnread ? <SidebarBadge>{unreadCount}</SidebarBadge> : null}
</Flex>
</SidebarItem>
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const CreateChannelButton = ({ updateChannelList }: { updateChannelList:
return <Dialog.Root open={isOpen} onOpenChange={onOpenChange}>
<Dialog.Trigger>
<IconButton variant='ghost' size='1' color='gray' aria-label='Create Channel' className='h-[18px]' title='Create Channel'>
<BiPlus className='text-slate-12 mt-0.5' />
<BiPlus className='text-slate-12' />
</IconButton>
</Dialog.Trigger>
<Dialog.Content className={DIALOG_CONTENT_CLASS}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { UserContext } from "../../../utils/auth/UserProvider"
import { useGetUser } from "@/hooks/useGetUser"
import { useIsUserActive } from "@/hooks/useIsUserActive"
import { ChannelListContext, ChannelListContextType, DMChannelListItem, ExtraUsersData, UnreadCountData } from "../../../utils/channel/ChannelListProvider"
import { Flex, Text } from "@radix-ui/themes"
import { Box, Flex, Text } from "@radix-ui/themes"
import { UserAvatar } from "@/components/common/UserAvatar"
import { useToast } from "@/hooks/useToast"

Expand All @@ -27,7 +27,11 @@ export const DirectMessageList = ({ unread_count }: { unread_count?: UnreadCount
<Flex gap='2' align='center'>
<SidebarGroupLabel className='cal-sans'>Direct Messages</SidebarGroupLabel>
</Flex>
{!showData && unread_count && unread_count?.total_unread_count_in_dms > 0 && <SidebarBadge>{unread_count.total_unread_count_in_dms}</SidebarBadge>}
{!showData && unread_count && unread_count?.total_unread_count_in_dms > 0 &&
<Box pr='2'>
<SidebarBadge>{unread_count.total_unread_count_in_dms}</SidebarBadge>
</Box>
}
</Flex>
</SidebarGroupItem>
<SidebarGroup>
Expand Down Expand Up @@ -56,15 +60,22 @@ const DirectMessageItemList = ({ unread_count }: { unread_count?: UnreadCountDat

const DirectMessageItem = ({ channel, unreadCount }: { channel: DMChannelListItem, unreadCount: UnreadCountData['channels'] }) => {

const { currentUser } = useContext(UserContext)

const unreadCountForChannel = useMemo(() => unreadCount.find((unread) => unread.name == channel.name)?.unread_count, [channel.name, unreadCount])

return <DirectMessageItemElement channel={channel} unreadCount={unreadCountForChannel} />

}

export const DirectMessageItemElement = ({ channel, unreadCount }: { channel: DMChannelListItem, unreadCount?: number }) => {

const { currentUser } = useContext(UserContext)
const userData = useGetUser(channel.peer_user_id)
const isActive = useIsUserActive(channel.peer_user_id)

const { channelID } = useParams()

const showUnread = unreadCountForChannel && channelID !== channel.name
const showUnread = unreadCount && channelID !== channel.name

return <SidebarItem to={channel.name} className={'py-0.5'}>
<SidebarIcon>
Expand All @@ -74,7 +85,7 @@ const DirectMessageItem = ({ channel, unreadCount }: { channel: DMChannelListIte
<Text size='2' className="text-ellipsis line-clamp-1" weight={showUnread ? 'bold' : 'regular'}>
{channel.peer_user_id !== currentUser ? userData?.full_name ?? channel.peer_user_id : `${userData?.full_name} (You)`}
</Text>
{showUnread ? <SidebarBadge>{unreadCountForChannel}</SidebarBadge> : null}
{showUnread ? <SidebarBadge>{unreadCount}</SidebarBadge> : null}
</Flex>
</SidebarItem>
}
Expand Down
10 changes: 8 additions & 2 deletions raven-app/src/components/layout/Sidebar/SidebarBody.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ChannelList } from '../../feature/channels/ChannelList'
import { DirectMessageList } from '../../feature/direct-messages/DirectMessageList'
import { SidebarItem } from './SidebarComp'
import { AccessibleIcon, Box, Flex, ScrollArea, Text } from '@radix-ui/themes'
import { AccessibleIcon, Box, Flex, ScrollArea, Separator, Text } from '@radix-ui/themes'
import { BiBookmark } from 'react-icons/bi'
import useUnreadMessageCount from '@/hooks/useUnreadMessageCount'
import UnreadChannels from './UnreadChannels'

export const SidebarBody = () => {

Expand All @@ -12,7 +13,7 @@ export const SidebarBody = () => {
return (
<ScrollArea type="hover" scrollbars="vertical" className='h-[calc(100vh-7rem)]'>
<Flex direction='column' gap='2' className='overflow-x-hidden' px='2'>
<Box>
<Box pb='2'>
<SidebarItem to={'saved-messages'} className='pl-1.5 py-0.5'>
<AccessibleIcon label='Saved Messages'>
<BiBookmark className='text-slate-12 mt-0.5' size='14' />
Expand All @@ -22,6 +23,11 @@ export const SidebarBody = () => {
</Box>
</SidebarItem>
</Box>
<UnreadChannels unread_count={unread_count?.message} />
<Box width='100%' py='2'>
<Separator size='4' />
</Box>

<ChannelList unread_count={unread_count?.message} />
<DirectMessageList unread_count={unread_count?.message} />
</Flex>
Expand Down
2 changes: 1 addition & 1 deletion raven-app/src/components/layout/Sidebar/SidebarComp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const SidebarItem = ({ to, children, end, active = false, activeStyles, c
<Flex
gap='2'
align='center'
px='3'
px='2'
className={clsx('cursor-pointer text-black dark:text-gray-100 user-select-none rounded-md no-underline transition-all duration-200 hover:bg-gray-3 dark:hover:bg-gray-5', isActive ? activeClass : '', className)}
{...props}>
{children}
Expand Down
83 changes: 83 additions & 0 deletions raven-app/src/components/layout/Sidebar/UnreadChannels.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { ChannelListContext, ChannelListContextType, UnreadCountData } from '@/utils/channel/ChannelListProvider'
import { useContext, useMemo } from 'react'
import { SidebarGroup, SidebarGroupItem, SidebarGroupLabel, SidebarGroupList } from './SidebarComp'
import { Flex } from '@radix-ui/themes'
import { MdOutlineMarkChatUnread } from 'react-icons/md'
import { ChannelItemElement } from '@/components/feature/channels/ChannelList'
import { DirectMessageItemElement } from '@/components/feature/direct-messages/DirectMessageList'

type Props = {}

const UnreadChannels = ({ unread_count }: { unread_count?: UnreadCountData }) => {

const { channels, dm_channels } = useContext(ChannelListContext) as ChannelListContextType

const { unread_channels, unread_dm_channels } = useMemo(() => {
// Only show channels with unread messages
const activeChannels = channels.filter(channel => channel.is_archived === 0)

const channelsWithUnread = []
const dmsWithUnread = []
for (const channel of activeChannels) {
const count = unread_count?.channels.find((unread) => unread.name === channel.name)?.unread_count
if (count) {
channelsWithUnread.push({
...channel,
unread_count: count
})
}
}

for (const channel of dm_channels) {
const count = unread_count?.channels.find((unread) => unread.name === channel.name)?.unread_count
if (count) {
dmsWithUnread.push({
...channel,
unread_count: count
})
}
}

return {
unread_channels: channelsWithUnread,
unread_dm_channels: dmsWithUnread
}

}, [channels, dm_channels, unread_count])

if (unread_channels.length === 0 && unread_dm_channels.length === 0) {
return null
}


return (
<SidebarGroup>
<SidebarGroupItem gap='2' className={'pl-1.5'}>
<MdOutlineMarkChatUnread className='text-slate-12 mt-0.5' size='14' />
<Flex width='100%' justify='between' align='center' gap='2'>
<SidebarGroupLabel className='cal-sans'>Unread</SidebarGroupLabel>
</Flex>
</SidebarGroupItem>
<SidebarGroup>
<SidebarGroupList>
{unread_channels.map((channel) => <ChannelItemElement
channel={channel}
unreadCount={channel.unread_count}
key={channel.name} />)}

{unread_dm_channels.map((channel) => <DirectMessageItemElement
channel={channel}
unreadCount={channel.unread_count}
key={channel.name} />)}

{/* {filteredChannels.map((channel) => <ChannelItem
channel={channel}
unreadCount={unread_count?.channels ?? []}
key={channel.name} />)} */}
</SidebarGroupList>
</SidebarGroup>
</SidebarGroup>
)
}

export default UnreadChannels

0 comments on commit 3053986

Please sign in to comment.