import { TextField } from "mdui" import useEventListener from "../useEventListener.ts" import RecentsListItem from "./RecentsListItem.tsx" import React from "react" import useAsyncEffect from "../useAsyncEffect.ts" import Client from "../../api/Client.ts" import { checkApiSuccessOrSncakbar } from "../snackbar.ts" import data from "../../Data.ts" import EventBus from "../../EventBus.ts" import isMobileUI from "../isMobileUI.ts" import Chat from "../../api/client_data/Chat.ts" import AllChatsListItem from "./AllChatsListItem.tsx" interface Args extends React.HTMLAttributes { display: boolean currentChatId: string openChatInfoDialog: (chat: Chat) => void } export default function AllChatsList({ currentChatId, display, openChatInfoDialog, ...props }: Args) { 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() { const re = await Client.invoke("User.getMyAllChats", { token: data.access_token, }) if (re.code != 200) { if (re.code != 401 && re.code != 400) checkApiSuccessOrSncakbar(re, "获取所有对话列表失败") return } setAllChatsList(re.data!.all_chats as Chat[]) } updateAllChats() EventBus.on('AllChatsList.updateAllChats', () => updateAllChats()) return () => { EventBus.off('AllChatsList.updateAllChats') } }) return { allChatsList.filter((chat) => searchText == '' || chat.title.includes(searchText) || chat.id.includes(searchText) ).map((v) => { openChatInfoDialog(v) }} chat={v} /> ) } }