Signal-Desktop/ts/components/Stories.stories.tsx

134 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-03-04 21:14:52 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React from 'react';
import { v4 as uuid } from 'uuid';
import { action } from '@storybook/addon-actions';
import type { AttachmentType } from '../types/Attachment';
2022-04-15 00:08:46 +00:00
import type { ConversationType } from '../state/ducks/conversations';
2022-03-04 21:14:52 +00:00
import type { PropsType } from './Stories';
import { Stories } from './Stories';
import enMessages from '../../_locales/en/messages.json';
import { setupI18n } from '../util/setupI18n';
import { getDefaultConversation } from '../test-both/helpers/getDefaultConversation';
import {
fakeAttachment,
fakeThumbnail,
} from '../test-both/helpers/fakeAttachment';
import * as durations from '../util/durations';
const i18n = setupI18n('en', enMessages);
2022-06-07 00:48:02 +00:00
export default {
title: 'Components/Stories',
};
2022-03-04 21:14:52 +00:00
function createStory({
attachment,
group,
timestamp,
}: {
attachment?: AttachmentType;
2022-04-15 00:08:46 +00:00
group?: Pick<
ConversationType,
| 'acceptedMessageRequest'
| 'avatarPath'
| 'color'
| 'id'
| 'name'
| 'profileName'
| 'sharedGroupNames'
| 'title'
>;
2022-03-04 21:14:52 +00:00
timestamp: number;
}) {
const replies = Math.random() > 0.5;
let hasReplies = false;
let hasRepliesFromSelf = false;
if (replies) {
hasReplies = true;
hasRepliesFromSelf = Math.random() > 0.5;
}
const sender = getDefaultConversation();
return {
conversationId: sender.id,
group,
stories: [
{
attachment,
hasReplies,
hasRepliesFromSelf,
isMe: false,
isUnread: Math.random() > 0.5,
messageId: uuid(),
sender,
timestamp,
},
],
};
}
2022-03-29 01:10:08 +00:00
function getAttachmentWithThumbnail(url: string): AttachmentType {
return fakeAttachment({
url,
thumbnail: fakeThumbnail(url),
});
}
2022-03-04 21:14:52 +00:00
const getDefaultProps = (): PropsType => ({
hiddenStories: [],
i18n,
preferredWidthFromStorage: 380,
2022-03-29 01:10:08 +00:00
queueStoryDownload: action('queueStoryDownload'),
2022-03-04 21:14:52 +00:00
renderStoryViewer: () => <div />,
2022-06-16 19:12:50 +00:00
showConversation: action('showConversation'),
2022-03-04 21:14:52 +00:00
stories: [
createStory({
2022-03-29 01:10:08 +00:00
attachment: getAttachmentWithThumbnail(
'/fixtures/tina-rolf-269345-unsplash.jpg'
),
2022-03-04 21:14:52 +00:00
timestamp: Date.now() - 2 * durations.MINUTE,
}),
createStory({
2022-03-29 01:10:08 +00:00
attachment: getAttachmentWithThumbnail(
'/fixtures/koushik-chowdavarapu-105425-unsplash.jpg'
),
2022-03-04 21:14:52 +00:00
timestamp: Date.now() - 5 * durations.MINUTE,
}),
createStory({
2022-04-15 00:08:46 +00:00
group: getDefaultConversation({ title: 'BBQ in the park' }),
2022-03-29 01:10:08 +00:00
attachment: getAttachmentWithThumbnail(
'/fixtures/nathan-anderson-316188-unsplash.jpg'
),
2022-03-04 21:14:52 +00:00
timestamp: Date.now() - 65 * durations.MINUTE,
}),
createStory({
2022-03-29 01:10:08 +00:00
attachment: getAttachmentWithThumbnail('/fixtures/snow.jpg'),
2022-03-04 21:14:52 +00:00
timestamp: Date.now() - 92 * durations.MINUTE,
}),
createStory({
2022-03-29 01:10:08 +00:00
attachment: getAttachmentWithThumbnail('/fixtures/kitten-1-64-64.jpg'),
2022-03-04 21:14:52 +00:00
timestamp: Date.now() - 164 * durations.MINUTE,
}),
createStory({
2022-04-15 00:08:46 +00:00
group: getDefaultConversation({ title: 'Breaking Signal for Science' }),
2022-03-29 01:10:08 +00:00
attachment: getAttachmentWithThumbnail('/fixtures/kitten-2-64-64.jpg'),
2022-03-04 21:14:52 +00:00
timestamp: Date.now() - 380 * durations.MINUTE,
}),
createStory({
2022-03-29 01:10:08 +00:00
attachment: getAttachmentWithThumbnail('/fixtures/kitten-3-64-64.jpg'),
2022-03-04 21:14:52 +00:00
timestamp: Date.now() - 421 * durations.MINUTE,
}),
],
toggleHideStories: action('toggleHideStories'),
toggleStoriesView: action('toggleStoriesView'),
});
2022-06-07 00:48:02 +00:00
export const Blank = (): JSX.Element => (
<Stories {...getDefaultProps()} stories={[]} />
);
export const Many = (): JSX.Element => <Stories {...getDefaultProps()} />;