Files
ai_academy/frontend/src/utils/tenantSwitch.ts
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

100 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Tenant-switch navigation helper.
//
// Switching the active tenant always lands the user on the platform's KB
// list. 之前是「在当前路径 reload」+ 个别敏感路径回退到 KB 列表,但即便不带
// resource id 的页面(设置、Agent 列表等)reload 后也常常因为新空间下没有
// 对应数据出现空状态,体验跟跳到固定首页其实差不多——干脆统一跳到 KB 列表,
// 用一次 full navigation 把所有 store / SSE / 请求都重置一遍。
import { updateMyPreferences } from '@/api/auth'
const SAFE_FALLBACK_PATH = '/platform/knowledge-bases'
/**
* Return the URL to navigate to after a tenant switch. 目前始终返回 KB 列表
* 作为登录页,保留函数签名是为了未来需要按路由做特殊处理时留个口子。
*/
export function tenantSwitchTargetPath(_currentPath: string): string {
return SAFE_FALLBACK_PATH
}
/**
* Perform the post-switch navigation. 统一跳到 KB 列表。
*/
export function navigateAfterTenantSwitch(): void {
window.location.href = tenantSwitchTargetPath(window.location.pathname)
}
// 切换成功后的 toast 跨 hard reload 传递:调用方在 reload 前把信息塞进
// sessionStorage,App.vue 启动时 consume 一次再弹出。直接在 reload 前调
// NotifyPlugin 会被刷掉,根本来不及看清。
const PENDING_TOAST_KEY = 'academy_pending_tenant_switch_toast'
export interface PendingTenantSwitchToast {
name: string
/** Human-readable role label, e.g. "所有者" / "Owner". Drives the role chip text. */
role?: string
/** Raw role enum, e.g. "owner". Drives the role chip color + icon. */
roleEnum?: string
}
export function stashTenantSwitchToast(payload: PendingTenantSwitchToast): void {
try {
sessionStorage.setItem(PENDING_TOAST_KEY, JSON.stringify(payload))
} catch {
// sessionStorage 写失败(隐私模式等)就静默放弃,toast 是锦上添花
}
}
export function consumePendingTenantSwitchToast(): PendingTenantSwitchToast | null {
try {
const raw = sessionStorage.getItem(PENDING_TOAST_KEY)
if (!raw) return null
sessionStorage.removeItem(PENDING_TOAST_KEY)
const parsed = JSON.parse(raw)
if (parsed && typeof parsed.name === 'string') {
return {
name: parsed.name,
role: typeof parsed.role === 'string' ? parsed.role : undefined,
roleEnum: typeof parsed.roleEnum === 'string' ? parsed.roleEnum : undefined,
}
}
return null
} catch {
return null
}
}
/**
* Persist the user's "last active tenant" preference server-side so a
* fresh login (new device, new refresh token, cleared browser) drops
* them back into this workspace instead of always bouncing to their
* home tenant.
*
* Conventions:
* - Pass the target tenant id when switching to a peer tenant.
* - Pass `null` (which sends `0`) when switching back to the home
* tenant — that clears the preference and reverts the user to the
* "always start at home" default.
*
* Fire-and-forget: callers usually trigger a full-page reload right
* after; this returns the in-flight promise so callers can race it
* against a small budget if they want best-effort completion before
* navigation. Failures are logged but never thrown — losing one
* persist is recoverable on the next switch.
*/
export function persistLastActiveTenantPreference(
tenantId: number | null,
): Promise<void> {
const payload = { last_active_tenant_id: tenantId == null ? 0 : tenantId }
return updateMyPreferences(payload)
.then((res) => {
if (!res.success) {
console.warn('persistLastActiveTenantPreference: server rejected update', res.message)
}
})
.catch((err) => {
console.warn('persistLastActiveTenantPreference: network/serialization error', err)
})
}