import { ChevronDown, Hash, Volume2 } from "lucide-react"; import React from "react"; import { NavLink } from "react-router"; import { useShallow } from "zustand/react/shallow"; import { deleteChannel } from "~/lib/api/client/server"; import { ChannelType, type ServerChannel } from "~/lib/api/types"; import { cn } from "~/lib/utils"; import { useChannelsVoiceStateStore } from "~/stores/channels-voice-state"; import { useGatewayStore } from "~/stores/gateway-store"; import { useUsersStore } from "~/stores/users-store"; import { useVoiceStateStore } from "~/stores/voice-state-store"; import { Button } from "../ui/button"; import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from "../ui/context-menu"; import UserAvatar from "../user-avatar"; interface ChannelListItemProps { channel: ServerChannel; } const onDeleteChannel = async (channel: ServerChannel) => { const response = await deleteChannel(channel.serverId, channel.id); }; function ServerCategory({ channel }: ChannelListItemProps) { return (
{channel.name}
); } function ServerVoice({ channel }: ChannelListItemProps) { const updateVoiceState = useGatewayStore((state) => state.updateVoiceState); const voiceStateChannel = useVoiceStateStore((state) => state.activeChannel); const channelVoiceState = useChannelsVoiceStateStore((state) => state.channels[channel.id]) || {}; const userIds = Object.keys(channelVoiceState.users ?? {}); const { users, fetchUsersIfNotPresent } = useUsersStore( useShallow((state) => ({ users: state.users, fetchUsersIfNotPresent: state.fetchUsersIfNotPresent, })), ); const channelUsers = React.useMemo(() => userIds.map((userId) => users[userId]).filter(Boolean), [userIds, users]); React.useEffect(() => { fetchUsersIfNotPresent(userIds); }, [userIds]); const onClick = () => { if (voiceStateChannel?.serverId === channel.serverId && voiceStateChannel.channelId === channel.id) return; updateVoiceState(channel.serverId, channel.id); }; return ( <> onDeleteChannel(channel)}> Delete {channelUsers.length > 0 && (
{channelUsers.map((user) => (
{user.displayName || user.username}
))}
)} ); } function ServerText({ channel }: ChannelListItemProps) { return ( {({ isActive }) => ( onDeleteChannel(channel)}> Delete )} ); } export default function ServerChannelListItem({ channel }: ChannelListItemProps) { switch (channel.type) { case ChannelType.SERVER_CATEGORY: return ; case ChannelType.SERVER_VOICE: return ; case ChannelType.SERVER_TEXT: return ; default: return null; } }