Skip to content

Commit

Permalink
feat: header logged in state (#11124)
Browse files Browse the repository at this point in the history
* feat: add header logged in state

* fix merge conflict

* use img instead of next/image

* fixe conflicts
  • Loading branch information
a-hariti committed Aug 28, 2024
1 parent b6e1a71 commit f4b5bca
Showing 1 changed file with 58 additions and 7 deletions.
65 changes: 58 additions & 7 deletions src/components/header.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use client';

import {Fragment, useEffect, useState} from 'react';
import {HamburgerMenuIcon} from '@radix-ui/react-icons';
import Image from 'next/image';
import Link from 'next/link';

import SentryLogoSVG from 'sentry-docs/logos/sentry-logo-dark.svg';

Expand All @@ -18,7 +22,48 @@ type Props = {
noSearch?: boolean;
};

type User = {
avatarUrl: string;
email: string;
id: string;
isActive: boolean;
name: string;
username: string;
};

function Avatar({user}: {user: User}) {
return (
<Link href="https://sentry.io" className="">
<div className="flex items-center space-x-2 px-4">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={user.avatarUrl}
alt={user.name}
width={32}
height={32}
className="rounded-full h-8 w-8"
/>
<div className="flex flex-col">
<span className="font-medium text-sm text-primary">{user.name}</span>
<span className="text-xs text-gray-500">{user.username}</span>
</div>
</div>
</Link>
);
}

export function Header({pathname, searchPlatforms, noSearch}: Props) {
// this should work on production (given the request is not blocked by ad blockers),
// but it doesn't on dev or previews because of CORS
// I implemeted a workaround in the form of an api route while building this
const [user, setUser] = useState<User | null>(null);
useEffect(() => {
fetch('https://sentry.io/api/0/users/me/', {credentials: 'include'})
.then(r => r.json())
.then(setUser)
// eslint-disable-next-line no-console
.catch(console.error);
}, []);
return (
<header className="bg-[var(--gray-1)] h-[var(--header-height)] w-full z-50 border-b border-[var(--gray-a3)] fixed top-0">
{/* define a header-height variable for consumption by other components */}
Expand Down Expand Up @@ -64,13 +109,19 @@ export function Header({pathname, searchPlatforms, noSearch}: Props) {
<div className="hidden lg-xl:flex justify-end flex-1 space-x-2 items-center">
<NavLink href="https://sentry.io/changelog/">Changelog</NavLink>
<NavLink href="https://try.sentry-demo.com/demo/start/">Sandbox</NavLink>
<NavLink href="https://sentry.io/">Sign In</NavLink>
<NavLink
href="https://sentry.io/signup/"
className="transition-all duration-300 ease-in-out hover:bg-gradient-to-r hover:from-[#fa7faa] hover:via-[#ff9691] hover:to-[#ffb287]"
>
Get Started
</NavLink>
{user ? (
<Avatar user={user} />
) : (
<Fragment>
<NavLink href="https://sentry.io/">Sign In</NavLink>
<NavLink
href="https://sentry.io/signup/"
className="transition-all duration-300 ease-in-out hover:bg-gradient-to-r hover:from-[#fa7faa] hover:via-[#ff9691] hover:to-[#ffb287]"
>
Get Started
</NavLink>
</Fragment>
)}
<ThemeToggle />
</div>
<div className="lg-xl:hidden ml-auto">
Expand Down

0 comments on commit f4b5bca

Please sign in to comment.