-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathclient.tsx
More file actions
130 lines (118 loc) · 3.16 KB
/
Copy pathclient.tsx
File metadata and controls
130 lines (118 loc) · 3.16 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
'use client';
import {
type ComponentPropsWithoutRef,
createContext,
forwardRef,
type PropsWithChildren,
type ReactNode,
type Ref,
useContext,
} from 'react';
import { HeaderSection } from '@/vibes/soul/sections/header-section';
type HeaderSectionProps = ComponentPropsWithoutRef<typeof HeaderSection>;
type NavigationProps = HeaderSectionProps['navigation'];
// MakeswiftHeader does not support streamable links
type ContextProps = Omit<HeaderSectionProps, 'navigation'> & {
navigation: Omit<NavigationProps, 'links'> & {
links: Awaited<NavigationProps['links']>;
};
};
const PropsContext = createContext<ContextProps>({
navigation: {
giftCertificatesHref: '',
accountHref: '',
cartHref: '',
searchHref: '',
links: [],
},
});
export const PropsContextProvider = ({
value,
children,
}: PropsWithChildren<{ value: ContextProps }>) => (
<PropsContext.Provider value={value}>{children}</PropsContext.Provider>
);
interface ImageProps {
src?: string;
alt: string;
width: number;
height: number;
}
interface Props {
banner: {
id: string;
show: boolean;
allowClose: boolean;
children?: ReactNode;
};
links: Array<{
label: string;
link: { href: string };
groups: Array<{
label: string;
link: { href: string };
links: Array<{
label: string;
link: { href: string };
}>;
}>;
}>;
logo: {
desktop: ImageProps;
mobile: ImageProps;
link?: { href: string };
};
linksPosition: 'center' | 'left' | 'right';
}
function combineLinks(
passedLinks: Awaited<NavigationProps['links']>,
links: Props['links'],
): ContextProps['navigation']['links'] {
return [
...passedLinks,
...links.map(({ label, link, groups }) => ({
label,
href: link.href,
groups: groups.map((group) => ({
label: group.label,
href: group.link.href,
links: group.links.map((item) => ({ label: item.label, href: item.link.href })),
})),
})),
];
}
export const MakeswiftHeader = forwardRef(
({ banner, links, logo, linksPosition }: Props, ref: Ref<HTMLDivElement>) => {
const { navigation: passedProps, banner: passedBanner } = useContext(PropsContext);
const combinedBanner = banner.show
? {
...passedBanner,
id: banner.id,
hideDismiss: !banner.allowClose,
children: banner.children ?? passedBanner?.children,
}
: undefined;
return (
<HeaderSection
banner={combinedBanner}
navigation={{
...passedProps,
links: combineLinks(passedProps.links, links),
logo: logo.desktop.src
? { src: logo.desktop.src, alt: logo.desktop.alt }
: passedProps.logo,
logoWidth: logo.desktop.width,
logoHeight: logo.desktop.height,
mobileLogo: logo.mobile.src
? { src: logo.mobile.src, alt: logo.mobile.alt }
: passedProps.mobileLogo,
mobileLogoWidth: logo.mobile.width,
mobileLogoHeight: logo.mobile.height,
linksPosition,
logoHref: logo.link?.href ?? passedProps.logoHref,
}}
ref={ref}
/>
);
},
);