FileList.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import React from 'react'
  2. import { StyleSheet, View, FlatList, Text, Image } from 'react-native'
  3. import getFileTypeIcon from '@uppy/dashboard/lib/utils/getFileTypeIcon.js'
  4. import renderStringFromJSX from 'preact-render-to-string'
  5. const fileIcon = require('./assets/file-icon.png')
  6. const truncateString = (str) => {
  7. const maxChars = 20
  8. if (str.length > maxChars) {
  9. return `${str.substring(0, 25)}...`
  10. }
  11. return str
  12. }
  13. function FileIcon () {
  14. return (
  15. <View style={styles.itemIconContainer}>
  16. <Image
  17. style={styles.itemIcon}
  18. source={fileIcon}
  19. />
  20. </View>
  21. )
  22. }
  23. function UppyDashboardFileIcon ({ type }) {
  24. const icon = renderStringFromJSX(getFileTypeIcon(type).icon)
  25. if (!icon) {
  26. return <FileIcon />
  27. }
  28. const { color } = getFileTypeIcon(type)
  29. return (
  30. <View
  31. style={{
  32. ...styles.itemIconContainer,
  33. backgroundColor: color,
  34. }}
  35. >
  36. <Text style={styles.itemType}>logo</Text>
  37. </View>
  38. )
  39. }
  40. export default function FileList ({ uppy }) {
  41. const uppyFiles = uppy.store.state.files
  42. const uppyFilesArray = Object.keys(uppyFiles).map((id) => uppyFiles[id])
  43. return (
  44. <View style={styles.container}>
  45. <FlatList
  46. data={uppyFilesArray}
  47. keyExtractor={(item) => item.id}
  48. numColumns={2}
  49. renderItem={({ item }) => {
  50. return (
  51. <View style={styles.item}>
  52. {item.type === 'image' ? (
  53. <Image
  54. style={styles.itemImage}
  55. source={{ uri: item.data.uri }}
  56. />
  57. ) : (
  58. <UppyDashboardFileIcon type={item.type} />
  59. )}
  60. <Text style={styles.itemName}>{truncateString(item.name, 20)}</Text>
  61. <Text style={styles.itemType}>{item.type}</Text>
  62. </View>
  63. )
  64. }}
  65. />
  66. </View>
  67. )
  68. }
  69. const styles = StyleSheet.create({
  70. container: {
  71. marginTop: 20,
  72. marginBottom: 20,
  73. flex: 1,
  74. justifyContent: 'center',
  75. alignItems:'center',
  76. marginRight: -25,
  77. },
  78. item: {
  79. width: 100,
  80. marginTop: 5,
  81. marginBottom: 15,
  82. marginRight: 25,
  83. },
  84. itemImage: {
  85. width: 100,
  86. height: 100,
  87. borderRadius: 5,
  88. marginBottom: 5,
  89. },
  90. itemIconContainer: {
  91. width: 100,
  92. height: 100,
  93. borderRadius: 5,
  94. marginBottom: 5,
  95. backgroundColor: '#cfd3d6',
  96. alignItems: 'center',
  97. justifyContent: 'center',
  98. },
  99. itemIcon: {
  100. width: 42,
  101. height: 56,
  102. },
  103. itemIconSVG: {
  104. width: 50,
  105. height: 50,
  106. },
  107. itemName: {
  108. fontSize: 13,
  109. color: '#2c3e50',
  110. fontWeight: '600',
  111. },
  112. itemType: {
  113. fontWeight: '600',
  114. fontSize: 12,
  115. color: '#95a5a6',
  116. },
  117. })