|
| 1 | +import React from 'react'; |
1 | 2 | import { ComponentType } from 'types/component-props/_component-common';
|
2 | 3 | import { RegionProps } from 'types/component-props/layouts';
|
3 | 4 | import { PartType } from 'types/component-props/parts';
|
4 | 5 | import { translator } from 'translations';
|
5 | 6 | import { usePageConfig } from 'store/hooks/usePageConfig';
|
6 |
| - |
7 |
| -import styles from './SectionNavigation.module.scss'; |
8 | 7 | import { LenkeBase } from 'components/_common/lenke/LenkeBase';
|
9 | 8 | import { AnalyticsEvents } from 'utils/amplitude';
|
10 | 9 |
|
| 10 | +import styles from './SectionNavigation.module.scss'; |
| 11 | + |
11 | 12 | type SectionNavigationProps = {
|
12 |
| - introRegion: RegionProps<'intro'>; |
13 |
| - contentRegion: RegionProps<'content'>; |
| 13 | + introRegion?: RegionProps<'intro'>; |
| 14 | + contentRegion?: RegionProps<'content'>; |
14 | 15 | };
|
15 | 16 |
|
16 | 17 | type Anchor = {
|
17 | 18 | anchorId: string;
|
18 | 19 | title: string;
|
19 | 20 | };
|
20 | 21 |
|
21 |
| -const getAnchorsFromComponents = (components: RegionProps['components']) => { |
22 |
| - return components |
23 |
| - .reduce((acc, component) => { |
24 |
| - if ( |
25 |
| - component.type === ComponentType.Part && |
26 |
| - component.descriptor === PartType.Header && |
27 |
| - component.config?.titleTag === 'h3' |
28 |
| - ) { |
29 |
| - acc.push({ |
30 |
| - anchorId: component.config.anchorId as string, |
31 |
| - title: component.config.title as string, |
32 |
| - }); |
33 |
| - } |
34 |
| - return acc; |
35 |
| - }, [] as Anchor[]) |
36 |
| - .filter((anchor) => !!anchor.anchorId && !!anchor.title); |
| 22 | +const getAnchorsFromComponents = (region?: RegionProps) => { |
| 23 | + return region |
| 24 | + ? region.components.reduce<Anchor[]>((acc, component) => { |
| 25 | + if ( |
| 26 | + component.type === ComponentType.Part && |
| 27 | + component.descriptor === PartType.Header && |
| 28 | + component.config && |
| 29 | + component.config.titleTag === 'h3' && |
| 30 | + component.config.anchorId && |
| 31 | + component.config.title |
| 32 | + ) { |
| 33 | + acc.push({ |
| 34 | + anchorId: component.config.anchorId as string, |
| 35 | + title: component.config.title as string, |
| 36 | + }); |
| 37 | + } |
| 38 | + return acc; |
| 39 | + }, []) |
| 40 | + : []; |
37 | 41 | };
|
38 | 42 |
|
39 | 43 | export const SectionNavigation = ({
|
40 | 44 | introRegion,
|
41 | 45 | contentRegion,
|
42 | 46 | }: SectionNavigationProps) => {
|
43 | 47 | const { language } = usePageConfig();
|
44 |
| - const introAnchors = getAnchorsFromComponents(introRegion.components); |
45 |
| - const contentAnchors = getAnchorsFromComponents(contentRegion.components); |
| 48 | + const introAnchors = getAnchorsFromComponents(introRegion); |
| 49 | + const contentAnchors = getAnchorsFromComponents(contentRegion); |
46 | 50 | const allAnchors = [...introAnchors, ...contentAnchors];
|
47 | 51 |
|
48 |
| - const getLabels = translator('sectionNavigation', language); |
49 |
| - |
50 | 52 | if (allAnchors.length === 0) {
|
51 | 53 | return null;
|
52 | 54 | }
|
53 | 55 |
|
| 56 | + const getLabels = translator('sectionNavigation', language); |
| 57 | + |
54 | 58 | return (
|
55 | 59 | <ul
|
56 | 60 | aria-label={getLabels('navigationLabel')}
|
|
0 commit comments