8000 refactor: replace and remove deprecated Avatar component by BrunoQuaresma · Pull Request #15930 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

refactor: replace and remove deprecated Avatar component #15930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
dbf1eb9
Add avatarLetters utility
BrunoQuaresma Dec 17, 2024
8f24c9e
Fix avatar for non squared icon
BrunoQuaresma Dec 17, 2024
e689d42
Replace avatar o template menu
BrunoQuaresma Dec 17, 2024
6f8c280
Move UserAvatar to modules/users
BrunoQuaresma Dec 17, 2024
e72cecb
Only use one letter on fallback to simplify changes
BrunoQuaresma Dec 17, 2024
758f240
Refactor UserAvatar to user the new Avatar component
BrunoQuaresma Dec 17, 2024
22c241c
Move GroupAvatar to modules/groups
BrunoQuaresma Dec 17, 2024
8ecbfc5
Update GroupAvatar to use latest Avatar component
BrunoQuaresma Dec 17, 2024
081bb47
Move BuildAvatar to modules/builds
BrunoQuaresma Dec 17, 2024
83ca623
Update BuildAvatar to use latest Avatar component
BrunoQuaresma Dec 17, 2024
dff92f7
Move AvatarCard to components/Avatar
BrunoQuaresma Dec 17, 2024
52ea623
Update AccountUserGroups to use the new Avatar component
BrunoQuaresma Dec 17, 2024
16c194d
Move AvatarData to components/Avatar
BrunoQuaresma Dec 17, 2024
1caeffd
Update AvatarData to use new Avatar component
BrunoQuaresma Dec 17, 2024
4f12e95
Update remaining components
BrunoQuaresma Dec 17, 2024
8000
76c211d
Fix lint issues
BrunoQuaresma Dec 17, 2024
08fee38
Remove deprecated Avatar
BrunoQuaresma Dec 17, 2024
bfd9244
Merge branch 'main' of https://github.com/coder/coder into bq/use-new…
BrunoQuaresma Dec 18, 2024
d6fc56b
Merge branch 'main' of https://github.com/coder/coder into bq/use-new…
BrunoQuaresma Dec 18, 2024
f47ed9d
Align avatar with the timeline vertical line
BrunoQuaresma Dec 18, 2024
7304999
Fix selected template avatar
BrunoQuaresma Dec 18, 2024
9fdb4fd
Adjust components for the new Avatar
BrunoQuaresma Dec 18, 2024
0a0b1cf
Fix a few style inconsistencies
BrunoQuaresma Dec 18, 2024
f079dcd
Merge branch 'main' of https://github.com/coder/coder into bq/use-new…
BrunoQuaresma Dec 19, 2024
dc410a7
Simplify Avatar usage
BrunoQuaresma Dec 19, 2024
37a678d
Fix missed src
BrunoQuaresma Dec 19, 2024
9e03115
Fix remaining issues
BrunoQuaresma Dec 19, 2024
6453ce3
E2E fix + review requests
BrunoQuaresma Dec 19, 2024
bb8e9af
Fix fmt
BrunoQuaresma Dec 19, 2024
417cab7
Fix assertions
BrunoQuaresma Dec 20, 2024
ad61871
Fix locators
BrunoQuaresma Dec 20, 2024
d57833b
Change locator to use .summary
BrunoQuaresma Dec 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions site/src/components/Avatar/Avatar.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Meta, StoryObj } from "@storybook/react";
import { Avatar, AvatarFallback, AvatarImage } from "./Avatar";
import { Avatar } from "./Avatar";

const meta: Meta<typeof Avatar> = {
title: "components/Avatar",
component: Avatar,
args: {
children: <AvatarImage src="https://github.com/kylecarbs.png" />,
src: "https://github.com/kylecarbs.png",
},
};

Expand All @@ -26,48 +26,51 @@ export const IconLgSize: Story = {
args: {
size: "lg",
variant: "icon",
children: (
<AvatarImage src="https://em-content.zobj.net/source/apple/391/billed-cap_1f9e2.png" />
),
src: "https://em-content.zobj.net/source/apple/391/billed-cap_1f9e2.png",
},
};

export const IconDefaultSize: Story = {
args: {
variant: "icon",
children: (
<AvatarImage src="https://em-content.zobj.net/source/apple/391/billed-cap_1f9e2.png" />
),
src: "https://em-content.zobj.net/source/apple/391/billed-cap_1f9e2.png",
},
};

export const IconSmSize: Story = {
args: {
variant: "icon",
size: "sm",
children: (
<AvatarImage src="https://em-content.zobj.net/source/apple/391/billed-cap_1f9e2.png" />
),
src: "https://em-content.zobj.net/source/apple/391/billed-cap_1f9e2.png",
},
};

export const NonSquaredIcon: Story = {
args: {
variant: "icon",
src: "/icon/docker.png",
},
};

