Skip to content

Commit a5ed8d2

Browse files
authored
ENG-686 Refactor DiscourseNodeUtil to utilize RenderRoamBlockString for embedded blocks (#961)
- Removed legacy content rendering logic and replaced it with RenderRoamBlockString for better handling of embedded Roam blocks. - Simplified state management by eliminating unnecessary refs and state variables. - Enhanced readability and maintainability of the component by streamlining the rendering logic for titles and embedded content.
1 parent 91fe58c commit a5ed8d2

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

apps/roam/src/components/canvas/DiscourseNodeUtil.tsx

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import { getSetting } from "~/utils/extensionSettings";
4545
import DiscourseContextOverlay from "~/components/DiscourseContextOverlay";
4646
import { getDiscourseNodeColors } from "~/utils/getDiscourseNodeColors";
4747
import { render as renderToast } from "roamjs-components/components/Toast";
48+
import { RenderRoamBlockString } from "~/utils/roamReactComponents";
4849

4950
const escapeRegExp = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
5051

@@ -445,10 +446,6 @@ export class BaseDiscourseNodeUtil extends BaseBoxShapeUtil<DiscourseNodeShape>
445446

446447
const isEditing = this.editor.getEditingShapeId() === shape.id;
447448
// eslint-disable-next-line react-hooks/rules-of-hooks
448-
const contentRef = useRef<HTMLDivElement>(null);
449-
// eslint-disable-next-line react-hooks/rules-of-hooks
450-
const [loaded, setLoaded] = useState("");
451-
// eslint-disable-next-line react-hooks/rules-of-hooks
452449
const [overlayMounted, setOverlayMounted] = useState(false);
453450
// eslint-disable-next-line react-hooks/rules-of-hooks
454451
const dialogRenderedRef = useRef(false);
@@ -479,24 +476,9 @@ export class BaseDiscourseNodeUtil extends BaseBoxShapeUtil<DiscourseNodeShape>
479476
return null;
480477
}, [shape.type, shape.props.uid]);
481478

482-
// eslint-disable-next-line react-hooks/rules-of-hooks
483-
useEffect(() => {
484-
if (
485-
shape.props.uid !== loaded &&
486-
!isPageUid(shape.props.uid) &&
487-
contentRef.current &&
488-
isLiveBlock(shape.props.uid)
489-
) {
490-
window.roamAlphaAPI.ui.components.renderBlock({
491-
el: contentRef.current,
492-
uid: shape.props.uid,
493-
});
494-
// TODO: resize shape props once this is rendered
495-
setLoaded(shape.props.uid);
496-
}
497-
}, [setLoaded, loaded, contentRef, shape.props.uid]);
498-
499479
const { backgroundColor, textColor } = this.getColors();
480+
const showEmbeddedRoamBlock =
481+
!isPageUid(shape.props.uid) && isLiveBlock(shape.props.uid);
500482

501483
// eslint-disable-next-line react-hooks/rules-of-hooks
502484
useEffect(() => {
@@ -737,7 +719,6 @@ export class BaseDiscourseNodeUtil extends BaseBoxShapeUtil<DiscourseNodeShape>
737719
) : null}
738720

739721
<div
740-
ref={contentRef}
741722
className="relative"
742723
style={{
743724
...DEFAULT_STYLE_PROPS,
@@ -760,10 +741,21 @@ export class BaseDiscourseNodeUtil extends BaseBoxShapeUtil<DiscourseNodeShape>
760741
/>
761742
</div>
762743
)}
763-
{alias
764-
? new RegExp(alias).exec(shape.props.title)?.[1] ||
765-
shape.props.title
766-
: shape.props.title}
744+
{showEmbeddedRoamBlock ? (
745+
<div className="w-full min-w-0">
746+
<RenderRoamBlockString
747+
key={shape.props.uid}
748+
string={
749+
getTextByBlockUid(shape.props.uid) || shape.props.title
750+
}
751+
/>
752+
</div>
753+
) : alias ? (
754+
new RegExp(alias).exec(shape.props.title)?.[1] ||
755+
shape.props.title
756+
) : (
757+
shape.props.title
758+
)}
767759
</div>
768760
</div>
769761
</HTMLContainer>

0 commit comments

Comments
 (0)