diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 414eabaa..17026875 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -10,6 +10,7 @@ import { getCurrentUser } from '@/api/auth' import { consumePendingTenantSwitchToast } from '@/utils/tenantSwitch' import { useRoleLabel } from '@/composables/useRoleLabel' import { notifyLoginSuccess } from '@/utils/loginNotify' +import { renderWorkspaceNotifyContent } from '@/utils/workspaceNotifyContent' // TDesign locale configs import enUSConfig from 'tdesign-vue-next/esm/locale/en_US' @@ -18,7 +19,7 @@ import koKRConfig from 'tdesign-vue-next/esm/locale/ko_KR' import ruRUConfig from 'tdesign-vue-next/esm/locale/ru_RU' const { locale, t } = useI18n() -const { formatRole } = useRoleLabel() +const { formatRole, roleIcon } = useRoleLabel() const router = useRouter() const authStore = useAuthStore() const settingsStore = useSettingsStore() @@ -143,7 +144,7 @@ const handleGlobalOIDCCallback = async () => { if (response.success) { clearOIDCCallbackState('/') await persistOIDCLoginResponse(response) - notifyLoginSuccess(response, t, formatRole) + notifyLoginSuccess(response, t, formatRole, roleIcon) return } @@ -203,11 +204,18 @@ watch( const showPendingTenantSwitchToast = () => { const pending = consumePendingTenantSwitchToast() if (!pending) return + const templateKey = pending.role + ? 'tenant.switchSuccessContentWithRole' + : 'tenant.switchSuccessContent' NotifyPlugin.success({ title: t('tenant.switchSuccessTitle'), - content: pending.role - ? t('tenant.switchSuccessContentWithRole', { name: pending.name, role: pending.role }) - : t('tenant.switchSuccessContent', { name: pending.name }), + content: renderWorkspaceNotifyContent({ + template: t(templateKey), + name: pending.name, + roleLabel: pending.role, + roleEnum: pending.roleEnum, + roleIconName: pending.roleEnum ? roleIcon(pending.roleEnum) : undefined, + }), duration: 6000, closeBtn: true, }) diff --git a/frontend/src/components/TenantSelector.vue b/frontend/src/components/TenantSelector.vue index 89103e99..68cf215c 100644 --- a/frontend/src/components/TenantSelector.vue +++ b/frontend/src/components/TenantSelector.vue @@ -180,7 +180,11 @@ const selectTenant = (tenantId: number) => { const membership = (authStore.memberships ?? []).find((m) => Number(m.tenant_id) === tenantId) const roleLabel = membership ? formatRole(membership.role) : '' // Toast 在 reload 后由 App.vue 弹出(直接在这里弹会被 hard reload 干掉)。 - stashTenantSwitchToast({ name: displayName, role: roleLabel || undefined }) + stashTenantSwitchToast({ + name: displayName, + role: roleLabel || undefined, + roleEnum: membership?.role || undefined, + }) // Persist "last active tenant" preference (switching to home clears // it). Fire-and-forget, but race it against the existing 500ms grace // window so most writes finish before the hard reload tears the page diff --git a/frontend/src/components/UserMenu.vue b/frontend/src/components/UserMenu.vue index 7a01f648..7c44fdb6 100644 --- a/frontend/src/components/UserMenu.vue +++ b/frontend/src/components/UserMenu.vue @@ -455,6 +455,7 @@ const switchToTenant = (m: Membership) => { stashTenantSwitchToast({ name: tenantDisplayName(m), role: formatRole(m.role) || undefined, + roleEnum: m.role || undefined, }) // Persist "last active tenant" preference (switching to home clears // it). Hard reload so every cached store / open SSE stream / in-flight diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts index f554153f..e9e050d2 100755 --- a/frontend/src/i18n/locales/en-US.ts +++ b/frontend/src/i18n/locales/en-US.ts @@ -1297,8 +1297,8 @@ export default { forgotPassword: 'Forgot Password?', loginSuccess: 'Login successful!', loginSuccessTitle: 'Login successful', - loginSuccessContent: 'Welcome back. You are now in "{name}"', - loginSuccessContentWithRole: 'Welcome back. You are now in "{name}" as {role}', + loginSuccessContent: 'Welcome back. You are now in {name}', + loginSuccessContentWithRole: 'Welcome back. You are now in {name} · as {role}', loginFailed: 'Login failed', loggingIn: 'Logging in...', register: 'Register', @@ -2393,8 +2393,8 @@ export default { searchHint: 'Search by name or enter tenant ID directly', noMatch: 'No matching tenants found', switchSuccessTitle: 'Workspace switched', - switchSuccessContent: 'You are now in "{name}"', - switchSuccessContentWithRole: 'You are now in "{name}" as {role}', + switchSuccessContent: 'You are now in {name}', + switchSuccessContentWithRole: 'You are now in {name} · as {role}', loadTenantsFailed: 'Failed to load tenant list', loading: 'Loading...', loadMore: 'Load more', diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.ts index 80e64057..7a363a39 100755 --- a/frontend/src/i18n/locales/ko-KR.ts +++ b/frontend/src/i18n/locales/ko-KR.ts @@ -1148,8 +1148,8 @@ export default { forgotPassword: "비밀번호를 잊으셨나요?", loginSuccess: "로그인 성공!", loginSuccessTitle: "로그인 성공", - loginSuccessContent: "환영합니다. 「{name}」에 입장했습니다", - loginSuccessContentWithRole: "환영합니다. 「{name}」에 입장했습니다 · 역할: {role}", + loginSuccessContent: "환영합니다. {name} 에 입장했습니다", + loginSuccessContentWithRole: "환영합니다. {name} 에 입장했습니다 · 역할 {role}", loginFailed: "로그인 실패", loggingIn: "로그인 중...", oidcLogin: "OIDC로 로그인", @@ -1641,8 +1641,8 @@ export default { searchHint: "이름으로 검색하거나 테넌트 ID를 직접 입력할 수 있습니다", noMatch: "일치하는 테넌트를 찾을 수 없습니다", switchSuccessTitle: "공간이 전환되었습니다", - switchSuccessContent: "「{name}」에 입장했습니다", - switchSuccessContentWithRole: "「{name}」에 입장했습니다 · 역할: {role}", + switchSuccessContent: "{name} 에 입장했습니다", + switchSuccessContentWithRole: "{name} 에 입장했습니다 · 역할 {role}", loadTenantsFailed: "테넌트 목록 로드 실패", loading: "로딩 중...", loadMore: "더 보기", diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts index 1a22672f..eaa81d61 100755 --- a/frontend/src/i18n/locales/ru-RU.ts +++ b/frontend/src/i18n/locales/ru-RU.ts @@ -1249,8 +1249,8 @@ export default { forgotPassword: 'Забыли пароль?', loginSuccess: 'Вход выполнен успешно!', loginSuccessTitle: 'Вход выполнен', - loginSuccessContent: 'С возвращением. Вы вошли в «{name}»', - loginSuccessContentWithRole: 'С возвращением. Вы вошли в «{name}» · роль: {role}', + loginSuccessContent: 'С возвращением. Вы вошли в {name}', + loginSuccessContentWithRole: 'С возвращением. Вы вошли в {name} · роль {role}', loginFailed: 'Ошибка входа', loggingIn: 'Вход...', register: 'Регистрация', @@ -1439,8 +1439,8 @@ export default { searchHint: 'Поиск по имени или введите ID арендатора напрямую', noMatch: 'Не найдено подходящих арендаторов', switchSuccessTitle: 'Пространство переключено', - switchSuccessContent: 'Вы вошли в «{name}»', - switchSuccessContentWithRole: 'Вы вошли в «{name}» · роль: {role}', + switchSuccessContent: 'Вы вошли в {name}', + switchSuccessContentWithRole: 'Вы вошли в {name} · роль {role}', loadTenantsFailed: 'Не удалось загрузить список арендаторов', loading: 'Загрузка...', loadMore: 'Загрузить еще', diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts index 182432f4..38c0bf94 100755 --- a/frontend/src/i18n/locales/zh-CN.ts +++ b/frontend/src/i18n/locales/zh-CN.ts @@ -1143,8 +1143,8 @@ export default { forgotPassword: "忘记密码?", loginSuccess: "登录成功!", loginSuccessTitle: "登录成功", - loginSuccessContent: "欢迎,你已进入「{name}」", - loginSuccessContentWithRole: "欢迎,你已进入「{name}」,身份:{role}", + loginSuccessContent: "欢迎,你已进入 {name}", + loginSuccessContentWithRole: "欢迎,你已进入 {name} · 身份 {role}", loginFailed: "登录失败", loggingIn: "登录中...", oidcLogin: "使用 OIDC 登录", @@ -1621,8 +1621,8 @@ export default { searchHint: "支持按名称搜索或直接输入空间 ID", noMatch: "未找到匹配的空间", switchSuccessTitle: "已切换空间", - switchSuccessContent: "你已进入「{name}」", - switchSuccessContentWithRole: "你已进入「{name}」,身份:{role}", + switchSuccessContent: "你已进入 {name}", + switchSuccessContentWithRole: "你已进入 {name} · 身份 {role}", loadTenantsFailed: "加载空间列表失败", loading: "加载中...", loadMore: "加载更多", diff --git a/frontend/src/utils/loginNotify.ts b/frontend/src/utils/loginNotify.ts index c0b3e02f..94cd2d5c 100644 --- a/frontend/src/utils/loginNotify.ts +++ b/frontend/src/utils/loginNotify.ts @@ -4,13 +4,15 @@ // flows feel identical to the user. // // Kept as a free function — not a composable — because the caller -// already has `t` and `formatRole` in scope from useI18n / useRoleLabel -// and there is no per-instance state worth tracking. +// 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 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 @@ -25,6 +27,7 @@ export function notifyLoginSuccess( response: LoginResponseLike | null | undefined, t: Translator, formatRole: RoleFormatter, + roleIcon: RoleIconResolver, ): void { const activeTenant = response?.active_tenant || response?.tenant if (!activeTenant) return @@ -34,13 +37,27 @@ export function notifyLoginSuccess( const membership = Array.isArray(response?.memberships) ? response!.memberships!.find((m) => Number(m?.tenant_id) === activeTenantId) : null - const roleLabel = membership?.role ? formatRole(membership.role) : '' + 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: roleLabel - ? t('auth.loginSuccessContentWithRole', { name: tenantName, role: roleLabel }) - : t('auth.loginSuccessContent', { name: tenantName }), + content: renderWorkspaceNotifyContent({ + template: t(templateKey), + name: tenantName, + roleLabel: roleLabel || undefined, + roleEnum: roleEnum || undefined, + roleIconName: roleIconName || undefined, + }), duration: 6000, closeBtn: true, }) diff --git a/frontend/src/utils/tenantSwitch.ts b/frontend/src/utils/tenantSwitch.ts index e4f8ebdb..8c320335 100644 --- a/frontend/src/utils/tenantSwitch.ts +++ b/frontend/src/utils/tenantSwitch.ts @@ -32,7 +32,10 @@ const PENDING_TOAST_KEY = 'weknora_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 { @@ -50,7 +53,11 @@ export function consumePendingTenantSwitchToast(): PendingTenantSwitchToast | nu 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 } + return { + name: parsed.name, + role: typeof parsed.role === 'string' ? parsed.role : undefined, + roleEnum: typeof parsed.roleEnum === 'string' ? parsed.roleEnum : undefined, + } } return null } catch { diff --git a/frontend/src/utils/workspaceNotifyContent.ts b/frontend/src/utils/workspaceNotifyContent.ts new file mode 100644 index 00000000..f6b470d5 --- /dev/null +++ b/frontend/src/utils/workspaceNotifyContent.ts @@ -0,0 +1,156 @@ +// Shared content renderer for the post-login and post-tenant-switch +// NotifyPlugin cards. Both cards present the same shape ("you are in +// as "), so we render them with a unified visual +// language — a neutral chip for the workspace name and a role-coloured +// chip for the role — instead of plain interpolated text. +// +// The renderer interpolates a template string carrying `{name}` and +// optionally `{role}` placeholders. Everything around those placeholders +// is rendered verbatim, which lets translators reorder the sentence +// however their language wants without further code changes. +// +// Why not just pass html to NotifyPlugin's `content` string? +// TDesign's plugin treats `content` as text. Passing a render function +// (TNode) is the supported way to inject styled VNodes; doing it +// through a shared helper keeps the chip styles in one place rather +// than scattered across two call sites. + +import { h, type VNode } from 'vue' +import { Icon as TIcon } from 'tdesign-vue-next' + +// Inline styles (rather than a global stylesheet) because NotifyPlugin +// renders into a teleported overlay outside any scoped