@swisper.ai/web-file-preview 0.0.2 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/file-preview/components/renderer-types.d.ts +4 -1
- package/dist/components/file-preview/components/renderer-types.d.ts.map +1 -1
- package/dist/components/file-preview/components/viewer/viewer-components.stories.d.ts.map +1 -1
- package/dist/components/file-preview/file-preview.d.ts.map +1 -1
- package/dist/components/file-preview/file-preview.stories.d.ts +49 -2
- package/dist/components/file-preview/file-preview.stories.d.ts.map +1 -1
- package/dist/components/file-thumbnail/components/previews/html-preview.d.ts.map +1 -1
- package/dist/components/file-thumbnail/components/previews/image-preview.d.ts.map +1 -1
- package/dist/components/file-thumbnail/components/previews/markdown-preview.d.ts.map +1 -1
- package/dist/components/file-thumbnail/components/previews/reveal-class-name.d.ts +4 -2
- package/dist/components/file-thumbnail/components/previews/reveal-class-name.d.ts.map +1 -1
- package/dist/components/file-thumbnail/components/previews/scaled-page.d.ts +4 -1
- package/dist/components/file-thumbnail/components/previews/scaled-page.d.ts.map +1 -1
- package/dist/components/file-thumbnail/file-thumbnail.d.ts +2 -0
- package/dist/components/file-thumbnail/file-thumbnail.d.ts.map +1 -1
- package/dist/components/file-thumbnail/file-thumbnail.stories.d.ts +76 -10
- package/dist/components/file-thumbnail/file-thumbnail.stories.d.ts.map +1 -1
- package/dist/index.css +1 -1
- package/dist/index.es.js +50 -33
- package/dist/index.es.js.map +1 -1
- package/package.json +15 -7
- package/src/__storybook__/parent-block.tsx +21 -0
- package/src/components/file-preview/__stories__/fixtures.ts +66 -0
- package/src/components/file-preview/components/renderer-types.ts +4 -1
- package/src/components/file-preview/components/viewer/viewer-components.stories.tsx +2 -8
- package/src/components/file-preview/file-preview.json +15 -0
- package/src/components/file-preview/file-preview.stories.tsx +378 -38
- package/src/components/file-preview/file-preview.tsx +7 -2
- package/src/components/file-thumbnail/__stories__/fixtures.ts +99 -0
- package/src/components/file-thumbnail/__stories__/types.ts +26 -0
- package/src/components/file-thumbnail/components/previews/html-preview.tsx +5 -1
- package/src/components/file-thumbnail/components/previews/image-preview.tsx +3 -1
- package/src/components/file-thumbnail/components/previews/markdown-preview.tsx +5 -1
- package/src/components/file-thumbnail/components/previews/reveal-class-name.ts +8 -8
- package/src/components/file-thumbnail/components/previews/scaled-page.tsx +6 -1
- package/src/components/file-thumbnail/file-thumbnail.json +17 -1
- package/src/components/file-thumbnail/file-thumbnail.stories.tsx +551 -84
- package/src/components/file-thumbnail/file-thumbnail.tsx +24 -7
- /package/src/components/file-thumbnail/components/shared/__tests__/{generation-queue.spec.ts → generation-queue.test.ts} +0 -0
- /package/src/components/file-thumbnail/hooks/__tests__/{use-object-url.spec.ts → use-object-url.test.ts} +0 -0
- /package/src/lib/__tests__/{file-kinds.spec.ts → file-kinds.test.ts} +0 -0
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
import type { Meta, StoryObj } from '@storybook/react-vite';
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
import { expect, fn, waitFor } from 'storybook/test';
|
|
2
4
|
|
|
5
|
+
import { isFilePreviewSupported } from '@/lib/file-kinds';
|
|
6
|
+
import { Button, Typography } from '@swisper.ai/ui';
|
|
7
|
+
|
|
8
|
+
import { ParentBlock } from '../../__storybook__/parent-block';
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
BROKEN_IMAGE,
|
|
12
|
+
BROKEN_PDF,
|
|
13
|
+
HTML_URL,
|
|
14
|
+
IMAGE_URL,
|
|
15
|
+
INVOICES_CSV,
|
|
16
|
+
MARKDOWN_URL,
|
|
17
|
+
PDF_URL,
|
|
18
|
+
PLAYGROUND_SOURCES,
|
|
19
|
+
PRESENTATION_URL,
|
|
20
|
+
SPREADSHEET_URL,
|
|
21
|
+
WORD_URL,
|
|
22
|
+
} from './__stories__/fixtures';
|
|
3
23
|
import {
|
|
4
24
|
FilePreview,
|
|
5
25
|
FilePreviewError,
|
|
@@ -7,15 +27,47 @@ import {
|
|
|
7
27
|
FilePreviewLoader,
|
|
8
28
|
type FilePreviewProps,
|
|
9
29
|
} from './file-preview';
|
|
30
|
+
import { FilePreviewThemeProvider, type FilePreviewTheme } from './hooks/use-file-preview-theme';
|
|
31
|
+
|
|
32
|
+
interface DocumentPreviewProps extends Omit<FilePreviewProps, 'children'> {
|
|
33
|
+
/** Offered instead of a retry when the file type cannot be previewed at all. */
|
|
34
|
+
onDownload?: () => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* How a host composes a preview: the file, the default loader, and an error slot with a
|
|
39
|
+
* neutral line and a way forward. The developer message goes to `onError`, never on screen.
|
|
40
|
+
*/
|
|
41
|
+
function DocumentPreview({ onDownload, ...props }: DocumentPreviewProps) {
|
|
42
|
+
// Remounts `FilePreview` on retry: the same `src` or `fileBlob` would not load again.
|
|
43
|
+
const [attempt, setAttempt] = useState(0);
|
|
44
|
+
const isSupported = isFilePreviewSupported(props.fileName);
|
|
10
45
|
|
|
11
|
-
function renderPreview(args: FilePreviewProps) {
|
|
12
46
|
return (
|
|
13
|
-
<FilePreview {...
|
|
47
|
+
<FilePreview key={attempt} {...(props as FilePreviewProps)}>
|
|
14
48
|
<FilePreviewFile />
|
|
15
|
-
<FilePreviewError className="flex items-center justify-center">
|
|
16
|
-
{({ error }) => error.message}
|
|
17
|
-
</FilePreviewError>
|
|
18
49
|
<FilePreviewLoader />
|
|
50
|
+
<FilePreviewError className="flex flex-col items-center justify-center gap-8 p-24 text-center">
|
|
51
|
+
{() =>
|
|
52
|
+
isSupported ? (
|
|
53
|
+
<>
|
|
54
|
+
<Typography variant="body-small-high">This file could not be opened.</Typography>
|
|
55
|
+
<Button variant="secondary" onClick={() => setAttempt((count) => count + 1)}>
|
|
56
|
+
Try again
|
|
57
|
+
</Button>
|
|
58
|
+
</>
|
|
59
|
+
) : (
|
|
60
|
+
<>
|
|
61
|
+
<Typography variant="body-small-high">
|
|
62
|
+
This file type cannot be previewed. Download it instead.
|
|
63
|
+
</Typography>
|
|
64
|
+
<Button variant="secondary" onClick={onDownload}>
|
|
65
|
+
Download
|
|
66
|
+
</Button>
|
|
67
|
+
</>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
</FilePreviewError>
|
|
19
71
|
</FilePreview>
|
|
20
72
|
);
|
|
21
73
|
}
|
|
@@ -24,13 +76,6 @@ const meta: Meta<typeof FilePreview> = {
|
|
|
24
76
|
title: 'Files/FilePreview',
|
|
25
77
|
component: FilePreview,
|
|
26
78
|
tags: ['autodocs'],
|
|
27
|
-
decorators: [
|
|
28
|
-
(Story) => (
|
|
29
|
-
<div className="h-[600px] w-[800px]">
|
|
30
|
-
<Story />
|
|
31
|
-
</div>
|
|
32
|
-
),
|
|
33
|
-
],
|
|
34
79
|
argTypes: {
|
|
35
80
|
src: {
|
|
36
81
|
control: 'text',
|
|
@@ -54,37 +99,39 @@ const meta: Meta<typeof FilePreview> = {
|
|
|
54
99
|
},
|
|
55
100
|
className: {
|
|
56
101
|
control: 'text',
|
|
57
|
-
description:
|
|
102
|
+
description:
|
|
103
|
+
'Extra classes merged onto the root. The preview fills its container, so give it or its parent a height.',
|
|
58
104
|
},
|
|
59
105
|
onLoad: {
|
|
60
|
-
action: 'loaded',
|
|
61
106
|
description: 'Called when the file has loaded and rendered.',
|
|
62
107
|
},
|
|
63
108
|
onError: {
|
|
64
|
-
action: 'errored',
|
|
65
109
|
description:
|
|
66
110
|
'Called with the `Error` when the file fails to load, or once when `fileName` is not a supported type.',
|
|
67
111
|
},
|
|
68
112
|
},
|
|
69
113
|
// `src`/`fileBlob` stay out of meta args: each story passes exactly one, and both together throw.
|
|
70
|
-
// Callbacks are logged through their `action` argTypes (`storybook/test` is not a dependency here).
|
|
71
114
|
args: {
|
|
72
115
|
fileName: 'quarterly-report.pdf',
|
|
116
|
+
className: 'h-150 max-w-200',
|
|
117
|
+
onLoad: fn(),
|
|
118
|
+
onError: fn(),
|
|
73
119
|
},
|
|
74
|
-
render:
|
|
120
|
+
render: (args) => <DocumentPreview {...args} />,
|
|
75
121
|
};
|
|
76
122
|
|
|
77
123
|
export default meta;
|
|
78
124
|
type Story = StoryObj<typeof FilePreview>;
|
|
79
125
|
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
126
|
+
/** A PDF, the most common file: the format is picked from `fileName`. */
|
|
127
|
+
export const Default: Story = {
|
|
128
|
+
args: {
|
|
129
|
+
src: PDF_URL,
|
|
130
|
+
fileName: 'quarterly-report.pdf',
|
|
131
|
+
},
|
|
132
|
+
};
|
|
87
133
|
|
|
134
|
+
/** A PDF in the shared viewer: page navigation, zoom, rotate and a thumbnail sidebar. */
|
|
88
135
|
export const Pdf: Story = {
|
|
89
136
|
name: 'PDF',
|
|
90
137
|
args: {
|
|
@@ -93,6 +140,7 @@ export const Pdf: Story = {
|
|
|
93
140
|
},
|
|
94
141
|
};
|
|
95
142
|
|
|
143
|
+
/** A PowerPoint deck as continuous slides, with slide navigation, zoom and thumbnails. */
|
|
96
144
|
export const Presentation: Story = {
|
|
97
145
|
name: 'PowerPoint',
|
|
98
146
|
args: {
|
|
@@ -101,22 +149,16 @@ export const Presentation: Story = {
|
|
|
101
149
|
},
|
|
102
150
|
};
|
|
103
151
|
|
|
152
|
+
/** A CSV export the user picked from disk, passed as `fileBlob`: 500 rows in a virtualised table. */
|
|
104
153
|
export const Csv: Story = {
|
|
105
154
|
name: 'CSV',
|
|
106
155
|
args: {
|
|
107
|
-
fileBlob:
|
|
108
|
-
|
|
109
|
-
'Region,Q1,Q2,Owner\n' +
|
|
110
|
-
'North,120,135,"Smith, J."\n' +
|
|
111
|
-
'South,98,110,Lee\n' +
|
|
112
|
-
Array.from({ length: 500 }, (_, i) => `Row ${i + 1},${i * 2},${i * 3},Auto`).join('\n'),
|
|
113
|
-
],
|
|
114
|
-
{ type: 'text/csv' },
|
|
115
|
-
),
|
|
116
|
-
fileName: 'regions.csv',
|
|
156
|
+
fileBlob: INVOICES_CSV,
|
|
157
|
+
fileName: 'invoices-2026.csv',
|
|
117
158
|
},
|
|
118
159
|
};
|
|
119
160
|
|
|
161
|
+
/** An Excel workbook, read-only, with formulas, charts and sheet tabs. */
|
|
120
162
|
export const Spreadsheet: Story = {
|
|
121
163
|
args: {
|
|
122
164
|
src: SPREADSHEET_URL,
|
|
@@ -124,6 +166,7 @@ export const Spreadsheet: Story = {
|
|
|
124
166
|
},
|
|
125
167
|
};
|
|
126
168
|
|
|
169
|
+
/** A Word document laid out as pages and reflowed to the container width. */
|
|
127
170
|
export const Word: Story = {
|
|
128
171
|
args: {
|
|
129
172
|
src: WORD_URL,
|
|
@@ -131,6 +174,7 @@ export const Word: Story = {
|
|
|
131
174
|
},
|
|
132
175
|
};
|
|
133
176
|
|
|
177
|
+
/** A Markdown file, sanitised and rendered with GitHub-flavoured tables and lists. */
|
|
134
178
|
export const Markdown: Story = {
|
|
135
179
|
args: {
|
|
136
180
|
src: MARKDOWN_URL,
|
|
@@ -138,6 +182,7 @@ export const Markdown: Story = {
|
|
|
138
182
|
},
|
|
139
183
|
};
|
|
140
184
|
|
|
185
|
+
/** An HTML file in a `sandbox=""` frame: shown as authored, with script switched off. */
|
|
141
186
|
export const Html: Story = {
|
|
142
187
|
name: 'HTML',
|
|
143
188
|
args: {
|
|
@@ -156,19 +201,314 @@ export const Loading: Story = {
|
|
|
156
201
|
};
|
|
157
202
|
|
|
158
203
|
// Not named `Error`, which would shadow the global in this module.
|
|
204
|
+
/** A file its renderer cannot open: the error slot shows a neutral line and a retry. */
|
|
159
205
|
export const LoadError: Story = {
|
|
160
206
|
name: 'Error',
|
|
161
207
|
args: {
|
|
162
|
-
|
|
163
|
-
fileBlob: new Blob(['this is not a pdf'], { type: 'application/pdf' }),
|
|
208
|
+
fileBlob: BROKEN_PDF,
|
|
164
209
|
fileName: 'broken.pdf',
|
|
165
210
|
},
|
|
166
211
|
};
|
|
167
212
|
|
|
168
|
-
|
|
213
|
+
type DocumentStory = StoryObj<typeof DocumentPreview>;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* A legacy `.doc`, rejected by its extension before any request is made. A host checks
|
|
217
|
+
* `isFilePreviewSupported(fileName)` first and offers the download without rendering a preview.
|
|
218
|
+
*/
|
|
219
|
+
export const UnsupportedFile: DocumentStory = {
|
|
220
|
+
args: {
|
|
221
|
+
src: 'https://example.com/files/quarterly-report.doc',
|
|
222
|
+
fileName: 'quarterly-report.doc',
|
|
223
|
+
onDownload: fn(),
|
|
224
|
+
},
|
|
225
|
+
render: (args) => <DocumentPreview {...args} />,
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
export const LoadsFromSrc: Story = {
|
|
229
|
+
tags: ['e2e', '!autodocs'],
|
|
230
|
+
args: {
|
|
231
|
+
src: IMAGE_URL,
|
|
232
|
+
fileName: 'quarterly-report-page-1.png',
|
|
233
|
+
},
|
|
234
|
+
play: async ({ args, canvas, step }) => {
|
|
235
|
+
const image = await canvas.findByRole('img', { name: 'quarterly-report-page-1.png' });
|
|
236
|
+
|
|
237
|
+
await step('Shows the file once its renderer reports it loaded', async () => {
|
|
238
|
+
await waitFor(() => expect(args.onLoad).toHaveBeenCalledTimes(1));
|
|
239
|
+
await expect(image).toBeVisible();
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
await step('Leaves no loader or error behind', async () => {
|
|
243
|
+
await expect(canvas.queryByRole('status')).not.toBeInTheDocument();
|
|
244
|
+
await expect(canvas.queryByRole('alert')).not.toBeInTheDocument();
|
|
245
|
+
await expect(args.onError).not.toHaveBeenCalled();
|
|
246
|
+
});
|
|
247
|
+
},
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
export const RetryAfterLoadError: Story = {
|
|
251
|
+
tags: ['e2e', '!autodocs'],
|
|
252
|
+
args: {
|
|
253
|
+
fileBlob: BROKEN_IMAGE,
|
|
254
|
+
fileName: 'team-offsite.png',
|
|
255
|
+
},
|
|
256
|
+
play: async ({ args, canvas, userEvent, step }) => {
|
|
257
|
+
await step('Shows a neutral line, and hands the developer message to onError', async () => {
|
|
258
|
+
const alert = await canvas.findByRole('alert');
|
|
259
|
+
await expect(alert).toHaveTextContent('This file could not be opened.');
|
|
260
|
+
await expect(alert).not.toHaveTextContent('[web-file-preview]');
|
|
261
|
+
await expect(canvas.queryByRole('status')).not.toBeInTheDocument();
|
|
262
|
+
await expect(args.onError).toHaveBeenCalledTimes(1);
|
|
263
|
+
await expect(args.onError).toHaveBeenCalledWith(
|
|
264
|
+
expect.objectContaining({ message: expect.stringContaining('team-offsite.png') }),
|
|
265
|
+
);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
await step('Loads the file again on Try again', async () => {
|
|
269
|
+
await userEvent.click(canvas.getByRole('button', { name: 'Try again' }));
|
|
270
|
+
await waitFor(() => expect(args.onError).toHaveBeenCalledTimes(2));
|
|
271
|
+
await expect(await canvas.findByRole('alert')).toBeVisible();
|
|
272
|
+
});
|
|
273
|
+
},
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
export const UnsupportedFileOffersDownload: DocumentStory = {
|
|
277
|
+
tags: ['e2e', '!autodocs'],
|
|
169
278
|
args: {
|
|
170
|
-
// Legacy .doc is rejected by extension before any request is made.
|
|
171
279
|
src: 'https://example.com/files/quarterly-report.doc',
|
|
172
280
|
fileName: 'quarterly-report.doc',
|
|
281
|
+
onDownload: fn(),
|
|
282
|
+
},
|
|
283
|
+
render: (args) => <DocumentPreview {...args} />,
|
|
284
|
+
play: async ({ args, canvas, userEvent, step }) => {
|
|
285
|
+
await step('Shows the error on the first paint, with no loader and no retry', async () => {
|
|
286
|
+
await expect(canvas.getByRole('alert')).toHaveTextContent(
|
|
287
|
+
'This file type cannot be previewed.',
|
|
288
|
+
);
|
|
289
|
+
await expect(canvas.queryByRole('status')).not.toBeInTheDocument();
|
|
290
|
+
await expect(canvas.queryByRole('button', { name: 'Try again' })).not.toBeInTheDocument();
|
|
291
|
+
await waitFor(() => expect(args.onError).toHaveBeenCalledTimes(1));
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
await step('Offers the download instead', async () => {
|
|
295
|
+
await userEvent.click(canvas.getByRole('button', { name: 'Download' }));
|
|
296
|
+
await expect(args.onDownload).toHaveBeenCalledTimes(1);
|
|
297
|
+
});
|
|
298
|
+
},
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
/** The preview in each place it is allowed: a dialog body, the chat side panel, a drawer and a document page. */
|
|
302
|
+
export const Examples: Story = {
|
|
303
|
+
parameters: { controls: { disable: true } },
|
|
304
|
+
render: () => (
|
|
305
|
+
<div className="flex max-w-200 flex-col gap-24">
|
|
306
|
+
<ParentBlock parent="ui/DialogBody">
|
|
307
|
+
<section
|
|
308
|
+
aria-label="Attach file"
|
|
309
|
+
className="flex flex-col overflow-hidden rounded-15 border border-card-border-base bg-paper-elevation-2"
|
|
310
|
+
>
|
|
311
|
+
<Typography variant="title-x-small" className="p-16">
|
|
312
|
+
Attach invoices-2026.csv?
|
|
313
|
+
</Typography>
|
|
314
|
+
<div className="h-80 border-y border-component-divider">
|
|
315
|
+
<DocumentPreview fileBlob={INVOICES_CSV} fileName="invoices-2026.csv" />
|
|
316
|
+
</div>
|
|
317
|
+
<div className="flex justify-end gap-8 p-16">
|
|
318
|
+
<Button variant="tertiary">Cancel</Button>
|
|
319
|
+
<Button variant="primary">Attach file</Button>
|
|
320
|
+
</div>
|
|
321
|
+
</section>
|
|
322
|
+
</ParentBlock>
|
|
323
|
+
|
|
324
|
+
<ParentBlock parent="chat side panel">
|
|
325
|
+
<aside
|
|
326
|
+
aria-label="Document preview"
|
|
327
|
+
className="flex flex-col overflow-hidden rounded-15 border border-card-border-base bg-paper-elevation-1"
|
|
328
|
+
>
|
|
329
|
+
<Typography
|
|
330
|
+
variant="body-small-high"
|
|
331
|
+
className="border-b border-component-divider px-16 py-12"
|
|
332
|
+
>
|
|
333
|
+
quarterly-report.pdf
|
|
334
|
+
</Typography>
|
|
335
|
+
<div className="h-120">
|
|
336
|
+
<DocumentPreview src={PDF_URL} fileName="quarterly-report.pdf" />
|
|
337
|
+
</div>
|
|
338
|
+
</aside>
|
|
339
|
+
</ParentBlock>
|
|
340
|
+
|
|
341
|
+
<ParentBlock parent="drawer">
|
|
342
|
+
<aside
|
|
343
|
+
aria-label="Meeting notes"
|
|
344
|
+
className="ml-auto flex w-full max-w-120 flex-col overflow-hidden border-l border-component-divider bg-paper-elevation-2"
|
|
345
|
+
>
|
|
346
|
+
<Typography variant="title-x-small" className="p-16">
|
|
347
|
+
quarterly-report.md
|
|
348
|
+
</Typography>
|
|
349
|
+
<div className="h-100">
|
|
350
|
+
<DocumentPreview src={MARKDOWN_URL} fileName="quarterly-report.md" />
|
|
351
|
+
</div>
|
|
352
|
+
</aside>
|
|
353
|
+
</ParentBlock>
|
|
354
|
+
|
|
355
|
+
<ParentBlock parent="dialog body">
|
|
356
|
+
<div
|
|
357
|
+
role="group"
|
|
358
|
+
aria-label="Image preview"
|
|
359
|
+
className="flex flex-col overflow-hidden rounded-15 border border-card-border-base bg-paper-elevation-2"
|
|
360
|
+
>
|
|
361
|
+
<Typography variant="title-x-small" className="p-16">
|
|
362
|
+
quarterly-report-page-1.png
|
|
363
|
+
</Typography>
|
|
364
|
+
<div className="h-100">
|
|
365
|
+
<DocumentPreview src={IMAGE_URL} fileName="quarterly-report-page-1.png" />
|
|
366
|
+
</div>
|
|
367
|
+
</div>
|
|
368
|
+
</ParentBlock>
|
|
369
|
+
|
|
370
|
+
<ParentBlock parent="document detail page">
|
|
371
|
+
<section aria-label="Quarterly report" className="flex flex-col gap-12">
|
|
372
|
+
<div className="flex flex-col gap-4">
|
|
373
|
+
<Typography variant="title-small">Quarterly report</Typography>
|
|
374
|
+
<Typography variant="caption-md" className="text-text-secondary">
|
|
375
|
+
quarterly-report.docx · Word document · Uploaded 14 Sep 2026
|
|
376
|
+
</Typography>
|
|
377
|
+
</div>
|
|
378
|
+
<div className="h-120 overflow-hidden rounded-10 border border-card-border-base">
|
|
379
|
+
<DocumentPreview src={WORD_URL} fileName="quarterly-report.docx" />
|
|
380
|
+
</div>
|
|
381
|
+
</section>
|
|
382
|
+
</ParentBlock>
|
|
383
|
+
</div>
|
|
384
|
+
),
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
interface FilePreviewPlaygroundProps {
|
|
388
|
+
src: string;
|
|
389
|
+
fileName: string;
|
|
390
|
+
className: string;
|
|
391
|
+
theme: FilePreviewTheme;
|
|
392
|
+
hasLoader: boolean;
|
|
393
|
+
loaderLabel: string;
|
|
394
|
+
hasError: boolean;
|
|
395
|
+
errorMessage: string;
|
|
396
|
+
asChild: boolean;
|
|
397
|
+
onLoad: () => void;
|
|
398
|
+
onError: (error: Error) => void;
|
|
399
|
+
onRetry: () => void;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function FilePreviewPlayground({
|
|
403
|
+
src,
|
|
404
|
+
fileName,
|
|
405
|
+
className,
|
|
406
|
+
theme,
|
|
407
|
+
hasLoader,
|
|
408
|
+
loaderLabel,
|
|
409
|
+
hasError,
|
|
410
|
+
errorMessage,
|
|
411
|
+
asChild,
|
|
412
|
+
onLoad,
|
|
413
|
+
onError,
|
|
414
|
+
onRetry,
|
|
415
|
+
}: FilePreviewPlaygroundProps) {
|
|
416
|
+
const [attempt, setAttempt] = useState(0);
|
|
417
|
+
|
|
418
|
+
const handleRetry = () => {
|
|
419
|
+
setAttempt((count) => count + 1);
|
|
420
|
+
onRetry();
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
return (
|
|
424
|
+
<FilePreviewThemeProvider theme={theme}>
|
|
425
|
+
<FilePreview
|
|
426
|
+
key={attempt}
|
|
427
|
+
src={src}
|
|
428
|
+
fileName={fileName}
|
|
429
|
+
className={className}
|
|
430
|
+
onLoad={onLoad}
|
|
431
|
+
onError={onError}
|
|
432
|
+
>
|
|
433
|
+
<FilePreviewFile />
|
|
434
|
+
{hasLoader && (
|
|
435
|
+
<FilePreviewLoader
|
|
436
|
+
className={loaderLabel ? 'flex items-center justify-center' : undefined}
|
|
437
|
+
>
|
|
438
|
+
{loaderLabel ? (
|
|
439
|
+
<Typography variant="body-small-high" className="text-text-secondary">
|
|
440
|
+
{loaderLabel}
|
|
441
|
+
</Typography>
|
|
442
|
+
) : undefined}
|
|
443
|
+
</FilePreviewLoader>
|
|
444
|
+
)}
|
|
445
|
+
{hasError && (
|
|
446
|
+
<FilePreviewError
|
|
447
|
+
asChild={asChild}
|
|
448
|
+
className="flex flex-col items-center justify-center gap-8 p-24 text-center"
|
|
449
|
+
>
|
|
450
|
+
{() => (
|
|
451
|
+
<section>
|
|
452
|
+
<Typography variant="body-small-high">{errorMessage}</Typography>
|
|
453
|
+
<Button variant="secondary" className="mt-8" onClick={handleRetry}>
|
|
454
|
+
Try again
|
|
455
|
+
</Button>
|
|
456
|
+
</section>
|
|
457
|
+
)}
|
|
458
|
+
</FilePreviewError>
|
|
459
|
+
)}
|
|
460
|
+
</FilePreview>
|
|
461
|
+
</FilePreviewThemeProvider>
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** Every part of the preview at once, with each part and prop on the Controls panel. */
|
|
466
|
+
export const Playground: StoryObj<typeof FilePreviewPlayground> = {
|
|
467
|
+
tags: ['!autodocs'],
|
|
468
|
+
args: {
|
|
469
|
+
src: 'PDF',
|
|
470
|
+
fileName: 'quarterly-report.pdf',
|
|
471
|
+
className: 'h-150 max-w-200',
|
|
472
|
+
theme: 'dark',
|
|
473
|
+
hasLoader: true,
|
|
474
|
+
loaderLabel: '',
|
|
475
|
+
hasError: true,
|
|
476
|
+
errorMessage: 'This file could not be opened.',
|
|
477
|
+
asChild: false,
|
|
478
|
+
onLoad: fn(),
|
|
479
|
+
onError: fn(),
|
|
480
|
+
onRetry: fn(),
|
|
481
|
+
},
|
|
482
|
+
argTypes: {
|
|
483
|
+
src: {
|
|
484
|
+
control: 'select',
|
|
485
|
+
options: Object.keys(PLAYGROUND_SOURCES),
|
|
486
|
+
mapping: PLAYGROUND_SOURCES,
|
|
487
|
+
description:
|
|
488
|
+
'The file to fetch. Change `fileName` with it: the extension picks the renderer, not the URL.',
|
|
489
|
+
},
|
|
490
|
+
theme: {
|
|
491
|
+
control: 'inline-radio',
|
|
492
|
+
options: ['dark', 'light'],
|
|
493
|
+
description: '`theme` on a `FilePreviewThemeProvider` above the preview.',
|
|
494
|
+
},
|
|
495
|
+
hasLoader: { control: 'boolean', description: 'Renders `FilePreviewLoader`.' },
|
|
496
|
+
loaderLabel: {
|
|
497
|
+
control: 'text',
|
|
498
|
+
description:
|
|
499
|
+
'Own content for `FilePreviewLoader`, e.g. "Loading the quarterly report". Empty shows the default skeleton.',
|
|
500
|
+
},
|
|
501
|
+
hasError: { control: 'boolean', description: 'Renders `FilePreviewError`.' },
|
|
502
|
+
errorMessage: {
|
|
503
|
+
control: 'text',
|
|
504
|
+
description: 'The line `FilePreviewError` shows; pick "Missing file" as `src` to see it.',
|
|
505
|
+
},
|
|
506
|
+
asChild: {
|
|
507
|
+
control: 'boolean',
|
|
508
|
+
description:
|
|
509
|
+
'`asChild` on `FilePreviewError`: the alert role and classes land on the story’s own `section`.',
|
|
510
|
+
},
|
|
511
|
+
onRetry: { description: 'Called when "Try again" remounts the preview.' },
|
|
173
512
|
},
|
|
513
|
+
render: (args) => <FilePreviewPlayground {...args} />,
|
|
174
514
|
};
|
|
@@ -312,11 +312,12 @@ export function FilePreview({
|
|
|
312
312
|
return (
|
|
313
313
|
<FilePreviewContext.Provider value={contextValue}>
|
|
314
314
|
<div
|
|
315
|
+
data-slot="file-preview"
|
|
315
316
|
data-testid="file-preview"
|
|
316
317
|
data-status={status}
|
|
317
318
|
// Scopes the design system's `theme-light:` variant to this preview.
|
|
318
319
|
data-theme={theme}
|
|
319
|
-
className={cn('relative size-full', className)}
|
|
320
|
+
className={cn('group/file-preview relative size-full', className)}
|
|
320
321
|
>
|
|
321
322
|
{children}
|
|
322
323
|
</div>
|
|
@@ -353,7 +354,9 @@ export const FilePreviewFile = forwardRef<HTMLElement, FilePreviewFileProps>(
|
|
|
353
354
|
objectUrl,
|
|
354
355
|
fileName,
|
|
355
356
|
theme,
|
|
356
|
-
|
|
357
|
+
// Hidden from the root's `data-status`, so a half-rendered file never shows under the
|
|
358
|
+
// loader. Only `loading` needs it: nothing renders on `error`.
|
|
359
|
+
className: cn('size-full group-data-[status=loading]/file-preview:invisible', className),
|
|
357
360
|
onLoad: handleLoad,
|
|
358
361
|
onError: handleError,
|
|
359
362
|
rootRef: ref,
|
|
@@ -402,6 +405,7 @@ export const FilePreviewError = forwardRef<HTMLDivElement, FilePreviewErrorProps
|
|
|
402
405
|
<Component
|
|
403
406
|
ref={ref}
|
|
404
407
|
role="alert"
|
|
408
|
+
data-slot="file-preview-error"
|
|
405
409
|
data-testid="file-preview-error"
|
|
406
410
|
className={cn('absolute inset-0', className)}
|
|
407
411
|
>
|
|
@@ -489,6 +493,7 @@ export const FilePreviewLoader = forwardRef<HTMLDivElement, FilePreviewLoaderPro
|
|
|
489
493
|
role="status"
|
|
490
494
|
aria-live="polite"
|
|
491
495
|
aria-busy="true"
|
|
496
|
+
data-slot="file-preview-loader"
|
|
492
497
|
data-testid="file-preview-loader"
|
|
493
498
|
className={cn('absolute inset-0', className)}
|
|
494
499
|
>
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { GalleryDocument, LibraryDocument, QueuedUpload, ThumbnailFile } from './types';
|
|
2
|
+
|
|
3
|
+
// Vite serves these same-origin, so the kinds that are fetched need no CORS.
|
|
4
|
+
const fixture = (name: string) => new URL(`../../../__fixtures__/${name}`, import.meta.url).href;
|
|
5
|
+
|
|
6
|
+
export const PAGE_IMAGE_URL = fixture('quarterly-report-page-1.png');
|
|
7
|
+
export const PDF_URL = fixture('quarterly-report.pdf');
|
|
8
|
+
export const WORD_URL = fixture('quarterly-report.docx');
|
|
9
|
+
export const SPREADSHEET_URL = fixture('quarterly-model.xlsx');
|
|
10
|
+
export const PRESENTATION_URL = fixture('quarterly-deck.pptx');
|
|
11
|
+
export const MARKDOWN_URL = fixture('quarterly-report.md');
|
|
12
|
+
export const HTML_URL = fixture('quarterly-report.html');
|
|
13
|
+
export const CSV_URL = fixture('quarterly-regions.csv');
|
|
14
|
+
|
|
15
|
+
/** One file of every kind the tile draws. */
|
|
16
|
+
export const DRAWN_FILES: ThumbnailFile[] = [
|
|
17
|
+
{ src: PDF_URL, fileName: 'quarterly-report.pdf' },
|
|
18
|
+
{ src: WORD_URL, fileName: 'quarterly-report.docx' },
|
|
19
|
+
{ src: SPREADSHEET_URL, fileName: 'quarterly-model.xlsx' },
|
|
20
|
+
{ src: PRESENTATION_URL, fileName: 'quarterly-deck.pptx' },
|
|
21
|
+
{ src: MARKDOWN_URL, fileName: 'quarterly-report.md' },
|
|
22
|
+
{ src: HTML_URL, fileName: 'quarterly-report.html' },
|
|
23
|
+
{ src: CSV_URL, fileName: 'quarterly-regions.csv' },
|
|
24
|
+
{ src: PAGE_IMAGE_URL, fileName: 'quarterly-report-page-1.png' },
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A gallery long enough to scroll. Each copy has its own `#n` fragment, so it is a different
|
|
29
|
+
* file to the session cache but the same request.
|
|
30
|
+
*/
|
|
31
|
+
export const SCROLLING_FILES: ThumbnailFile[] = Array.from({ length: 48 }, (_, index) => {
|
|
32
|
+
const file = DRAWN_FILES[index % DRAWN_FILES.length];
|
|
33
|
+
|
|
34
|
+
return { src: `${file.src}#${index}`, fileName: file.fileName };
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
/** The same gallery of images only, which draw without an engine. */
|
|
38
|
+
export const SCROLLING_IMAGES: ThumbnailFile[] = Array.from({ length: 24 }, (_, index) => ({
|
|
39
|
+
src: `${PAGE_IMAGE_URL}#page-${index + 1}`,
|
|
40
|
+
fileName: `site-inspection-page-${index + 1}.png`,
|
|
41
|
+
}));
|
|
42
|
+
|
|
43
|
+
// Mirrors the documents shared in a chat: mixed kinds, one name far longer than its row.
|
|
44
|
+
export const LIBRARY_DOCUMENTS: LibraryDocument[] = [
|
|
45
|
+
{
|
|
46
|
+
documentId: 'doc_01',
|
|
47
|
+
src: PDF_URL,
|
|
48
|
+
fileName: 'quarterly-report.pdf',
|
|
49
|
+
sharedAt: '2 hours ago',
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
documentId: 'doc_02',
|
|
53
|
+
src: SPREADSHEET_URL,
|
|
54
|
+
fileName: 'quarterly-model.xlsx',
|
|
55
|
+
sharedAt: 'Yesterday',
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
documentId: 'doc_03',
|
|
59
|
+
src: WORD_URL,
|
|
60
|
+
fileName: 'supplier-framework-agreement-countersigned-2026-final.docx',
|
|
61
|
+
sharedAt: '3 weeks ago',
|
|
62
|
+
},
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
// One upload done, one on its way, one waiting its turn.
|
|
66
|
+
export const QUEUED_UPLOADS: QueuedUpload[] = [
|
|
67
|
+
{
|
|
68
|
+
uploadId: 'upl_01',
|
|
69
|
+
fileName: 'quarterly-report-page-1.png',
|
|
70
|
+
src: PAGE_IMAGE_URL,
|
|
71
|
+
status: 'Uploaded',
|
|
72
|
+
},
|
|
73
|
+
{ uploadId: 'upl_02', fileName: 'quarterly-regions.csv', src: null, status: 'Uploading, 64%' },
|
|
74
|
+
{
|
|
75
|
+
uploadId: 'upl_03',
|
|
76
|
+
fileName: 'supplier-framework-agreement.docx',
|
|
77
|
+
src: null,
|
|
78
|
+
status: 'Waiting',
|
|
79
|
+
},
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
/** Files attached to a prompt that has not been sent yet; the archive has no picture. */
|
|
83
|
+
export const ATTACHED_FILES: ThumbnailFile[] = [
|
|
84
|
+
{ src: PAGE_IMAGE_URL, fileName: 'quarterly-report-page-1.png' },
|
|
85
|
+
{ src: MARKDOWN_URL, fileName: 'quarterly-report.md' },
|
|
86
|
+
{ src: '/files/site-photos.zip', fileName: 'site-photos.zip' },
|
|
87
|
+
];
|
|
88
|
+
|
|
89
|
+
/** A long gallery whose pictures the server already renders, so no engine loads. */
|
|
90
|
+
export const GALLERY_DOCUMENTS: GalleryDocument[] = Array.from({ length: 12 }, (_, index) => {
|
|
91
|
+
const fileName = `invoice-2026-${String(index + 1).padStart(2, '0')}.pdf`;
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
documentId: `doc_${index + 11}`,
|
|
95
|
+
fileName,
|
|
96
|
+
src: `/files/${fileName}`,
|
|
97
|
+
previewImageUrl: `${PAGE_IMAGE_URL}#invoice-${index + 1}`,
|
|
98
|
+
};
|
|
99
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** A file a tile can draw from a URL. */
|
|
2
|
+
export interface ThumbnailFile {
|
|
3
|
+
src: string;
|
|
4
|
+
fileName: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** A row of the documents library: the file, and when it was shared. */
|
|
8
|
+
export interface LibraryDocument extends ThumbnailFile {
|
|
9
|
+
documentId: string;
|
|
10
|
+
/** Pre-formatted, the way the row shows it. */
|
|
11
|
+
sharedAt: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** A file in an upload queue. `src` is `null` until the upload has finished. */
|
|
15
|
+
export interface QueuedUpload {
|
|
16
|
+
uploadId: string;
|
|
17
|
+
fileName: string;
|
|
18
|
+
src: string | null;
|
|
19
|
+
status: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** A tile of a gallery whose pictures the server renders. */
|
|
23
|
+
export interface GalleryDocument extends ThumbnailFile {
|
|
24
|
+
documentId: string;
|
|
25
|
+
previewImageUrl: string;
|
|
26
|
+
}
|