import { TextField } from "mdui" import React from "react" import AllChatsListItem from "./AllChatsListItem.tsx" import useEventListener from "../../utils/useEventListener.ts" import useAsyncEffect from "../../utils/useAsyncEffect.ts" import { CallbackError, Chat, User, UserMySelf } from "lingchair-client-protocol" import getClient from "../../getClient.ts" import showSnackbar from "../../utils/showSnackbar.ts" import isMobileUI from "../../utils/isMobileUI.ts" interface Args extends React.HTMLAttributes { display: boolean currentChatId: string openChatInfoDialog: (chat: Chat) => void } export default function AllChatsList({ ...props }: React.HTMLAttributes) { const searchRef = React.useRef(null) const [searchText, setSearchText] = React.useState('') const [allChatsList, setAllChatsList] = React.useState([]) useEventListener(searchRef, 'input', (e) => { setSearchText((e.target as unknown as TextField).value) }) useAsyncEffect(async () => { async function updateAllChats() { try { setAllChatsList(await (await UserMySelf.getMySelfOrThrow(getClient())).getMyAllChatsOrThrow()) } catch (e) { if (e instanceof CallbackError) if (e.code == 401 || e.code == 400) showSnackbar({ message: '获取所有对话失败: ' + e.message }) } } updateAllChats() return () => { } }) return { allChatsList.filter((chat) => searchText == '' || chat.getTitle().includes(searchText) || chat.getId().includes(searchText) ).map((v) => { openChatInfoDialog(v) }} chat={v} /> ) } }