chore: 添加 EventBus

* 並讓對話列表先用上了
This commit is contained in:
CrescentLeaf
2025-09-25 16:52:23 +08:00
parent c0c6c6ed1c
commit 8d739dd863
2 changed files with 27 additions and 8 deletions

12
client/EventBus.ts Normal file
View File

@@ -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]()
}
}

View File

@@ -7,10 +7,12 @@ import Client from "../../api/Client.ts"
import data from "../../Data.ts" import data from "../../Data.ts"
import { checkApiSuccessOrSncakbar } from "../snackbar.ts" import { checkApiSuccessOrSncakbar } from "../snackbar.ts"
import Chat from "../../api/client_data/Chat.ts" import Chat from "../../api/client_data/Chat.ts"
import EventBus from "../../EventBus.ts"
interface Args extends React.HTMLAttributes<HTMLElement> { interface Args extends React.HTMLAttributes<HTMLElement> {
display: boolean display: boolean
chatInfoDialogRef: React.MutableRefObject<Dialog> chatInfoDialogRef: React.MutableRefObject<Dialog>
addContactDialogRef: React.MutableRefObject<Dialog>
setChatInfo: React.Dispatch<React.SetStateAction<Chat>> setChatInfo: React.Dispatch<React.SetStateAction<Chat>>
} }
@@ -18,6 +20,7 @@ export default function ContactsList({
display, display,
setChatInfo, setChatInfo,
chatInfoDialogRef, chatInfoDialogRef,
addContactDialogRef,
...props ...props
}: Args) { }: Args) {
const searchRef = React.useRef<HTMLElement>(null) const searchRef = React.useRef<HTMLElement>(null)
@@ -30,13 +33,17 @@ export default function ContactsList({
}) })
useAsyncEffect(async () => { useAsyncEffect(async () => {
const re = await Client.invoke("User.getMyContacts", { async function updateContacts() {
token: data.access_token, const re = await Client.invoke("User.getMyContacts", {
}) token: data.access_token,
if (re.code != 200) })
return checkApiSuccessOrSncakbar(re, "获取對話列表失败") if (re.code != 200)
return checkApiSuccessOrSncakbar(re, "获取對話列表失败")
setContactsList(re.data!.contacts_list as Chat[]) setContactsList(re.data!.contacts_list as Chat[])
}
updateContacts()
EventBus.on('ContactsList.updateContacts', () => updateContacts())
}) })
return <mdui-list style={{ return <mdui-list style={{
@@ -55,7 +62,7 @@ export default function ContactsList({
width: '100%', width: '100%',
marginTop: '13px', marginTop: '13px',
marginBottom: '15px', marginBottom: '15px',
}} icon="person_add"></mdui-list-item> }} icon="person_add" onClick={() => addContactDialogRef.current!.open = true}></mdui-list-item>
{/* <mdui-list-item rounded style={{ {/* <mdui-list-item rounded style={{
width: '100%', width: '100%',
marginBottom: '15px', marginBottom: '15px',