-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathMainLayoutHeader.tsx
More file actions
132 lines (123 loc) · 4.17 KB
/
MainLayoutHeader.tsx
File metadata and controls
132 lines (123 loc) · 4.17 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
import classNames from 'classnames';
import type { ReactElement, ReactNode } from 'react';
import React, { useCallback } from 'react';
import dynamic from 'next/dynamic';
import HeaderLogo from './HeaderLogo';
import { useViewSize, ViewSize } from '../../hooks';
import { useReadingStreak } from '../../hooks/streaks';
import { LogoPosition } from '../Logo';
import { useFeatureTheme } from '../../hooks/utils/useFeatureTheme';
import { useScrollTopClassName } from '../../hooks/useScrollTopClassName';
import { useSettingsContext } from '../../contexts/SettingsContext';
import { useActiveFeedNameContext } from '../../contexts';
import { useFeedName } from '../../hooks/feed/useFeedName';
import FeedNav from '../feeds/FeedNav';
import { MobileExploreHeader } from '../header/MobileExploreHeader';
import useActiveNav from '../../hooks/useActiveNav';
export interface MainLayoutHeaderProps {
hasBanner?: boolean;
sidebarRendered?: boolean;
additionalButtons?: ReactNode;
onLogoClick?: (e: React.MouseEvent) => unknown;
hideSearchField?: boolean;
onSignupClick?: () => boolean | void;
}
const SearchPanel = dynamic(
() =>
import(
/* webpackChunkName: "searchPanel" */ '../search/SearchPanel/SearchPanel'
),
);
const HeaderButtons = dynamic(
() => import(/* webpackChunkName: "headerButtons" */ './HeaderButtons'),
{ ssr: false },
);
function MainLayoutHeader({
hasBanner,
sidebarRendered,
additionalButtons,
onLogoClick,
hideSearchField,
onSignupClick,
}: MainLayoutHeaderProps): ReactElement {
const { loadedSettings } = useSettingsContext();
const { streak, isStreaksEnabled } = useReadingStreak();
const isStreakLarge = streak?.current > 99; // if we exceed 100, we need to display it differently in the UI
const { feedName } = useActiveFeedNameContext();
const { isAnyExplore, isSearch } = useFeedName({
feedName,
});
const isLaptop = useViewSize(ViewSize.Laptop);
const isSearchPage = isSearch || isAnyExplore;
const featureTheme = useFeatureTheme();
const scrollClassName = useScrollTopClassName({ enabled: !!featureTheme });
const { profile } = useActiveNav(feedName);
const isMobileProfile = profile && !isLaptop;
const RenderSearchPanel = useCallback(
() =>
!hideSearchField &&
loadedSettings && (
<SearchPanel
className={{
container: classNames(
'left-0 top-0 z-header items-center py-3 tablet:left-16 laptop:left-0',
isSearchPage
? 'relative right-0 tablet:!left-0 laptop:top-0'
: 'hidden laptop:flex',
hasBanner && 'tablet:top-18',
),
field: 'mx-2 laptop:mx-auto',
}}
/>
),
[loadedSettings, isSearchPage, hasBanner, hideSearchField],
);
if (loadedSettings && !isLaptop) {
if (isSearchPage) {
return (
<div className="sticky top-0 z-header w-full bg-background-default tablet:pl-16">
<RenderSearchPanel />
{!isSearch && <MobileExploreHeader path={feedName as string} />}
</div>
);
}
return (
<>
<FeedNav />
</>
);
}
return (
<header
className={classNames(
'fixed top-0 z-header h-14 flex-row content-center items-center justify-center gap-3 border-b border-border-subtlest-tertiary bg-background-default px-4 py-3 tablet:px-8 laptop:left-0 laptop:h-16 laptop:w-full laptop:px-4',
isMobileProfile ? 'hidden laptop:flex' : 'flex',
hasBanner && 'laptop:top-8',
isSearchPage && 'mb-16 laptop:mb-0',
scrollClassName,
)}
style={featureTheme ? featureTheme.navbar : undefined}
>
{sidebarRendered !== undefined && (
<>
<div>
<HeaderLogo
position={
isStreaksEnabled && isStreakLarge
? LogoPosition.Relative
: LogoPosition.Absolute
}
onLogoClick={onLogoClick}
/>
</div>
<RenderSearchPanel />
<HeaderButtons
additionalButtons={additionalButtons}
onSignupClick={onSignupClick}
/>
</>
)}
</header>
);
}
export default MainLayoutHeader;