From 8d739dd863d15d4993e9008c5ed34cb03b43aa02 Mon Sep 17 00:00:00 2001 From: CrescentLeaf Date: Thu, 25 Sep 2025 16:52:23 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=B7=BB=E5=8A=A0=20EventBus=20*=20?= =?UTF-8?q?=E4=B8=A6=E8=AE=93=E5=B0=8D=E8=A9=B1=E5=88=97=E8=A1=A8=E5=85=88?= =?UTF-8?q?=E7=94=A8=E4=B8=8A=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/EventBus.ts | 12 ++++++++++++ client/ui/main/ContactsList.tsx | 23 +++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 client/EventBus.ts diff --git a/client/EventBus.ts b/client/EventBus.ts new file mode 100644 index 0000000..1db9873 --- /dev/null +++ b/client/EventBus.ts @@ -0,0 +1,12 @@ +export default class EventBus { + static events: { [key: string]: () => void } = {} + static on(eventName: string, func: () => void) { + this.events[eventName] = func + } + static off(eventName: string) { + delete this.events[eventName] + } + static emit(eventName: string) { + this.events[eventName]() + } +} \ No newline at end of file diff --git a/client/ui/main/ContactsList.tsx b/client/ui/main/ContactsList.tsx index a7f5664..f8ad1dc 100644 --- a/client/ui/main/ContactsList.tsx +++ b/client/ui/main/ContactsList.tsx @@ -7,10 +7,12 @@ import Client from "../../api/Client.ts" import data from "../../Data.ts" import { checkApiSuccessOrSncakbar } from "../snackbar.ts" import Chat from "../../api/client_data/Chat.ts" +import EventBus from "../../EventBus.ts" interface Args extends React.HTMLAttributes { display: boolean chatInfoDialogRef: React.MutableRefObject + addContactDialogRef: React.MutableRefObject setChatInfo: React.Dispatch> } @@ -18,6 +20,7 @@ export default function ContactsList({ display, setChatInfo, chatInfoDialogRef, + addContactDialogRef, ...props }: Args) { const searchRef = React.useRef(null) @@ -30,13 +33,17 @@ export default function ContactsList({ }) useAsyncEffect(async () => { - const re = await Client.invoke("User.getMyContacts", { - token: data.access_token, - }) - if (re.code != 200) - return checkApiSuccessOrSncakbar(re, "获取對話列表失败") - - setContactsList(re.data!.contacts_list as Chat[]) + async function updateContacts() { + const re = await Client.invoke("User.getMyContacts", { + token: data.access_token, + }) + if (re.code != 200) + return checkApiSuccessOrSncakbar(re, "获取對話列表失败") + + setContactsList(re.data!.contacts_list as Chat[]) + } + updateContacts() + EventBus.on('ContactsList.updateContacts', () => updateContacts()) }) return 添加對話 + }} icon="person_add" onClick={() => addContactDialogRef.current!.open = true}>添加對話 {/*