Skip to content

Commit be0a514

Browse files
committed
feat(chat): basic support for message list
1 parent 57e0497 commit be0a514

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

packages/chat/client/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export default class ChatService extends Service {
3535
result[key].assignees.push(data.bot.sid!)
3636
}
3737
}
38-
console.log(1, result, this.logins)
3938
return result
4039
})
4140

packages/chat/client/index.vue

+56-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<template v-if="guildKey">
66
<div class="w-3rem h-full items-center justify-center flex
77
bg-gray-700 bg-op-0 hover:bg-op-100 transition cursor-pointer"
8-
@click="guildKey = undefined, channels = []">
8+
@click="setGuild()">
99
<k-icon name="arrow-left" class="h-4"></k-icon>
1010
</div>
1111
<div class="font-bold flex items-center flex-1">{{ ctx.chat.guilds.value[guildKey].name }}</div>
@@ -14,18 +14,21 @@
1414
<div class="font-bold flex items-center flex-1 justify-center">消息列表</div>
1515
</template>
1616
</div>
17+
1718
<el-scrollbar v-if="guildKey">
1819
<div v-for="(channel, index) in channels" :key="channel.id"
1920
class="flex px-4 py-1 items-center
20-
bg-gray-700 bg-op-0 hover:bg-op-100 transition cursor-pointer">
21+
bg-gray-700 bg-op-0 hover:bg-op-100 transition cursor-pointer"
22+
@click="setChannel(channel)"
23+
>
2124
{{ channel.name }}
2225
</div>
2326
</el-scrollbar>
2427
<el-scrollbar v-else>
2528
<div v-for="(guild, key) in ctx.chat.guilds.value" :key="key"
2629
class="flex px-4 py-3 gap-x-4 justify-between
2730
bg-gray-700 bg-op-0 hover:bg-op-100 transition cursor-pointer"
28-
@click="onClickGuild(guild)"
31+
@click="setGuild(guild)"
2932
>
3033
<img v-if="guild.avatar" :src="withProxy(guild.avatar)" width="48" height="48" class="b-rd-full"/>
3134
<div v-else
@@ -41,21 +44,41 @@
4144
</div>
4245
</el-scrollbar>
4346
</template>
47+
48+
<template v-if="channelId">
49+
<el-scrollbar>
50+
<div v-for="message in messages" :key="message.id" class="message">
51+
<message-content :content="message.content!"></message-content>
52+
</div>
53+
</el-scrollbar>
54+
<div class="footer shrink-0">
55+
<chat-input class="h-6 px-4 py-2" v-model="input" @send="handleSend" placeholder="发送消息"></chat-input>
56+
</div>
57+
</template>
58+
<template v-else>
59+
<k-empty>选择一个频道开始聊天</k-empty>
60+
</template>
4461
</k-layout>
4562
</template>
4663

4764
<script lang="ts" setup>
4865
49-
import { onMounted, ref, computed, watch, reactive } from 'vue'
66+
import { ref } from 'vue'
5067
import { useContext, useRpc } from '@cordisjs/client'
5168
import type { Data } from '../src'
52-
import type { Universal } from '@satorijs/core'
69+
import { Universal } from '@satorijs/core'
70+
import { ChatInput, MessageContent } from '@satorijs/components-vue'
5371
5472
const data = useRpc<Data>()
5573
const ctx = useContext()
5674
75+
const platform = ref<string>()
5776
const guildKey = ref<string>()
77+
const channelId = ref<string>()
5878
const channels = ref<Universal.Channel[]>([])
79+
const messages = ref<Universal.Message[]>([])
80+
81+
const input = ref('')
5982
6083
function short(name: string) {
6184
return name.slice(0, 2)
@@ -65,14 +88,30 @@ function withProxy(url: string) {
6588
return (data.value.proxy ? data.value.proxy + '/' : '') + url
6689
}
6790
68-
async function onClickGuild(guild: Universal.Guild & { platform: string; assignees: string[] }) {
69-
guildKey.value = `${guild.platform}/${guild.id}`
91+
async function setGuild(guild?: Universal.Guild & { platform: string; assignees: string[] }) {
7092
channels.value = []
71-
for await (const channel of ctx.chat.logins[guild.assignees[0]].bot.getChannelIter(guild.id)) {
93+
channelId.value = undefined
94+
if (!guild) return guildKey.value = undefined
95+
platform.value = guild.platform
96+
guildKey.value = `${guild.platform}/${guild.id}`
97+
for await (const channel of ctx.bots[guild.assignees[0]].getChannelIter(guild.id)) {
7298
channels.value.push(channel)
7399
}
74100
}
75101
102+
async function setChannel(channel: Universal.Channel) {
103+
channelId.value = channel.id
104+
messages.value = []
105+
for await (const message of ctx.bots[ctx.chat.guilds.value[guildKey.value!].assignees[0]].getMessageIter(channel.id)) {
106+
if (channelId.value !== channel.id || messages.value.length >= 100) return
107+
messages.value.unshift(message)
108+
}
109+
}
110+
111+
function handleSend(content: string) {
112+
return ctx.bots[ctx.chat.guilds.value[guildKey.value!].assignees[0]].sendMessage(channelId.value!, content)
113+
}
114+
76115
</script>
77116

78117
<style lang="scss" scoped>
@@ -82,4 +121,13 @@ async function onClickGuild(guild: Universal.Guild & { platform: string; assigne
82121
border-bottom: var(--k-color-divider-dark) 1px solid;
83122
}
84123
124+
.k-layout :deep(main) {
125+
display: flex;
126+
flex-direction: column;
127+
}
128+
129+
.footer {
130+
border-top: var(--k-color-divider-dark) 1px solid;
131+
}
132+
85133
</style>

0 commit comments

Comments
 (0)