-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
269 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
import { styled } from '@/stitches.config'; | ||
import { setLocalStorage } from '@/utils/LocalStorage'; | ||
import { useKindeAuth } from '@kinde-oss/kinde-auth-react'; | ||
import { useNavigate } from '@tanstack/react-router'; | ||
import { motion, useMotionValueEvent, useScroll } from 'framer-motion'; | ||
import { Globe, MapPin, Search } from 'lucide-react'; | ||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
import { AirBnbIcon, HumBurgerIcon } from '../assets'; | ||
import { useOutsideClick } from '../hooks'; | ||
import { Button } from './Button'; | ||
import { Row } from './Common'; | ||
import { Divider } from './Divider'; | ||
import UserMenu from './UserMenu'; | ||
|
||
const locations = [ | ||
'Banglore', | ||
'London', | ||
'Paris', | ||
'New York', | ||
'San Francisco', | ||
'Tokyo', | ||
]; | ||
|
||
function HeaderAnimate() { | ||
const navigate = useNavigate(); | ||
const { getToken, isAuthenticated, isLoading, login, user } = useKindeAuth(); | ||
const [showUserMenu, setShowUserMenu] = useState(false); | ||
const menuRef = useRef<HTMLDivElement>(null); | ||
useOutsideClick(menuRef, () => setShowUserMenu(false)); | ||
|
||
const [selectLocation, setSelectLocation] = | ||
useState<(typeof locations)[number]>(''); | ||
const [showLocationOtions, setShowLocationOtions] = useState(false); | ||
|
||
// console.log(user && getPermissions().permissions); | ||
|
||
useEffect(() => { | ||
if (!isAuthenticated) { | ||
localStorage.removeItem('access_token'); | ||
localStorage.removeItem('user'); | ||
return; | ||
} | ||
getToken().then((token) => { | ||
if (token) { | ||
setLocalStorage('access_token', token); | ||
setLocalStorage('user', user); | ||
} | ||
}); | ||
}, [getToken, isAuthenticated, user]); | ||
|
||
const handleSwitchToHosting = () => { | ||
navigate({ to: '/hosting' }); | ||
}; | ||
|
||
const [isHidden, setIsHidden] = useState(false); | ||
const { scrollY } = useScroll(); | ||
const lastRef = useRef(0); | ||
|
||
useMotionValueEvent(scrollY, 'change', (y) => { | ||
const difference = y - lastRef.current; | ||
if (Math.abs(difference) > 50) { | ||
setIsHidden(difference > 0); | ||
lastRef.current = y; | ||
} | ||
}); | ||
|
||
return ( | ||
<motion.div | ||
style={{ | ||
backgroundColor: 'white', | ||
maxWidth: '74rem', | ||
paddingBottom: '1rem', | ||
paddingTop: '1rem', | ||
position: 'fixed', | ||
top: '0', | ||
width: '100%', | ||
zIndex: '10', | ||
}} | ||
variants={{ | ||
hidden: { y: '-100%' }, | ||
visible: { y: '0%' }, | ||
}} | ||
animate={isHidden ? 'hidden' : 'visible'} | ||
onFocusCapture={() => setIsHidden(false)} | ||
transition={{ duration: 0.3 }} | ||
whileHover="visible" | ||
> | ||
<HeaderWrapper> | ||
<span | ||
onClick={() => navigate({ to: '/' })} | ||
style={{ cursor: 'pointer' }} | ||
> | ||
<AirBnbIcon /> | ||
</span> | ||
<Button | ||
color="outline" | ||
shadow="1" | ||
style={{ marginLeft: '4rem', position: 'relative' }} | ||
> | ||
<Button | ||
style={{ | ||
cursor: 'pointer', | ||
fontWeight: selectLocation ? 'bold' : 'normal', | ||
}} | ||
color="text" | ||
onClick={() => setShowLocationOtions(!showLocationOtions)} | ||
> | ||
{selectLocation || 'Anywhere'} | ||
</Button> | ||
{showLocationOtions && ( | ||
<div | ||
style={{ | ||
backgroundColor: 'white', | ||
border: '1px solid #eee', | ||
borderRadius: '5px', | ||
left: '0', | ||
position: 'absolute', | ||
top: '3.5rem', | ||
zIndex: '1', | ||
}} | ||
> | ||
{locations.map((location) => ( | ||
<Row | ||
style={{ | ||
alignItems: 'center', | ||
cursor: 'pointer', | ||
justifyContent: 'flex-start', | ||
paddingLeft: '1rem', | ||
}} | ||
> | ||
<MapPin size={15} strokeWidth={2.6} /> | ||
<Button | ||
onClick={() => { | ||
setSelectLocation(location); | ||
setShowLocationOtions(false); | ||
}} | ||
color="text" | ||
key={location} | ||
> | ||
{location} | ||
</Button> | ||
</Row> | ||
))} | ||
</div> | ||
)} | ||
<Divider /> | ||
<Button color="text">Any week</Button> | ||
<Divider /> | ||
<Button color="textSecondary">Add guest</Button> | ||
<Button color="primary"> | ||
Search <Search size={14} strokeWidth={2.6} /> | ||
</Button> | ||
</Button> | ||
|
||
<GroupWrapper> | ||
<Button | ||
color={'lightText'} | ||
onClick={() => handleSwitchToHosting()} | ||
size={'m'} | ||
style={{ fontWeight: '600' }} | ||
> | ||
Switch to hosting | ||
</Button> | ||
<Globe size={20} /> | ||
{isAuthenticated && !isLoading && user && ( | ||
<Button | ||
color={'outline'} | ||
gap="s" | ||
onClick={() => setShowUserMenu(!showUserMenu)} | ||
onMouseEnter={() => setShowUserMenu(true)} | ||
round={'l'} | ||
size={'s'} | ||
> | ||
<HumBurgerIcon /> | ||
<RoundIcon>{user?.given_name?.charAt(0)}</RoundIcon> | ||
</Button> | ||
)} | ||
{showUserMenu && <UserMenu menuRef={menuRef} />} | ||
{!user && !isLoading && ( | ||
<Button onClick={() => login({})}>Login</Button> | ||
)} | ||
</GroupWrapper> | ||
</HeaderWrapper> | ||
</motion.div> | ||
); | ||
} | ||
|
||
export default HeaderAnimate; | ||
|
||
const HeaderWrapper = styled('div', { | ||
alignItems: 'center', | ||
display: 'flex', | ||
justifyContent: 'space-between', | ||
margin: '0rem 2rem', | ||
}); | ||
const RoundIcon = styled('span', { | ||
alignItems: 'center', | ||
backgroundColor: 'black', | ||
borderRadius: '100%', | ||
color: 'white', | ||
cursor: 'pointer', | ||
display: 'flex', | ||
fontSize: '10px', | ||
fontWeight: '500', | ||
height: '16px', | ||
justifyContent: 'center', | ||
padding: '0.5rem', | ||
width: '16px', | ||
}); | ||
|
||
const GroupWrapper = styled('span', { | ||
alignItems: 'center', | ||
display: 'inline-flex', | ||
gap: '1rem', | ||
justifyContent: 'center', | ||
position: 'relative', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters