From b65786e45f861bf280b28a61480ef22484979a36 Mon Sep 17 00:00:00 2001 From: richardo2016x Date: Fri, 28 Jun 2024 03:04:33 +0800 Subject: [PATCH] feat: other improvement. --- .../src/components/Image/AppFastImage.tsx | 81 +++++++++++++++++++ .../src/screens/Dapps/components/DappCard.tsx | 6 +- .../src/screens/Dapps/components/DappIcon.tsx | 30 ++++--- 3 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 apps/mobile/src/components/Image/AppFastImage.tsx diff --git a/apps/mobile/src/components/Image/AppFastImage.tsx b/apps/mobile/src/components/Image/AppFastImage.tsx new file mode 100644 index 000000000..3aabfb82f --- /dev/null +++ b/apps/mobile/src/components/Image/AppFastImage.tsx @@ -0,0 +1,81 @@ +import { useRefState } from '@/hooks/common/useRefState'; +import { useThemeStyles } from '@/hooks/theme'; +import { createGetStyles } from '@/utils/styles'; +import React from 'react'; +import { StyleSheet, View } from 'react-native'; +import FastImage from 'react-native-fast-image'; + +type FastImageProps = React.ComponentProps; +export default function AppFastImage( + props: FastImageProps & { + PlaceholderContent?: React.ReactNode; + viewProps?: React.ComponentProps; + }, +) { + const { styles } = useThemeStyles(getStyles); + const { + PlaceholderContent, + viewProps, + onLoadEnd: propOnLoadEnd, + onError: propOnError, + ...rest + } = props; + + const { + stateRef: firstLoadedRef, + state: firstLoaded, + setRefState, + } = useRefState(false); + const onLoadEnd = React.useCallback< + FastImageProps['onLoadEnd'] & object + >(() => { + if (!firstLoadedRef.current) setRefState(true, true); + + propOnLoadEnd?.(); + }, [firstLoadedRef, setRefState, propOnLoadEnd]); + + const onError = React.useCallback(() => { + if (!firstLoadedRef.current) setRefState(true, true); + + propOnError?.(); + }, [firstLoadedRef, setRefState, propOnError]); + + return ( + + + {PlaceholderContent && !firstLoaded && ( + {PlaceholderContent} + )} + + ); +} + +const getStyles = createGetStyles(colors => { + return { + container: { + position: 'relative', + }, + placeholder: { + position: 'absolute', + top: 0, + left: 0, + right: 0, + bottom: 0, + width: '100%', + height: '100%', + justifyContent: 'center', + alignItems: 'center', + }, + }; +}); diff --git a/apps/mobile/src/screens/Dapps/components/DappCard.tsx b/apps/mobile/src/screens/Dapps/components/DappCard.tsx index 0ba55ae61..772ce7387 100644 --- a/apps/mobile/src/screens/Dapps/components/DappCard.tsx +++ b/apps/mobile/src/screens/Dapps/components/DappCard.tsx @@ -16,6 +16,7 @@ import { TouchableOpacity, TouchableWithoutFeedback, } from 'react-native-gesture-handler'; +import FastImage from 'react-native-fast-image'; import { DappIcon } from './DappIcon'; import { stringUtils } from '@rabby-wallet/base-utils'; import { CHAINS } from '@/constant/chains'; @@ -47,7 +48,7 @@ export const DappCardListBy = ({ {data.map(item => { return ( - @@ -112,6 +113,7 @@ export const DappCard = ({ data?.info?.logo_url ? { uri: data.info?.logo_url, + priority: 'low', } : undefined } @@ -121,7 +123,7 @@ export const DappCard = ({ {isActive ? : null} <> {data?.isConnected && chain ? ( - ; + ...rest +}: React.ComponentProps & { origin: string; - source?: ImageURISource; }) => { const colors = useThemeColors(); const styles = React.useMemo(() => getStyles(colors), [colors]); @@ -57,9 +49,15 @@ export const DappIcon = ({ ); - if (source?.uri) { + if (source) { return ( - + ); }