-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathLayoutHeader.tsx
More file actions
168 lines (159 loc) · 5.98 KB
/
LayoutHeader.tsx
File metadata and controls
168 lines (159 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { useContext, useRef, useState, useMemo } from 'react';
import { useRouter } from 'next/router';
import { Button, Flex, View, VisuallyHidden } from '@aws-amplify/ui-react';
import classNames from 'classnames';
import { Platform } from '@/data/platforms';
import {
ALGOLIA_API_KEY,
ALGOLIA_INDEX_NAME,
ALGOLIA_APP_ID
} from '../../constants/algolia';
import { IconMenu, IconDoubleChevron } from '@/components/Icons';
import { Menu } from '@/components/Menu';
import { LayoutContext } from '@/components/Layout';
import { PlatformNavigator } from '@/components/PlatformNavigator';
import flatDirectory from '@/directory/flatDirectory.json';
import { DocSearch } from '@docsearch/react';
import '@docsearch/css';
import { PageLastUpdated } from '../PageLastUpdated';
import Feedback from '../Feedback';
import RepoActions from '../Menu/RepoActions';
import { usePathWithoutHash } from '@/utils/usePathWithoutHash';
import { SearchFilters } from '@/components/Search';
import type { GenFilter, PlatformFilter } from '@/components/Search';
export const LayoutHeader = ({
currentPlatform,
isGen1,
pageType = 'inner',
showLastUpdatedDate = true,
showTOC
}: {
currentPlatform: Platform;
isGen1: boolean;
pageType?: 'home' | 'inner';
showLastUpdatedDate: boolean;
showTOC?: boolean;
}) => {
const { menuOpen, toggleMenuOpen } = useContext(LayoutContext);
const menuButtonRef = useRef<HTMLButtonElement>(null);
const sidebarMenuButtonRef = useRef<HTMLButtonElement>(null);
const router = useRouter();
const asPathWithNoHash = usePathWithoutHash();
const [platformFilter, setPlatformFilter] = useState<PlatformFilter>('all');
const [genFilter, setGenFilter] = useState<GenFilter>('gen2');
const searchParams = useMemo(() => ({
...(platformFilter !== 'all' && { optionalFacetFilters: [`platform:${platformFilter}`] }),
...(genFilter !== 'both' && { facetFilters: [`gen:${genFilter}`] })
}), [platformFilter, genFilter]);
const handleMenuToggle = () => {
if (!menuOpen) {
toggleMenuOpen(true);
// For keyboard navigators, move focus to the close menu button in the nav
setTimeout(() => sidebarMenuButtonRef?.current?.focus(), 0);
} else {
toggleMenuOpen(false);
// For keyboard navigators, move focus back to menu button in header
menuButtonRef?.current?.focus();
}
};
// Search result transform function that will strip out the pageMain anchor tag
// Algolia search results include the anchor tag where the content was found but since we
// are aggregating records this ends up always being the pageMain anchor tag which is the
// page's main content section. This adds focus to the main content section on every search
// and creates a funny user experience. Removing this tag will avoid that.
const transformItems = (items) => {
items.map((item) => {
if (item.url.includes('#pageMain')) {
item.url = item.url.replace('#pageMain', '');
}
});
return items;
};
return (
<View as="header" className="layout-header">
<Flex className={`layout-search layout-search--${pageType}`}>
<Button
onClick={() => handleMenuToggle()}
size="small"
ref={menuButtonRef}
className="search-menu-toggle mobile-toggle"
>
<IconMenu aria-hidden="true" />
Menu
</Button>
<View
className={classNames(
'layout-search__search',
`layout-search__search--${pageType}`,
{ 'layout-search__search--toc': showTOC }
)}
>
<View className="layout-search__search__container">
<DocSearch
appId={process.env.ALGOLIA_APP_ID || ALGOLIA_APP_ID}
indexName={process.env.ALGOLIA_INDEX_NAME || ALGOLIA_INDEX_NAME}
apiKey={process.env.ALGOLIA_API_KEY || ALGOLIA_API_KEY}
searchParameters={searchParams}
transformItems={transformItems}
getMissingResultsUrl={({ query }) =>
`https://github.com/aws-amplify/docs/issues/new?title=${encodeURIComponent(`[search] Missing results for: ${query}`)}&labels=v2`
}
/>
<SearchFilters
platformFilter={platformFilter}
genFilter={genFilter}
onPlatformChange={setPlatformFilter}
onGenChange={setGenFilter}
/>
</View>
</View>
</Flex>
<View
className={classNames('layout-sidebar', {
'layout-sidebar--expanded': menuOpen
})}
>
<View
className={classNames('layout-sidebar__backdrop', {
'layout-sidebar__backdrop--expanded': menuOpen
})}
onClick={() => toggleMenuOpen(false)}
></View>
<View
className={classNames('layout-sidebar__inner', {
'layout-sidebar__inner--expanded': menuOpen
})}
>
<Button
size="small"
colorTheme="overlay"
className={classNames('layout-sidebar__mobile-toggle', {
'layout-sidebar__mobile-toggle--open': menuOpen
})}
ref={sidebarMenuButtonRef}
onClick={() => handleMenuToggle()}
>
<IconDoubleChevron />
<VisuallyHidden>Close menu</VisuallyHidden>
</Button>
<div className="layout-sidebar-platform">
<PlatformNavigator
currentPlatform={currentPlatform}
isGen1={isGen1}
/>
</div>
<div className="layout-sidebar-menu">
<Menu currentPlatform={currentPlatform} path={asPathWithNoHash} />
<div className="layout-sidebar-feedback">
<RepoActions router={router}></RepoActions>
<Feedback router={router}></Feedback>
</div>
{showLastUpdatedDate && (
<PageLastUpdated directoryData={flatDirectory[router.pathname]} />
)}
</div>
</View>
</View>
</View>
);
};