Files
Enterprise-Knowledge-Base/frontend/src/components/IMChannelsOverviewPanel.vue
T
wizardchen 466182c8d6 feat(frontend): tenant switcher in user menu + role-aware UI gates
PR 4 of #1303 wired the backend X-Tenant-ID gate to accept active
tenant_members rows (not just superusers), but the frontend never
exposed a switcher to ordinary multi-tenant members. The only
existing component, the sidebar TenantSelector, is gated to
canAccessAllTenants and queries /tenants/search — both wrong for a
Contributor that an Owner just invited to a second tenant.

This PR closes both gaps:

1. Switcher entry inside the user-avatar dropdown
   - New "Switch Tenant" submenu in UserMenu.vue, hover-triggered, with
     a teleported floating panel (same pattern as the existing IM
     overview submenu so the layout/animation read consistently).
   - Data source is authStore.memberships, populated by /auth/login.
     No new API call: the JWT-issued claim already enumerates every
     tenant the user is an active member of plus their role.
   - Visibility: only shown when memberships.length > 1. Cluster
     superusers keep using the sidebar TenantSelector for "any tenant
     in the system" — the avatar entry is the curated "tenants I'm a
     member of" list.
   - Click writes via authStore.setSelectedTenant(...), then
     window.location.reload() so every cached store / open SSE
     stream / in-flight request is re-keyed under the new tenant.
     Same approach as the existing TenantSelector.

2. Role-aware UI gates on high-friction surfaces
   - authStore exposes a new hasRole(min: 'viewer'|'contributor'|
     'admin'|'owner') helper (numeric ordering matches
     middleware/rbac.go) so v-if gates line up with the server-side
     matrix instead of duplicating ['admin','owner'].includes(...).
   - Gates applied per the PR 4 audit:
     * KB list "Create" button — Contributor+
     * Agent list "Create" button — Contributor+
     * Model / VectorStore / WebSearch / MCP — Add+Edit+Delete+Test
       are Admin+ (and the per-card action menus return [] for
       non-Admins so the dropdowns disappear, not just disable).
     * Parser / Storage engine drawers — Save button Admin+
     * Retrieval settings — slider/selector inputs disabled for
       non-Admins (the form auto-saves on change, no dedicated Save).
     * API key reset — Owner+ (Reset is the highest-impact tenant op).
     * IM channel toggle/edit/delete + Add — Admin+. Same in the
       avatar-menu IM submenu (toggle disabled for non-Admins).
   - KB / agent per-card edit/delete already key off kb.isMine /
     agent.isMine + (for builtins) is_builtin; that path is left as-is.

3. i18n
   - Three new keys under tenant.switcher.* in en-US/zh-CN/ko-KR/ru-RU.
   - Role labels reuse the existing tenantMember.role.* bundle from
     PR 3, with a "show raw role" fallback if a locale lacks the key.

The server is the source of truth for every gated action — these
changes only hide buttons that would 403, never grant new authority.
authStore.hasRole and currentTenantRole both carry a SECURITY note
saying they read from localStorage and are UI-rendering-only.

vue-tsc --build error count unchanged from baseline (90, all
pre-existing issues unrelated to this PR).
2026-05-18 17:28:58 +08:00

397 lines
10 KiB
Vue

