Skip to content

Commit

Permalink
Fix: add unsupported icon for resource can't that generate the thumbnail
Browse files Browse the repository at this point in the history
Related Issues:
   - #25
  • Loading branch information
Bill2015 committed Feb 25, 2024
1 parent d854298 commit 10b46bd
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/api/resource/Dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface ResourceFileDto {
path: string,

ext: string,

media_type: string,
}

export interface ResourceResDto {
Expand Down
Binary file added src/assets/icons/not-supported-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 0 additions & 21 deletions src/components/display/ResourceThumbnailDisplayer.tsx

This file was deleted.

3 changes: 1 addition & 2 deletions src/components/display/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
export * from './icons';
export * from './thumbnail';
export * from './ResponsiveImage';
export * from './YoutubeThunbnail';
export * from './EditableText';
export * from './ResourceThumbnailDisplayer';
export * from './DateTimeDisplayer';
export * from './TagTypography';
export * from './SubTitle';
25 changes: 25 additions & 0 deletions src/components/display/thumbnail/LocalFileThumbnailDisplayer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable react/jsx-props-no-spreading */
import { LocalImageThumbnail, LocalUnsupportThumbnail } from './file';
import { LocalThumbnailProps } from './file/types';

const LOCAL_MEDIA_TYPE = new Map<string, typeof LocalImageThumbnail>();

LOCAL_MEDIA_TYPE.set('image/jpeg', LocalImageThumbnail);
LOCAL_MEDIA_TYPE.set('image/png', LocalImageThumbnail);
LOCAL_MEDIA_TYPE.set('image/gif', LocalImageThumbnail);

interface LocalFileThumbnailDisplayerProps extends LocalThumbnailProps {
mediaType: string;
}

export function LocalFileThumbnailDisplayer(props: LocalFileThumbnailDisplayerProps) {
const { filePath, mediaType, ...imgProps } = props;

if (LOCAL_MEDIA_TYPE.has(mediaType) === false) {
return <LocalUnsupportThumbnail mediaType={mediaType} {...imgProps} />;
}

const Thumbnail = LOCAL_MEDIA_TYPE.get(mediaType)!;

return <Thumbnail filePath={filePath} {...imgProps} />;
}
29 changes: 29 additions & 0 deletions src/components/display/thumbnail/ResourceThumbnailDisplayer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable react/jsx-props-no-spreading */
import { ResponsiveImageProps } from '@components/display';

import { YoutubeThumbnail } from './url';
import { LocalFileThumbnailDisplayer } from './LocalFileThumbnailDisplayer';

export interface ResourceThumbnailDisplayerProps extends ResponsiveImageProps {
url?: string | undefined | null;

filePath?: string | undefined | null;

mediaType?: string;
}

export function ResourceThumbnailDisplayer(props: ResourceThumbnailDisplayerProps) {
const { url, filePath, mediaType, ...imgProps } = props;

return (
url
? <YoutubeThumbnail url={url!} {...imgProps} />
: (
<LocalFileThumbnailDisplayer
mediaType={mediaType!}
filePath={filePath!}
{...imgProps}
/>
)
);
}
9 changes: 9 additions & 0 deletions src/components/display/thumbnail/file/LocalImageThumbnail.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable react/jsx-props-no-spreading */
import { convertFileSrc } from '@tauri-apps/api/tauri';
import { ResponsiveImage } from '@components/display';
import { LocalThumbnailProps } from './types';

export function LocalImageThumbnail(props: LocalThumbnailProps) {
const { filePath, ...imgProps } = props;
return <ResponsiveImage src={convertFileSrc(filePath)} {...imgProps} />;
}
13 changes: 13 additions & 0 deletions src/components/display/thumbnail/file/LocalUnsupportThumbnail.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable react/jsx-props-no-spreading */
import { ResponsiveImage, ResponsiveImageProps } from '@components/display/ResponsiveImage';
import UnsupportImage from '@assets/icons/not-supported-icon.png';

export interface LocalUnsupportThumbnailProps extends Omit<ResponsiveImageProps, 'src' | 'ref'> {
mediaType: string;
}

export function LocalUnsupportThumbnail(props: LocalUnsupportThumbnailProps) {
const { mediaType, ...imgProps } = props;

return (<ResponsiveImage src={UnsupportImage} {...imgProps} alt={mediaType} />);
}
2 changes: 2 additions & 0 deletions src/components/display/thumbnail/file/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './LocalUnsupportThumbnail';
export * from './LocalImageThumbnail';
5 changes: 5 additions & 0 deletions src/components/display/thumbnail/file/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ResponsiveImageProps } from '@components/display/ResponsiveImage';

export interface LocalThumbnailProps extends Omit<ResponsiveImageProps, 'src' | 'ref'> {
filePath: string;
}
3 changes: 3 additions & 0 deletions src/components/display/thumbnail/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './file';
export * from './url';
export * from './ResourceThumbnailDisplayer';
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* eslint-disable react/jsx-props-no-spreading */
import { useMemo, useRef, useState } from 'react';
import { CiImageOff } from 'react-icons/ci';
import { Center, Image } from '@mantine/core';
import { getYoutubeVideoId } from '@utils/urlParser';
import { useBackGroundImage } from '@hooks/ui-hooks';

import { useMemo, useRef, useState } from 'react';
import { ResponsiveImageProps } from './ResponsiveImage';
import { ResponsiveImageProps } from '@components/display';

export interface YoutubeThumbnailProps extends Omit<ResponsiveImageProps, 'src' | 'ref'> {
url: string;
Expand Down
1 change: 1 addition & 0 deletions src/components/display/thumbnail/url/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './YoutubeThunbnail';
1 change: 1 addition & 0 deletions src/pages/category-list/components/CategoryCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function CategoryCard(props: CategoryCardProps) {
key={data.id}
url={data.url?.full}
filePath={`${data.root_path}${data.file?.path}`}
mediaType={data.file?.media_type}
alt={data.name}
useBackgoundImg
/>
Expand Down
7 changes: 6 additions & 1 deletion src/pages/resource-list/components/ResourceCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ export function ResourceCard(props: ResourceCardProps) {
</>
)}
<UnstyledButton>
<ResourceThumbnailDisplayer url={data.url?.full} filePath={`${data.root_path}${data.file?.path}`} alt={data.name} />
<ResourceThumbnailDisplayer
url={data.url?.full}
filePath={`${data.root_path}${data.file?.path}`}
alt={data.name}
mediaType={data.file?.media_type}
/>
</UnstyledButton>
</Box>
</Card.Section>
Expand Down

0 comments on commit 10b46bd

Please sign in to comment.