Skip to content

Commit f2d5d3e

Browse files
author
Kerwin
committed
fix: 首次打开页面无法滚动倒底部
perf: 首次打开页面仅加载当前聊天记录
1 parent daa52b8 commit f2d5d3e

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

src/store/modules/chat/index.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,33 @@ export const useChatStore = defineStore('chat-store', {
2626
actions: {
2727
async syncHistory() {
2828
const rooms = (await fetchGetChatRooms()).data
29-
// if (rooms.length <= 0)
30-
// return
31-
32-
let uuid = null
29+
let uuid = this.active
3330
this.history = []
3431
this.chat = []
35-
await rooms.forEach(async (r: Chat.History) => {
32+
for (const r of rooms) {
3633
this.history.unshift(r)
37-
uuid = r.uuid
38-
const chatData = (await fetchGetChatHistory(r.uuid)).data
39-
this.chat.unshift({ uuid: r.uuid, data: chatData })
40-
})
34+
if (uuid == null)
35+
uuid = r.uuid
36+
this.chat.unshift({ uuid: r.uuid, data: [] })
37+
if (uuid === r.uuid)
38+
await this.syncChat(r)
39+
}
4140
if (uuid == null) {
4241
uuid = Date.now()
4342
this.addHistory({ title: 'New Chat', uuid: Date.now(), isEdit: false })
4443
}
45-
4644
this.active = uuid
4745
this.reloadRoute(uuid)
4846
},
47+
48+
async syncChat(h: Chat.History) {
49+
const chatIndex = this.chat.findIndex(item => item.uuid === h.uuid)
50+
if (chatIndex <= -1 || this.chat[chatIndex].data.length <= 0) {
51+
const chatData = (await fetchGetChatHistory(h.uuid)).data
52+
this.chat.unshift({ uuid: h.uuid, data: chatData })
53+
}
54+
},
55+
4956
setUsingContext(context: boolean) {
5057
this.usingContext = context
5158
this.recordState()
@@ -64,7 +71,8 @@ export const useChatStore = defineStore('chat-store', {
6471
if (index !== -1) {
6572
this.history[index] = { ...this.history[index], ...edit }
6673
this.recordState()
67-
fetchRenameChatRoom(this.history[index].title, this.history[index].uuid)
74+
if (!edit.isEdit)
75+
fetchRenameChatRoom(this.history[index].title, this.history[index].uuid)
6876
}
6977
},
7078

src/views/chat/layout/sider/List.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang='ts'>
2-
import { computed, onMounted } from 'vue'
2+
import { computed, nextTick, onMounted } from 'vue'
33
import { NInput, NPopconfirm, NScrollbar } from 'naive-ui'
44
import { SvgIcon } from '@/components/common'
55
import { useAppStore, useChatStore } from '@/store'
@@ -23,14 +23,19 @@ async function handleSyncChat() {
2323
// if (chatStore.history.length == 1 && chatStore.history[0].title == 'New Chat'
2424
// && chatStore.chat[0].data.length <= 0)
2525
await chatStore.syncHistory()
26+
const scrollRef = document.querySelector('#scrollRef')
27+
if (scrollRef)
28+
nextTick(() => scrollRef.scrollTop = scrollRef.scrollHeight)
2629
}
2730
2831
async function handleSelect({ uuid }: Chat.History) {
2932
if (isActive(uuid))
3033
return
3134
32-
if (chatStore.active)
33-
chatStore.updateHistory(chatStore.active, { isEdit: false })
35+
// 这里不需要 不然每次切换都rename
36+
// if (chatStore.active)
37+
// chatStore.updateHistory(chatStore.active, { isEdit: false })
38+
await chatStore.syncChat({ uuid } as Chat.History)
3439
await chatStore.setActive(uuid)
3540
3641
if (isMobile.value)

0 commit comments

Comments
 (0)