Skip to content

Commit c06bc6f

Browse files
committed
🐛(frontend) fix TOC display without headings
The table of contents was displayed even when there were no headings in the document. It was not the expected behavior. We now ensure that the TOC is only shown when there are headings present, we added a test to verify this behavior.
1 parent 80ee409 commit c06bc6f

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

src/frontend/apps/e2e/__tests__/app-impress/doc-table-content.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ test.describe('Doc Table Content', () => {
1919

2020
await page.locator('.ProseMirror').click();
2121

22+
await expect(page.getByRole('button', { name: 'Summary' })).toBeHidden();
23+
2224
await page.keyboard.type('# Level 1\n## Level 2\n### Level 3');
2325

2426
const summaryContainer = page.locator('#summaryContainer');

src/frontend/apps/impress/src/features/docs/doc-table-content/components/TableContent.tsx

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ import { css } from 'styled-components';
44

55
import { Box, BoxButton, Icon, Text } from '@/components';
66
import { useCunninghamTheme } from '@/cunningham';
7-
import { useEditorStore, useHeadingStore } from '@/docs/doc-editor';
7+
import {
8+
DocsBlockNoteEditor,
9+
HeadingBlock,
10+
useEditorStore,
11+
useHeadingStore,
12+
} from '@/docs/doc-editor';
813
import { MAIN_LAYOUT_ID } from '@/layouts/conf';
914

1015
import { Heading } from './Heading';
1116

1217
export const TableContent = () => {
1318
const { spacingsTokens, colorsTokens } = useCunninghamTheme();
1419
const [containerHeight, setContainerHeight] = useState('100vh');
20+
const { headings } = useHeadingStore();
21+
const { editor } = useEditorStore();
1522

1623
const { t } = useTranslation();
1724
const [isOpen, setIsOpen] = useState(false);
@@ -30,6 +37,15 @@ export const TableContent = () => {
3037
setIsOpen(true);
3138
};
3239

40+
if (
41+
!editor ||
42+
!headings ||
43+
headings.length === 0 ||
44+
(headings.length === 1 && !headings[0].contentText)
45+
) {
46+
return null;
47+
}
48+
3349
return (
3450
<Box
3551
$height={containerHeight}
@@ -96,19 +112,27 @@ export const TableContent = () => {
96112
/>
97113
</BoxButton>
98114
)}
99-
{isOpen && <TableContentOpened setIsOpen={setIsOpen} />}
115+
{isOpen && (
116+
<TableContentOpened
117+
setIsOpen={setIsOpen}
118+
headings={headings}
119+
editor={editor}
120+
/>
121+
)}
100122
</Box>
101123
</Box>
102124
);
103125
};
104126

105127
const TableContentOpened = ({
106128
setIsOpen,
129+
headings,
130+
editor,
107131
}: {
108132
setIsOpen: (isOpen: boolean) => void;
133+
headings: HeadingBlock[];
134+
editor: DocsBlockNoteEditor;
109135
}) => {
110-
const { headings } = useHeadingStore();
111-
const { editor } = useEditorStore();
112136
const { spacingsTokens, colorsTokens } = useCunninghamTheme();
113137
const [headingIdHighlight, setHeadingIdHighlight] = useState<string>();
114138
const { t } = useTranslation();
@@ -172,15 +196,6 @@ const TableContentOpened = ({
172196
setIsOpen(false);
173197
};
174198

175-
if (
176-
!editor ||
177-
!headings ||
178-
headings.length === 0 ||
179-
(headings.length === 1 && !headings[0].contentText)
180-
) {
181-
return null;
182-
}
183-
184199
return (
185200
<Box
186201
$width="100%"

0 commit comments

Comments
 (0)