signal-desktop/ts/components/conversation/conversation-details/AddGroupMembersModal.stories.tsx

159 lines
4.6 KiB
TypeScript
Raw Normal View History

2021-03-11 21:29:31 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { ComponentProps } from 'react';
import React, { useState } from 'react';
2021-03-11 21:29:31 +00:00
import { times } from 'lodash';
import { action } from '@storybook/addon-actions';
import { sleep } from '../../../util/sleep';
import { makeLookup } from '../../../util/makeLookup';
import { deconstructLookup } from '../../../util/deconstructLookup';
2021-09-18 00:30:08 +00:00
import { setupI18n } from '../../../util/setupI18n';
import type { ConversationType } from '../../../state/ducks/conversations';
2021-03-11 21:29:31 +00:00
import enMessages from '../../../../_locales/en/messages.json';
import { getDefaultConversation } from '../../../test-both/helpers/getDefaultConversation';
import { AddGroupMembersModal } from './AddGroupMembersModal';
import { ChooseGroupMembersModal } from './AddGroupMembersModal/ChooseGroupMembersModal';
import { ConfirmAdditionsModal } from './AddGroupMembersModal/ConfirmAdditionsModal';
2021-03-11 21:29:31 +00:00
import { RequestState } from './util';
2021-11-02 23:01:13 +00:00
import { ThemeType } from '../../../types/Util';
import { makeFakeLookupConversationWithoutServiceId } from '../../../test-both/helpers/fakeLookupConversationWithoutServiceId';
2021-03-11 21:29:31 +00:00
const i18n = setupI18n('en', enMessages);
2022-06-07 00:48:02 +00:00
export default {
title: 'Components/Conversation/ConversationDetails/AddGroupMembersModal',
};
2021-03-11 21:29:31 +00:00
const allCandidateContacts = times(50, () => getDefaultConversation());
let allCandidateContactsLookup = makeLookup(allCandidateContacts, 'id');
const lookupConversationWithoutServiceId =
makeFakeLookupConversationWithoutServiceId(convo => {
allCandidateContacts.push(convo);
allCandidateContactsLookup = makeLookup(allCandidateContacts, 'id');
});
2021-03-11 21:29:31 +00:00
type PropsType = ComponentProps<typeof AddGroupMembersModal>;
const createProps = (
overrideProps: Partial<PropsType> = {},
candidateContacts: Array<ConversationType> = []
): PropsType => ({
2021-03-11 21:29:31 +00:00
clearRequestError: action('clearRequestError'),
conversationIdsAlreadyInGroup: new Set(),
groupTitle: 'Tahoe Trip',
i18n,
onClose: action('onClose'),
makeRequest: async (conversationIds: ReadonlyArray<string>) => {
action('onMakeRequest')(conversationIds);
},
maxGroupSize: 1001,
maxRecommendedGroupSize: 151,
2021-03-11 21:29:31 +00:00
requestState: RequestState.Inactive,
renderChooseGroupMembersModal: props => {
const { selectedConversationIds } = props;
return (
<ChooseGroupMembersModal
{...props}
candidateContacts={candidateContacts}
selectedContacts={deconstructLookup(
allCandidateContactsLookup,
selectedConversationIds
)}
regionCode="US"
getPreferredBadge={() => undefined}
theme={ThemeType.light}
i18n={i18n}
lookupConversationWithoutServiceId={lookupConversationWithoutServiceId}
showUserNotFoundModal={action('showUserNotFoundModal')}
2022-06-17 00:38:28 +00:00
isUsernamesEnabled
/>
);
},
renderConfirmAdditionsModal: props => {
const { selectedConversationIds } = props;
return (
<ConfirmAdditionsModal
{...props}
i18n={i18n}
selectedContacts={deconstructLookup(
allCandidateContactsLookup,
selectedConversationIds
)}
/>
);
},
2021-03-11 21:29:31 +00:00
...overrideProps,
});
2022-11-18 00:45:19 +00:00
export function Default(): JSX.Element {
return <AddGroupMembersModal {...createProps()} />;
}
export function Only3Contacts(): JSX.Element {
return (
<AddGroupMembersModal
{...createProps({}, allCandidateContacts.slice(0, 3))}
/>
);
}
2021-03-11 21:29:31 +00:00
2022-06-07 00:48:02 +00:00
Only3Contacts.story = {
name: 'Only 3 contacts',
};
2022-11-18 00:45:19 +00:00
export function NoCandidateContacts(): JSX.Element {
return <AddGroupMembersModal {...createProps({}, [])} />;
}
2021-03-11 21:29:31 +00:00
2022-06-07 00:48:02 +00:00
NoCandidateContacts.story = {
name: 'No candidate contacts',
};
2022-11-18 00:45:19 +00:00
export function EveryoneAlreadyAdded(): JSX.Element {
return (
<AddGroupMembersModal
{...createProps({
conversationIdsAlreadyInGroup: new Set(
allCandidateContacts.map(contact => contact.id)
),
})}
/>
);
}
2022-06-07 00:48:02 +00:00
EveryoneAlreadyAdded.story = {
name: 'Everyone already added',
};
2021-03-11 21:29:31 +00:00
2022-11-18 00:45:19 +00:00
function RequestFailsAfter1SecondWrapper() {
const [requestState, setRequestState] = useState(RequestState.Inactive);
return (
<AddGroupMembersModal
{...createProps({
clearRequestError: () => {
setRequestState(RequestState.Inactive);
},
makeRequest: async () => {
setRequestState(RequestState.Active);
await sleep(1000);
setRequestState(RequestState.InactiveWithError);
},
requestState,
})}
/>
);
}
export function RequestFailsAfter1Second(): JSX.Element {
return <RequestFailsAfter1SecondWrapper />;
}
2022-06-07 00:48:02 +00:00
RequestFailsAfter1Second.story = {
name: 'Request fails after 1 second',
};