export const FallbackLgSize: Story = {
args: {
src: "",
size: "lg",

children: <AvatarFallback>AR</AvatarFallback>,
fallback: "Adriana Rodrigues",
},
};

export const FallbackDefaultSize: Story = {
args: {
children: <AvatarFallback>AR</AvatarFallback>,
src: "",
fallback: "Adriana Rodrigues",
},
};

export const FallbackSmSize: Story = {
args: {
src: "",
size: "sm",
children: <AvatarFallback>AR</AvatarFallback>,
fallback: "Adriana Rodrigues",
},
};
96 changes: 50 additions & 46 deletions site/src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import * as AvatarPrimitive from "@radix-ui/react-avatar";
import { type VariantProps, cva } from "class-variance-authority";
/**
* Copied from shadc/ui on 12/16/2024
* @see {@link https://ui.shadcn.com/docs/components/avatar}
*
* This component was updated to support the variants and match the styles from
* the Figma design:
* @see {@link https://www.figma.com/design/WfqIgsTFXN2BscBSSyXWF8/Coder-kit?node-id=711-383&t=xqxOSUk48GvDsjGK-0}
*
* It was also simplified to make usage easier and reduce boilerplate.
* @see {@link https://github.com/coder/coder/pull/15930#issuecomment-2552292440}
*/

import { useTheme } from "@emotion/react";
import * as AvatarPrimitive from "@radix-ui/react-avatar";
import { type VariantProps, cva } from "class-variance-authority";
import * as React from "react";
import { getExternalImageStylesFromUrl } from "theme/externalImages";
import { cn } from "utils/cn";

