simple-list.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { View, Row } from 'app/design/view';
  2. import { Text } from 'app/design/typography';
  3. import Time from 'app/ui/atoms/time';
  4. import Html from 'app/ui/atoms/html';
  5. import { BlockWrapper } from 'app/components/block-wrapper'
  6. import { looksLikeHtml, stripTags } from 'app/lib/util';
  7. function normalize(s) {
  8. return stripTags(s)?.trim() || '';
  9. }
  10. export default function ElementSimpleList({ data, blockWrapperProps }) {
  11. const keys = Object.keys(data[0]);
  12. const renderData = (data) => {
  13. if (Number.isInteger(data) && data > 1000000000) {
  14. return <Time className='text-card-foreground' ts={data} format="datetime"></Time>
  15. }
  16. const isHtml = looksLikeHtml(data);
  17. const text = isHtml ? String(data ?? '') : normalize(data);
  18. if (isHtml) {
  19. return <Html data={text} />;
  20. }
  21. return <Text className='text-card-foreground'>{text}</Text>
  22. }
  23. const renderItem = (item, index, view) => {
  24. return (
  25. <View key={index} className={(view == 'row' ? 'flex-row gap-x-4' : '') + ' mb-4'}>
  26. <Text className='font-medium text-card-foreground'>{item[0]}: </Text>
  27. <Row>
  28. {renderData(item[1])}
  29. {
  30. item[2] && <>
  31. <Text className='text-xs text-card-foreground'> - </Text>
  32. {renderData(item[2])}
  33. </>
  34. }
  35. </Row>
  36. </View>
  37. );
  38. };
  39. return (
  40. <BlockWrapper {...blockWrapperProps}>
  41. <View>
  42. {data.map((item, index) => {
  43. if (Array.isArray(item[keys[0]])) {
  44. return item.map((item1, index1) => renderItem(item1, index + '-' + index1, 'row'));
  45. }
  46. else {
  47. return renderItem(Object.values(item), index);
  48. }
  49. })}
  50. </View>
  51. </BlockWrapper>
  52. );
  53. }