main.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { createRoot } from "react-dom/client";
  2. import "../../share-ui/src/styles.css";
  3. import { applyTheme, type Theme } from "../../ui/src/useTheme.js";
  4. import {
  5. ConversationShareLandingLoader,
  6. ConversationShareLandingStatus,
  7. } from "../../web/src/share/ConversationShareLandingPage.js";
  8. import {
  9. ConversationSharePreviewClient,
  10. resolveConversationShareRouteLocale,
  11. } from "../../web/src/share/conversationSharePreviewClient.js";
  12. import {
  13. isConversationSharePath,
  14. resolveConversationShareCodeFromPath,
  15. } from "../../web/src/share/conversationShareRoute.js";
  16. import { resolveWebRemoteControlInitialTheme } from "../../web/src/webRemoteControlThemeSeed.js";
  17. import { createWebAuthService } from "../../web/src/auth/webAuthService.js";
  18. import {
  19. WEB_ZAI_OAUTH_CONFIG,
  20. resolveWebAuthDevReturnTo,
  21. } from "../../web/src/auth/webZaiOAuthConfig.js";
  22. import { parseOAuthState, resolveSafeAppReturnTo } from "../../web/src/auth/oauthStateCodec.js";
  23. import { ShareCallbackPage } from "./ShareAuthPages.js";
  24. const root = createRoot(document.getElementById("root")!);
  25. const webAuthService = createWebAuthService();
  26. function resolveThemePreference(): Theme {
  27. const saved = localStorage.getItem("zcode-theme");
  28. return resolveWebRemoteControlInitialTheme({ storedTheme: saved, defaultTheme: "zai-light" });
  29. }
  30. // Share 是独立入口,不会经过完整 Web 的 bootstrap;必须在渲染前同步根主题 class,
  31. // 否则页面外壳使用默认浅色 token,而 MessageResponse 会按 zai-dark 选择 github-dark,
  32. // 最终产生浅色页面套深色 Markdown 代码块的混合样式。
  33. const INITIAL_THEME = resolveThemePreference();
  34. applyTheme(INITIAL_THEME);
  35. function isOAuthCallback(): boolean {
  36. return (
  37. window.location.pathname === "/cn/share/callback" ||
  38. window.location.pathname === "/share/callback"
  39. );
  40. }
  41. function renderCallback(): void {
  42. const state = parseOAuthState(new URLSearchParams(window.location.search).get("state") ?? "");
  43. const retryTarget = resolveSafeAppReturnTo(state?.app_return_to) ?? "/share";
  44. root.render(
  45. <ShareCallbackPage
  46. authService={webAuthService}
  47. onSuccess={({ appReturnTo }) => window.location.replace(appReturnTo ?? "/share")}
  48. onRetry={() => window.location.replace(retryTarget)}
  49. />,
  50. );
  51. }
  52. async function renderShare(): Promise<void> {
  53. const locale = resolveConversationShareRouteLocale(window.location.pathname);
  54. document.documentElement.lang = locale;
  55. document.title = locale === "zh-CN" ? "ZCode 会话分享" : "ZCode Conversation Share";
  56. const shareCode = resolveConversationShareCodeFromPath(window.location.pathname);
  57. if (!shareCode) {
  58. root.render(
  59. <ConversationShareLandingStatus
  60. state={{ kind: "error", error: "invalid_contract" }}
  61. locale={locale}
  62. />,
  63. );
  64. return;
  65. }
  66. const endpointOrigin =
  67. import.meta.env.VITE_ZCODE_BASE_URL?.trim().replace(/\/+$/u, "") || window.location.origin;
  68. const mockMode =
  69. import.meta.env.DEV && import.meta.env.VITE_CONVERSATION_SHARE_PREVIEW_MOCK === "true";
  70. const client = mockMode
  71. ? new (
  72. await import("../../web/src/share/mockConversationSharePreviewClient.js")
  73. ).MockConversationSharePreviewClient()
  74. : new ConversationSharePreviewClient({ baseUrl: `${endpointOrigin}/api/v1` });
  75. const getMockToken = () =>
  76. mockMode && window.sessionStorage.getItem("zcode:share:mock-auth") === "owner"
  77. ? "mock-owner-token"
  78. : null;
  79. root.render(
  80. <ConversationShareLandingLoader
  81. shareCode={shareCode}
  82. client={client}
  83. getAccessToken={() => getMockToken() ?? webAuthService.getZCodeJwtToken()}
  84. onLogin={(provider) => {
  85. if (mockMode) {
  86. window.sessionStorage.setItem("zcode:share:mock-auth", "owner");
  87. window.location.reload();
  88. return;
  89. }
  90. webAuthService.startLogin({
  91. provider,
  92. appReturnTo: window.location.href,
  93. redirectUri: WEB_ZAI_OAUTH_CONFIG.shareRedirectUri,
  94. devReturnTo: resolveWebAuthDevReturnTo(WEB_ZAI_OAUTH_CONFIG, "share"),
  95. });
  96. }}
  97. onLogout={() => {
  98. if (mockMode) window.sessionStorage.removeItem("zcode:share:mock-auth");
  99. void webAuthService.logout();
  100. window.location.reload();
  101. }}
  102. locale={locale}
  103. theme={INITIAL_THEME}
  104. />,
  105. );
  106. }
  107. async function main(): Promise<void> {
  108. if (isOAuthCallback()) {
  109. renderCallback();
  110. return;
  111. }
  112. if (isConversationSharePath(window.location.pathname)) {
  113. await renderShare();
  114. return;
  115. }
  116. root.render(
  117. <ConversationShareLandingStatus state={{ kind: "error", error: "invalid_contract" }} />,
  118. );
  119. }
  120. void main();