feat(client): creat Group

This commit is contained in:
CrescentLeaf
2026-01-25 00:29:45 +08:00
parent 3044cabcaa
commit 44ada8206d
4 changed files with 27 additions and 18 deletions

View File

@@ -10,7 +10,7 @@ import { CallbackError, Chat, UserMySelf } from "lingchair-client-protocol"
import showCircleProgressDialog from "./showCircleProgressDialog.ts" import showCircleProgressDialog from "./showCircleProgressDialog.ts"
import RegisterDialog from "./main-page/RegisterDialog.tsx" import RegisterDialog from "./main-page/RegisterDialog.tsx"
import sleep from "../utils/sleep.ts" import sleep from "../utils/sleep.ts"
import { $, dialog, NavigationDrawer } from "mdui" import { $, NavigationDrawer } from "mdui"
import getClient from "../getClient.ts" import getClient from "../getClient.ts"
import showSnackbar from "../utils/showSnackbar.ts" import showSnackbar from "../utils/showSnackbar.ts"
import AllChatsList from "./main-page/AllChatsList.tsx" import AllChatsList from "./main-page/AllChatsList.tsx"
@@ -135,7 +135,7 @@ function Root() {
}}></mdui-divider> }}></mdui-divider>
<mdui-list-item rounded icon="settings"></mdui-list-item> <mdui-list-item rounded icon="settings"></mdui-list-item>
<mdui-list-item rounded icon="person_add" onClick={() => AppStateRef.current!.openAddFavouriteChat()}></mdui-list-item> <mdui-list-item rounded icon="person_add" onClick={() => AppStateRef.current!.openAddFavouriteChat()}></mdui-list-item>
<mdui-list-item rounded icon="group_add"></mdui-list-item> <mdui-list-item rounded icon="group_add" onClick={() => AppStateRef.current!.openCreateGroup()}></mdui-list-item>
</mdui-list> </mdui-list>
<div style={{ <div style={{
flexGrow: 1, flexGrow: 1,

View File

@@ -2,12 +2,13 @@ import { Chat, User } from 'lingchair-client-protocol'
import * as React from 'react' import * as React from 'react'
type AppState = { type AppState = {
openChatInfo: (chat: Chat | string) => void, openChatInfo: (chat: Chat | string) => void
openUserInfo: (user: Chat | User | string) => void, openUserInfo: (user: Chat | User | string) => void
openEditMyProfile: () => void, openEditMyProfile: () => void
openAddFavouriteChat: () => void, openAddFavouriteChat: () => void
openChat: (chat: string | Chat, inDialog?: boolean) => void, openCreateGroup: () => void
closeChat: () => void, openChat: (chat: string | Chat, inDialog?: boolean) => void
closeChat: () => void
} }
const AppStateContext = React.createContext<AppState>({ const AppStateContext = React.createContext<AppState>({
@@ -15,6 +16,7 @@ const AppStateContext = React.createContext<AppState>({
openUserInfo: () => {}, openUserInfo: () => {},
openEditMyProfile: () => {}, openEditMyProfile: () => {},
openAddFavouriteChat: () => {}, openAddFavouriteChat: () => {},
openCreateGroup: () => {},
openChat: () => {}, openChat: () => {},
closeChat: () => {}, closeChat: () => {},
}) })

View File

@@ -12,6 +12,7 @@ import MainSharedContext, { Shared } from "../MainSharedContext.ts"
import ChatFragmentDialog from "./ChatFragmentDialog.tsx" import ChatFragmentDialog from "./ChatFragmentDialog.tsx"
import useAsyncEffect from "../../utils/useAsyncEffect.ts" import useAsyncEffect from "../../utils/useAsyncEffect.ts"
import ClientCache from "../../ClientCache.ts" import ClientCache from "../../ClientCache.ts"
import CreateGroupDialog from "./CreateGroupDialog.tsx"
const config = await fetch('/config.json').then((re) => re.json()) const config = await fetch('/config.json').then((re) => re.json())
@@ -33,6 +34,7 @@ export default function DialogContextWrapper({ children, useRef }: { children: R
const editMyProfileDialogRef = React.useRef<Dialog>() const editMyProfileDialogRef = React.useRef<Dialog>()
const addFavouriteChatDialogRef = React.useRef<Dialog>() const addFavouriteChatDialogRef = React.useRef<Dialog>()
const createGroupDialogRef = React.useRef<Dialog>()
const setCurrentSelectedChatId = useContextSelector( const setCurrentSelectedChatId = useContextSelector(
MainSharedContext, MainSharedContext,
@@ -66,6 +68,9 @@ export default function DialogContextWrapper({ children, useRef }: { children: R
static openAddFavouriteChat() { static openAddFavouriteChat() {
addFavouriteChatDialogRef.current!.open = true addFavouriteChatDialogRef.current!.open = true
} }
static openCreateGroup() {
createGroupDialogRef.current!.open = true
}
static async openChat(chat: string | Chat, inDialog?: boolean) { static async openChat(chat: string | Chat, inDialog?: boolean) {
if (chat instanceof Chat) chat = chat.getId() if (chat instanceof Chat) chat = chat.getId()
@@ -87,6 +92,7 @@ export default function DialogContextWrapper({ children, useRef }: { children: R
<UserOrChatInfoDialog chat={userOrChatInfoDialogState[userOrChatInfoDialogState.length - 1] || lastUserOrChatInfoDialogStateRef.current} useRef={userOrChatInfoDialogRef} /> <UserOrChatInfoDialog chat={userOrChatInfoDialogState[userOrChatInfoDialogState.length - 1] || lastUserOrChatInfoDialogStateRef.current} useRef={userOrChatInfoDialogRef} />
<EditMyProfileDialog useRef={editMyProfileDialogRef} /> <EditMyProfileDialog useRef={editMyProfileDialogRef} />
<AddFavourtieChatDialog useRef={addFavouriteChatDialogRef} /> <AddFavourtieChatDialog useRef={addFavouriteChatDialogRef} />
<CreateGroupDialog useRef={createGroupDialogRef} />
{children} {children}
</AppStateContext.Provider> </AppStateContext.Provider>
} }

View File

@@ -1,9 +1,9 @@
import * as React from 'react' import * as React from 'react'
import { Dialog, TextField } from "mdui" import { Dialog, TextField } from "mdui"
import showSnackbar from '../../utils/showSnackbar' import showSnackbar from '../../utils/showSnackbar'
import { CallbackError } from 'lingchair-client-protocol' import { CallbackError, Chat } from 'lingchair-client-protocol'
import useEventListener from '../../utils/useEventListener.ts' import useEventListener from '../../utils/useEventListener.ts'
import ClientCache from '../../ClientCache.ts' import getClient from '../../getClient.ts'
export default function CreateGroupDialog({ useRef }: { useRef: React.MutableRefObject<Dialog | undefined> }) { export default function CreateGroupDialog({ useRef }: { useRef: React.MutableRefObject<Dialog | undefined> }) {
const inputTargetRef = React.useRef<TextField>(null) const inputTargetRef = React.useRef<TextField>(null)
@@ -12,29 +12,30 @@ export default function CreateGroupDialog({ useRef }: { useRef: React.MutableRef
inputTargetRef.current!.value = '' inputTargetRef.current!.value = ''
}) })
async function addFavouriteChat() { async function createGroup() {
try { try {
await (await ClientCache.getMySelf())!.addFavouriteChatsOrThrow([inputTargetRef.current!.value]) await Chat.createGroupOrThrow(getClient(), inputTargetRef.current!.value)
inputTargetRef.current!.value = '' inputTargetRef.current!.value = ''
showSnackbar({ showSnackbar({
message: '添加成功!' message: '创建成功!'
}) })
useRef.current!.open = false
} catch (e) { } catch (e) {
if (e instanceof CallbackError) if (e instanceof CallbackError)
showSnackbar({ showSnackbar({
message: '添加收藏对话失败: ' + e.message message: '创建群组失败: ' + e.message
}) })
} }
} }
return ( return (
<mdui-dialog close-on-overlay-click close-on-esc headline="添加收藏对话" ref={useRef}> <mdui-dialog close-on-overlay-click close-on-esc headline="创建群组" ref={useRef}>
<mdui-text-field clearable label="对话 / 用户 (ID 或 别名)" ref={inputTargetRef} onKeyDown={(event: KeyboardEvent) => { <mdui-text-field clearable label="群组标题" ref={inputTargetRef} onKeyDown={(event: KeyboardEvent) => {
if (event.key == 'Enter') if (event.key == 'Enter')
addFavouriteChat() createGroup()
}}></mdui-text-field> }}></mdui-text-field>
<mdui-button slot="action" variant="text" onClick={() => useRef.current!.open = false}></mdui-button> <mdui-button slot="action" variant="text" onClick={() => useRef.current!.open = false}></mdui-button>
<mdui-button slot="action" variant="text" onClick={() => addFavouriteChat()}></mdui-button> <mdui-button slot="action" variant="text" onClick={() => createGroup()}></mdui-button>
</mdui-dialog> </mdui-dialog>
) )
} }