Skip to content

Commit

Permalink
✨ Use profile image from oauth provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevella committed Sep 7, 2024
1 parent 13a1c36 commit 4f5fc2b
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 29 deletions.
4 changes: 1 addition & 3 deletions apps/web/declarations/next-auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ declare module "next-auth" {
interface Session {
user: {
id: string;
name?: string | null;
email?: string | null;
timeZone?: string | null;
timeFormat?: TimeFormat | null;
locale?: string | null;
weekStart?: number | null;
};
} & DefaultSession["user"];
}

interface User extends DefaultUser {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/app/[locale]/(admin)/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import {
import Link from "next/link";
import { usePathname } from "next/navigation";

import { CurrentUserAvatar } from "@/components/current-user-avatar";
import { ProBadge } from "@/components/pro-badge";
import { Trans } from "@/components/trans";
import { CurrentUserAvatar } from "@/components/user";
import { IfGuest, useUser } from "@/components/user-provider";
import { IfFreeUser } from "@/contexts/plan";
import { IconComponent } from "@/types";
Expand Down
14 changes: 14 additions & 0 deletions apps/web/src/components/current-user-avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use client";
import { Avatar, AvatarFallback, AvatarImage } from "@rallly/ui/avatar";

import { useUser } from "@/components/user-provider";

export const CurrentUserAvatar = ({ className }: { className?: string }) => {
const { user } = useUser();
return (
<Avatar className={className}>
<AvatarImage src={user.image ?? undefined} />
<AvatarFallback>{user.name[0]}</AvatarFallback>
</Avatar>
);
};
8 changes: 3 additions & 5 deletions apps/web/src/components/settings/profile-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
import { Input } from "@rallly/ui/input";
import { useForm } from "react-hook-form";

import { CurrentUserAvatar } from "@/components/current-user-avatar";
import { Trans } from "@/components/trans";
import { UserAvatar } from "@/components/user";
import { useUser } from "@/components/user-provider";

export const ProfileSettings = () => {
Expand All @@ -26,9 +26,7 @@ export const ProfileSettings = () => {
},
});

const { control, watch, handleSubmit, formState, reset } = form;

const watchName = watch("name");
const { control, handleSubmit, formState, reset } = form;

return (
<div className="grid gap-y-4">
Expand All @@ -41,7 +39,7 @@ export const ProfileSettings = () => {
>
<div className="flex flex-col gap-y-4">
<div>
<UserAvatar name={watchName} size="lg" />
<CurrentUserAvatar className="size-14" />
</div>
<FormField
control={control}
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/components/user-dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import {
} from "lucide-react";
import Link from "next/link";

import { CurrentUserAvatar } from "@/components/current-user-avatar";
import { LoginLink } from "@/components/login-link";
import { RegisterLink } from "@/components/register-link";
import { Trans } from "@/components/trans";
import { CurrentUserAvatar } from "@/components/user";
import { IfCloudHosted, IfSelfHosted } from "@/contexts/environment";
import { Plan, usePlan } from "@/contexts/plan";
import { isFeedbackEnabled } from "@/utils/constants";
Expand Down Expand Up @@ -57,7 +57,7 @@ export const UserDropdown = ({ className }: { className?: string }) => {
className={cn("group min-w-0", className)}
>
<Button variant="ghost">
<CurrentUserAvatar size="xs" className="shrink-0 " />
<CurrentUserAvatar className="size-6" />
<span className="truncate">{user.name}</span>
<Icon>
<ChevronDownIcon />
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/components/user-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const userSchema = z.object({
timeZone: z.string().nullish(),
timeFormat: z.enum(["hours12", "hours24"]).nullish(),
weekStart: z.number().min(0).max(6).nullish(),
image: z.string().nullish(),
});

export const UserContext = React.createContext<{
Expand Down Expand Up @@ -76,9 +77,10 @@ export const UserProvider = (props: { children?: React.ReactNode }) => {
id: user.id as string,
name: user.name ?? t("guest"),
email: user.email || null,
isGuest: !user.email,
isGuest,
tier,
timeZone: user.timeZone ?? null,
image: user.image ?? null,
},
refresh: session.update,
ownsObject: ({ userId }) => {
Expand Down
16 changes: 0 additions & 16 deletions apps/web/src/components/user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,8 @@ import { cn } from "@rallly/ui";
import { Icon } from "@rallly/ui/icon";
import { User2Icon } from "lucide-react";

import { useUser } from "@/components/user-provider";
import { getRandomAvatarColor } from "@/utils/color-hash";

export const CurrentUserAvatar = ({
size = "md",
className,
}: Omit<UserAvatarProps, "name">) => {
const { user } = useUser();

return (
<UserAvatar
className={className}
name={user.isGuest ? undefined : user.name}
size={size}
/>
);
};

interface UserAvatarProps {
name?: string;
size?: "xs" | "sm" | "md" | "lg";
Expand Down
4 changes: 4 additions & 0 deletions apps/web/src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const providers: Provider[] = [
locale: true,
timeFormat: true,
timeZone: true,
image: true,
},
});

Expand Down Expand Up @@ -265,6 +266,7 @@ const getAuthOptions = (...args: GetServerSessionParams) =>
timeZone: session.timeZone,
weekStart: session.weekStart,
name: session.name,
image: session.image,
},
});
} catch (e) {
Expand All @@ -278,6 +280,7 @@ const getAuthOptions = (...args: GetServerSessionParams) =>
token.timeFormat = user.timeFormat;
token.timeZone = user.timeZone;
token.weekStart = user.weekStart;
token.picture = user.image;
}
return token;
},
Expand All @@ -288,6 +291,7 @@ const getAuthOptions = (...args: GetServerSessionParams) =>
session.user.timeZone = token.timeZone;
session.user.locale = token.locale;
session.user.weekStart = token.weekStart;
session.user.image = token.picture;
return session;
},
},
Expand Down
1 change: 1 addition & 0 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"dependencies": {
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-avatar": "^1.1.0",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-dialog": "^1.0.4",
"@radix-ui/react-dropdown-menu": "^2.0.4",
Expand Down
50 changes: 50 additions & 0 deletions packages/ui/src/avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"use client";

import * as React from "react";
import * as AvatarPrimitive from "@radix-ui/react-avatar";

import { cn } from "@rallly/ui";

const Avatar = React.forwardRef<
React.ElementRef<typeof AvatarPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root>
>(({ className, ...props }, ref) => (
<AvatarPrimitive.Root
ref={ref}
className={cn(
"relative flex size-10 shrink-0 overflow-hidden rounded-full",
className,
)}
{...props}
/>
));
Avatar.displayName = AvatarPrimitive.Root.displayName;

const AvatarImage = React.forwardRef<
React.ElementRef<typeof AvatarPrimitive.Image>,
React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Image>
>(({ className, ...props }, ref) => (
<AvatarPrimitive.Image
ref={ref}
className={cn("aspect-square h-full w-full", className)}
{...props}
/>
));
AvatarImage.displayName = AvatarPrimitive.Image.displayName;

const AvatarFallback = React.forwardRef<
React.ElementRef<typeof AvatarPrimitive.Fallback>,
React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback>
>(({ className, ...props }, ref) => (
<AvatarPrimitive.Fallback
ref={ref}
className={cn(
"bg-muted flex h-full w-full items-center justify-center rounded-full",
className,
)}
{...props}
/>
));
AvatarFallback.displayName = AvatarPrimitive.Fallback.displayName;

export { Avatar, AvatarImage, AvatarFallback };
2 changes: 1 addition & 1 deletion packages/ui/src/command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as React from "react";

import { Dialog, DialogContent } from "./dialog";
import { cn } from "./lib/utils";
import { Icon } from "@rallly/ui/icon";
import { Icon } from "./icon";

const Command = React.forwardRef<
React.ElementRef<typeof CommandPrimitive>,
Expand Down
5 changes: 5 additions & 0 deletions packages/ui/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"extends": "@rallly/tsconfig/next.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/utils": ["src/lib/utils.ts"],
"@/components/*": ["src/*"],
"@/ui/*": ["src/*"],
},
},
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"],
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2872,6 +2872,16 @@
"@babel/runtime" "^7.13.10"
"@radix-ui/react-primitive" "1.0.3"

"@radix-ui/react-avatar@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@radix-ui/react-avatar/-/react-avatar-1.1.0.tgz#457c81334c93f4608df15f081e7baa286558d6a2"
integrity sha512-Q/PbuSMk/vyAd/UoIShVGZ7StHHeRFYU7wXmi5GV+8cLXflZAEpHL/F697H1klrzxKXNtZ97vWiC0q3RKUH8UA==
dependencies:
"@radix-ui/react-context" "1.1.0"
"@radix-ui/react-primitive" "2.0.0"
"@radix-ui/react-use-callback-ref" "1.1.0"
"@radix-ui/react-use-layout-effect" "1.1.0"

"@radix-ui/react-checkbox@^1.0.4":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@radix-ui/react-checkbox/-/react-checkbox-1.0.4.tgz#98f22c38d5010dd6df4c5744cac74087e3275f4b"
Expand Down

0 comments on commit 4f5fc2b

Please sign in to comment.