const avatarVariants = cva(
"relative flex shrink-0 overflow-hidden rounded border border-solid bg-surface-secondary text-content-secondary",
{
variants: {
size: {
lg: "h-10 w-10 rounded-[6px] text-sm font-medium",
default: "h-6 w-6 text-2xs",
sm: "h-[18px] w-[18px] text-[8px]",
lg: "h-[--avatar-lg] w-[--avatar-lg] rounded-[6px] text-sm font-medium",
default: "h-[--avatar-default] w-[--avatar-default] text-2xs",
sm: "h-[--avatar-sm] w-[--avatar-sm] text-[8px]",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm, do you think default is being used alot more than lg and sm? otherwise I would name this "md"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, I used "default" and "sm" for Button.tsx because I imagined default would be used most of the time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I think about this more, I feel default makes alot more sense for a variant than it does for a size

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I think we can use md instead.

},
variant: {
default: "",
Expand All @@ -32,63 +38,61 @@ const avatarVariants = cva(
{
size: "lg",
variant: "icon",
className: "p-[9px]",
className: "p-2",
},
{
size: "default",
variant: "icon",
className: "p-[3px]",
className: "p-1",
},
{
size: "sm",
variant: "icon",
className: "p-[2px]",
className: "p-[3px]",
},
],
},
);

export interface AvatarProps
extends React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root>,
VariantProps<typeof avatarVariants> {}
export type AvatarProps = AvatarPrimitive.AvatarProps &
VariantProps<typeof avatarVariants> & {
src?: string;
alt?: string;
fallback?: string;
};

const Avatar = React.forwardRef<
React.ElementRef<typeof AvatarPrimitive.Root>,
AvatarProps
>(({ className, size, variant, ...props }, ref) => (
<AvatarPrimitive.Root
ref={ref}
className={cn(avatarVariants({ size, variant, 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;
>(
(
{ className, size, variant, src, alt, fallback, children, ...props },
ref,
) => {
const theme = useTheme();

const AvatarFallback = React.forwardRef<
React.ElementRef<typeof AvatarPrimitive.Fallback>,
React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback>
>(({ className, ...props }, ref) => (
<AvatarPrimitive.Fallback
ref={ref}
className={cn(
"flex h-full w-full items-center justify-center rounded-full",
className,
)}
{...props}
/>
));
AvatarFallback.displayName = AvatarPrimitive.Fallback.displayName;
return (
<AvatarPrimitive.Root
ref={ref}
className={cn(avatarVariants({ size, variant, className }))}
{...props}
>
<AvatarPrimitive.Image
src={src}
className="aspect-square h-full w-full object-contain"
css={getExternalImageStylesFromUrl(theme.externalImages, src)}
alt={alt}
/>
{fallback && (
<AvatarPrimitive.Fallback className="flex h-full w-full items-center justify-center rounded-full">
{fallback.charAt(0).toUpperCase()}
</AvatarPrimitive.Fallback>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a separate PR but it would be nice if we started using 2 letters for the fallback case. For example, the first letter of the first 2 words in display name. For example, if you look at the users page on dev.coder.com there are many duplicated avatars for the letters A and B that it doesn't serve much purpose as a identifier.

Copy link
Collaborator Author
@BrunoQuaresma BrunoQuaresma Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was planning to address this in another PR if necessary. Since we don’t control the name or display name, we can’t enforce them to have at least two words to display two letters. In such cases, we would display only one letter, correct? Since this involves some logic and could slightly impact the design, I left it out of the scope for this PR.

)}
{children}
</AvatarPrimitive.Root>
);
},
);
Avatar.displayName = AvatarPrimitive.Root.displayName;

export { Avatar, AvatarImage, AvatarFallback };
export { Avatar };
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { type CSSObject, useTheme } from "@emotion/react";
import { Avatar } from "components/deprecated/Avatar/Avatar";
import { Avatar } from "components/Avatar/Avatar";
import type { FC, ReactNode } from "react";

type AvatarCardProps = {
header: string;
imgUrl: string;
altText: string;
background?: boolean;

subtitle?: ReactNode;
maxWidth?: number | "none";
};
Expand All @@ -16,7 +14,6 @@ export const AvatarCard: FC<AvatarCardProps> = ({
header,
imgUrl,
altText,
background,
subtitle,
maxWidth = "none",
}) => {
Expand Down Expand Up @@ -72,9 +69,7 @@ export const AvatarCard: FC<AvatarCardProps> = ({
)}
</div>

<Avatar background={background} src={imgUrl} alt={altText} size="md">
{header}
</Avatar>
<Avatar size="lg" src={imgUrl} alt={altText} fallback={header} />
</div>
);
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useTheme } from "@emotion/react";
import { Avatar } from "components/Avatar/Avatar";
import { Stack } from "components/Stack/Stack";
import { Avatar } from "components/deprecated/Avatar/Avatar";
import type { FC, ReactNode } from "react";

export interface AvatarDataProps {
Expand Down Expand Up @@ -29,17 +29,17 @@ export const AvatarData: FC<AvatarDataProps> = ({
const theme = useTheme();
if (!avatar) {
avatar = (
<Avatar background src={src}>
{(typeof title === "string" ? title : imgFallbackText) || "-"}
</Avatar>
<Avatar
src={src}
fallback={(typeof title === "string" ? title : imgFallbackText) || "-"}
/>
);
}

return (
<Stack
spacing={1.5}
spacing={1}
direction="row"
alignItems="center"
css={{
minHeight: 40, // Make it predictable for the skeleton
width: "100%",
Expand Down
50 changes: 0 additions & 50 deletions site/src/components/BuildAvatar/BuildAvatar.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions site/src/components/Filter/SelectFilter.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { action } from "@storybook/addon-actions";
import type { Meta, StoryObj } from "@storybook/react";
import { expect, userEvent, within } from "@storybook/test";
import { UserAvatar } from "components/UserAvatar/UserAvatar";
import { UserAvatar } from "modules/users/UserAvatar/UserAvatar";
import { useState } from "react";
import { withDesktopViewport } from "testHelpers/storybook";
import {
Expand All @@ -11,7 +11,7 @@ import {
} from "./SelectFilter";

const options: SelectFilterOption[] = Array.from({ length: 50 }, (_, i) => ({
startIcon: <UserAvatar username={`username ${i + 1}`} size="xs" />,
startIcon: <UserAvatar username={`username ${i + 1}`} size="sm" />,
label: `Option ${i + 1}`,
value: `option-${i + 1}`,
}));
Expand Down
10 changes: 5 additions & 5 deletions site/src/components/Filter/UserFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
type SelectFilterOption,
SelectFilterSearch,
} from "components/Filter/SelectFilter";
import { UserAvatar } from "components/UserAvatar/UserAvatar";
import { useAuthenticated } from "contexts/auth/RequireAuth";
import { UserAvatar } from "modules/users/UserAvatar/UserAvatar";
import type { FC } from "react";
import { type UseFilterMenuOptions, useFilterMenu } from "./menu";

Expand All @@ -26,7 +26,7 @@ export const useUserFilterMenu = ({
<UserAvatar
username={me.username}
avatarURL={me.avatar_url}
size="xs"
size="sm"
/>
),
},
Expand All @@ -48,7 +48,7 @@ export const useUserFilterMenu = ({
<UserAvatar
username={me.username}
avatarURL={me.avatar_url}
size="xs"
size="sm"
/>
),
};
Expand All @@ -64,7 +64,7 @@ export const useUserFilterMenu = ({
<UserAvatar
username={firstUser.username}
avatarURL={firstUser.avatar_url}
size="xs"
size="sm"
/>
),
};
Expand All @@ -80,7 +80,7 @@ export const useUserFilterMenu = ({
<UserAvatar
username={user.username}
avatarURL={user.avatar_url}
size="xs"
size="sm"
/>
),
}));
Expand Down
Loading
Loading
0