You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-desktop/ts/components/leftpane/overlay/OverlayClosedGroup.tsx

219 lines
6.7 KiB
TypeScript

import { useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import useKey from 'react-use/lib/useKey';
import styled from 'styled-components';
import { isEmpty } from 'lodash';
import { SessionButton } from '../../basic/SessionButton';
import { SessionSpinner } from '../../loading';
import { useSet } from '../../../hooks/useSet';
import { VALIDATION } from '../../../session/constants';
import { createClosedGroup } from '../../../session/conversations/createClosedGroup';
import { ToastUtils } from '../../../session/utils';
import LIBSESSION_CONSTANTS from '../../../session/utils/libsession/libsession_constants';
import { clearSearch } from '../../../state/ducks/search';
import { resetLeftOverlayMode } from '../../../state/ducks/section';
import { getPrivateContactsPubkeys } from '../../../state/selectors/conversations';
import {
getSearchResultsContactOnly,
getSearchTerm,
useIsSearching,
} from '../../../state/selectors/search';
import { MemberListItem } from '../../MemberListItem';
import { SessionSearchInput } from '../../SessionSearchInput';
import { Flex } from '../../basic/Flex';
import { SpacerLG, SpacerMD } from '../../basic/Text';
import { SessionInput } from '../../inputs';
import { StyledLeftPaneOverlay } from './OverlayMessage';
3 years ago
const StyledMemberListNoContacts = styled.div`
text-align: center;
padding: 20px;
`;
const StyledNoResults = styled.div`
width: 100%;
padding: var(--margins-xl) var(--margins-sm);
text-align: center;
`;
3 years ago
const StyledGroupMemberListContainer = styled.div`
display: flex;
flex-direction: column;
flex: 1;
3 years ago
width: 100%;
overflow-x: hidden;
3 years ago
overflow-y: auto;
&::-webkit-scrollbar-track {
background-color: var(--background-secondary-color);
}
`;
const NoContacts = () => {
return (
<StyledMemberListNoContacts>{window.i18n('conversationsNone')}</StyledMemberListNoContacts>
);
};
/**
* Makes some validity check and return true if the group was indeed created
*/
async function createClosedGroupWithErrorHandling(
groupName: string,
groupMemberIds: Array<string>,
onError: (error: string) => void
): Promise<boolean> {
// Validate groupName and groupMembers length
if (groupName.length === 0) {
ToastUtils.pushToastError('invalidGroupName', window.i18n('groupNameEnterPlease'));
onError(window.i18n('groupNameEnterPlease'));
return false;
}
if (groupName.length > LIBSESSION_CONSTANTS.BASE_GROUP_MAX_NAME_LENGTH) {
Merge branch 'unstable' into standardised_strings_merge # Conflicts: # .gitignore # _locales/en/messages.json # ts/components/DebugLogView.tsx # ts/components/SessionWrapperModal.tsx # ts/components/basic/SessionHTMLRenderer.tsx # ts/components/basic/SessionRadio.tsx # ts/components/buttons/MenuButton.tsx # ts/components/conversation/SessionConversation.tsx # ts/components/conversation/SubtleNotification.tsx # ts/components/conversation/TimerNotification.tsx # ts/components/conversation/composition/CompositionBox.tsx # ts/components/conversation/message/message-content/MessageText.tsx # ts/components/conversation/message/message-item/InteractionNotification.tsx # ts/components/conversation/message/reactions/ReactionPopup.tsx # ts/components/conversation/right-panel/overlay/OverlayRightPanelSettings.tsx # ts/components/conversation/right-panel/overlay/disappearing-messages/DisappearingModes.tsx # ts/components/conversation/right-panel/overlay/disappearing-messages/OverlayDisappearingMessages.tsx # ts/components/dialog/BanOrUnbanUserDialog.tsx # ts/components/dialog/DeleteAccountModal.tsx # ts/components/dialog/EditProfileDialog.tsx # ts/components/dialog/ModeratorsAddDialog.tsx # ts/components/dialog/OnionStatusPathDialog.tsx # ts/components/dialog/ReactListModal.tsx # ts/components/dialog/SessionSeedModal.tsx # ts/components/dialog/SessionSetPasswordDialog.tsx # ts/components/dialog/UserDetailsDialog.tsx # ts/components/leftpane/LeftPaneSectionHeader.tsx # ts/components/leftpane/LeftPaneSettingSection.tsx # ts/components/leftpane/conversation-list-item/InteractionItem.tsx # ts/components/leftpane/overlay/OverlayClosedGroup.tsx # ts/components/leftpane/overlay/OverlayCommunity.tsx # ts/components/leftpane/overlay/OverlayMessage.tsx # ts/components/leftpane/overlay/SessionJoinableDefaultRooms.tsx # ts/components/leftpane/overlay/choose-action/ContactsListWithBreaks.tsx # ts/components/leftpane/overlay/choose-action/OverlayChooseAction.tsx # ts/components/menu/Menu.tsx # ts/components/registration/RegistrationStages.tsx # ts/components/registration/RegistrationUserDetails.tsx # ts/components/registration/SignInTab.tsx # ts/components/registration/SignUpTab.tsx # ts/components/settings/SessionSettings.tsx # ts/components/settings/SessionSettingsHeader.tsx # ts/components/settings/ZoomingSessionSlider.tsx # ts/components/settings/section/CategoryAppearance.tsx # ts/components/settings/section/CategoryHelp.tsx # ts/components/settings/section/CategoryPermissions.tsx # ts/components/settings/section/CategoryPrivacy.tsx # ts/hooks/useParamSelector.ts # ts/mains/main_renderer.tsx # ts/models/message.ts # ts/node/menu.ts # ts/node/tray_icon.ts # ts/session/constants.ts # ts/session/disappearing_messages/timerOptions.ts # ts/session/utils/Toast.tsx # ts/state/selectors/search.ts # ts/test/session/unit/selectors/conversations_test.ts # ts/types/LocalizerKeys.ts # ts/types/Util.ts # ts/window.d.ts # yarn.lock
8 months ago
onError(window.i18n('groupNameEnterShorter'));
return false;
}
// >= because we add ourself as a member AFTER this. so a 10 group is already invalid as it will be 11 when we are included
// the same is valid with groups count < 1
if (groupMemberIds.length < 1) {
Merge branch 'unstable' into standardised_strings_merge # Conflicts: # .gitignore # _locales/en/messages.json # ts/components/DebugLogView.tsx # ts/components/SessionWrapperModal.tsx # ts/components/basic/SessionHTMLRenderer.tsx # ts/components/basic/SessionRadio.tsx # ts/components/buttons/MenuButton.tsx # ts/components/conversation/SessionConversation.tsx # ts/components/conversation/SubtleNotification.tsx # ts/components/conversation/TimerNotification.tsx # ts/components/conversation/composition/CompositionBox.tsx # ts/components/conversation/message/message-content/MessageText.tsx # ts/components/conversation/message/message-item/InteractionNotification.tsx # ts/components/conversation/message/reactions/ReactionPopup.tsx # ts/components/conversation/right-panel/overlay/OverlayRightPanelSettings.tsx # ts/components/conversation/right-panel/overlay/disappearing-messages/DisappearingModes.tsx # ts/components/conversation/right-panel/overlay/disappearing-messages/OverlayDisappearingMessages.tsx # ts/components/dialog/BanOrUnbanUserDialog.tsx # ts/components/dialog/DeleteAccountModal.tsx # ts/components/dialog/EditProfileDialog.tsx # ts/components/dialog/ModeratorsAddDialog.tsx # ts/components/dialog/OnionStatusPathDialog.tsx # ts/components/dialog/ReactListModal.tsx # ts/components/dialog/SessionSeedModal.tsx # ts/components/dialog/SessionSetPasswordDialog.tsx # ts/components/dialog/UserDetailsDialog.tsx # ts/components/leftpane/LeftPaneSectionHeader.tsx # ts/components/leftpane/LeftPaneSettingSection.tsx # ts/components/leftpane/conversation-list-item/InteractionItem.tsx # ts/components/leftpane/overlay/OverlayClosedGroup.tsx # ts/components/leftpane/overlay/OverlayCommunity.tsx # ts/components/leftpane/overlay/OverlayMessage.tsx # ts/components/leftpane/overlay/SessionJoinableDefaultRooms.tsx # ts/components/leftpane/overlay/choose-action/ContactsListWithBreaks.tsx # ts/components/leftpane/overlay/choose-action/OverlayChooseAction.tsx # ts/components/menu/Menu.tsx # ts/components/registration/RegistrationStages.tsx # ts/components/registration/RegistrationUserDetails.tsx # ts/components/registration/SignInTab.tsx # ts/components/registration/SignUpTab.tsx # ts/components/settings/SessionSettings.tsx # ts/components/settings/SessionSettingsHeader.tsx # ts/components/settings/ZoomingSessionSlider.tsx # ts/components/settings/section/CategoryAppearance.tsx # ts/components/settings/section/CategoryHelp.tsx # ts/components/settings/section/CategoryPermissions.tsx # ts/components/settings/section/CategoryPrivacy.tsx # ts/hooks/useParamSelector.ts # ts/mains/main_renderer.tsx # ts/models/message.ts # ts/node/menu.ts # ts/node/tray_icon.ts # ts/session/constants.ts # ts/session/disappearing_messages/timerOptions.ts # ts/session/utils/Toast.tsx # ts/state/selectors/search.ts # ts/test/session/unit/selectors/conversations_test.ts # ts/types/LocalizerKeys.ts # ts/types/Util.ts # ts/window.d.ts # yarn.lock
8 months ago
onError(window.i18n('groupCreateErrorNoMembers'));
return false;
}
if (groupMemberIds.length >= VALIDATION.CLOSED_GROUP_SIZE_LIMIT) {
Merge branch 'unstable' into standardised_strings_merge # Conflicts: # .gitignore # _locales/en/messages.json # ts/components/DebugLogView.tsx # ts/components/SessionWrapperModal.tsx # ts/components/basic/SessionHTMLRenderer.tsx # ts/components/basic/SessionRadio.tsx # ts/components/buttons/MenuButton.tsx # ts/components/conversation/SessionConversation.tsx # ts/components/conversation/SubtleNotification.tsx # ts/components/conversation/TimerNotification.tsx # ts/components/conversation/composition/CompositionBox.tsx # ts/components/conversation/message/message-content/MessageText.tsx # ts/components/conversation/message/message-item/InteractionNotification.tsx # ts/components/conversation/message/reactions/ReactionPopup.tsx # ts/components/conversation/right-panel/overlay/OverlayRightPanelSettings.tsx # ts/components/conversation/right-panel/overlay/disappearing-messages/DisappearingModes.tsx # ts/components/conversation/right-panel/overlay/disappearing-messages/OverlayDisappearingMessages.tsx # ts/components/dialog/BanOrUnbanUserDialog.tsx # ts/components/dialog/DeleteAccountModal.tsx # ts/components/dialog/EditProfileDialog.tsx # ts/components/dialog/ModeratorsAddDialog.tsx # ts/components/dialog/OnionStatusPathDialog.tsx # ts/components/dialog/ReactListModal.tsx # ts/components/dialog/SessionSeedModal.tsx # ts/components/dialog/SessionSetPasswordDialog.tsx # ts/components/dialog/UserDetailsDialog.tsx # ts/components/leftpane/LeftPaneSectionHeader.tsx # ts/components/leftpane/LeftPaneSettingSection.tsx # ts/components/leftpane/conversation-list-item/InteractionItem.tsx # ts/components/leftpane/overlay/OverlayClosedGroup.tsx # ts/components/leftpane/overlay/OverlayCommunity.tsx # ts/components/leftpane/overlay/OverlayMessage.tsx # ts/components/leftpane/overlay/SessionJoinableDefaultRooms.tsx # ts/components/leftpane/overlay/choose-action/ContactsListWithBreaks.tsx # ts/components/leftpane/overlay/choose-action/OverlayChooseAction.tsx # ts/components/menu/Menu.tsx # ts/components/registration/RegistrationStages.tsx # ts/components/registration/RegistrationUserDetails.tsx # ts/components/registration/SignInTab.tsx # ts/components/registration/SignUpTab.tsx # ts/components/settings/SessionSettings.tsx # ts/components/settings/SessionSettingsHeader.tsx # ts/components/settings/ZoomingSessionSlider.tsx # ts/components/settings/section/CategoryAppearance.tsx # ts/components/settings/section/CategoryHelp.tsx # ts/components/settings/section/CategoryPermissions.tsx # ts/components/settings/section/CategoryPrivacy.tsx # ts/hooks/useParamSelector.ts # ts/mains/main_renderer.tsx # ts/models/message.ts # ts/node/menu.ts # ts/node/tray_icon.ts # ts/session/constants.ts # ts/session/disappearing_messages/timerOptions.ts # ts/session/utils/Toast.tsx # ts/state/selectors/search.ts # ts/test/session/unit/selectors/conversations_test.ts # ts/types/LocalizerKeys.ts # ts/types/Util.ts # ts/window.d.ts # yarn.lock
8 months ago
onError(window.i18n('groupAddMemberMaximum'));
return false;
}
await createClosedGroup(groupName, groupMemberIds, window.sessionFeatureFlags.useClosedGroupV3);
return true;
}
export const OverlayClosedGroup = () => {
const dispatch = useDispatch();
const privateContactsPubkeys = useSelector(getPrivateContactsPubkeys);
const [groupName, setGroupName] = useState('');
const [groupNameError, setGroupNameError] = useState<string | undefined>(undefined);
const [loading, setLoading] = useState(false);
const {
uniqueValues: selectedMemberIds,
addTo: addToSelected,
removeFrom: removeFromSelected,
} = useSet<string>([]);
const isSearch = useIsSearching();
const searchTerm = useSelector(getSearchTerm);
const searchResultContactsOnly = useSelector(getSearchResultsContactOnly);
function closeOverlay() {
dispatch(clearSearch());
dispatch(resetLeftOverlayMode());
}
async function onEnterPressed() {
setGroupNameError(undefined);
if (loading) {
window?.log?.warn('Closed group creation already in progress');
return;
}
setLoading(true);
const groupCreated = await createClosedGroupWithErrorHandling(
groupName,
selectedMemberIds,
setGroupNameError
);
if (groupCreated) {
closeOverlay();
return;
}
setLoading(false);
}
useKey('Escape', closeOverlay);
const contactsToRender = isSearch ? searchResultContactsOnly : privateContactsPubkeys;
const noContactsForClosedGroup = isEmpty(searchTerm) && contactsToRender.length === 0;
const disableCreateButton = loading || (!selectedMemberIds.length && !groupName.length);
return (
<StyledLeftPaneOverlay
container={true}
flexDirection={'column'}
flexGrow={1}
alignItems={'center'}
>
<Flex
container={true}
width={'100%'}
flexDirection="column"
alignItems="center"
padding={'var(--margins-md)'}
>
<SessionInput
autoFocus={true}
type="text"
placeholder={window.i18n('groupNameEnter')}
value={groupName}
onValueChanged={setGroupName}
onEnterPressed={onEnterPressed}
error={groupNameError}
maxLength={LIBSESSION_CONSTANTS.BASE_GROUP_MAX_NAME_LENGTH}
textSize="md"
centerText={true}
monospaced={true}
isTextArea={true}
inputDataTestId="new-closed-group-name"
editable={!loading}
/>
<SpacerMD />
<SessionSpinner loading={loading} />
<SpacerLG />
</Flex>
<SessionSearchInput />
<StyledGroupMemberListContainer>
{noContactsForClosedGroup ? (
<NoContacts />
) : searchTerm && !contactsToRender.length ? (
<StyledNoResults>
{window.i18n('searchMatchesNoneSpecific', { query: searchTerm })}
</StyledNoResults>
) : (
contactsToRender.map((pubkey: string) => (
<MemberListItem
key={`member-list-${pubkey}`}
pubkey={pubkey}
isSelected={selectedMemberIds.includes(pubkey)}
onSelect={addToSelected}
onUnselect={removeFromSelected}
withBorder={false}
disabled={loading}
/>
))
)}
</StyledGroupMemberListContainer>
<SpacerLG style={{ flexShrink: 0 }} />
<Flex container={true} width={'100%'} flexDirection="column" padding={'var(--margins-md)'}>
<SessionButton
text={window.i18n('create')}
disabled={disableCreateButton}
onClick={onEnterPressed}
dataTestId="next-button"
margin="auto 0 0" // just to keep that button at the bottom of the overlay (even with an empty list)
/>
</Flex>
<SpacerLG />
</StyledLeftPaneOverlay>
);
};