Skip to content

Commit 0ea73a5

Browse files
committed
feat(chat): get message from header / footer
1 parent bfb2c1d commit 0ea73a5

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

packages/chat/client/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export default class ChatService extends Service {
103103
id: '@separator',
104104
}, {
105105
id: '.inspect',
106-
label: '查看频道信息',
106+
label: '查看消息信息',
107107
}])
108108
})
109109
}

packages/chat/client/index.vue

+49-8
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@
4343

4444
<template v-if="channelId">
4545
<el-scrollbar>
46-
<div v-if="messages?.prev">正在加载更多消息……</div>
46+
<div ref="header" v-if="messages?.prev"
47+
class="items-center flex justify-center py-2
48+
bg-gray-700 bg-op-0 hover:bg-op-100 transition cursor-pointer"
49+
@click="appendHeader"
50+
>正在加载更多消息……</div>
4751
<div v-for="(message, index) in messages?.data" :key="message.id"
48-
class="relative px-4 py-0 break-words
52+
class="relative px-4 py-0 break-words last:mb-2
4953
bg-gray-700 bg-op-0 hover:bg-op-100 transition"
5054
:class="{ 'mt-2': !isSuccessive(index) }"
5155
@contextmenu.stop="triggerMessage($event, message)">
@@ -67,7 +71,11 @@
6771
class="ml-14 lh-relaxed"
6872
></message-content>
6973
</div>
70-
<div v-if="messages?.next">正在加载更多消息……</div>
74+
<div ref="footer" v-if="messages?.next"
75+
class="items-center flex justify-center py-2
76+
bg-gray-700 bg-op-0 hover:bg-op-100 transition cursor-pointer"
77+
@click="appendFooter"
78+
>正在加载更多消息……</div>
7179
</el-scrollbar>
7280
<div class="footer shrink-0">
7381
<chat-input class="h-6 px-4 py-2" v-model="input" @send="handleSend" placeholder="发送消息"></chat-input>
@@ -114,8 +122,8 @@
114122
<script lang="ts" setup>
115123
116124
import { ref } from 'vue'
117-
import { useContext, useRpc, useMenu } from '@cordisjs/client'
118-
import type { Data } from '../src'
125+
import { useContext, useMenu } from '@cordisjs/client'
126+
import { useIntersectionObserver } from '@vueuse/core'
119127
import { Universal } from '@satorijs/core'
120128
import { ChatInput, MessageContent } from '@satorijs/components-vue'
121129
import Avatar from './avatar.vue'
@@ -130,6 +138,8 @@ const channels = ref<Universal.Channel[]>([])
130138
const messages = ref<Universal.TwoWayList<Universal.Message>>()
131139
132140
const input = ref('')
141+
const header = ref<HTMLDivElement>()
142+
const footer = ref<HTMLDivElement>()
133143
134144
const showGuild = ref<Universal.Guild>()
135145
const showChannel = ref<Universal.Channel>()
@@ -168,16 +178,47 @@ async function setGuild(guild?: Universal.Guild & { platform: string; assignees:
168178
}
169179
}
170180
181+
function useBot() {
182+
return ctx.bots[ctx.chat.guilds.value[guildKey.value!].assignees[0]]
183+
}
184+
171185
async function setChannel(channel: Universal.Channel) {
172186
channelId.value = channel.id
173187
messages.value = undefined
174-
const result = await ctx.bots[ctx.chat.guilds.value[guildKey.value!].assignees[0]].getMessageList(channel.id)
188+
const result = await useBot().getMessageList(channel.id)
175189
result.next = undefined
176-
if (channelId.value === channel.id) messages.value = result
190+
if (channelId.value !== channel.id) return
191+
messages.value = result
192+
}
193+
194+
let headerTask: Promise<void> | undefined
195+
196+
function appendHeader() {
197+
if (headerTask) return
198+
const id = channelId.value!
199+
headerTask = useBot().getMessageList(id, messages.value!.prev, 'before').then((result) => {
200+
headerTask = undefined
201+
if (channelId.value !== id) return
202+
messages.value!.data.unshift(...result.data)
203+
messages.value!.prev = result.prev
204+
})
205+
}
206+
207+
let footerTask: Promise<void> | undefined
208+
209+
function appendFooter() {
210+
if (footerTask) return
211+
const id = channelId.value!
212+
footerTask = useBot().getMessageList(id, messages.value!.next, 'after').then((result) => {
213+
footerTask = undefined
214+
if (channelId.value !== id) return
215+
messages.value!.data.push(...result.data)
216+
messages.value!.next = result.next
217+
})
177218
}
178219
179220
function handleSend(content: string) {
180-
return ctx.bots[ctx.chat.guilds.value[guildKey.value!].assignees[0]].sendMessage(channelId.value!, content)
221+
return useBot().sendMessage(channelId.value!, content)
181222
}
182223
183224
function formatTime(date: Date) {

0 commit comments

Comments
 (0)