Skip to content

Commit 8dca2e4

Browse files
authored
add loader and change icon for images in code browser (#2402)
1 parent 828f679 commit 8dca2e4

3 files changed

Lines changed: 41 additions & 16 deletions

File tree

components/Package/CodeBrowser/CodeBrowserContent.tsx

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type SyntheticEvent, useState } from 'react';
1+
import { type SyntheticEvent, useEffect, useState } from 'react';
22
import { Pressable, View } from 'react-native';
33
import { type Theme } from 'react-shiki';
44
import useSWR from 'swr';
@@ -29,12 +29,16 @@ type Props = {
2929
};
3030

3131
export default function CodeBrowserContent({ packageName, filePath, fileData }: Props) {
32-
const fileExtension = filePath.split('.').at(-1) ?? 'text';
3332
const [rawPreview, setRawPreview] = useState(false);
3433
const [imageData, setImageData] = useState<
3534
SyntheticEvent<HTMLImageElement>['currentTarget'] | null
3635
>(null);
3736

37+
useEffect(() => {
38+
setImageData(null);
39+
}, [filePath]);
40+
41+
const fileExtension = filePath.split('.').at(-1) ?? 'text';
3842
const isTooBig = Boolean(fileData?.size && fileData.size > 1024 * 1024 * 4);
3943
const isPreviewDisabled = PREVIEW_DISABLED_FILES.includes(fileExtension) || isTooBig;
4044
const isImageFile = IMAGE_FILES.includes(fileExtension);
@@ -146,14 +150,18 @@ export default function CodeBrowserContent({ packageName, filePath, fileData }:
146150
</CodeBrowserContentHeader>
147151
<View style={tw`flex flex-1 items-center justify-center`}>
148152
{isImageFile && (
149-
<img
150-
src={`https://unpkg.com/${packageName}/${filePath}`}
151-
alt=""
152-
style={tw`max-h-full max-w-full`}
153-
onLoad={(event: SyntheticEvent<HTMLImageElement>) => {
154-
setImageData(event.currentTarget);
155-
}}
156-
/>
153+
<>
154+
<img
155+
key={filePath}
156+
src={`https://unpkg.com/${packageName}/${filePath}`}
157+
alt={filePath}
158+
style={tw`max-h-full max-w-full`}
159+
onLoad={(event: SyntheticEvent<HTMLImageElement>) => {
160+
setImageData(event.currentTarget);
161+
}}
162+
/>
163+
{!imageData && <ThreeDotsLoader />}
164+
</>
157165
)}
158166
{!isImageFile && isPreviewDisabled && (
159167
<View style={tw`flex flex-col items-center gap-1`}>

components/Package/CodeBrowser/CodeBrowserContentFooter.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type PropsWithChildren, type ReactNode } from 'react';
22
import { View } from 'react-native';
33
import { type Style } from 'twrnc';
44

5+
import { useLayout } from '~/common/styleguide';
56
import tw from '~/util/tailwind';
67

78
type Props = PropsWithChildren<{
@@ -15,10 +16,12 @@ export default function CodeBrowserContentFooter({
1516
leftSlot = <View />,
1617
rightSlot = <View />,
1718
}: Props) {
19+
const { isSmallScreen } = useLayout();
1820
return (
1921
<View
2022
style={[
2123
tw`relative flex min-h-[26px] flex-row items-center justify-between gap-3 border-t border-palette-gray2 bg-default px-3 pb-px dark:border-default`,
24+
isSmallScreen && tw`border-b border-t-0`,
2225
style,
2326
]}>
2427
{leftSlot}

components/Package/CodeBrowser/CodeBrowserFileRow.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ import { useMemo, useState } from 'react';
22
import { Pressable, View } from 'react-native';
33

44
import { P } from '~/common/styleguide';
5-
import { FileIcon, FileMetadataIcon, FolderIcon, WarningBlockquote } from '~/components/Icons';
5+
import {
6+
FileIcon,
7+
FileMetadataIcon,
8+
FolderIcon,
9+
ImageFileIcon,
10+
WarningBlockquote,
11+
} from '~/components/Icons';
612
import Tooltip from '~/components/Tooltip';
7-
import { getFileWarning } from '~/util/codeBrowser';
13+
import { getFileWarning, IMAGE_FILES } from '~/util/codeBrowser';
814
import tw from '~/util/tailwind';
915

1016
type Props = {
@@ -26,11 +32,19 @@ export default function CodeBrowserFileRow({
2632
}: Props) {
2733
const [isHovered, setIsHovered] = useState(false);
2834
const warning = isDirectory ? undefined : getFileWarning(label);
35+
const fileExtension = label.split('.').at(-1) ?? 'text';
36+
const isImageFile = IMAGE_FILES.includes(fileExtension);
2937

30-
const Icon = useMemo(
31-
() => (isDirectory ? FolderIcon : isNested ? FileMetadataIcon : FileIcon),
32-
[isDirectory, isNested]
33-
);
38+
const Icon = useMemo(() => {
39+
if (isDirectory) {
40+
return FolderIcon;
41+
} else if (isNested) {
42+
return FileMetadataIcon;
43+
} else if (isImageFile) {
44+
return ImageFileIcon;
45+
}
46+
return FileIcon;
47+
}, [isDirectory, isNested, isImageFile]);
3448

3549
const rowStyle = [
3650
tw`flex flex-row items-center gap-1.5 px-3 py-[3px] last:mb-20`,

0 commit comments

Comments
 (0)