feat(frontend): add session source filter to sidebar (#1560)

IM-channel sessions were mixed in with the user's own chats. The sidebar
now defaults to web sessions; tenants with a configured IM channel get a
source switcher in the "对话" header to reach a specific platform.

- getSessionsList: optional `source` query param
- getMessageList: token-guarded loads (racing source switches / route
  changes can't append stale rows) + per-source pagination reset
- SourceSwitcherDropdown: scope dropdown mirroring KBSwitcherDropdown,
  scales to many platforms, reuses platform logos, closes on select
- empty state when a source has no sessions
- pure logic in sessionSourceFilter.ts with unit tests; zh/en/ko/ru i18n
This commit is contained in:
nullkey
2026-06-10 16:42:31 +08:00
committed by lyingbug
parent 232280d959
commit ac84dacc00
9 changed files with 398 additions and 8 deletions
+116 -6
View File
@@ -94,6 +94,14 @@
class="menu-pending-badge"
:title="t('organization.settings.pendingJoinRequestsBadge')">{{
orgStore.totalPendingJoinRequestCount }}</span>
<!-- 「对话」标题行右侧:会话来源切换图标 -->
<span v-if="item.path === 'creatChat'" class="menu-trailing">
<SourceSwitcherDropdown v-if="showSourceFilter && !batchMode"
:sources="sourceOptions" :current="currentSource" @select="switchSource">
<t-icon name="filter" @click.stop :title="currentSourceLabel"
:class="['menu-source-icon', { 'menu-source-icon-active': currentSource !== DEFAULT_SESSION_SOURCE }]" />
</SourceSwitcherDropdown>
</span>
</template>
</div>
</div>
@@ -138,6 +146,10 @@
</div>
</div>
</template>
<!-- 空状态:加载完成且当前来源无会话(切到无会话来源时不再白屏) -->
<div v-if="!loading && groupedSessions.length === 0" class="submenu_empty">
{{ t('menu.noSessions') }}
</div>
</div>
<!-- 批量管理底部操作条 -->
@@ -175,6 +187,15 @@ import { onMounted, watch, computed, ref, h } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { getSessionsList, delSession, batchDelSessions, deleteAllSessions, clearSessionMessages, pinSession, unpinSession } from "@/api/chat/index";
import { useChatResourcesStore } from '@/stores/chatResources';
import { listAllIMChannels } from '@/api/agent/index';
import SourceSwitcherDropdown from './SourceSwitcherDropdown.vue';
import {
DEFAULT_SESSION_SOURCE,
buildSourceOptions,
configuredPlatforms,
shouldShowSourceFilter,
type SessionSource,
} from './sessionSourceFilter';
import { logout as logoutApi } from '@/api/auth';
import { useMenuStore } from '@/stores/menu';
import { useAuthStore } from '@/stores/auth';
@@ -228,8 +249,25 @@ const currentpath = ref('');
const currentPage = ref(1);
const page_size = ref(30);
const total = ref(0);
// Monotonic token to discard stale list responses when loads race (see getMessageList).
let listRequestToken = 0;
const currentSecondpath = ref('');
const scrollContainer = ref<HTMLElement | null>(null);
// Default to the user's own web chats; only tenants with a configured IM
// channel get the switcher to reach a platform's sessions.
const currentSource = ref<SessionSource>(DEFAULT_SESSION_SOURCE);
const imPlatforms = ref<string[]>([]);
const showSourceFilter = computed(() => shouldShowSourceFilter(imPlatforms.value));
// Options for the source switcher: web first, then each configured IM platform.
// Logos are resolved here (where platformLogo lives) so the dropdown stays dumb.
const sourceOptions = computed(() =>
buildSourceOptions(imPlatforms.value, t('menu.myChats'), (p) => t(`agentEditor.im.${p}`))
.map((o) => ({ ...o, logo: o.platform ? platformLogo(o.platform) : '' }))
);
// Full label of the active source, shown as the trigger icon's tooltip.
const currentSourceLabel = computed(() =>
sourceOptions.value.find((o) => o.value === currentSource.value)?.label ?? t('menu.myChats')
);
// 计算总页数
const totalPages = computed(() => Math.ceil(total.value / page_size.value));
const hasMore = computed(() => currentPage.value < totalPages.value);
@@ -641,8 +679,10 @@ const checkScrollBottom = () => {
}
const handleScroll = debounce(checkScrollBottom, 200)
const getMessageList = async (isLoadMore = false) => {
if (loading.value) return Promise.resolve();
loading.value = true;
// Scroll-driven load-more must never stack. A fresh load (first load, route
// change, or source switch) instead supersedes any in-flight request via the
// token below, so it is allowed through even while one is still running.
if (isLoadMore && loading.value) return Promise.resolve();
// 只有在首次加载或路由变化时才清空数组,滚动加载时不清空
if (!isLoadMore) {
@@ -650,9 +690,16 @@ const getMessageList = async (isLoadMore = false) => {
usemenuStore.clearMenuArr();
}
return getSessionsList(currentPage.value, page_size.value).then((res: any) => {
// Snapshot the source and claim a request token: when loads race, only the
// newest may mutate the list, so a slow earlier response can't append rows
// to the wrong source or revive a list the user has already switched away from.
const requestSource = currentSource.value;
const token = ++listRequestToken;
loading.value = true;
return getSessionsList(currentPage.value, page_size.value, requestSource).then((res: any) => {
if (token !== listRequestToken) return; // superseded by a newer load
if (res.data && res.data.length) {
// Display all sessions globally without filtering
res.data.forEach((item: any) => {
let obj = {
title: item.title ? item.title : t('menu.newSession'),
@@ -669,11 +716,14 @@ const getMessageList = async (isLoadMore = false) => {
usemenuStore.updatemenuArr(obj)
});
}
if ((res as any).total) {
total.value = (res as any).total;
// Use != null so an empty source (total === 0) correctly clears the count
// instead of leaving the previous source's total behind.
if (res && res.total != null) {
total.value = res.total;
}
loading.value = false;
}).catch(() => {
if (token !== listRequestToken) return; // a newer load now owns `loading`
loading.value = false;
})
}
@@ -693,6 +743,27 @@ async function loadCurrentKbInfo(kbId: string) {
}
}
// Switch the session source filter. getMessageList(false) resets pagination and
// clears the list itself, and its request token makes any in-flight load for the
// previous source a no-op, so the switch always lands on the chosen source.
const switchSource = (source: SessionSource) => {
if (source === currentSource.value) return;
currentSource.value = source;
getMessageList();
};
// Detect whether this tenant has any IM channel configured. The filter control
// is only shown when it does, so non-IM users see an unchanged sidebar. Failures
// (e.g. no permission) silently leave the filter hidden.
const loadImPlatforms = async () => {
try {
const res: any = await listAllIMChannels();
imPlatforms.value = configuredPlatforms(res?.data || []);
} catch {
imPlatforms.value = [];
}
};
onMounted(async () => {
const routeName = typeof route.name === 'string' ? route.name : (route.name ? String(route.name) : '')
currentpath.value = routeName;
@@ -712,6 +783,8 @@ onMounted(async () => {
// 加载对话列表
getMessageList();
// 检测当前租户是否配置了 IM 渠道,决定是否显示来源筛选
loadImPlatforms();
// 若组织列表未加载则拉取一次,用于侧栏「待审批」角标
if (orgStore.organizations.length === 0) {
orgStore.fetchOrganizations();
@@ -1448,6 +1521,43 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
position: relative;
}
/* Right-aligned trailing controls on the「对话」title row (source-switch icon). */
.menu-trailing {
margin-left: auto;
display: inline-flex;
align-items: center;
gap: 8px;
flex-shrink: 0;
}
/* Source switcher icon: muted by default, brand-coloured when a non-web source is active. */
.menu-source-icon {
font-size: 15px;
color: var(--td-text-color-placeholder);
opacity: 0.7;
flex-shrink: 0;
cursor: pointer;
transition: opacity 0.2s ease, color 0.2s ease;
}
.menu_item:hover .menu-source-icon {
opacity: 1;
}
.menu-source-icon-active {
color: var(--td-brand-color);
opacity: 1;
}
/* Empty state when the active source has no sessions. */
.submenu_empty {
padding: 24px 14px;
text-align: center;
font-size: 12px;
color: var(--td-text-color-placeholder);
user-select: none;
}
// 顶部 logo_row 右侧的图标按钮组(搜索 + 折叠),与折叠按钮风格一致
.logo_actions {
display: flex;