feat: 最近对话
This commit is contained in:
@@ -3,18 +3,19 @@ import RecentChat from "../../api/client_data/RecentChat.ts"
|
|||||||
import useEventListener from "../useEventListener.ts"
|
import useEventListener from "../useEventListener.ts"
|
||||||
import RecentsListItem from "./RecentsListItem.tsx"
|
import RecentsListItem from "./RecentsListItem.tsx"
|
||||||
import React from "react"
|
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";
|
||||||
|
|
||||||
interface Args extends React.HTMLAttributes<HTMLElement> {
|
interface Args extends React.HTMLAttributes<HTMLElement> {
|
||||||
recentsList: RecentChat[]
|
|
||||||
setRecentsList: React.Dispatch<React.SetStateAction<RecentChat[]>>
|
|
||||||
display: boolean
|
display: boolean
|
||||||
currentChatId: string
|
currentChatId: string
|
||||||
openChatFragment: (id: string) => void
|
openChatFragment: (id: string) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function RecentsList({
|
export default function RecentsList({
|
||||||
recentsList,
|
|
||||||
setRecentsList,
|
|
||||||
currentChatId,
|
currentChatId,
|
||||||
display,
|
display,
|
||||||
openChatFragment,
|
openChatFragment,
|
||||||
@@ -22,11 +23,26 @@ export default function RecentsList({
|
|||||||
}: Args) {
|
}: Args) {
|
||||||
const searchRef = React.useRef<HTMLElement>(null)
|
const searchRef = React.useRef<HTMLElement>(null)
|
||||||
const [searchText, setSearchText] = React.useState('')
|
const [searchText, setSearchText] = React.useState('')
|
||||||
|
const [recentsList, setRecentsList] = React.useState<RecentChat[]>([])
|
||||||
|
|
||||||
useEventListener(searchRef, 'input', (e) => {
|
useEventListener(searchRef, 'input', (e) => {
|
||||||
setSearchText((e.target as unknown as TextField).value)
|
setSearchText((e.target as unknown as TextField).value)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
useAsyncEffect(async () => {
|
||||||
|
async function updateRecents() {
|
||||||
|
const re = await Client.invoke("User.getMyRecentChats", {
|
||||||
|
token: data.access_token,
|
||||||
|
})
|
||||||
|
if (re.code != 200)
|
||||||
|
return checkApiSuccessOrSncakbar(re, "获取最近對話列表失败")
|
||||||
|
|
||||||
|
setRecentsList(re.data!.recent_chats as RecentChat[])
|
||||||
|
}
|
||||||
|
updateRecents()
|
||||||
|
EventBus.on('RecentsList.updateRecents', () => updateRecents())
|
||||||
|
})
|
||||||
|
|
||||||
return <mdui-list style={{
|
return <mdui-list style={{
|
||||||
overflowY: 'auto',
|
overflowY: 'auto',
|
||||||
paddingRight: '10px',
|
paddingRight: '10px',
|
||||||
@@ -44,7 +60,7 @@ export default function RecentsList({
|
|||||||
searchText == '' ||
|
searchText == '' ||
|
||||||
chat.title.includes(searchText) ||
|
chat.title.includes(searchText) ||
|
||||||
chat.id.includes(searchText) ||
|
chat.id.includes(searchText) ||
|
||||||
chat.content?.includes(searchText)
|
chat.content.includes(searchText)
|
||||||
).map((v) =>
|
).map((v) =>
|
||||||
<RecentsListItem
|
<RecentsListItem
|
||||||
active={currentChatId == v.id}
|
active={currentChatId == v.id}
|
||||||
|
|||||||
@@ -207,20 +207,22 @@ export default class UserApi extends BaseApi {
|
|||||||
|
|
||||||
const user = User.findById(token.author) as User
|
const user = User.findById(token.author) as User
|
||||||
const recentChats = user.getRecentChats()
|
const recentChats = user.getRecentChats()
|
||||||
|
const recentChatsList = []
|
||||||
|
for (const [chatId, content] of recentChats) {
|
||||||
|
const chat = Chat.findById(chatId)
|
||||||
|
recentChatsList.push({
|
||||||
|
content,
|
||||||
|
id: chatId,
|
||||||
|
title: chat?.getTitle(user) || "未知",
|
||||||
|
avatar: chat?.getAvatarFileHash(user) ? "uploaded_files/" + chat?.getAvatarFileHash(user) : undefined
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
msg: "成功",
|
msg: "成功",
|
||||||
code: 200,
|
code: 200,
|
||||||
data: {
|
data: {
|
||||||
recent_chats: recentChats.forEach((content: string, chatId: string) => {
|
recent_chats: recentChatsList.reverse(),
|
||||||
const chat = Chat.findById(chatId)
|
|
||||||
return {
|
|
||||||
content,
|
|
||||||
id: chatId,
|
|
||||||
title: chat?.getTitle(user) || "未知",
|
|
||||||
avatar: chat?.getAvatarFileHash(user) ? "uploaded_files/" + chat?.getAvatarFileHash(user) : undefined
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -132,9 +132,9 @@ export default class User {
|
|||||||
map.set(chatId, content)
|
map.set(chatId, content)
|
||||||
this.setAttr("recent_chats", JSON.stringify(map, MapJson.replacer))
|
this.setAttr("recent_chats", JSON.stringify(map, MapJson.replacer))
|
||||||
}
|
}
|
||||||
getRecentChats() {
|
getRecentChats(): Map<string, string> {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(this.bean.recent_chats, MapJson.reviver) as Map<string, string>
|
return JSON.parse(this.bean.recent_chats, MapJson.reviver)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(chalk.yellow(`警告: 最近对话列表解析失敗: ${(e as Error).message}`))
|
console.log(chalk.yellow(`警告: 最近对话列表解析失敗: ${(e as Error).message}`))
|
||||||
return new Map()
|
return new Map()
|
||||||
|
|||||||
Reference in New Issue
Block a user