signal-desktop/ts/components/Avatar.stories.tsx

284 lines
7 KiB
TypeScript
Raw Normal View History

2022-03-04 21:14:52 +00:00
// Copyright 2020-2022 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2022-06-07 00:48:02 +00:00
import type { Meta, Story } from '@storybook/react';
import * as React from 'react';
2022-07-22 00:44:35 +00:00
import { action } from '@storybook/addon-actions';
2022-06-24 16:52:48 +00:00
import { expect } from '@storybook/jest';
2022-07-22 00:44:35 +00:00
import { isBoolean } from 'lodash';
2022-06-24 16:52:48 +00:00
import { within, userEvent } from '@storybook/testing-library';
2022-07-22 00:44:35 +00:00
import type { AvatarColorType } from '../types/Colors';
import type { Props } from './Avatar';
import enMessages from '../../_locales/en/messages.json';
2022-07-22 00:44:35 +00:00
import { Avatar, AvatarBlur, AvatarSize } from './Avatar';
import { AvatarColors } from '../types/Colors';
2022-07-22 00:44:35 +00:00
import { HasStories } from '../types/Stories';
import { ThemeType } from '../types/Util';
2022-07-22 00:44:35 +00:00
import { getFakeBadge } from '../test-both/helpers/getFakeBadge';
import { setupI18n } from '../util/setupI18n';
const i18n = setupI18n('en', enMessages);
2021-05-28 16:15:17 +00:00
const colorMap: Record<string, AvatarColorType> = AvatarColors.reduce(
(m, color) => ({
...m,
[color]: color,
}),
{}
);
const conversationTypeMap: Record<string, Props['conversationType']> = {
direct: 'direct',
group: 'group',
};
2022-06-07 00:48:02 +00:00
export default {
title: 'Components/Avatar',
component: Avatar,
argTypes: {
badge: {
control: false,
},
blur: {
control: { type: 'radio' },
defaultValue: undefined,
2022-06-07 00:48:02 +00:00
options: {
Undefined: undefined,
2022-06-07 00:48:02 +00:00
NoBlur: AvatarBlur.NoBlur,
BlurPicture: AvatarBlur.BlurPicture,
BlurPictureWithClickToView: AvatarBlur.BlurPictureWithClickToView,
},
},
color: {
defaultValue: AvatarColors[0],
options: colorMap,
},
conversationType: {
control: { type: 'radio' },
options: conversationTypeMap,
},
2022-06-24 16:52:48 +00:00
onClick: { action: true },
2022-06-07 00:48:02 +00:00
size: {
control: false,
},
storyRing: {
control: { type: 'radio' },
2022-07-22 00:44:35 +00:00
options: [undefined, ...Object.values(HasStories)],
2022-06-07 00:48:02 +00:00
},
theme: {
control: { type: 'radio' },
defaultValue: ThemeType.light,
options: ThemeType,
},
},
} as Meta;
const createProps = (overrideProps: Partial<Props> = {}): Props => ({
acceptedMessageRequest: isBoolean(overrideProps.acceptedMessageRequest)
? overrideProps.acceptedMessageRequest
: true,
2022-06-07 00:48:02 +00:00
avatarPath: overrideProps.avatarPath || '',
2021-11-02 23:01:13 +00:00
badge: overrideProps.badge,
blur: overrideProps.blur,
2022-06-07 00:48:02 +00:00
color: overrideProps.color || AvatarColors[0],
conversationType: overrideProps.conversationType || 'direct',
i18n,
2021-05-07 22:21:10 +00:00
isMe: false,
2022-06-07 00:48:02 +00:00
loading: Boolean(overrideProps.loading),
noteToSelf: Boolean(overrideProps.noteToSelf),
onClick: action('onClick'),
2021-11-18 20:01:53 +00:00
onClickBadge: action('onClickBadge'),
2022-06-07 00:48:02 +00:00
phoneNumber: overrideProps.phoneNumber || '',
searchResult: Boolean(overrideProps.searchResult),
2021-05-07 22:21:10 +00:00
sharedGroupNames: [],
size: 80,
title: overrideProps.title || '',
theme: overrideProps.theme || ThemeType.light,
storyRing: overrideProps.storyRing,
});
2022-06-07 00:48:02 +00:00
const sizes = Object.values(AvatarSize).filter(
x => typeof x === 'number'
) as Array<AvatarSize>;
2022-11-18 00:45:19 +00:00
// eslint-disable-next-line react/function-component-definition
2022-06-07 00:48:02 +00:00
const Template: Story<Props> = args => (
<>
{sizes.map(size => (
<Avatar key={size} {...args} size={size} />
))}
</>
);
2022-11-18 00:45:19 +00:00
// eslint-disable-next-line react/function-component-definition
2022-06-07 00:48:02 +00:00
const TemplateSingle: Story<Props> = args => (
2022-12-09 20:37:45 +00:00
<Avatar {...args} size={AvatarSize.EIGHTY} />
2022-06-07 00:48:02 +00:00
);
2022-06-07 00:48:02 +00:00
export const Default = Template.bind({});
Default.args = createProps({
avatarPath: '/fixtures/giphy-GVNvOUpeYmI7e.gif',
2021-11-02 23:01:13 +00:00
});
2022-06-24 16:52:48 +00:00
Default.play = async ({ args, canvasElement }) => {
const canvas = within(canvasElement);
const [avatar] = canvas.getAllByRole('button');
await userEvent.click(avatar);
await expect(args.onClick).toHaveBeenCalled();
};
2022-06-07 00:48:02 +00:00
Default.story = {
name: 'Avatar',
};
2021-11-02 23:01:13 +00:00
2022-06-07 00:48:02 +00:00
export const WithBadge = Template.bind({});
WithBadge.args = createProps({
avatarPath: '/fixtures/kitten-3-64-64.jpg',
badge: getFakeBadge(),
2021-05-10 17:50:04 +00:00
});
2022-06-07 00:48:02 +00:00
WithBadge.story = {
name: 'With badge',
};
2021-05-10 17:50:04 +00:00
2022-06-07 00:48:02 +00:00
export const WideImage = Template.bind({});
WideImage.args = createProps({
avatarPath: '/fixtures/wide.jpg',
});
2022-06-07 00:48:02 +00:00
WideImage.story = {
name: 'Wide image',
};
2022-06-07 00:48:02 +00:00
export const OneWordName = Template.bind({});
OneWordName.args = createProps({
title: 'John',
});
2022-06-07 00:48:02 +00:00
OneWordName.story = {
name: 'One-word Name',
};
2022-06-07 00:48:02 +00:00
export const TwoWordName = Template.bind({});
TwoWordName.args = createProps({
title: 'John Smith',
});
2022-06-07 00:48:02 +00:00
TwoWordName.story = {
name: 'Two-word Name',
};
2022-06-07 00:48:02 +00:00
export const WideInitials = Template.bind({});
WideInitials.args = createProps({
title: 'Walter White',
});
2022-06-07 00:48:02 +00:00
WideInitials.story = {
name: 'Wide initials',
};
2022-06-07 00:48:02 +00:00
export const ThreeWordName = Template.bind({});
ThreeWordName.args = createProps({
title: 'Walter H. White',
});
2022-06-07 00:48:02 +00:00
ThreeWordName.story = {
name: 'Three-word name',
};
2022-06-07 00:48:02 +00:00
export const NoteToSelf = Template.bind({});
NoteToSelf.args = createProps({
noteToSelf: true,
});
2022-06-07 00:48:02 +00:00
NoteToSelf.story = {
name: 'Note to Self',
};
2022-06-07 00:48:02 +00:00
export const ContactIcon = Template.bind({});
ContactIcon.args = createProps();
2022-06-07 00:48:02 +00:00
export const GroupIcon = Template.bind({});
GroupIcon.args = createProps({
conversationType: 'group',
});
2022-06-07 00:48:02 +00:00
export const SearchIcon = Template.bind({});
SearchIcon.args = createProps({
searchResult: true,
2021-11-12 01:17:29 +00:00
});
2022-11-18 00:45:19 +00:00
export function Colors(): JSX.Element {
const props = createProps();
2022-06-07 00:48:02 +00:00
return (
<>
{AvatarColors.map(color => (
<Avatar key={color} {...props} color={color} />
))}
</>
);
2022-11-18 00:45:19 +00:00
}
2022-06-07 00:48:02 +00:00
export const BrokenColor = Template.bind({});
BrokenColor.args = createProps({
color: 'nope' as AvatarColorType,
});
2022-06-07 00:48:02 +00:00
export const BrokenAvatar = Template.bind({});
BrokenAvatar.args = createProps({
avatarPath: 'badimage.png',
});
2022-06-07 00:48:02 +00:00
export const BrokenAvatarForGroup = Template.bind({});
BrokenAvatarForGroup.args = createProps({
avatarPath: 'badimage.png',
conversationType: 'group',
});
2022-06-07 00:48:02 +00:00
BrokenAvatarForGroup.story = {
name: 'Broken Avatar for Group',
};
2022-06-07 00:48:02 +00:00
export const Loading = Template.bind({});
Loading.args = createProps({
loading: true,
});
2021-04-27 13:20:17 +00:00
2022-06-07 00:48:02 +00:00
export const BlurredBasedOnProps = TemplateSingle.bind({});
BlurredBasedOnProps.args = createProps({
acceptedMessageRequest: false,
avatarPath: '/fixtures/kitten-3-64-64.jpg',
});
2022-06-07 00:48:02 +00:00
BlurredBasedOnProps.story = {
name: 'Blurred based on props',
};
2022-06-07 00:48:02 +00:00
export const ForceBlurred = TemplateSingle.bind({});
ForceBlurred.args = createProps({
avatarPath: '/fixtures/kitten-3-64-64.jpg',
blur: AvatarBlur.BlurPicture,
2021-04-27 13:20:17 +00:00
});
2022-06-07 00:48:02 +00:00
ForceBlurred.story = {
name: 'Force-blurred',
};
2021-04-27 13:20:17 +00:00
2022-06-07 00:48:02 +00:00
export const BlurredWithClickToView = TemplateSingle.bind({});
BlurredWithClickToView.args = createProps({
avatarPath: '/fixtures/kitten-3-64-64.jpg',
blur: AvatarBlur.BlurPictureWithClickToView,
});
BlurredWithClickToView.story = {
name: 'Blurred with "click to view"',
};
2021-04-27 13:20:17 +00:00
2022-06-07 00:48:02 +00:00
export const StoryUnread = TemplateSingle.bind({});
StoryUnread.args = createProps({
avatarPath: '/fixtures/kitten-3-64-64.jpg',
2022-07-22 00:44:35 +00:00
storyRing: HasStories.Unread,
2021-04-27 13:20:17 +00:00
});
2022-06-07 00:48:02 +00:00
StoryUnread.story = {
name: 'Story: unread',
};
2022-03-04 21:14:52 +00:00
2022-06-07 00:48:02 +00:00
export const StoryRead = TemplateSingle.bind({});
StoryRead.args = createProps({
avatarPath: '/fixtures/kitten-3-64-64.jpg',
2022-07-22 00:44:35 +00:00
storyRing: HasStories.Read,
2022-06-07 00:48:02 +00:00
});
StoryRead.story = {
name: 'Story: read',
};