import Client from "../api/Client.ts" import data from "../Data.ts" import ChatFragment from "./chat/ChatFragment.tsx" import useEventListener from './useEventListener.ts' import User from "../api/client_data/User.ts" import Avatar from "./Avatar.tsx" import * as React from 'react' import { Dialog, NavigationRail, TextField } from "mdui" import Split from 'split.js' import 'mdui/jsx.zh-cn.d.ts' import { checkApiSuccessOrSncakbar } from "./snackbar.ts" import RegisterDialog from "./dialog/RegisterDialog.tsx" import LoginDialog from "./dialog/LoginDialog.tsx" import MyProfileDialog from "./dialog/MyProfileDialog.tsx" import ContactsList from "./main/ContactsList.tsx" import RecentsList from "./main/RecentsList.tsx" import useAsyncEffect from "./useAsyncEffect.ts" import ChatInfoDialog from "./dialog/ChatInfoDialog.tsx" import Chat from "../api/client_data/Chat.ts" import AddContactDialog from './dialog/AddContactDialog.tsx' import CreateGroupDialog from './dialog/CreateGroupDialog.tsx' import DataCaches from "../api/DataCaches.ts" import getUrlForFileByHash from "../getUrlForFileByHash.ts" import Message from "../api/client_data/Message.ts" import EventBus from "../EventBus.ts" import AllChatsList from "./main/AllChatsList.tsx"; declare global { namespace React { namespace JSX { interface IntrinsicAttributes { id?: string slot?: string } } } } export default function App() { const [navigationItemSelected, setNavigationItemSelected] = React.useState('Recents') const navigationRailRef = React.useRef(null) useEventListener(navigationRailRef, 'change', (event) => { setNavigationItemSelected((event.target as HTMLElement as NavigationRail).value as string) }) const loginDialogRef = React.useRef(null) const loginInputAccountRef = React.useRef(null) const loginInputPasswordRef = React.useRef(null) const registerDialogRef = React.useRef(null) const registerInputUserNameRef = React.useRef(null) const registerInputNickNameRef = React.useRef(null) const registerInputPasswordRef = React.useRef(null) const myProfileDialogRef = React.useRef(null) const openMyProfileDialogButtonRef = React.useRef(null) useEventListener(openMyProfileDialogButtonRef, 'click', (_event) => { myProfileDialogRef.current!.open = true }) const addContactDialogRef = React.useRef(null) const createGroupDialogRef = React.useRef(null) const chatInfoDialogRef = React.useRef(null) const [chatInfo, setChatInfo] = React.useState(null as unknown as Chat) const [myUserProfileCache, setMyUserProfileCache] = React.useState(null as unknown as User) const [isShowChatFragment, setIsShowChatFragment] = React.useState(false) const [currentChatId, setCurrentChatId] = React.useState('') const [sharedFavouriteChats, setSharedFavouriteChats] = React.useState([]) useAsyncEffect(async () => { const split = Split(['#SideBar', '#ChatFragment'], { sizes: data.split_sizes ? data.split_sizes : [25, 75], minSize: [200, 400], gutterSize: 2, onDragEnd: function () { data.split_sizes = split.getSizes() data.apply() } }) Client.connect() const re = await Client.auth(data.access_token || "") if (re.code == 401) loginDialogRef.current!.open = true else if (re.code != 200) { if (checkApiSuccessOrSncakbar(re, "验证失败")) return } else if (re.code == 200) { setMyUserProfileCache(Client.myUserProfile as User) } }) function openChatInfoDialog(chat: Chat) { setChatInfo(chat) chatInfoDialogRef.current!.open = true } function openChatFragment(chatId: string) { setCurrentChatId(chatId) setIsShowChatFragment(true) } async function openUserInfoDialog(user: User | string) { const re = await Client.invoke("Chat.getIdForPrivate", { token: data.access_token, target: typeof user == 'object' ? user.id : user, }) if (re.code != 200) { checkApiSuccessOrSncakbar(re, '获取对话失败') return } openChatInfoDialog(re.data as Chat) /* if (typeof user == 'object') { setUserInfo(user) } else { setUserInfo(await DataCaches.getUserProfile(user)) } userProfileDialogRef.current!.open = true */ } // deno-lint-ignore no-window window.openUserInfoDialog = openUserInfoDialog // deno-lint-ignore no-window window.openChatInfoDialog = openChatInfoDialog if ('Notification' in window) { Notification.requestPermission() React.useEffect(() => { interface OnMessageData { chat: string msg: Message } async function onMessage(_event: unknown) { EventBus.emit('RecentsList.updateRecents') const event = _event as OnMessageData if (currentChatId != event.chat) { const chat = await DataCaches.getChatInfo(event.chat) const user = await DataCaches.getUserProfile(event.msg.user_id) const notification = new Notification(`${user.nickname} (对话: ${chat.title})`, { icon: getUrlForFileByHash(chat.avatar_file_hash), body: event.msg.text, }) notification.addEventListener('click', () => { setCurrentChatId(chat.id) setIsShowChatFragment(true) notification.close() }) } } Client.on('Client.onMessage', onMessage) return () => { Client.off('Client.onMessage', onMessage) } }, [currentChatId]) } return (
{ EventBus.emit('RecentsList.updateRecents') EventBus.emit('ContactsList.updateContacts') EventBus.emit('AllChatsList.updateAllChats') }}> addContactDialogRef.current!.open = true}>添加收藏对话 createGroupDialogRef.current!.open = true}>创建群组 { // 侧边列表 } { // 聊天页面 }
{ !isShowChatFragment &&
选择以开始对话......
} { isShowChatFragment && }
) }