index.tsx 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import React, { useCallback } from 'react';
  2. import { useNode } from './Node.hooks';
  3. import { withErrorBoundary } from 'react-error-boundary';
  4. import { ErrorBoundaryFallbackComponent } from '../_shared/ErrorBoundaryFallbackComponent';
  5. import { Node } from '@/appflowy_app/stores/reducers/document/slice';
  6. import TextBlock from '../TextBlock';
  7. function NodeComponent({ id }: { id: string }) {
  8. const { node, childIds } = useNode(id);
  9. const renderBlock = useCallback((props: { node: Node; childIds?: string[] }) => {
  10. switch (props.node.type) {
  11. case 'text':
  12. return <TextBlock {...props} />;
  13. default:
  14. break;
  15. }
  16. }, []);
  17. if (!node) return null;
  18. return (
  19. <div data-block-id={node.id} className='relative my-[1px]'>
  20. {renderBlock({
  21. node,
  22. childIds,
  23. })}
  24. <div className='block-overlay' />
  25. </div>
  26. );
  27. }
  28. const NodeWithErrorBoundary = withErrorBoundary(NodeComponent, {
  29. FallbackComponent: ErrorBoundaryFallbackComponent,
  30. });
  31. export default React.memo(NodeWithErrorBoundary);