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}>添加對話 {/*