// @ts-nocheck import { useState, useEffect, FC } from "react"; import clsx from "clsx"; import { useDrop } from "react-dnd"; import { NativeTypes } from "react-dnd-html5-backend"; import { useNavigate, NavLink, useMatch } from "react-router-dom"; import ContextMenu from "./ContextMenu"; import getUnreadCount, { renderPreviewMessage } from "../utils"; import User from "../../../common/component/User"; import Avatar from "../../../common/component/Avatar"; import IconLock from "../../../assets/icons/lock.svg"; import IconVoicing from "../../../assets/icons/voicing.svg"; import useContextMenu from "../../../common/hook/useContextMenu"; import useUploadFile from "../../../common/hook/useUploadFile"; import { useAppSelector } from "../../../app/store"; import { fromNowTime } from "../../../common/utils"; import { ChatContext } from "../../../types/common"; interface IProps { type?: ChatContext; id: number; mid: number; setDeleteChannelId: (param: number) => void; setInviteChannelId: (param: number) => void; } const Session: FC = ({ type = "dm", id, mid, setDeleteChannelId, setInviteChannelId }) => { const navPath = type == "dm" ? `/chat/dm/${id}` : `/chat/channel/${id}`; // const { pathname } = useLocation(); const isCurrentPath = useMatch(navPath); const navigate = useNavigate(); const { addStageFile } = useUploadFile({ context: type, id }); const [{ isActive }, drop] = useDrop( () => ({ accept: [NativeTypes.FILE], drop({ files }) { if (files.length) { const filesData = files.map((file) => { const { size, type, name } = file; const url = URL.createObjectURL(file); return { size, type, name, url }; }); addStageFile(filesData); navigate(type == "dm" ? `/chat/dm/${id}` : `/chat/channel/${id}`); } }, collect: (monitor) => ({ isActive: monitor.canDrop() && monitor.isOver() }) }), [type, id] ); const { visible: contextMenuVisible, handleContextMenuEvent, hideContextMenu } = useContextMenu(); const [data, setData] = useState<{ name: string; icon: string; mid: number; is_public: boolean; }>(); const { messageData, userData, channelData, readIndex, loginUid, mids, muted, voiceList } = useAppSelector( (store) => { return { voiceList: store.voice.list, mids: type == "dm" ? store.userMessage.byId[id] : store.channelMessage[id], loginUid: store.authData.user?.uid || 0, readIndex: type == "dm" ? store.footprint.readUsers[id] : store.footprint.readChannels[id], messageData: store.message, userData: store.users.byId, channelData: store.channels.byId, muted: type == "dm" ? store.footprint.muteUsers[id] : store.footprint.muteChannels[id] }; } ); useEffect(() => { const tmp = type == "dm" ? userData[id] : channelData[id]; if (!tmp) return; if ("avatar" in tmp) { // user const { name, avatar } = tmp; setData({ name, icon: avatar, mid, is_public: true }); } else { // channel const { name, icon = "", is_public } = tmp; setData({ name, icon, mid, is_public }); } }, [id, mid, type, userData, channelData]); if (!data) return null; const previewMsg = messageData[mid] || {}; const { name, icon, is_public } = data; const { unreads = 0 } = getUnreadCount({ mids, readIndex, messageData, loginUid }); const isVoicing = voiceList.some((item) => { return item.context == type && item.id === id; }); return (
  • clsx(`nav flex gap-2 rounded-lg p-2 w-full md:hover:bg-gray-500/20`, isActive && "shadow-[inset_0_0_0_2px_#52edff]", linkActive && "bg-gray-500/20")} to={navPath} onContextMenu={handleContextMenuEvent} >
    {type == "dm" ? ( ) : ( )} {isVoicing && }
    {name} {!is_public && } {fromNowTime(previewMsg.created_at)}
    0 ? `w-36` : ``)}>{renderPreviewMessage(previewMsg)} {unreads > 0 && !isCurrentPath && ( {unreads > 99 ? "99+" : unreads} )}
  • ); }; export default Session;