item-renderer.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { View } from 'app/design/view';
  2. import { memo } from 'react';
  3. import Unit from 'app/components/unit';
  4. import { BlockByName2 } from 'app/components/block'
  5. import { isTasksHomeUri } from 'app/components/elements/tasks/helpers'
  6. function isTasksListBlock(item) {
  7. const content = item?.data?.content;
  8. if (Array.isArray(content) && content.some((entry) => entry?.type === 'tasks_list')) {
  9. return true;
  10. }
  11. if (item?.data?.type === 'tasks_list') return true;
  12. const name = typeof item?.block === 'string' ? item.block : item?.block?.name;
  13. return typeof name === 'string' && name.includes('tasks_list');
  14. }
  15. /** `routeIndex`: unit rows get it instead of `route` (see useListScene). */
  16. function ItemRenderer_({ route, routeIndex, item, unit, module, unitMode, unitType, sidebar }) {
  17. if (item?.type === 'block') {
  18. return <BlockItemRenderer item={item} route={route} sidebar={sidebar} />;
  19. } else {
  20. return (
  21. <View key={`${routeIndex ?? route?.index}-${item.id}`}>
  22. <Unit unitType={unitType} module={module} unit={unit} data={item} mode={unitMode} />
  23. </View>
  24. );
  25. }
  26. }
  27. export function BlockItemRenderer({ route, item, sidebar }) {
  28. const isTasksList = isTasksListBlock(item);
  29. const fillViewport = isTasksList && (
  30. isTasksHomeUri(route?.pageData?.uri) || isTasksHomeUri(route?.key) || isTasksHomeUri(route?.link)
  31. );
  32. const maxWidthClass = isTasksList ? '' : 'u-max-width-block';
  33. const block = BlockByName2({
  34. b: item.data,
  35. name: item.block,
  36. contentOnly:item?.data?.source == "system:get_create_post_form",
  37. fill: fillViewport,
  38. extraProps: {
  39. pageTitle: route?.title,
  40. pageContext: route?.pageData?.context,
  41. },
  42. wrapperClassses: `${item?.block?.classes || ''} ${fillViewport ? 'h-full min-h-0 mb-0' : 'mb-0.5 sm:mb-3 lg:mb-4'} w-full mx-auto ${maxWidthClass}`.trim()
  43. }
  44. );
  45. if (!block) {
  46. return null;
  47. }
  48. return block
  49. }
  50. export const ItemRenderer = memo(ItemRenderer_);
  51. export const ItemRendererMemo = memo(ItemRenderer);