Files
Enterprise-Knowledge-Base/frontend/src/utils/loginNotify.ts
T
wizardchen 559b5a6e20 feat(ui): render workspace-context notifications with styled chips
The login-success and tenant-switch toasts previously showed the
workspace name and role inline as bracketed plain text, which read
flat and made the two key data points hard to scan at a glance.

Render them through a shared `renderWorkspaceNotifyContent` helper
that produces a NotifyPlugin VNode with:

* a neutral rounded chip for the workspace name (with a generic
  workspace icon), and
* a role-coloured chip for the role (with the existing per-role icon
  from useRoleLabel), so owner / admin / contributor / viewer each
  get a distinct visual identity that matches the role badges used
  elsewhere in the product.

The i18n templates lose the surrounding 「」 / "" / «» quote marks
because the chip itself now provides the visual delineation; double
emphasis looked off. Translators control the surrounding wording via
`{name}` and `{role}` placeholders and the renderer splits on those
markers to interleave plain text and chips in the original order.

The PendingTenantSwitchToast payload (sessionStorage-stashed for the
hard-reload survival trick) grows a `roleEnum` field so the post-
reload toast can colour the chip — without that, the switch toast
would fall back to a neutral chip while the login toast got the
colour treatment.

Note on icon resolution: the chip renderer imports TDesign's `Icon`
component directly rather than referencing it by string tag in `h()`
— Vue's render function does not auto-resolve component names the
way templates do, so `h('t-icon', ...)` would silently render an
unknown HTML element.
2026-05-18 20:37:57 +08:00

65 lines
2.5 KiB
TypeScript

// Rich "you're now in {workspace} as {role}" notification for the
// post-login moment. Shared between the password (views/auth/Login.vue)
// and OIDC (App.vue handleGlobalOIDCCallback) login paths so the two
// flows feel identical to the user.
//
// Kept as a free function — not a composable — because the caller
// already has `t`, `formatRole` and `roleIcon` in scope from useI18n /
// useRoleLabel and there is no per-instance state worth tracking.
import { NotifyPlugin } from 'tdesign-vue-next'
import { renderWorkspaceNotifyContent } from './workspaceNotifyContent'
type Translator = (key: string, params?: Record<string, unknown>) => string
type RoleFormatter = (role: string | null | undefined) => string
type RoleIconResolver = (role: string | null | undefined) => string
interface LoginResponseLike {
// Password-login response uses `active_tenant`; the OIDC callback
// response uses `tenant` (legacy backward-compat name on the Go side).
// Accept either so callers don't have to normalise.
active_tenant?: { id?: number | string; name?: string }
tenant?: { id?: number | string; name?: string }
memberships?: Array<{ tenant_id?: number | string; role?: string }>
}
export function notifyLoginSuccess(
response: LoginResponseLike | null | undefined,
t: Translator,
formatRole: RoleFormatter,
roleIcon: RoleIconResolver,
): void {
const activeTenant = response?.active_tenant || response?.tenant
if (!activeTenant) return
const tenantName = activeTenant.name || String(activeTenant.id || '')
const activeTenantId = Number(activeTenant.id)
const membership = Array.isArray(response?.memberships)
? response!.memberships!.find((m) => Number(m?.tenant_id) === activeTenantId)
: null
const roleEnum = membership?.role
const roleLabel = roleEnum ? formatRole(roleEnum) : ''
const roleIconName = roleEnum ? roleIcon(roleEnum) : ''
// Fetch the template without interpolation so the workspace / role
// placeholders survive and the renderer can swap them for styled chips.
// vue-i18n returns the message verbatim when the named keys aren't
// provided in the params bag.
const templateKey = roleLabel
? 'auth.loginSuccessContentWithRole'
: 'auth.loginSuccessContent'
NotifyPlugin.success({
title: t('auth.loginSuccessTitle'),
content: renderWorkspaceNotifyContent({
template: t(templateKey),
name: tenantName,
roleLabel: roleLabel || undefined,
roleEnum: roleEnum || undefined,
roleIconName: roleIconName || undefined,
}),
duration: 6000,
closeBtn: true,
})
}