-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathOfficePreviewPanel.tsx
More file actions
225 lines (196 loc) · 6.46 KB
/
Copy pathOfficePreviewPanel.tsx
File metadata and controls
225 lines (196 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { cn } from '@cherrystudio/ui/lib/utils'
import { loggerService } from '@logger'
import { EmptyState, LoadingState } from '@renderer/components/chat/primitives'
import { AlertCircle, FileText } from 'lucide-react'
import { type ComponentType, type ReactNode, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
const logger = loggerService.withContext('OfficePreviewPanel')
interface OfficeDocumentPreviewProps {
filePath: string
fileName: string
refreshKey: number
sourceSize?: number
actions?: ReactNode
}
type OfficeDocumentPreviewPanel = ComponentType<OfficeDocumentPreviewProps>
export interface OfficePreviewPanelProps {
filePath: string
fileName?: string
sourceFilePath?: string
sourceSize?: number
className?: string
refreshKey?: number
actions?: ReactNode
}
let wordPreviewPanelPromise: Promise<OfficeDocumentPreviewPanel> | null = null
let pptxPreviewPanelPromise: Promise<OfficeDocumentPreviewPanel> | null = null
let xlsxPreviewPanelPromise: Promise<OfficeDocumentPreviewPanel> | null = null
function loadWordPreviewPanel() {
wordPreviewPanelPromise ??= import('./WordPreviewPanel')
.then((module) => module.default)
.catch((err: unknown) => {
wordPreviewPanelPromise = null
throw err
})
return wordPreviewPanelPromise
}
function loadPptxPreviewPanel() {
pptxPreviewPanelPromise ??= import('./PptxPreviewPanel')
.then((module) => module.default)
.catch((err: unknown) => {
pptxPreviewPanelPromise = null
throw err
})
return pptxPreviewPanelPromise
}
function loadXlsxPreviewPanel() {
xlsxPreviewPanelPromise ??= import('./XlsxPreviewPanel')
.then((module) => module.XlsxPreviewPanel)
.catch((err: unknown) => {
xlsxPreviewPanelPromise = null
throw err
})
return xlsxPreviewPanelPromise
}
// Extension → lazy panel loader. Doubles as the source of truth for which office
// documents get an inline preview; anything not listed falls back to "open externally".
const OFFICE_PREVIEW_LOADERS: Record<string, () => Promise<OfficeDocumentPreviewPanel>> = {
docx: loadWordPreviewPanel,
pptx: loadPptxPreviewPanel,
xlsx: loadXlsxPreviewPanel
}
const SUPPORTED_OFFICE_PREVIEW_EXTENSIONS = new Set(Object.keys(OFFICE_PREVIEW_LOADERS))
function extOf(name: string | undefined): string {
if (!name) return ''
const dot = name.lastIndexOf('.')
return dot < 0 ? '' : name.slice(dot + 1).toLowerCase()
}
function getFileDisplayName(filePath: string, fileName?: string): string {
if (fileName) return fileName
const segments = filePath.replace(/\\/g, '/').split('/')
return segments.at(-1) ?? filePath
}
function getPreviewExtension(filePath: string, fileName?: string): string {
const fromName = extOf(fileName)
if (fromName) return fromName
return extOf(filePath)
}
function isAbsoluteFilePath(filePath: string): boolean {
return filePath.startsWith('/') || /^[A-Za-z]:[\\/]/.test(filePath)
}
function UnsupportedOfficePreview({ extension, actions }: { extension: string; actions?: ReactNode }) {
const { t } = useTranslation()
return (
<EmptyState
icon={FileText}
title={t('agent.preview_pane.office.title', { extension: extension ? `.${extension}` : '' })}
description={t('agent.preview_pane.office.description')}
actions={actions}
/>
)
}
function OfficePreviewError({ actions }: { actions?: ReactNode }) {
const { t } = useTranslation()
return (
<EmptyState icon={AlertCircle} title={t('common.error')} description={t('files.preview.error')} actions={actions} />
)
}
function SupportedOfficePreview({
extension,
filePath,
fileName,
refreshKey,
sourceSize,
actions
}: OfficeDocumentPreviewProps & { extension: string }) {
const { t } = useTranslation()
const [loadedPreview, setLoadedPreview] = useState<{
extension: string
Component: OfficeDocumentPreviewPanel
} | null>(null)
const [loadError, setLoadError] = useState<Error | null>(null)
const PreviewPanel = loadedPreview?.extension === extension ? loadedPreview.Component : null
useEffect(() => {
if (PreviewPanel) return
let cancelled = false
setLoadError(null)
const loader = OFFICE_PREVIEW_LOADERS[extension]
if (!loader) return
loader()
.then((Component) => {
if (!cancelled) setLoadedPreview({ extension, Component })
})
.catch((err: unknown) => {
if (cancelled) return
const normalized = err instanceof Error ? err : new Error(String(err))
logger.error(`Failed to load ${extension} preview panel`, normalized)
setLoadError(normalized)
})
return () => {
cancelled = true
}
}, [extension, PreviewPanel])
if (loadError) {
return <OfficePreviewError actions={actions} />
}
if (!PreviewPanel) {
return (
<div className="flex h-full w-full items-center justify-center">
<LoadingState label={t('common.loading')} />
</div>
)
}
return (
<PreviewPanel
filePath={filePath}
fileName={fileName}
refreshKey={refreshKey}
sourceSize={sourceSize}
actions={actions}
/>
)
}
export function OfficePreviewPanel({
filePath,
fileName,
sourceFilePath,
sourceSize,
className,
refreshKey = 0,
actions
}: OfficePreviewPanelProps) {
const extension = getPreviewExtension(filePath, fileName)
const displayName = getFileDisplayName(filePath, fileName)
const supported = SUPPORTED_OFFICE_PREVIEW_EXTENSIONS.has(extension)
const previewFilePath = sourceFilePath ?? (isAbsoluteFilePath(filePath) ? filePath : undefined)
if (!supported) {
return (
<div className={cn('flex h-full min-h-[320px] min-w-0 flex-col bg-background', className)}>
<UnsupportedOfficePreview extension={extension} actions={actions} />
</div>
)
}
if (!previewFilePath) {
return (
<div className={cn('flex h-full min-h-[320px] min-w-0 flex-col bg-background', className)}>
<OfficePreviewError actions={actions} />
</div>
)
}
return (
<div className={cn('flex h-full min-h-[320px] min-w-0 flex-col overflow-hidden bg-background', className)}>
<div className="min-h-0 flex-1 overflow-hidden">
<SupportedOfficePreview
key={`${previewFilePath}-${refreshKey}`}
extension={extension}
filePath={previewFilePath}
fileName={displayName}
refreshKey={refreshKey}
sourceSize={sourceSize}
actions={actions}
/>
</div>
</div>
)
}
export default OfficePreviewPanel