previewPaneOfficeContent.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { Component, lazy, Suspense, useMemo, type ErrorInfo, type ReactNode } from "react";
  2. import type { FileBinaryPreview } from "@zcode/shared";
  3. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  4. import { decodeBase64ToArrayBuffer, type OfficeFilePreviewKind } from "@/lib/officeFilePreview.js";
  5. import { logger } from "@/logger.js";
  6. const LazyXlsxContent = lazy(async () => {
  7. const module = await import("@/previewPaneOfficeXlsxContent.js");
  8. return { default: module.PreviewPaneOfficeXlsxContent };
  9. });
  10. const LazyDocxContent = lazy(async () => {
  11. const module = await import("@/previewPaneOfficeDocxContent.js");
  12. return { default: module.PreviewPaneOfficeDocxContent };
  13. });
  14. const LazyLegacyDocContent = lazy(async () => {
  15. const module = await import("@/previewPaneOfficeLegacyDocContent.js");
  16. return { default: module.PreviewPaneOfficeLegacyDocContent };
  17. });
  18. class OfficePreviewErrorBoundary extends Component<
  19. {
  20. children: ReactNode;
  21. fallback: ReactNode;
  22. resetKey: string;
  23. },
  24. { hasError: boolean }
  25. > {
  26. state = { hasError: false };
  27. static getDerivedStateFromError() {
  28. return { hasError: true };
  29. }
  30. componentDidCatch(error: Error, info: ErrorInfo) {
  31. logger.error("[PreviewPane] Office 预览组件渲染失败", {
  32. error: error.message,
  33. componentStack: info.componentStack ?? "",
  34. });
  35. }
  36. componentDidUpdate(previousProps: Readonly<{ resetKey: string }>) {
  37. if (this.state.hasError && previousProps.resetKey !== this.props.resetKey) {
  38. this.setState({ hasError: false });
  39. }
  40. }
  41. render() {
  42. return this.state.hasError ? this.props.fallback : this.props.children;
  43. }
  44. }
  45. function OfficePreviewLoading() {
  46. return (
  47. <div
  48. aria-busy="true"
  49. className="h-full min-h-0 w-full bg-background"
  50. data-office-preview-pending
  51. />
  52. );
  53. }
  54. function OfficePreviewErrorMessage({ children }: { children: ReactNode }) {
  55. return (
  56. <div className="p-3 text-ui-base text-destructive" role="alert">
  57. {children}
  58. </div>
  59. );
  60. }
  61. export function PreviewPaneOfficeContent({
  62. error,
  63. kind,
  64. loading,
  65. onOpenBrowserUrl,
  66. preview,
  67. resolvedTheme,
  68. sourcePath,
  69. }: {
  70. error: string | null;
  71. kind: OfficeFilePreviewKind;
  72. loading: boolean;
  73. onOpenBrowserUrl?: (url: string) => void;
  74. preview: FileBinaryPreview | null;
  75. resolvedTheme: "light" | "dark";
  76. sourcePath: string;
  77. }) {
  78. const { intl } = useZCodeIntl();
  79. const errorMessage = intl.formatMessage({
  80. id: "codeViewer.officeUnavailable",
  81. });
  82. const buffer = useMemo(
  83. () => (preview ? decodeBase64ToArrayBuffer(preview.dataBase64) : null),
  84. [preview],
  85. );
  86. if (loading || (!error && !buffer)) {
  87. return <OfficePreviewLoading />;
  88. }
  89. if (error || !buffer) {
  90. return <OfficePreviewErrorMessage>{error ?? errorMessage}</OfficePreviewErrorMessage>;
  91. }
  92. const fallback = <OfficePreviewErrorMessage>{errorMessage}</OfficePreviewErrorMessage>;
  93. return (
  94. <OfficePreviewErrorBoundary
  95. fallback={fallback}
  96. resetKey={`${kind}:${sourcePath}:${preview?.totalBytes ?? 0}`}
  97. >
  98. <Suspense fallback={<OfficePreviewLoading />}>
  99. {kind === "excel" ? (
  100. <LazyXlsxContent
  101. buffer={buffer}
  102. errorMessage={errorMessage}
  103. isDark={resolvedTheme === "dark"}
  104. onOpenBrowserUrl={onOpenBrowserUrl}
  105. sourcePath={sourcePath}
  106. />
  107. ) : kind === "docx" ? (
  108. <LazyDocxContent
  109. buffer={buffer}
  110. errorMessage={errorMessage}
  111. onOpenBrowserUrl={onOpenBrowserUrl}
  112. sourcePath={sourcePath}
  113. />
  114. ) : (
  115. <LazyLegacyDocContent
  116. buffer={buffer}
  117. errorMessage={errorMessage}
  118. onOpenBrowserUrl={onOpenBrowserUrl}
  119. sourcePath={sourcePath}
  120. />
  121. )}
  122. </Suspense>
  123. </OfficePreviewErrorBoundary>
  124. );
  125. }