Skip to content

Commit

Permalink
feat(status): show bots in plugin details, fix koishijs#238
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 20, 2023
1 parent 44f248e commit 64cdc99
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 6 deletions.
10 changes: 8 additions & 2 deletions plugins/status/client/bots/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
<k-status v-if="store.status">
<template #tooltip>
<span v-if="!Object.values(store.status.bots).length" class="el-popper__empty"></span>
<bot-preview v-for="(bot, key) in store.status.bots" :key="key" :data="bot"></bot-preview>
<template v-for="(bot, key) in store.status.bots" :key="key">
<bot-preview
:data="bot"
:class="{ 'has-link': bot.paths?.length }"
@click="router.push('/plugins/' + bot.paths[0].replace(/\./, '/'))"
></bot-preview>
</template>
</template>
<status-light v-for="(bot, key) in store.status.bots" :key="key" :class="getStatus(bot.status)"></status-light>
<k-icon name="arrow-up"/>
Expand All @@ -15,7 +21,7 @@
<script setup lang="ts">
import { computed } from 'vue'
import { store } from '@koishijs/client'
import { store, router } from '@koishijs/client'
import { getStatus } from './utils'
import BotPreview from './preview.vue'
import StatusLight from './light.vue'
Expand Down
13 changes: 10 additions & 3 deletions plugins/status/client/bots/preview.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div class="bot">
<section class="bot-view">
<div class="avatar" :style="{ backgroundImage: `url(${withProxy(data.user.avatar)})` }" @click="$emit('avatar-click')">
<el-tooltip :content="statusNames[data.status]" placement="right">
<status-light :class="getStatus(data.status)"></status-light>
Expand All @@ -19,7 +19,7 @@
</span>
</div>
</div>
</div>
</section>
</template>

<script lang="ts" setup>
Expand All @@ -45,7 +45,7 @@ defineProps<{

<style scoped lang="scss">
div.bot {
.bot-view {
width: 15rem;
padding: 0.75rem 1rem;
font-size: 14px;
Expand Down Expand Up @@ -108,6 +108,13 @@ div.bot {
overflow: hidden;
}
}
&.has-link {
cursor: pointer;
&:hover {
background-color: var(--bg1);
}
}
}
</style>
41 changes: 41 additions & 0 deletions plugins/status/client/config.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<template>
<template v-if="bots?.length">
<h2 class="k-schema-header">
机器人
</h2>
<div class="bots-container">
<bot-preview v-for="(bot, sid) in bots" :key="sid" :data="bot"/>
</div>
</template>
</template>

<script setup lang="ts">
import { store } from '@koishijs/client'
import { inject, computed } from 'vue'
import BotPreview from './bots/preview.vue'
const current: any = inject('manager.settings.current')
const bots = computed(() => {
return Object.values(store.status?.bots || {}).filter(bot => {
return bot.paths?.includes(current.value.path)
})
})
</script>

<style scoped lang="scss">
.bots-container {
display: flex;
flex-wrap: wrap;
gap: 1rem;
.bot-view {
background-color: var(--bg0);
border-radius: 0.5rem;
}
}
</style>
7 changes: 7 additions & 0 deletions plugins/status/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {} from '@koishijs/plugin-status/src'
import Bots from './bots'
import Load from './load'
import Analytics from './analytics.vue'
import Config from './config.vue'
import EnvInfo from './envinfo.vue'
import './icons'

Expand All @@ -19,4 +20,10 @@ export default defineExtension((ctx) => {
type: 'analytic-number',
component: Analytics,
})

ctx.slot({
type: 'plugin-details',
component: Config,
order: -500,
})
})
6 changes: 5 additions & 1 deletion plugins/status/src/envinfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Context, Dict, Schema, version } from 'koishi'
import { DataService } from '@koishijs/console'
import { readFile } from 'fs/promises'
import { helpers } from 'envinfo'
import which from 'which-pm-runs'

Expand All @@ -26,9 +27,12 @@ class EnvInfoProvider extends DataService<Dict<Dict<string>>> {
}
binaries[agent.name] = agent.version
}
// do not use `require` directly to avoid caching
const metapath = require.resolve('@koishijs/console/package.json')
const meta = await readFile(metapath, 'utf8').then(JSON.parse)
const koishi = {
Core: version,
Console: require('@koishijs/console/package.json').version,
Console: meta.version,
}
if (process.env.KOISHI_AGENT) {
const [name, version] = process.env.KOISHI_AGENT.split('/')
Expand Down
2 changes: 2 additions & 0 deletions plugins/status/src/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class ProfileProvider extends DataService<ProfileProvider.Payload> {
if (bot.hidden) continue
bots[bot.sid] = {
...bot.toJSON(),
paths: this.ctx.loader?.paths(bot.ctx.scope),
error: bot.error?.message,
messageSent: bot._messageSent.get(),
messageReceived: bot._messageReceived.get(),
Expand All @@ -161,6 +162,7 @@ namespace ProfileProvider {

export interface BotData extends Universal.Login {
error?: string
paths?: string[]
messageSent: number
messageReceived: number
}
Expand Down

0 comments on commit 64cdc99

Please sign in to comment.