Remove image from session, add /api/me, update Avatar component (#493)
* Remove image from session, add /api/me, update Avatar component * Fixed Avatar on Desktop * Added emailMd5 to session + load md5 from props on /settings/profile * Explicitly select which fields to expose in /api/me * Abstracted Gravatar, defaults avatar for /api/me, removed session dep from UserDropdown * Delete md5.js :) Co-authored-by: Bailey Pumfleet <[email protected]>
This commit is contained in:
1 parent
3894ee12d1
commit
bc97237858
13 files changed
+193
-291
No files matched your search
+17
-32
@@ -1,37 +1,22 @@
|
||||
import { useState } from "react";
|
||||
import md5 from "../lib/md5";
|
||||
import * as AvatarPrimitive from "@radix-ui/react-avatar";
|
||||
import { defaultAvatarSrc } from "@lib/profile";
|
||||
|
||||
export default function Avatar({
|
||||
user,
|
||||
className = "",
|
||||
fallback,
|
||||
imageSrc = "",
|
||||
}: {
|
||||
user: any;
|
||||
export type AvatarProps = {
|
||||
className?: string;
|
||||
fallback?: JSX.Element;
|
||||
imageSrc?: string;
|
||||
}) {
|
||||
const [gravatarAvailable, setGravatarAvailable] = useState(true);
|
||||
displayName: string;
|
||||
gravatarFallbackMd5?: string;
|
||||
};
|
||||
|
||||
if (imageSrc) {
|
||||
return <img src={imageSrc} alt="Avatar" className={className} />;
|
||||
}
|
||||
|
||||
if (user.avatar) {
|
||||
return <img src={user.avatar} alt="Avatar" className={className} />;
|
||||
}
|
||||
|
||||
if (gravatarAvailable) {
|
||||
return (
|
||||
<img
|
||||
onError={() => setGravatarAvailable(false)}
|
||||
src={`https://www.gravatar.com/avatar/${md5(user.email)}?s=160&d=identicon&r=PG`}
|
||||
alt="Avatar"
|
||||
className={className}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return fallback || null;
|
||||
export default function Avatar({ imageSrc, displayName, gravatarFallbackMd5, className = "" }: AvatarProps) {
|
||||
return (
|
||||
<AvatarPrimitive.Root>
|
||||
<AvatarPrimitive.Image src={imageSrc} alt={displayName} className={className} />
|
||||
<AvatarPrimitive.Fallback delayMs={600}>
|
||||
{gravatarFallbackMd5 && (
|
||||
<img src={defaultAvatarSrc({ md5: gravatarFallbackMd5 })} alt={displayName} className={className} />
|
||||
)}
|
||||
</AvatarPrimitive.Fallback>
|
||||
</AvatarPrimitive.Root>
|
||||
);
|
||||
}
|
||||
+58
-50
@@ -1,7 +1,7 @@
|
||||
import Link from "next/link";
|
||||
import React, { Fragment, useEffect } from "react";
|
||||
import React, { Fragment, useEffect, useState } from "react";
|
||||
import { useRouter } from "next/router";
|
||||
import { useSession } from "next-auth/client";
|
||||
import { signOut, useSession } from "next-auth/client";
|
||||
import { Menu, Transition } from "@headlessui/react";
|
||||
import { collectPageParameters, telemetryEventTypes, useTelemetry } from "../lib/telemetry";
|
||||
import { SelectorIcon } from "@heroicons/react/outline";
|
||||
@@ -18,6 +18,8 @@ import {
|
||||
import Logo from "./Logo";
|
||||
import classNames from "@lib/classNames";
|
||||
import { Toaster } from "react-hot-toast";
|
||||
import Avatar from "@components/Avatar";
|
||||
import { User } from "@prisma/client";
|
||||
|
||||
export default function Shell(props) {
|
||||
const router = useRouter();
|
||||
@@ -112,7 +114,7 @@ export default function Shell(props) {
|
||||
</nav>
|
||||
</div>
|
||||
<div className="flex-shrink-0 flex p-4">
|
||||
<UserDropdown session={session} />
|
||||
<UserDropdown />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -191,45 +193,52 @@ export default function Shell(props) {
|
||||
) : null;
|
||||
}
|
||||
|
||||
function UserDropdown({ session, small, bottom }: { session: any; small?: boolean; bottom?: boolean }) {
|
||||
function UserDropdown({ small, bottom }: { small?: boolean; bottom?: boolean }) {
|
||||
const [user, setUser] = useState<User | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
fetch("/api/me")
|
||||
.then((res) => res.json())
|
||||
.then((responseBody) => {
|
||||
setUser(responseBody.user);
|
||||
});
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Menu as="div" className="w-full relative inline-block text-left">
|
||||
{({ open }) => (
|
||||
<>
|
||||
<div>
|
||||
<Menu.Button className="group w-full rounded-md text-sm text-left font-medium text-gray-700 focus:outline-none">
|
||||
<span className="flex w-full justify-between items-center">
|
||||
<span className="flex min-w-0 items-center justify-between space-x-3">
|
||||
<img
|
||||
className={classNames(
|
||||
small ? "w-8 h-8" : "w-10 h-10",
|
||||
"bg-gray-300 rounded-full flex-shrink-0"
|
||||
)}
|
||||
src={
|
||||
session.user.image
|
||||
? session.user.image
|
||||
: "https://eu.ui-avatars.com/api/?background=fff&color=039be5&name=" +
|
||||
encodeURIComponent(session.user.name || "")
|
||||
}
|
||||
alt=""
|
||||
/>
|
||||
{!small && (
|
||||
<span className="flex-1 flex flex-col min-w-0">
|
||||
<span className="text-gray-900 text-sm font-medium truncate">{session.user.name}</span>
|
||||
<span className="text-neutral-500 font-normal text-sm truncate">
|
||||
/{session.user.username}
|
||||
{user && (
|
||||
<Menu.Button className="group w-full rounded-md text-sm text-left font-medium text-gray-700 focus:outline-none">
|
||||
<span className="flex w-full justify-between items-center">
|
||||
<span className="flex min-w-0 items-center justify-between space-x-3">
|
||||
<Avatar
|
||||
imageSrc={user?.avatar}
|
||||
displayName={user?.name}
|
||||
className={classNames(
|
||||
small ? "w-8 h-8" : "w-10 h-10",
|
||||
"bg-gray-300 rounded-full flex-shrink-0"
|
||||
)}
|
||||
/>
|
||||
{!small && (
|
||||
<span className="flex-1 flex flex-col min-w-0">
|
||||
<span className="text-gray-900 text-sm font-medium truncate">{user?.name}</span>
|
||||
<span className="text-neutral-500 font-normal text-sm truncate">
|
||||
/{user?.username}
|
||||
</span>
|
||||
</span>
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
{!small && (
|
||||
<SelectorIcon
|
||||
className="flex-shrink-0 h-5 w-5 text-gray-400 group-hover:text-gray-500"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
)}
|
||||
</span>
|
||||
{!small && (
|
||||
<SelectorIcon
|
||||
className="flex-shrink-0 h-5 w-5 text-gray-400 group-hover:text-gray-500"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
)}
|
||||
</span>
|
||||
</Menu.Button>
|
||||
</Menu.Button>
|
||||
)}
|
||||
</div>
|
||||
<Transition
|
||||
show={open}
|
||||
@@ -247,7 +256,7 @@ function UserDropdown({ session, small, bottom }: { session: any; small?: boolea
|
||||
"w-64 z-10 absolute mt-1 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5 divide-y divide-gray-200 focus:outline-none"
|
||||
)}>
|
||||
<div className="py-1">
|
||||
<a href={"/" + session.user.username} className="flex px-4 py-2 text-sm text-neutral-500">
|
||||
<a href={"/" + user?.username} className="flex px-4 py-2 text-sm text-neutral-500">
|
||||
View public page <ExternalLinkIcon className="ml-1 mt-1 w-3 h-3 text-neutral-400" />
|
||||
</a>
|
||||
</div>
|
||||
@@ -311,22 +320,21 @@ function UserDropdown({ session, small, bottom }: { session: any; small?: boolea
|
||||
<div className="py-1">
|
||||
<Menu.Item>
|
||||
{({ active }) => (
|
||||
<Link href="/auth/logout">
|
||||
<a
|
||||
<a
|
||||
onClick={() => signOut({ callbackUrl: "/auth/logout" })}
|
||||
className={classNames(
|
||||
active ? "bg-gray-100 text-gray-900" : "text-gray-700",
|
||||
"flex px-4 py-2 text-sm font-medium"
|
||||
)}>
|
||||
<LogoutIcon
|
||||
className={classNames(
|
||||
active ? "bg-gray-100 text-gray-900" : "text-gray-700",
|
||||
"flex px-4 py-2 text-sm font-medium"
|
||||
)}>
|
||||
<LogoutIcon
|
||||
className={classNames(
|
||||
"text-neutral-400 group-hover:text-neutral-500",
|
||||
"mr-2 flex-shrink-0 h-5 w-5"
|
||||
)}
|
||||
aria-hidden="true"
|
||||
/>
|
||||
Sign out
|
||||
</a>
|
||||
</Link>
|
||||
"text-neutral-400 group-hover:text-neutral-500",
|
||||
"mr-2 flex-shrink-0 h-5 w-5"
|
||||
)}
|
||||
aria-hidden="true"
|
||||
/>
|
||||
Sign out
|
||||
</a>
|
||||
)}
|
||||
</Menu.Item>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user