<template>
<div class="im-submenu" @click.stop>
<div class="submenu-header">
<div class="titles">
<h4 class="title">{{ $t('imOverview.pageTitle') }}</h4>
<p class="subtitle">{{ $t('imOverview.subtitle') }}</p>
</div>
<t-button
variant="text"
theme="default"
size="small"
:loading="loading"
@click="loadAll"
>
<template #icon><t-icon name="refresh" /></template>
</t-button>
</div>
<div class="submenu-body">
<div v-if="loading && channels.length === 0" class="state-placeholder">
<t-loading size="small" />
</div>
<div v-else-if="channels.length === 0" class="state-placeholder">
<span class="empty-text">{{ $t('imOverview.empty') }}</span>
</div>
<div v-else class="channels-list">
<div
v-for="ch in channels"
:key="ch.id"
class="channel-item"
:class="{ 'is-disabled': !ch.enabled }"
role="button"
:tabindex="0"
:title="$t('imOverview.gotoAgentEditor')"
@click="gotoAgentEditor(ch)"
@keydown.enter="gotoAgentEditor(ch)"
@keydown.space.prevent="gotoAgentEditor(ch)"
>
<!-- Stacked rows: agent on top, IM channel below. Same avatar size
and typography → both read as co-equal identities of the row. -->
<div class="stack">
<div class="stack-row">
<span class="row-avatar">
<span
v-if="agentMeta(ch.agent_id)?.is_builtin"
class="builtin-avatar"
:class="agentMeta(ch.agent_id)?.config?.agent_mode === 'smart-reasoning' ? 'agent' : 'normal'"
>
<t-icon
:name="agentMeta(ch.agent_id)?.config?.agent_mode === 'smart-reasoning' ? 'control-platform' : 'chat'"
size="12px"
/>
</span>
<span
v-else-if="agentMeta(ch.agent_id)?.avatar"
class="builtin-avatar agent-emoji"
>{{ agentMeta(ch.agent_id)!.avatar }}</span>
<AgentAvatar
v-else
:name="ch.agent_name || ch.agent_id"
size="small"
/>
</span>
<span class="row-label" :title="ch.agent_name || agentMeta(ch.agent_id)?.name || ''">
{{ ch.agent_name || agentMeta(ch.agent_id)?.name || $t('imOverview.builtinAgent') }}
</span>
</div>
<div class="stack-row">
<img
class="row-avatar platform-logo"
:src="platformLogo(ch.platform)"
:alt="platformLabel(ch.platform)"
/>
<span class="row-label" :title="ch.name || platformLabel(ch.platform)">
{{ ch.name || platformLabel(ch.platform) }}
</span>
</div>
</div>
<div class="item-actions" @click.stop>
<t-switch
:value="ch.enabled"
size="small"
:loading="togglingId === ch.id"
:disabled="!authStore.hasRole('admin')"
@change="handleToggle(ch)"
/>
</div>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, watch } from 'vue';
import { useRouter } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { MessagePlugin } from 'tdesign-vue-next';
import { useAuthStore } from '@/stores/auth';
import {
listAllIMChannels,
listAgents,
toggleIMChannel,
type IMChannelOverview,
type CustomAgent,
} from '@/api/agent';
import AgentAvatar from '@/components/AgentAvatar.vue';
// Platform logos sourced from iconify (simple-icons / logos / tdesign / remix icon).
// Bundled as static assets so they share the app's caching story.
import wecomLogo from '@/assets/img/im/wecom.svg';
import feishuLogo from '@/assets/img/im/feishu.svg';
import slackLogo from '@/assets/img/im/slack.svg';
import telegramLogo from '@/assets/img/im/telegram.svg';
import dingtalkLogo from '@/assets/img/im/dingtalk.svg';
import mattermostLogo from '@/assets/img/im/mattermost.svg';
import wechatLogo from '@/assets/img/im/wechat.svg';
const PLATFORM_LOGO: Record<string, string> = {
wecom: wecomLogo,
feishu: feishuLogo,
slack: slackLogo,
telegram: telegramLogo,
dingtalk: dingtalkLogo,
mattermost: mattermostLogo,
wechat: wechatLogo,
};
const props = defineProps<{
active: boolean;
}>();
const emit = defineEmits<{
(e: 'close'): void;
(e: 'channels-changed', channels: IMChannelOverview[]): void;
}>();
const { t } = useI18n();
const router = useRouter();
const authStore = useAuthStore();
const channels = ref<IMChannelOverview[]>([]);
const agentMap = ref<Map<string, CustomAgent>>(new Map());
const loading = ref(false);
const togglingId = ref<string>('');
// Parent tells us when the submenu is active (hover/click-open). Reload only
// when transitioning into active so we don't refetch on every re-render.
watch(
() => props.active,
(v, old) => {
if (v && !old) loadAll();
},
{ immediate: true },
);
function platformLogo(p: string): string {
return PLATFORM_LOGO[p] || '';
}
function platformLabel(p: string): string {
return t(`agentEditor.im.${p}`);
}
function agentMeta(agentId: string): CustomAgent | undefined {
return agentMap.value.get(agentId);
}
async function loadAll() {
loading.value = true;
try {
const [chanResp, agentResp] = await Promise.all([
listAllIMChannels(),
listAgents(),
]);
channels.value = chanResp?.data || [];
const m = new Map<string, CustomAgent>();
(agentResp?.data || []).forEach((a) => m.set(a.id, a));
agentMap.value = m;
emit('channels-changed', channels.value);
} catch (err) {
console.error('Failed to load IM overview:', err);
MessagePlugin.error(t('imOverview.loadFailed'));
} finally {
loading.value = false;
}
}
async function handleToggle(ch: IMChannelOverview) {
if (togglingId.value) return;
togglingId.value = ch.id;
try {
await toggleIMChannel(ch.id);
const resp = await listAllIMChannels();
channels.value = resp?.data || [];
emit('channels-changed', channels.value);
} catch (err: any) {
console.error('Failed to toggle IM channel:', err);
MessagePlugin.error(err?.message || t('common.operationFailed'));
} finally {
togglingId.value = '';
}
}
function gotoAgentEditor(ch: IMChannelOverview) {
emit('close');
router.push({
path: '/platform/agents',
query: { edit: ch.agent_id, section: 'im' },
});
}
</script>
<style lang="less" scoped>
.im-submenu {
display: flex;
flex-direction: column;
width: 300px;
max-height: 520px;
background: var(--td-bg-color-container);
border-radius: 8px;
box-shadow: 0 6px 24px rgba(0, 0, 0, 0.14);
border: 1px solid var(--td-component-stroke);
overflow: hidden;
}
.submenu-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 8px;
padding: 12px 14px;
border-bottom: 1px solid var(--td-component-stroke);
.titles {
min-width: 0;
}
.title {
margin: 0 0 2px;
font-size: 13px;
font-weight: 600;
color: var(--td-text-color-primary);
line-height: 1.3;
}
.subtitle {
margin: 0;
font-size: 11px;
color: var(--td-text-color-secondary);
line-height: 1.4;
}
}
.submenu-body {
flex: 1;
overflow-y: auto;
padding: 6px;
display: flex;
flex-direction: column;
gap: 4px;
}
.state-placeholder {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 10px;
padding: 40px 0;
color: var(--td-text-color-placeholder);
.empty-text {
font-size: 12px;
}
}
.channels-list {
display: flex;
flex-direction: column;
gap: 2px;
}
.channel-item {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 10px;
background: transparent;
border-radius: 6px;
cursor: pointer;
outline: none;
transition: background 0.15s;
&:hover,
&:focus-visible {
background: var(--td-bg-color-container-hover);
}
&.is-disabled {
opacity: 0.55;
}
}
// Stacked rows: agent on top, channel below. Both lines share identical
// typography and avatar size so neither dominates — they read as two facets
// of the same row rather than a headline + caption.
.stack {
flex: 1;
min-width: 0;
display: flex;
flex-direction: column;
gap: 3px;
}
.stack-row {
display: flex;
align-items: center;
gap: 8px;
min-width: 0;
}
.row-avatar {
display: inline-flex;
align-items: center;
justify-content: center;
width: 20px;
height: 20px;
border-radius: 4px;
flex-shrink: 0;
&.platform-logo {
object-fit: contain;
background: transparent;
padding: 1px;
}
}
.row-label {
font-size: 12px;
color: var(--td-text-color-primary);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
min-width: 0;
line-height: 1.3;
}
// Switch stays right-anchored, separated from the stack by the natural row gap.
.item-actions {
margin-left: 8px;
flex-shrink: 0;
display: inline-flex;
align-items: center;
}
.builtin-avatar {
display: flex;
align-items: center;
justify-content: center;
width: 20px;
height: 20px;
border-radius: 4px;
flex-shrink: 0;
&.agent-emoji {
font-size: 13px;
line-height: 1;
background: var(--td-bg-color-container-hover);
}
&.normal {
background: linear-gradient(135deg, rgba(7, 192, 95, 0.15) 0%, rgba(7, 192, 95, 0.08) 100%);
color: var(--td-brand-color-active);
}
&.agent {
background: linear-gradient(135deg, rgba(124, 77, 255, 0.15) 0%, rgba(124, 77, 255, 0.08) 100%);
color: var(--td-brand-color);
}
}
// Size the generated AgentAvatar (hash-based gradient + letter) to match the
// builtin-avatar and platform-logo so all three rows align perfectly.
.stack-row {
:deep(.agent-avatar),
:deep(.agent-avatar-small) {
width: 20px;
height: 20px;
}
:deep(.agent-avatar-letter) {
font-size: 10px !important;
}
}
</style>