|
| 1 | +import ChannelName from '@/components/home/ChannelName'; |
| 2 | +import { ChannelMediaIcon } from '@/components/icons/ChannelMediaIcon'; |
| 3 | +import ChannelMessageIcon from '@/components/icons/ChannelMessageIcon'; |
| 4 | +import { useChannel, useServerUserProfilePermissions, useSetChannel } from '@/lib/store'; |
| 5 | +import { Channel } from '@/types/dbtypes'; |
| 6 | +import { SyntheticEvent, useState } from 'react'; |
| 7 | +import * as ContextMenu from '@radix-ui/react-context-menu'; |
| 8 | +import { useUser } from '@supabase/auth-helpers-react'; |
| 9 | +import { ServerPermissions } from '@/types/permissions'; |
| 10 | +import CreateInviteModal from '@/components/home/modals/CreateInviteModal'; |
| 11 | + |
| 12 | +export function ChannelListItem({ channel, idx }: { channel: Channel, idx: number }) { |
| 13 | + const currentUser = useUser(); |
| 14 | + const currentChannel = useChannel(); |
| 15 | + const setChannel = useSetChannel(); |
| 16 | + const currentUserPermissions = useServerUserProfilePermissions(channel.server_id, currentUser?.id!); |
| 17 | + const [showCreateInviteModal, setShowCreateInviteModal] = useState(false); |
| 18 | + |
| 19 | + function joinTextChannel(e: SyntheticEvent, channel: Channel) { |
| 20 | + e.stopPropagation(); |
| 21 | + setChannel(channel); |
| 22 | + } |
| 23 | + |
| 24 | + function joinVideoChannel(e: SyntheticEvent, channel: Channel) { |
| 25 | + e.stopPropagation(); |
| 26 | + setChannel(channel); |
| 27 | + } |
| 28 | + |
| 29 | + return ( |
| 30 | + <div |
| 31 | + className={`${ |
| 32 | + currentChannel?.channel_id == channel.channel_id |
| 33 | + ? 'bg-grey-600' |
| 34 | + : 'hover:bg-grey-600' |
| 35 | + } flex whitespace-nowrap items-center pt-2 pb-1 px-4 mt-1 hover:cursor-pointer rounded-lg max-w-[192px] ${ |
| 36 | + idx === 0 ? 'mt-2' : '' |
| 37 | + }`} |
| 38 | + onClick={(e) => { |
| 39 | + if (channel.is_media) { |
| 40 | + joinVideoChannel(e, channel); |
| 41 | + } |
| 42 | + |
| 43 | + else { |
| 44 | + joinTextChannel(e, channel); |
| 45 | + } |
| 46 | + }} |
| 47 | + key={channel.channel_id} |
| 48 | + > |
| 49 | + <CreateInviteModal |
| 50 | + showModal={showCreateInviteModal} |
| 51 | + setShowModal={setShowCreateInviteModal} |
| 52 | + channel={channel} |
| 53 | + /> |
| 54 | + <div className="w-auto"> |
| 55 | + {channel && channel.is_media ? ( |
| 56 | + <div className="flex flex-col"> |
| 57 | + <div className="flex flex-row items-center"> |
| 58 | + <ChannelMediaIcon /> |
| 59 | + <ChannelName {...channel} /> |
| 60 | + </div> |
| 61 | + </div> |
| 62 | + ) : ( |
| 63 | + <ContextMenu.Root> |
| 64 | + <ContextMenu.Trigger |
| 65 | + disabled={ |
| 66 | + (currentUserPermissions & ServerPermissions.CREATE_INVITES) === 0 |
| 67 | + } |
| 68 | + asChild |
| 69 | + > |
| 70 | + <div className="flex flex-row items-center"> |
| 71 | + <ChannelMessageIcon /> |
| 72 | + <ChannelName {...channel} /> |
| 73 | + </div> |
| 74 | + </ContextMenu.Trigger> |
| 75 | + <ContextMenu.Content className='ContextMenuContent'> |
| 76 | + <ContextMenu.Item |
| 77 | + className="ContextMenuItem" |
| 78 | + onSelect={() => { |
| 79 | + console.log('Open invite modal using channel id', channel.channel_id); |
| 80 | + setShowCreateInviteModal(true); |
| 81 | + }} |
| 82 | + > |
| 83 | + Create invite |
| 84 | + </ContextMenu.Item> |
| 85 | + </ContextMenu.Content> |
| 86 | + </ContextMenu.Root> |
| 87 | + )} |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + ); |
| 91 | +} |
0 commit comments