|
1 | | -import { type SyntheticEvent, useState } from 'react'; |
| 1 | +import { type SyntheticEvent, useEffect, useState } from 'react'; |
2 | 2 | import { Pressable, View } from 'react-native'; |
3 | 3 | import { type Theme } from 'react-shiki'; |
4 | 4 | import useSWR from 'swr'; |
@@ -29,12 +29,16 @@ type Props = { |
29 | 29 | }; |
30 | 30 |
|
31 | 31 | export default function CodeBrowserContent({ packageName, filePath, fileData }: Props) { |
32 | | - const fileExtension = filePath.split('.').at(-1) ?? 'text'; |
33 | 32 | const [rawPreview, setRawPreview] = useState(false); |
34 | 33 | const [imageData, setImageData] = useState< |
35 | 34 | SyntheticEvent<HTMLImageElement>['currentTarget'] | null |
36 | 35 | >(null); |
37 | 36 |
|
| 37 | + useEffect(() => { |
| 38 | + setImageData(null); |
| 39 | + }, [filePath]); |
| 40 | + |
| 41 | + const fileExtension = filePath.split('.').at(-1) ?? 'text'; |
38 | 42 | const isTooBig = Boolean(fileData?.size && fileData.size > 1024 * 1024 * 4); |
39 | 43 | const isPreviewDisabled = PREVIEW_DISABLED_FILES.includes(fileExtension) || isTooBig; |
40 | 44 | const isImageFile = IMAGE_FILES.includes(fileExtension); |
@@ -146,14 +150,18 @@ export default function CodeBrowserContent({ packageName, filePath, fileData }: |
146 | 150 | </CodeBrowserContentHeader> |
147 | 151 | <View style={tw`flex flex-1 items-center justify-center`}> |
148 | 152 | {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 | + </> |
157 | 165 | )} |
158 | 166 | {!isImageFile && isPreviewDisabled && ( |
159 | 167 | <View style={tw`flex flex-col items-center gap-1`}> |
|
0 commit comments