index.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import {
  2. useState
  3. } from "react";
  4. import {
  5. TouchableOpacity,
  6. Image,
  7. View
  8. } from "react-native";
  9. import type ISiteLogoProps from "./type";
  10. import stylesheet, {
  11. getSiteLogoSize,
  12. useStyles
  13. } from "./stylesheet";
  14. import {
  15. NCoreUIKitLocalize,
  16. NCoreUIKitTheme
  17. } from "../../core/hooks";
  18. import {
  19. Home as HomeIcon
  20. } from "lucide-react-native";
  21. import Loading from "../loading";
  22. import Text from "../text";
  23. const SiteLogo = ({
  24. logoBackgroundColor = "default",
  25. isWorkWithFlex1 = true,
  26. isShowBorder = false,
  27. subTitleCustomColor,
  28. isCustomImageSize,
  29. isShowIcon = true,
  30. isWorkWithAction,
  31. titleCustomColor,
  32. image: ImageProp,
  33. backgroundColor,
  34. subTitleVariant,
  35. customLocalize,
  36. icon: IconProp,
  37. size = "large",
  38. subTitleStyle,
  39. subTitleColor,
  40. titleVariant,
  41. imageSource,
  42. customTheme,
  43. borderColor,
  44. titleStyle,
  45. imageSpace,
  46. isDisabled,
  47. titleColor,
  48. imageProps,
  49. isLoading,
  50. iconColor,
  51. titleKey,
  52. imageUrl,
  53. subTitle,
  54. onPress,
  55. title,
  56. style
  57. }: ISiteLogoProps) => {
  58. const {
  59. borders,
  60. colors,
  61. spaces
  62. } = NCoreUIKitTheme.useContext(customTheme);
  63. const {
  64. localize
  65. } = NCoreUIKitLocalize.useContext(customLocalize);
  66. const currentSize = getSiteLogoSize({
  67. spaces,
  68. size
  69. });
  70. const [
  71. imageLoadError,
  72. setImageLoadError
  73. ] = useState(false);
  74. const {
  75. contentContainer: contentContainerDynamicStyle,
  76. titlesContainer: titlesContainerDynamicStyle,
  77. container: containerDynamicStyle,
  78. image: imageDynamicStyle,
  79. icon: iconDynamicStyle
  80. } = useStyles({
  81. logoBackgroundColor,
  82. isCustomImageSize,
  83. image: ImageProp,
  84. isWorkWithFlex1,
  85. backgroundColor,
  86. isShowBorder,
  87. borderColor,
  88. currentSize,
  89. isDisabled,
  90. imageSpace,
  91. isLoading,
  92. imageUrl,
  93. borders,
  94. colors,
  95. spaces
  96. });
  97. const renderIcon = () => {
  98. if(IconProp) {
  99. return <IconProp
  100. size={currentSize.iconSize}
  101. customColor={iconColor}
  102. color="default"
  103. style={{
  104. ...iconDynamicStyle
  105. }}
  106. />;
  107. }
  108. return <HomeIcon
  109. color={iconColor ? iconColor : colors.content.icon.default}
  110. size={currentSize.iconSize}
  111. style={{
  112. ...iconDynamicStyle
  113. }}
  114. />;
  115. };
  116. const renderLoading = () => {
  117. let loadingColor: keyof NCoreUIKit.IconContentColors = "default";
  118. if(title) {
  119. loadingColor = "onPrimary";
  120. } else if(imageUrl || ImageProp) {
  121. loadingColor = "mid";
  122. }
  123. return <Loading
  124. color={loadingColor}
  125. />;
  126. };
  127. const renderContent = () => {
  128. if(isLoading) {
  129. return renderLoading();
  130. }
  131. if(IconProp) {
  132. return renderIcon();
  133. }
  134. if(!ImageProp && !imageUrl) {
  135. return renderIcon();
  136. }
  137. if((imageUrl || ImageProp) && imageLoadError) {
  138. return renderIcon();
  139. }
  140. if(ImageProp) {
  141. return <ImageProp
  142. {...imageProps}
  143. onError={() => setImageLoadError(true)}
  144. height={currentSize.size}
  145. width={currentSize.size}
  146. style={[
  147. stylesheet.image,
  148. imageDynamicStyle,
  149. imageProps?.style
  150. ]}
  151. />;
  152. }
  153. if(imageUrl) {
  154. return <Image
  155. {...imageProps}
  156. onError={() => setImageLoadError(true)}
  157. height={currentSize.size}
  158. width={currentSize.size}
  159. source={{
  160. uri: imageUrl,
  161. ...imageSource
  162. }}
  163. style={[
  164. stylesheet.image,
  165. imageDynamicStyle,
  166. imageProps?.style
  167. ]}
  168. />;
  169. }
  170. return null;
  171. };
  172. const renderTitles = () => {
  173. if(!title) {
  174. return null;
  175. }
  176. return <View
  177. style={[
  178. stylesheet.titlesContainer,
  179. titlesContainerDynamicStyle
  180. ]}
  181. >
  182. <Text
  183. customColor={titleCustomColor ? colors.project[titleCustomColor] : undefined}
  184. variant={titleVariant ? titleVariant : currentSize.titleSize}
  185. color={titleColor ? titleColor : "high"}
  186. ellipsizeMode="tail"
  187. numberOfLines={1}
  188. style={{
  189. width: "100%",
  190. ...titleStyle
  191. }}
  192. >
  193. {titleKey ? localize(titleKey) : title}
  194. </Text>
  195. {
  196. subTitle ?
  197. <Text
  198. customColor={subTitleCustomColor ? colors.project[subTitleCustomColor] : undefined}
  199. variant={subTitleVariant ? subTitleVariant : currentSize.subTitleSize}
  200. color={subTitleColor ? subTitleColor : "low"}
  201. ellipsizeMode="tail"
  202. numberOfLines={1}
  203. style={{
  204. width: "100%",
  205. ...subTitleStyle
  206. }}
  207. >
  208. {subTitle}
  209. </Text>
  210. :
  211. null
  212. }
  213. </View>;
  214. };
  215. const renderContentContainer = () => {
  216. if(!isShowIcon) {
  217. return null;
  218. }
  219. return <View
  220. style={[
  221. stylesheet.contentContainer,
  222. contentContainerDynamicStyle
  223. ]}
  224. >
  225. {renderContent()}
  226. </View>;
  227. };
  228. return <TouchableOpacity
  229. disabled={!isWorkWithAction || isDisabled || isLoading}
  230. onPress={!isWorkWithAction || isDisabled || isLoading ? undefined : () => {
  231. if(onPress) onPress();
  232. }}
  233. style={[
  234. style,
  235. stylesheet.container,
  236. containerDynamicStyle
  237. ]}
  238. >
  239. {renderContentContainer()}
  240. {renderTitles()}
  241. </TouchableOpacity>;
  242. };
  243. export default SiteLogo;