signal-desktop/ts/components/conversation/TypingBubble.stories.tsx

225 lines
6.4 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2020 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2023-09-27 21:23:52 +00:00
import React, { useEffect, useState } from 'react';
import { times } from 'lodash';
import { action } from '@storybook/addon-actions';
import type { Meta } from '@storybook/react';
2023-09-27 21:23:52 +00:00
import { getDefaultConversation } from '../../test-both/helpers/getDefaultConversation';
2021-09-18 00:30:08 +00:00
import { setupI18n } from '../../util/setupI18n';
import enMessages from '../../../_locales/en/messages.json';
2023-09-27 21:23:52 +00:00
import type { TypingBubblePropsType } from './TypingBubble';
import { TypingBubble } from './TypingBubble';
2021-05-28 16:15:17 +00:00
import { AvatarColors } from '../../types/Colors';
import { getFakeBadge } from '../../test-both/helpers/getFakeBadge';
import { ThemeType } from '../../types/Util';
const i18n = setupI18n('en', enMessages);
2022-06-07 00:48:02 +00:00
export default {
title: 'Components/Conversation/TypingBubble',
argTypes: {},
args: {},
} satisfies Meta<TypingBubblePropsType>;
2023-09-27 21:23:52 +00:00
const CONTACTS = times(10, index => {
const letter = (index + 10).toString(36).toUpperCase();
2023-09-27 21:23:52 +00:00
return getDefaultConversation({
id: `contact-${index}`,
acceptedMessageRequest: false,
avatarPath: '',
2023-09-27 21:23:52 +00:00
badges: [],
color: AvatarColors[index],
name: `${letter} ${letter}`,
phoneNumber: '(202) 555-0001',
profileName: `${letter} ${letter}`,
isMe: false,
sharedGroupNames: [],
title: `${letter} ${letter}`,
2023-09-27 21:23:52 +00:00
});
});
2023-09-27 21:23:52 +00:00
const CONTACT_IDS = CONTACTS.map(contact => contact.id);
const CONTACTS_BY_ID = new Map(CONTACTS.map(contact => [contact.id, contact]));
const getConversation = (id: string) =>
CONTACTS_BY_ID.get(id) || getDefaultConversation();
const CONTACTS_WITH_BADGES = CONTACTS.map(contact => {
return { ...contact, badges: [getFakeBadge()] };
});
const CONTACTS_WITH_BADGES_BY_ID = new Map(
CONTACTS_WITH_BADGES.map(contact => [contact.id, contact])
);
const getConversationWithBadges = (id: string) =>
CONTACTS_WITH_BADGES_BY_ID.get(id) || getDefaultConversation();
const getTypingContactIdTimestamps = (count: number) =>
Object.fromEntries(CONTACT_IDS.slice(0, count).map(id => [id, Date.now()]));
const createProps = (
overrideProps: Partial<TypingBubblePropsType> = {}
2023-09-27 21:23:52 +00:00
): TypingBubblePropsType => {
return {
typingContactIdTimestamps:
overrideProps.typingContactIdTimestamps ??
getTypingContactIdTimestamps(1),
lastItemAuthorId: '123',
lastItemTimestamp: undefined,
i18n,
conversationId: '123',
conversationType: overrideProps.conversationType ?? 'direct',
2023-09-27 21:23:52 +00:00
getConversation: overrideProps.getConversation || getConversation,
getPreferredBadge: badges =>
badges.length > 0 ? getFakeBadge() : undefined,
showContactModal: action('showContactModal'),
theme: ThemeType.light,
};
};
2022-11-18 00:45:19 +00:00
export function Direct(): JSX.Element {
const props = createProps();
return <TypingBubble {...props} />;
2022-11-18 00:45:19 +00:00
}
2023-09-27 21:23:52 +00:00
export function DirectStoppedTyping(): JSX.Element {
const props = createProps();
const [afterTimeoutProps, setAfterTimeoutProps] = useState({});
useEffect(() => {
setTimeout(
() =>
setAfterTimeoutProps({
typingContactIdTimestamps: {},
}),
500
);
}, []);
return <TypingBubble {...props} {...afterTimeoutProps} />;
}
2022-11-18 00:45:19 +00:00
export function Group(): JSX.Element {
const props = createProps({ conversationType: 'group' });
return <TypingBubble {...props} />;
2022-11-18 00:45:19 +00:00
}
export function GroupStartsTyping(): JSX.Element {
const props = createProps({
conversationType: 'group',
typingContactIdTimestamps: {},
});
const [afterTimeoutProps, setAfterTimeoutProps] = useState({});
useEffect(() => {
setTimeout(
() =>
setAfterTimeoutProps({
typingContactIdTimestamps: getTypingContactIdTimestamps(1),
}),
500
);
}, []);
return <TypingBubble {...props} {...afterTimeoutProps} />;
}
2023-09-27 21:23:52 +00:00
export function GroupStoppedTyping(): JSX.Element {
const props = createProps({
conversationType: 'group',
2023-09-27 21:23:52 +00:00
typingContactIdTimestamps: getTypingContactIdTimestamps(1),
});
2023-09-27 21:23:52 +00:00
const [afterTimeoutProps, setAfterTimeoutProps] = useState({});
useEffect(() => {
setTimeout(
() => setAfterTimeoutProps({ typingContactIdTimestamps: {} }),
500
);
}, []);
return <TypingBubble {...props} {...afterTimeoutProps} />;
}
2022-11-18 00:45:19 +00:00
export function GroupWithBadge(): JSX.Element {
const props = createProps({
conversationType: 'group',
2023-09-27 21:23:52 +00:00
typingContactIdTimestamps: getTypingContactIdTimestamps(1),
getConversation: getConversationWithBadges,
});
return <TypingBubble {...props} />;
2022-11-18 00:45:19 +00:00
}
2022-06-07 00:48:02 +00:00
2023-09-27 21:23:52 +00:00
export function GroupMultiTyping1To2(): JSX.Element {
const props = createProps({
conversationType: 'group',
typingContactIdTimestamps: getTypingContactIdTimestamps(1),
});
const [afterTimeoutProps, setAfterTimeoutProps] = useState({});
useEffect(() => {
setTimeout(
() =>
setAfterTimeoutProps({
typingContactIdTimestamps: getTypingContactIdTimestamps(2),
}),
500
);
}, []);
return <TypingBubble {...props} {...afterTimeoutProps} />;
}
export function GroupMultiTyping2Then1PersonStops(): JSX.Element {
const props = createProps({
conversationType: 'group',
2023-09-27 21:23:52 +00:00
typingContactIdTimestamps: getTypingContactIdTimestamps(2),
});
2023-09-27 21:23:52 +00:00
const [afterTimeoutProps, setAfterTimeoutProps] = useState({});
useEffect(() => {
setTimeout(
() =>
setAfterTimeoutProps({
typingContactIdTimestamps: getTypingContactIdTimestamps(1),
}),
500
);
}, []);
return <TypingBubble {...props} {...afterTimeoutProps} />;
}
2023-09-27 21:23:52 +00:00
export function GroupMultiTyping3To4(): JSX.Element {
const props = createProps({
conversationType: 'group',
2023-09-27 21:23:52 +00:00
typingContactIdTimestamps: getTypingContactIdTimestamps(3),
});
2023-09-27 21:23:52 +00:00
const [afterTimeoutProps, setAfterTimeoutProps] = useState({});
useEffect(() => {
setTimeout(
() =>
setAfterTimeoutProps({
typingContactIdTimestamps: getTypingContactIdTimestamps(4),
}),
500
);
}, []);
return <TypingBubble {...props} {...afterTimeoutProps} />;
}
export function GroupMultiTyping10(): JSX.Element {
const props = createProps({
conversationType: 'group',
2023-09-27 21:23:52 +00:00
typingContactIdTimestamps: getTypingContactIdTimestamps(10),
});
return <TypingBubble {...props} />;
}
export function GroupMultiTypingWithBadges(): JSX.Element {
const props = createProps({
conversationType: 'group',
2023-09-27 21:23:52 +00:00
typingContactIdTimestamps: getTypingContactIdTimestamps(3),
getConversation: getConversationWithBadges,
});
return <TypingBubble {...props} />;
}