|
1 | 1 | import { type DocumentBlockFile, SiteInsightsLinkPosition } from '@gitbook/api'; |
2 | 2 |
|
| 3 | +import { t } from '@/intl/translate'; |
3 | 4 | import { getSimplifiedContentType } from '@/lib/files'; |
4 | 5 | import { resolveContentRef } from '@/lib/references'; |
5 | | -import { tcls } from '@/lib/tailwind'; |
6 | 6 |
|
7 | | -import { Link } from '../primitives'; |
| 7 | +import { getSpaceLanguage } from '@/intl/server'; |
| 8 | +import { Button, Link } from '../primitives'; |
| 9 | +import { DownloadButton } from '../primitives/DownloadButton'; |
8 | 10 | import type { BlockProps } from './Block'; |
9 | 11 | import { Caption } from './Caption'; |
10 | 12 | import { FileIcon } from './FileIcon'; |
11 | 13 |
|
12 | 14 | export async function File(props: BlockProps<DocumentBlockFile>) { |
13 | 15 | const { block, context } = props; |
14 | 16 |
|
15 | | - const contentRef = context.contentContext |
16 | | - ? await resolveContentRef(block.data.ref, context.contentContext) |
17 | | - : null; |
| 17 | + if (!context.contentContext) { |
| 18 | + return null; |
| 19 | + } |
| 20 | + |
| 21 | + const contentRef = await resolveContentRef(block.data.ref, context.contentContext); |
18 | 22 | const file = contentRef?.file; |
19 | 23 |
|
20 | 24 | if (!file) { |
21 | 25 | return null; |
22 | 26 | } |
23 | 27 |
|
| 28 | + const language = getSpaceLanguage(context.contentContext); |
24 | 29 | const contentType = getSimplifiedContentType(file.contentType); |
| 30 | + const insights = { |
| 31 | + type: 'link_click' as const, |
| 32 | + link: { |
| 33 | + target: block.data.ref, |
| 34 | + position: SiteInsightsLinkPosition.Content, |
| 35 | + }, |
| 36 | + }; |
25 | 37 |
|
26 | 38 | return ( |
27 | 39 | <Caption {...props} withBorder> |
28 | | - <Link |
29 | | - href={file.downloadURL} |
30 | | - download={file.name} |
31 | | - insights={{ |
32 | | - type: 'link_click', |
33 | | - link: { |
34 | | - target: block.data.ref, |
35 | | - position: SiteInsightsLinkPosition.Content, |
36 | | - }, |
37 | | - }} |
38 | | - className={tcls('group/file', 'flex', 'flex-row', 'items-center', 'px-5', 'py-3')} |
39 | | - > |
40 | | - <div |
41 | | - className={tcls( |
42 | | - 'min-w-14', |
43 | | - 'mr-5', |
44 | | - 'pr-5', |
45 | | - 'flex', |
46 | | - 'flex-col', |
47 | | - 'items-center', |
48 | | - 'gap-1', |
49 | | - 'border-r', |
50 | | - 'border-tint-subtle' |
51 | | - )} |
52 | | - > |
| 40 | + <div className="flex items-center px-5 py-3"> |
| 41 | + <div className="mr-5 flex min-w-14 flex-col items-center gap-1 border-tint-subtle border-r pr-5"> |
53 | 42 | <div> |
54 | | - <FileIcon |
55 | | - contentType={contentType} |
56 | | - className={tcls('size-5', 'text-primary')} |
57 | | - /> |
58 | | - </div> |
59 | | - <div |
60 | | - className={tcls( |
61 | | - 'text-xs', |
62 | | - 'text-tint', |
63 | | - 'group-hover/file:text-tint-strong' |
64 | | - )} |
65 | | - > |
66 | | - {getHumanFileSize(file.size)} |
| 43 | + <FileIcon contentType={contentType} className="size-5 text-primary" /> |
67 | 44 | </div> |
| 45 | + <div className="text-hint text-xs">{getHumanFileSize(file.size)}</div> |
68 | 46 | </div> |
69 | | - <div> |
70 | | - <div className={tcls('text-base')}>{file.name}</div> |
71 | | - <div className={tcls('text-sm', 'opacity-9', 'dark:opacity-8')}> |
72 | | - {contentType} |
| 47 | + <div className="flex-1"> |
| 48 | + <div className="text-base"> |
| 49 | + <Link |
| 50 | + href={file.downloadURL} |
| 51 | + target="_blank" |
| 52 | + insights={insights} |
| 53 | + className="hover:underline" |
| 54 | + > |
| 55 | + {file.name} |
| 56 | + </Link> |
73 | 57 | </div> |
| 58 | + <div className="text-sm opacity-9 dark:opacity-8">{contentType}</div> |
| 59 | + </div> |
| 60 | + <div className="flex shrink-0 gap-2"> |
| 61 | + <DownloadButton |
| 62 | + icon="download" |
| 63 | + size="xsmall" |
| 64 | + variant="secondary" |
| 65 | + downloadUrl={file.downloadURL} |
| 66 | + filename={file.name} |
| 67 | + insights={insights} |
| 68 | + > |
| 69 | + {t(language, 'download')} |
| 70 | + </DownloadButton> |
| 71 | + <Button |
| 72 | + icon="arrow-up-right-from-square" |
| 73 | + size="xsmall" |
| 74 | + variant="secondary" |
| 75 | + href={file.downloadURL} |
| 76 | + target="_blank" |
| 77 | + insights={insights} |
| 78 | + > |
| 79 | + {t(language, 'open')} |
| 80 | + </Button> |
74 | 81 | </div> |
75 | | - </Link> |
| 82 | + </div> |
76 | 83 | </Caption> |
77 | 84 | ); |
78 | 85 | } |
|
0 commit comments