Skip to content

Commit 948f07c

Browse files
committed
fix(ObjectPage): prevent crash on re-mount (#1317)
1 parent fd39fb3 commit 948f07c

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

packages/main/src/components/ObjectPage/ObjectPageUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const extractSectionIdFromHtmlId = (id: string) => {
1212
};
1313

1414
export const getLastObjectPageSection = (ref: RefObject<HTMLDivElement>): HTMLElement => {
15-
const sections = ref.current.querySelectorAll<HTMLElement>('[id^="ObjectPageSection"]');
15+
const sections = ref.current?.querySelectorAll<HTMLElement>('[id^="ObjectPageSection"]');
1616
if (!sections || sections.length < 1) {
1717
return null;
1818
}

packages/main/src/components/ObjectPage/index.tsx

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -316,23 +316,31 @@ const ObjectPage: FC<ObjectPagePropTypes> = forwardRef((props: ObjectPagePropTyp
316316

317317
useEffect(() => {
318318
const fillerDivObserver = new ResizeObserver(() => {
319+
let heightDiff = 0;
320+
319321
const maxHeight = Math.min(objectPageRef.current?.clientHeight, window.innerHeight);
320322
const availableScrollHeight = maxHeight - totalHeaderHeight;
321323
const lastSectionDomRef = getLastObjectPageSection(objectPageRef);
322-
const subSections = lastSectionDomRef.querySelectorAll('[id^="ObjectPageSubSection"]');
323324

324-
let lastSubSectionHeight;
325-
if (subSections.length > 0) {
326-
lastSubSectionHeight = (subSections[subSections.length - 1] as HTMLElement).offsetHeight;
327-
} else {
328-
lastSubSectionHeight =
329-
lastSectionDomRef.offsetHeight -
330-
lastSectionDomRef.querySelector<HTMLElement>("[role='heading']").offsetHeight;
331-
}
325+
if (lastSectionDomRef) {
326+
const subSections = lastSectionDomRef.querySelectorAll('[id^="ObjectPageSubSection"]');
327+
328+
let lastSubSectionHeight;
329+
if (subSections.length > 0) {
330+
lastSubSectionHeight = (subSections[subSections.length - 1] as HTMLElement).offsetHeight;
331+
} else {
332+
lastSubSectionHeight =
333+
lastSectionDomRef.offsetHeight -
334+
lastSectionDomRef.querySelector<HTMLElement>("[role='heading']").offsetHeight;
335+
}
336+
337+
heightDiff = Math.max(0, availableScrollHeight - lastSubSectionHeight);
332338

333-
let heightDiff = availableScrollHeight - lastSubSectionHeight;
339+
if (isNaN(heightDiff)) {
340+
heightDiff = 0;
341+
}
342+
}
334343

335-
heightDiff = heightDiff > 0 ? heightDiff : 0;
336344
objectPageRef.current?.style.setProperty(ObjectPageCssVariables.lastSectionMargin, `${heightDiff}px`);
337345
});
338346

0 commit comments

Comments
 (0)