--- a/web/src/main.tsx +++ b/web/src/main.tsx @@ -17,10 +17,11 @@ For commercial licensing, please contact support@quantumnous.com */ import { QueryClientProvider } from '@tanstack/react-query' -import { RouterProvider, createRouter } from '@tanstack/react-router' +import { RouterProvider, createMemoryHistory, createRouter } from '@tanstack/react-router' import { StrictMode } from 'react' import ReactDOM from 'react-dom/client' +import { applyAuthBundle, clearAuthentication, isAuthBundle } from '@/lib/auth-session' import { installBuildMetadata } from '@/lib/build-metadata' import { applyFaviconToDom } from '@/lib/dom-utils' import '@/lib/dayjs' @@ -43,30 +44,25 @@ initializeFrontendCache() installBuildMetadata() -const queryClient = createAppQueryClient(() => { - void router.navigate({ to: '/500' }) -}) - -// Create a new router instance -const router = createRouter({ - routeTree, - context: { queryClient }, - defaultPreload: 'intent', - defaultPreloadStaleTime: 0, -}) - -// Register the router instance for type safety -declare module '@tanstack/react-router' { - interface Register { - router: typeof router - } +function createView(path?: string) { + const queryClient = createAppQueryClient(() => { void router.navigate({ to: '/500' }) }) + const router = createRouter({ + routeTree, context: { queryClient }, defaultPreload: 'intent', defaultPreloadStaleTime: 0, + history: path ? createMemoryHistory({ initialEntries: [path] }) : undefined, + }) + return { router, queryClient } } -// Render the app -const rootElement = document.querySelector('#root') -if (!rootElement) { - throw new Error('Root element not found') +declare module '@tanstack/react-router' { + interface Register { router: ReturnType['router'] } } + +function mount(element: HTMLElement, path?: string, workspaceIdentity?: unknown) { + if (workspaceIdentity !== undefined) { + if (!isAuthBundle(workspaceIdentity)) throw new Error('网关返回的账号信息不完整,请重试。') + applyAuthBundle(workspaceIdentity, false) + } + const { router, queryClient } = createView(path) // Set document.title and favicon from cached status, then refresh from network ;(function initSystemBranding() { try { @@ -100,8 +96,7 @@ /* empty */ } })() -if (!rootElement.innerHTML) { - const root = ReactDOM.createRoot(rootElement) + const root = ReactDOM.createRoot(element) root.render( @@ -115,4 +110,30 @@ ) + + const off = path ? router.subscribe('onResolved', ({ toLocation }) => { + if (toLocation.pathname === '/sign-in') { + window.dispatchEvent(new Event('maltai-native-auth-needed')) + return + } + window.dispatchEvent(new CustomEvent('maltai-native-route', { detail: toLocation.href })) + }) : () => {} + return { + navigate(href: string) { if (router.history.location.href !== href) router.history.replace(href) }, + close() { + off() + root.unmount() + void queryClient.cancelQueries() + queryClient.clear() + router.history.destroy() + clearAuthentication(false, 'idle') + }, + } } + +if (document.documentElement.hasAttribute('data-maltai-shell')) { + Object.assign(window, { maltaiMountNative: mount }) +} else { + const root = document.getElementById('root') + if (root && !root.innerHTML) mount(root) +} --- a/web/src/routes/(auth)/sign-in.tsx +++ b/web/src/routes/(auth)/sign-in.tsx @@ -29,9 +29,12 @@ }) export const Route = createFileRoute('/(auth)/sign-in')({ - component: SignIn, + component: () => document.documentElement.hasAttribute('data-maltai-shell') ? null : , validateSearch: searchSchema, beforeLoad: async ({ search }) => { + if (document.documentElement.hasAttribute('data-maltai-shell')) { + return + } // 根 guard 可能因为没有会话提示而跳过了 refresh。此处必须回源确认, // 否则持有有效 Refresh Cookie 的用户会被要求重新输入密码。 await resolveAuthentication() --- a/web/src/features/auth/secure-verification/components/secure-verification-dialog.tsx +++ b/web/src/features/auth/secure-verification/components/secure-verification-dialog.tsx @@ -71,7 +71,7 @@ canVerify = canVerify && input.code.trim().length >= 6 } // Linked-account verification starts from the provider button itself. - const showSubmit = state.phase !== 'error' && input?.method !== 'oauth' + const showSubmit = state.phase !== 'error' && Boolean(input) && input?.method !== 'oauth' const error = 'error' in state ? state.error : undefined const formId = `${inputId}-form` @@ -158,7 +158,7 @@ > {!input ? (
-

{t('No verification method is available for this action.')}

+

{state.request.scope === 'channel.key.read' ? t('There is no available verification method to view this channel key. Leave the key field empty to keep it, or enter a new key to replace it.') : t('No verification method is available for this action.')}

{ready.requirements.methods.map( (option) => option.reason && ( --- a/web/src/i18n/locales/zh.json +++ b/web/src/i18n/locales/zh.json @@ -4366,6 +4366,7 @@ "No vendor data available": "暂无厂商数据", "Official plugins": "官方插件", "No vendors found": "未找到供应商", + "There is no available verification method to view this channel key. Leave the key field empty to keep it, or enter a new key to replace it.": "当前没有可用于查看此密钥的验证方式。留空可保留原密钥,填写新密钥后保存即可替换。", "No verification method is available for this action.": "此操作没有可用的验证方式。", "No version history": "暂无版本历史", "No X Found": "未找到 X", --- a/web/src/i18n/locales/zh-TW.json +++ b/web/src/i18n/locales/zh-TW.json @@ -4366,6 +4366,7 @@ "No vendor data available": "暫無廠商數據", "Official plugins": "官方外掛", "No vendors found": "找不到供應商", + "There is no available verification method to view this channel key. Leave the key field empty to keep it, or enter a new key to replace it.": "目前沒有可用於查看此金鑰的驗證方式。留空可保留原金鑰,輸入新金鑰後儲存即可替換。", "No verification method is available for this action.": "此操作沒有可用的驗證方式。", "No version history": "暫無版本歷史", "No X Found": "未找到 X", --- a/web/src/lib/http-client.ts +++ b/web/src/lib/http-client.ts @@ -75,6 +75,10 @@ }) as typeof api.get function redirectToSignIn(): void { + if (typeof document !== 'undefined' && document.documentElement.hasAttribute('data-maltai-shell')) { + window.dispatchEvent(new Event('maltai-native-auth-needed')) + return + } if ( typeof window !== 'undefined' && window.location.pathname !== '/sign-in'