diff --git a/src/components/DocumentView/File.tsx b/src/components/DocumentView/File.tsx index e4b99ed69..b3ebd5c1e 100644 --- a/src/components/DocumentView/File.tsx +++ b/src/components/DocumentView/File.tsx @@ -1,12 +1,10 @@ -import IconDownload from '@geist-ui/icons/download'; -import IconFileText from '@geist-ui/icons/fileText'; -import IconImage from '@geist-ui/icons/image'; -import IconPaperClip from '@geist-ui/icons/paperclip'; import { DocumentBlockFile } from '@gitbook/api'; +import { getSimplifiedContentType } from '@/lib/files'; import { tcls } from '@/lib/tailwind'; import { BlockProps } from './Block'; +import { FileIcon } from './FileIcon'; export async function File(props: BlockProps) { const { block, context, style } = props; @@ -18,42 +16,7 @@ export async function File(props: BlockProps) { return null; } - const contentType = (() => { - switch (file.contentType) { - case 'application/pdf': - case 'application/x-pdf': - return 'pdf'; - case 'image/png': - case 'image/jpeg': - case 'image/gif': - case 'image/webp': - case 'image/tiff': - case 'image/svg+xml': - return 'image'; - case 'application/zip': - case 'application/x-7z-compressed': - case 'application/x-zip-compressed': - case 'application/x-tar': - case 'application/x-rar-compressed': - case 'application/vnd.rar': - return 'archive'; - default: - return null; - } - })(); - - const icon = (() => { - switch (contentType) { - case 'pdf': - return ; - case 'image': - return ; - case 'archive': - return ; - default: - return ; - } - })(); + const contentType = getSimplifiedContentType(file.contentType); return ( ) { 'dark:border-light/2', )} > -
{icon}
+
+ +
; + case 'image': + return ; + case 'archive': + return ; + default: + return ; + } +} diff --git a/src/components/DocumentView/Table/RecordColumnValue.tsx b/src/components/DocumentView/Table/RecordColumnValue.tsx index 3b833e29c..d10071b6a 100644 --- a/src/components/DocumentView/Table/RecordColumnValue.tsx +++ b/src/components/DocumentView/Table/RecordColumnValue.tsx @@ -4,7 +4,9 @@ import assertNever from 'assert-never'; import { Checkbox, Emoji } from '@/components/primitives'; import { StyledLink } from '@/components/primitives'; +import { Image } from '@/components/utils'; import { getNodeFragmentByName } from '@/lib/document'; +import { getSimplifiedContentType } from '@/lib/files'; import { tcls } from '@/lib/tailwind'; import { filterOutNullable } from '@/lib/typescript'; @@ -12,6 +14,7 @@ import { TableRecordKV } from './Table'; import { getColumnAlignment } from './utils'; import { BlockProps } from '../Block'; import { Blocks } from '../Blocks'; +import { FileIcon } from '../FileIcon'; /** * Render the value for a column in a record. @@ -125,11 +128,41 @@ export async function RecordColumnValue( return ( - {files.filter(filterOutNullable).map((file, index) => ( - - {file.text} - - ))} + {files.filter(filterOutNullable).map((ref, index) => { + const contentType = ref.file + ? getSimplifiedContentType(ref.file.contentType) + : null; + + return ( + + {contentType === 'image' ? ( + {ref.text} + ) : ( + + )} + {ref.text} + + ); + })} ); case 'content-ref': { @@ -208,4 +241,4 @@ export async function RecordColumnValue( default: assertNever(definition); } -} +} \ No newline at end of file diff --git a/src/components/primitives/StyledLink.tsx b/src/components/primitives/StyledLink.tsx index 7d5d1fed8..acbb78ea8 100644 --- a/src/components/primitives/StyledLink.tsx +++ b/src/components/primitives/StyledLink.tsx @@ -1,14 +1,16 @@ -import { tcls } from '@/lib/tailwind'; +import { ClassValue, tcls } from '@/lib/tailwind'; import { Link, LinkProps } from '../primitives/Link'; /** * Styled version of Link component. */ -export function StyledLink(props: LinkProps) { +export function StyledLink(props: Omit & { style?: ClassValue }) { + const { style, ...rest } = props; + return ( {props.children} diff --git a/src/lib/files.ts b/src/lib/files.ts new file mode 100644 index 000000000..5c9ea9d69 --- /dev/null +++ b/src/lib/files.ts @@ -0,0 +1,25 @@ +export type SimplifiedFileType = 'image' | 'pdf' | 'archive'; + +/** + * Get a simplified content type for the given mime type. + */ +export function getSimplifiedContentType(mimeType: string): SimplifiedFileType | null { + if (mimeType.startsWith('image')) { + return 'image'; + } + + switch (mimeType) { + case 'application/pdf': + case 'application/x-pdf': + return 'pdf'; + case 'application/zip': + case 'application/x-7z-compressed': + case 'application/x-zip-compressed': + case 'application/x-tar': + case 'application/x-rar-compressed': + case 'application/vnd.rar': + return 'archive'; + default: + return null; + } +}